From 39be4f29bde53331c60d669c1f3d6ae73aa2b38a Mon Sep 17 00:00:00 2001 From: williamzhu17 Date: Sun, 11 May 2025 10:58:29 -0700 Subject: [PATCH] opt_balance_tree test cases --- passes/silimate/opt_balance_tree.cc | 25 ++++++- tests/silimate/opt_balance_tree.ys | 107 ++++++++++++++++++++++++---- 2 files changed, 114 insertions(+), 18 deletions(-) diff --git a/passes/silimate/opt_balance_tree.cc b/passes/silimate/opt_balance_tree.cc index da308e582..b575b9172 100644 --- a/passes/silimate/opt_balance_tree.cc +++ b/passes/silimate/opt_balance_tree.cc @@ -202,10 +202,10 @@ struct OptBalanceTreeWorker { cell->fixup_parameters(); } - void process_chain(vector &chain) { + bool process_chain(vector &chain) { // If chain size is less than 3, no balancing needed if (GetSize(chain) < 3) - return; + return false; // Get mid, midnext (at index mid+1) and end of chain Cell *mid_cell = chain[GetSize(chain) / 2]; @@ -262,6 +262,8 @@ struct OptBalanceTreeWorker { // Width reduce mid cell wreduce(mid_cell); + + return true; } void cleanup() { @@ -291,7 +293,24 @@ struct OptBalanceTreeWorker { // For each chain, if len >= 3, convert to tree via "rotation" and recurse on subtrees for (auto c : chain_start_cells) { vector chain = create_chain(c); - process_chain(chain); + bool processed = process_chain(chain); + + if (processed) { + // Rename cells and wires for formal check to pass as cells signals have changed functionalities post rotation + for (Cell *cell : chain) { + module->rename(cell, NEW_ID2_SUFFIX("rot_cell")); + } + for (Cell *cell : chain) { + SigSpec y_sig = sigmap(cell->getPort(ID::Y)); + if (y_sig.is_wire()) { + Wire *wire = y_sig.as_wire(); + if (wire && !wire->port_input && !wire->port_output) { + module->rename(y_sig.as_wire(), NEW_ID2_SUFFIX("rot_wire")); + } + } + } + } + cell_count[cell_type] += GetSize(chain); } diff --git a/tests/silimate/opt_balance_tree.ys b/tests/silimate/opt_balance_tree.ys index 8427d25cb..29b16b627 100644 --- a/tests/silimate/opt_balance_tree.ys +++ b/tests/silimate/opt_balance_tree.ys @@ -14,27 +14,31 @@ endmodule EOF check -assert -autoname -write_json pre.json -exec -- netlistsvg pre.json -o pre.svg - # Check equivalence after opt_balance_tree equiv_opt -assert opt_balance_tree design -load postopt -# opt_balance_tree -autoname -write_json post.json -exec -- netlistsvg post.json -o post.svg +# Checks if inputs to and gates has been rewired +select -set a_wires i:a %co +select -set driven_by_a @a_wires %co +select -set and_a_cell t:$and @driven_by_a %i -# Checks if y is still wired up to the correct gate -# select -set y_wires o:y %ci -# select -set y_driver @y_wires %ci -# select -set and_y_cell t:$and @y_driver %i -# select @and_y_cell -assert-count 1 -# select -set inputs @and_y_cell %ci -# select -assert-count 1 @inputs i:c %i +select -set b_wires i:b %co +select -set driven_by_b @b_wires %co +select -set and_b_cell t:$and @driven_by_b %i + +select -assert-none @and_a_cell @and_b_cell %d + +select -set c_wires i:c %co +select -set driven_by_c @c_wires %co +select -set and_c_cell t:$and @driven_by_c %i + +select -set d_wires i:d %co +select -set driven_by_d @d_wires %co +select -set and_d_cell t:$and @driven_by_d %i + +select -assert-none @and_c_cell @and_d_cell %d design -reset log -pop @@ -188,5 +192,78 @@ equiv_opt -assert opt_balance_tree design -load postopt select -assert-count 3 t:$and +design -reset +log -pop + +log -header "Interesting tree situation" +log -push +design -reset +read_verilog <