From b19d906075a581d60d36c2c28313467568619217 Mon Sep 17 00:00:00 2001 From: nella Date: Tue, 11 Aug 2026 06:57:01 +0200 Subject: [PATCH] Fix replace_known misindexing on constant S bits. --- passes/opt/opt_muxtree.cc | 9 ++++++--- tests/opt/bug6089.ys | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/opt/bug6089.ys diff --git a/passes/opt/opt_muxtree.cc b/passes/opt/opt_muxtree.cc index 525bd4e0a..61d8772a3 100644 --- a/passes/opt/opt_muxtree.cc +++ b/passes/opt/opt_muxtree.cc @@ -483,11 +483,14 @@ struct OptMuxtreeWorker bool did_something = false; int width_if_b = 0; - idict ctrl_bits; + // map wire bit -> first S position + dict ctrl_bits; if (portname == ID::B) width_if_b = GetSize(muxinfo.cell->getPort(ID::A)); - for (int bit : sig2bits(muxinfo.cell->getPort(ID::S), false)) - ctrl_bits(bit); + vector s_bits = sig2bits(muxinfo.cell->getPort(ID::S), false); + for (int j = 0; j < GetSize(s_bits); j++) + if (s_bits[j] >= 0) + ctrl_bits.insert({s_bits[j], j}); int slice_idx = 0, slice_off = 0; vector bits = sig2bits(sig, false); diff --git a/tests/opt/bug6089.ys b/tests/opt/bug6089.ys new file mode 100644 index 000000000..0bf5291ea --- /dev/null +++ b/tests/opt/bug6089.ys @@ -0,0 +1,21 @@ +read_verilog -sv <