From db5b76edc14649e82d87c258481a2b0a46b1c345 Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Fri, 14 Feb 2025 13:28:24 +1300 Subject: [PATCH 1/2] Add test for shifting by INT_MAX Currently resulting in CI failing on main during fsm checks which generate a circuit that simplifies to this. --- tests/opt/opt_expr_shr_int_max.ys | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/opt/opt_expr_shr_int_max.ys diff --git a/tests/opt/opt_expr_shr_int_max.ys b/tests/opt/opt_expr_shr_int_max.ys new file mode 100644 index 000000000..5fb3c9d37 --- /dev/null +++ b/tests/opt/opt_expr_shr_int_max.ys @@ -0,0 +1,9 @@ +read_verilog << EOF +module uut_00034(b, y); + input signed [30:0] b; + output [11:0] y = b >> ~31'b0; // shift by INT_MAX +endmodule +EOF + +# This should succeed, even with UBSAN halt_on_error +opt_expr From 4c728968a3cedb90dae206e34624d831997d1ecf Mon Sep 17 00:00:00 2001 From: Krystine Sherwin <93062060+KrystalDelusion@users.noreply.github.com> Date: Fri, 14 Feb 2025 13:29:24 +1300 Subject: [PATCH 2/2] Fix runtime error on shr INT_MAX --- passes/opt/opt_expr.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/passes/opt/opt_expr.cc b/passes/opt/opt_expr.cc index ac4a65156..62a0ffc48 100644 --- a/passes/opt/opt_expr.cc +++ b/passes/opt/opt_expr.cc @@ -1315,6 +1315,10 @@ skip_fine_alu: RTLIL::SigSpec sig_a = assign_map(cell->getPort(ID::A)); RTLIL::SigSpec sig_y(cell->type == ID($shiftx) ? RTLIL::State::Sx : RTLIL::State::S0, cell->getParam(ID::Y_WIDTH).as_int()); + // Limit indexing to the size of a, which is behaviourally identical (result is all 0) + // and avoids integer overflow of i + shift_bits when e.g. ID::B == INT_MAX + shift_bits = min(shift_bits, GetSize(sig_a)); + if (cell->type != ID($shiftx) && GetSize(sig_a) < GetSize(sig_y)) sig_a.extend_u0(GetSize(sig_y), cell->getParam(ID::A_SIGNED).as_bool());