Skip to content

Commit

Permalink
Quick fix for hash collision
Browse files Browse the repository at this point in the history
  • Loading branch information
gkronber committed Jul 3, 2024
1 parent 7709c90 commit 764540c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/EGraphs/egraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ mutable struct EGraph{ExpressionType,Analysis}
uf::UnionFind
"map from eclass id to eclasses"
classes::Dict{IdKey,EClass{Analysis}}
"hashcons mapping e-node hashes to their e-class id"
memo::Dict{IdKey,Id}
"hashcons mapping e-nodes to their e-class id"
memo::Dict{VecExpr,Id}
"Hashcons the constants in the e-graph"
constants::Dict{UInt64,Any}
"Nodes which need to be processed for rebuilding. The id is the id of the enode, not the canonical id of the eclass."
Expand All @@ -143,7 +143,7 @@ function EGraph{ExpressionType,Analysis}(; needslock::Bool = false) where {Expre
EGraph{ExpressionType,Analysis}(
UnionFind(),
Dict{IdKey,EClass{Analysis}}(),
Dict{IdKey,Id}(),
Dict{VecExpr,Id}(),
Dict{UInt64,Any}(),
Pair{VecExpr,Id}[],
UniqueQueue{Pair{VecExpr,Id}}(),
Expand Down Expand Up @@ -254,7 +254,7 @@ function lookup(g::EGraph, n::VecExpr)::Id
canonicalize!(g, n)
h = IdKey(v_hash(n))

haskey(g.memo, h) ? find(g, g.memo[h]) : 0
haskey(g.memo, n) ? find(g, g.memo[n]) : 0
end


Expand All @@ -274,7 +274,7 @@ function add!(g::EGraph{ExpressionType,Analysis}, n::VecExpr, should_copy::Bool)
canonicalize!(g, n)
h = IdKey(v_hash(n))

haskey(g.memo, h) && return g.memo[h]
haskey(g.memo, n) && return g.memo[n]

if should_copy
n = copy(n)
Expand All @@ -288,7 +288,7 @@ function add!(g::EGraph{ExpressionType,Analysis}, n::VecExpr, should_copy::Bool)
end
end

g.memo[h] = id
g.memo[n] = id

add_class_by_op(g, n, id)
eclass = EClass{Analysis}(id, VecExpr[n], Pair{VecExpr,Id}[], make(g, n))
Expand Down Expand Up @@ -433,9 +433,9 @@ function process_unions!(g::EGraph{ExpressionType,AnalysisType})::Int where {Exp
(node::VecExpr, eclass_id::Id) = pop!(g.pending)
canonicalize!(g, node)
h = IdKey(v_hash(node))
if haskey(g.memo, h)
old_class_id = g.memo[h]
g.memo[h] = eclass_id
if haskey(g.memo, node)
old_class_id = g.memo[node]
g.memo[node] = eclass_id
did_something = union!(g, old_class_id, eclass_id)
# TODO unique! can node dedup be moved here? compare performance
# did_something && unique!(g[eclass_id].nodes)
Expand Down Expand Up @@ -485,7 +485,7 @@ function check_memo(g::EGraph)::Bool

for (node, id) in test_memo
@assert id == find(g, id)
@assert id == find(g, g.memo[IdKey(v_hash(node))])
@assert id == find(g, g.memo[node])
end

true
Expand Down
3 changes: 3 additions & 0 deletions src/vecexpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ end

"""The hash of the e-node."""
@inline v_hash(n::VecExpr)::Id = @inbounds n[1]
Base.hash(e::VecExpr) == v_hash(e) # TODO this is piracy
# Base.(==)(a::VecExpr, b::VecExpr) = a == b

"""Set e-node hash to zero."""
@inline v_unset_hash!(n::VecExpr)::Id = @inbounds (n[1] = Id(0))
Expand Down Expand Up @@ -106,4 +108,5 @@ v_pair_first(p::UInt128)::UInt64 = UInt64(p >> 64)
v_pair_last(p::UInt128)::UInt64 = UInt64(p & 0xffffffffffffffff)



end

0 comments on commit 764540c

Please sign in to comment.