diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 22730bdb8..8464367bc 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -3264,7 +3264,12 @@ class RandomizeVisitor final : public VNVisitor { return; } - if (nodep->name() != "randomize") return; + if (nodep->name() != "randomize") { + // Iterate children so nested rand_mode/constraint_mode/randomize calls + // inside function arguments are still visited and transformed + iterateChildren(nodep); + return; + } if (nodep->classOrPackagep() && nodep->classOrPackagep()->name() == "std") { // Handle std::randomize; create wrapper function that calls basicStdRandomization on diff --git a/test_regress/t/t_randomize_rand_mode_funcarg.py b/test_regress/t/t_randomize_rand_mode_funcarg.py new file mode 100755 index 000000000..8a938befd --- /dev/null +++ b/test_regress/t/t_randomize_rand_mode_funcarg.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of either the GNU Lesser General Public License Version 3 +# or the Perl Artistic License Version 2.0. +# SPDX-FileCopyrightText: 2026 Wilson Snyder +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +import vltest_bootstrap + +test.scenarios('simulator') + +test.compile() + +test.execute() + +test.passes() diff --git a/test_regress/t/t_randomize_rand_mode_funcarg.v b/test_regress/t/t_randomize_rand_mode_funcarg.v new file mode 100644 index 000000000..99850ebab --- /dev/null +++ b/test_regress/t/t_randomize_rand_mode_funcarg.v @@ -0,0 +1,48 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain. +// SPDX-FileCopyrightText: 2026 PlanV GmbH +// SPDX-License-Identifier: CC0-1.0 + +// Test: rand_mode() used as a function argument (not standalone expression) +// Ensures nested rand_mode() calls inside function arguments are properly +// transformed and do not cause Internal Error in V3SplitVar. + +class RandModeClass; + rand int x; + rand int y; + + constraint c { x inside {[1:255]}; y inside {[1:255]}; } + + task check_mode(string name, int actual, int expected); + if (actual !== expected) begin + $display("Error: %s.rand_mode() = %0d, expected %0d", name, actual, expected); + $stop; + end + endtask + + // Task that calls check_mode with rand_mode() as argument + task test_funcarg; + check_mode("x", x.rand_mode(), 1); + check_mode("y", y.rand_mode(), 1); + + x.rand_mode(0); + check_mode("x", x.rand_mode(), 0); + check_mode("y", y.rand_mode(), 1); + + x.rand_mode(1); + check_mode("x", x.rand_mode(), 1); + + // Also test using rand_mode() in $display arguments + $display("x.rand_mode=%0d y.rand_mode=%0d", x.rand_mode(), y.rand_mode()); + endtask +endclass + +module t; + initial begin + automatic RandModeClass obj = new; + obj.test_funcarg(); + $write("*-* All Finished *-*\n"); + $finish; + end +endmodule