From 895b85a16ec108f8e71cc4b240079769d646ddcf Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sun, 27 Jul 2025 11:30:19 +0100 Subject: [PATCH] Fix Replicate with unsigned count but MSB set (#6231) (#6233) Correctly compute witdh of AstReplicate in its constructor when the count is unsigned but its MSB is set. --- src/V3AstNodeExpr.h | 5 +++-- test_regress/t/t_dfg_peephole.v | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/V3AstNodeExpr.h b/src/V3AstNodeExpr.h index d816fa2d5..9fc16f9ed 100644 --- a/src/V3AstNodeExpr.h +++ b/src/V3AstNodeExpr.h @@ -3367,8 +3367,9 @@ public: : ASTGEN_SUPER_Replicate(fl, lhsp, rhsp) { if (lhsp) { if (const AstConst* const constp = VN_CAST(rhsp, Const)) { - if (constp->num().isFourState() || constp->num().isNegative()) { // V3Width warns - dtypeSetLogicSized(lhsp->width(), VSigning::UNSIGNED); + if (constp->num().isFourState() + || (constp->dtypep()->isSigned() && constp->num().isNegative())) { + dtypeSetLogicSized(lhsp->width(), VSigning::UNSIGNED); // V3Width warns } else { dtypeSetLogicSized(lhsp->width() * constp->toSInt(), VSigning::UNSIGNED); } diff --git a/test_regress/t/t_dfg_peephole.v b/test_regress/t/t_dfg_peephole.v index cd2ed056a..1789a7b9d 100644 --- a/test_regress/t/t_dfg_peephole.v +++ b/test_regress/t/t_dfg_peephole.v @@ -243,6 +243,10 @@ module t ( assign ascending_assign[4:7] = arand_b[0:3]; `signal(ASCENDING_ASSIGN, ascending_assign); + // Special cases to be covered + `signal(REPLICATE_WIDTH, {4'd8{rand_a[0]}}); // Replicate count unsigned, but MSB set + if ($bits(REPLICATE_WIDTH) != 8) $fatal("%0d != 8", $bits(REPLICATE_WIDTH)); + // Sel from not requires the operand to have a sinle sink, so can't use // the chekc due to the raw expression referencing the operand wire [63:0] sel_from_not_tmp = ~(rand_a >> rand_b[2:0] << rand_a[3:0]);