From 5244766b7b2b5750a719c78d438aa11471d2d5e5 Mon Sep 17 00:00:00 2001 From: Pawel Kojma Date: Fri, 19 Dec 2025 18:37:20 +0100 Subject: [PATCH] Support reduction or in constraints (#6840) --- src/V3Randomize.cpp | 12 ++++++++++++ test_regress/t/t_randomize_method_constraints.v | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 77bf43812..624176bc9 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -1050,6 +1050,18 @@ class ConstraintExprVisitor final : public VNVisitor { VL_DO_DANGLING(nodep->deleteTree(), nodep); iterate(sump); } + void visit(AstRedOr* nodep) override { + if (editFormat(nodep)) return; + // Convert to (x != 0) + FileLine* const fl = nodep->fileline(); + AstNodeExpr* const argp = nodep->lhsp()->unlinkFrBack(); + V3Number numZero{fl, argp->width(), 0}; + AstNodeExpr* const neqp = new AstNeq{fl, argp, new AstConst{fl, numZero}}; + neqp->user1(true); + nodep->replaceWith(neqp); + VL_DO_DANGLING(nodep->deleteTree(), nodep); + iterate(neqp); + } void visit(AstNodeBiop* nodep) override { if (editFormat(nodep)) return; editSMT(nodep, nodep->lhsp(), nodep->rhsp()); diff --git a/test_regress/t/t_randomize_method_constraints.v b/test_regress/t/t_randomize_method_constraints.v index f7f310f3b..482f6ced8 100644 --- a/test_regress/t/t_randomize_method_constraints.v +++ b/test_regress/t/t_randomize_method_constraints.v @@ -15,6 +15,10 @@ class Cls; constraint A { v inside {ONE, THREE}; } constraint B { w == 5; x inside {1,2} || x inside {4,5}; } constraint C { z < 3 * 7; z > 5 + 8; u > 0; } + constraint D { + |redor == 1'b1; + redor[31:1] == 31'b0; + } rand logic[79:0] u; rand Enum v; @@ -22,6 +26,7 @@ class Cls; rand logic[47:0] x; rand logic[31:0] y; rand logic[22:0] z; + rand logic[31:0] redor; function new; u = 0; @@ -50,12 +55,14 @@ module t; $display("obj.x == %0d", obj.x); $display("obj.y == %0d", obj.y); $display("obj.z == %0d", obj.z); + $display("obj.redor == %0d", obj.redor); $display("rand_result == %0d", rand_result); $display("-------------------"); if (!(obj.v inside {ONE, THREE})) $stop; if (obj.w != 5) $stop; if (!(obj.x inside {1,2,4,5})) $stop; if (obj.z <= 13 || obj.z >= 21) $stop; + if (obj.redor != 1) $stop; end //$display("===================\nUnsatisfiable constraints for obj.y:"); //rand_result = obj.randomize() with { 256 < y && y < 256; };