fix trivial hash collisions

Data.Hashable can produce hashes differing little in their upper bits if
only the last character of the string is changed. Because we were using
the upper bits of the hash in shortHash, this could lead to avoidable
hash collisions. This change includes minor simplification optimizations
which surfaced this collision in the existing struct_ident_scope test.
This commit is contained in:
Zachary Snow 2021-07-07 20:14:11 -04:00
parent a863321dd7
commit 2f860ff220
2 changed files with 5 additions and 2 deletions

View File

@ -185,6 +185,7 @@ rangeSize (s, e) =
-- returns the size of a range known to be ordered -- returns the size of a range known to be ordered
rangeSizeHiLo :: Range -> Expr rangeSizeHiLo :: Range -> Expr
rangeSizeHiLo (SizedRange size) = size
rangeSizeHiLo (hi, lo) = rangeSizeHiLo (hi, lo) =
simplify $ BinOp Add (BinOp Sub hi lo) (RawNum 1) simplify $ BinOp Add (BinOp Sub hi lo) (RawNum 1)
@ -206,9 +207,10 @@ endianCondRange r r1 r2 =
-- returns the total size of a set of dimensions -- returns the total size of a set of dimensions
dimensionsSize :: [Range] -> Expr dimensionsSize :: [Range] -> Expr
dimensionsSize [] = RawNum 1
dimensionsSize ranges = dimensionsSize ranges =
simplify $ simplify $
foldl (BinOp Mul) (RawNum 1) $ foldl1 (BinOp Mul) $
map rangeSize $ map rangeSize $
ranges ranges

View File

@ -32,6 +32,7 @@ module Language.SystemVerilog.AST
) where ) where
import Text.Printf (printf) import Text.Printf (printf)
import Data.Bits ((.&.))
import Data.Hashable (hash) import Data.Hashable (hash)
import Language.SystemVerilog.AST.Attr as Attr import Language.SystemVerilog.AST.Attr as Attr
@ -82,5 +83,5 @@ lhsToExpr (LHSStream o e ls) = Stream o e $ map lhsToExpr ls
shortHash :: (Show a) => a -> String shortHash :: (Show a) => a -> String
shortHash x = shortHash x =
take 5 $ printf "%05X" val printf "%05X" $ val .&. 0xFFFFF
where val = hash $ show x where val = hash $ show x