From 84747f206adb9b166d9def2a6a7ea36618bcd23e Mon Sep 17 00:00:00 2001 From: Daniel Kroening Date: Sat, 28 Nov 2026 12:30:33 -0800 Subject: [PATCH 1/3] miniBDD: use a hash table for the unique table mini_bdd_mgrt::mk looks up every candidate node in the unique table (reverse_map) before creating it, so this lookup is on the hot path of every BDD operation. It was a std::map (a balanced tree), giving O(log n) pointer-chasing comparisons per node. Switch it to std::unordered_map with a hash over (var, low, high). Profiling the ebmc BDD model checker showed mini_bdd_mgrt::mk accounting for about a third of the run time, dominated by the tree traversal. On the 50 smallest HWMCC08 instances (ebmc --bdd, on top of the early variable quantification of diffblue/hw-cbmc#1801, 20s timeout), this is a 1.45x speedup on the instances solved by both, with no change in results (all still agree with the ABC reference). --- src/solvers/bdd/miniBDD/miniBDD.cpp | 29 ++++++++++++++++------------- src/solvers/bdd/miniBDD/miniBDD.h | 13 +++++++++++-- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/solvers/bdd/miniBDD/miniBDD.cpp b/src/solvers/bdd/miniBDD/miniBDD.cpp index f248e3b164c5..9426592bb78a 100644 --- a/src/solvers/bdd/miniBDD/miniBDD.cpp +++ b/src/solvers/bdd/miniBDD/miniBDD.cpp @@ -466,21 +466,24 @@ mini_bdd_mgrt::mk(unsigned var, const mini_bddt &low, const mini_bddt &high) } } -bool mini_bdd_mgrt::reverse_keyt:: -operator<(const mini_bdd_mgrt::reverse_keyt &y) const +bool mini_bdd_mgrt::reverse_keyt::operator==( + const mini_bdd_mgrt::reverse_keyt &y) const { - const reverse_keyt &x = *this; + return var == y.var && low == y.low && high == y.high; +} - if(x.var < y.var) - return true; - else if(x.var > y.var) - return false; - else if(x.low < y.low) - return true; - else if(x.low > y.low) - return false; - else - return x.high < y.high; +std::size_t mini_bdd_mgrt::reverse_key_hasht::operator()( + const mini_bdd_mgrt::reverse_keyt &key) const +{ + // Mix var, low and high into a single hash. The multiplier + // 0x9e3779b1 is the 32-bit approximation of 2^32 / phi (the golden + // ratio); multiplying by it spreads the bits of the operands, the same + // constant and scheme used by Boost's hash_combine and Knuth's + // multiplicative hashing. + std::size_t h = key.var; + h = h * 0x9e3779b1 + key.low; + h = h * 0x9e3779b1 + key.high; + return h; } void mini_bdd_mgrt::DumpTable(std::ostream &out) const diff --git a/src/solvers/bdd/miniBDD/miniBDD.h b/src/solvers/bdd/miniBDD/miniBDD.h index 4a1f8de179b3..9c110b2545d2 100644 --- a/src/solvers/bdd/miniBDD/miniBDD.h +++ b/src/solvers/bdd/miniBDD/miniBDD.h @@ -25,6 +25,7 @@ Author: Daniel Kroening, kroening@kroening.com #include #include #include +#include #include class mini_bddt @@ -127,10 +128,18 @@ class mini_bdd_mgrt unsigned var, low, high; reverse_keyt(unsigned _var, const mini_bddt &_low, const mini_bddt &_high); - bool operator<(const reverse_keyt &) const; + // Once we move to C++20 this can become `= default`. + bool operator==(const reverse_keyt &) const; }; - typedef std::map reverse_mapt; + // Hash for reverse_keyt so the unique table can be a hash table. + struct reverse_key_hasht + { + std::size_t operator()(const reverse_keyt &) const; + }; + + typedef std::unordered_map + reverse_mapt; reverse_mapt reverse_map; typedef std::stack freet; From 67dd866dee06ef6f349fa2788d4ba46ae69ee272 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Thu, 3 Dec 2026 23:13:52 +0000 Subject: [PATCH 2/3] miniBDD: correct the comment on the unique table's hash The comment said that 0x9e3779b1 is the 32-bit approximation of 2^32 / phi and that the constant and the scheme are those of Boost's hash_combine and of Knuth's multiplicative hashing. 2^32 / phi rounds to 0x9e3779b9 (2654435769); 0x9e3779b1 (2654435761) is the prime closest to it. Boost's hash_combine (of which src/goto-programs/graphml_witness.h has a copy) uses 0x9e3779b9 with shifts and exclusive or, and Knuth's multiplicative hashing keeps the high bits of the product, while here std::unordered_map gets the whole value. The constant itself is fine. Also document reverse_key_hasht with a Doxygen comment. Co-authored-by: Kiro --- src/solvers/bdd/miniBDD/miniBDD.cpp | 9 ++++----- src/solvers/bdd/miniBDD/miniBDD.h | 3 ++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/solvers/bdd/miniBDD/miniBDD.cpp b/src/solvers/bdd/miniBDD/miniBDD.cpp index 9426592bb78a..bd7c3faf2689 100644 --- a/src/solvers/bdd/miniBDD/miniBDD.cpp +++ b/src/solvers/bdd/miniBDD/miniBDD.cpp @@ -475,11 +475,10 @@ bool mini_bdd_mgrt::reverse_keyt::operator==( std::size_t mini_bdd_mgrt::reverse_key_hasht::operator()( const mini_bdd_mgrt::reverse_keyt &key) const { - // Mix var, low and high into a single hash. The multiplier - // 0x9e3779b1 is the 32-bit approximation of 2^32 / phi (the golden - // ratio); multiplying by it spreads the bits of the operands, the same - // constant and scheme used by Boost's hash_combine and Knuth's - // multiplicative hashing. + // Mix var, low and high into one value: multiply the value so far by an + // odd constant, then add the next component. 0x9e3779b1 (2654435761) is + // the prime closest to 2^32 / phi, a common multiplier in multiplicative + // hashing (Knuth, TAOCP vol. 3, section 6.4). std::size_t h = key.var; h = h * 0x9e3779b1 + key.low; h = h * 0x9e3779b1 + key.high; diff --git a/src/solvers/bdd/miniBDD/miniBDD.h b/src/solvers/bdd/miniBDD/miniBDD.h index 9c110b2545d2..a8ec75a39fe2 100644 --- a/src/solvers/bdd/miniBDD/miniBDD.h +++ b/src/solvers/bdd/miniBDD/miniBDD.h @@ -132,7 +132,8 @@ class mini_bdd_mgrt bool operator==(const reverse_keyt &) const; }; - // Hash for reverse_keyt so the unique table can be a hash table. + /// Hash of a reverse_keyt, combining var, low and high, so that the unique + /// table can be a hash table struct reverse_key_hasht { std::size_t operator()(const reverse_keyt &) const; From 1a44d84ce01c4274ab10f510648f15657f100c78 Mon Sep 17 00:00:00 2001 From: Michael Tautschnig Date: Thu, 3 Dec 2026 23:13:52 +0000 Subject: [PATCH 3/3] miniBDD: unit test of the unique table The existing miniBDD scenarios reach the unique table only through the Boolean operators, and none checks that released nodes are freed: removing the push onto the free list in mini_bdd_nodet::remove_reference leaves them passing. The new scenario checks that equivalent functions share a node over several rounds that reuse released nodes, that the number of nodes returns to its baseline once all results are released, that mk returns the existing node for the same arguments, and that a reused node is found under its new arguments only, and evaluates the results. It fails when remove_reference does not free the node, does not erase its key, or when mk does not enter a reused node, and passes both with the hash table and with the std::map that it replaced. Co-authored-by: Kiro --- unit/solvers/bdd/miniBDD/miniBDD.cpp | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/unit/solvers/bdd/miniBDD/miniBDD.cpp b/unit/solvers/bdd/miniBDD/miniBDD.cpp index 5bd689db184b..bc8708b40e5e 100644 --- a/unit/solvers/bdd/miniBDD/miniBDD.cpp +++ b/unit/solvers/bdd/miniBDD/miniBDD.cpp @@ -386,3 +386,67 @@ SCENARIO("miniBDD restrict", "[core][solver][miniBDD]") REQUIRE(exists(parity, vars.back().var()).is_true()); } } + +/// \return The value of \p f for \p assignment, whose bit `i - 1` is the +/// value of the variable with index i +static bool evaluate(const mini_bddt &f, unsigned assignment) +{ + const mini_bddt *node = &f; + while(!node->is_constant()) + { + node = + ((assignment >> (node->var() - 1)) & 1) ? &node->high() : &node->low(); + } + return node->is_true(); +} + +SCENARIO("miniBDD unique table", "[core][solver][miniBDD]") +{ + GIVEN("A manager with three variables") + { + mini_bdd_mgrt mgr; + mini_bddt a = mgr.Var("a"); + mini_bddt b = mgr.Var("b"); + mini_bddt c = mgr.Var("c"); + const std::size_t baseline = mgr.number_of_nodes(); + + THEN("Equivalent functions share a node, and released nodes are freed") + { + // later rounds reuse the nodes that earlier rounds released + for(int round = 0; round < 3; ++round) + { + mini_bddt f1 = (a & b) | c; + mini_bddt f2 = !(!c & !(b & a)); + REQUIRE(f1.node_number() == f2.node_number()); + } + REQUIRE(mgr.number_of_nodes() == baseline); + } + + THEN("mk returns the node that exists for its arguments") + { + mini_bddt n1 = mgr.mk(a.var(), b, c); + mini_bddt n2 = mgr.mk(a.var(), b, c); + REQUIRE(n1.node_number() == n2.node_number()); + } + + THEN("A reused node is found under its new arguments only") + { + unsigned released; + { + mini_bddt f = a & b; + released = f.node_number(); + } + // the node of a & b, now free, is reused for a | c + mini_bddt g = a | c; + REQUIRE(g.node_number() == released); + mini_bddt h = a & b; + REQUIRE(h.node_number() != g.node_number()); + REQUIRE((a | c).node_number() == g.node_number()); + for(unsigned x = 0; x < 8; ++x) + { + REQUIRE(evaluate(g, x) == ((x & 1) || (x & 4))); + REQUIRE(evaluate(h, x) == ((x & 1) && (x & 2))); + } + } + } +}