From 83b2061a358af75943b0dc16ba02017e5b366813 Mon Sep 17 00:00:00 2001 From: Yilou Wang Date: Mon, 13 Apr 2026 17:34:17 +0200 Subject: [PATCH] Fix std::randomize treated as this.randomize in parameterized-derived class (#7409) (#7416) Fixes #7409. --- src/V3LinkDot.cpp | 8 +- .../t/t_randomize_std_param_extends.py | 21 ++++ .../t/t_randomize_std_param_extends.v | 102 ++++++++++++++++++ 3 files changed, 129 insertions(+), 2 deletions(-) create mode 100755 test_regress/t/t_randomize_std_param_extends.py create mode 100644 test_regress/t/t_randomize_std_param_extends.v diff --git a/src/V3LinkDot.cpp b/src/V3LinkDot.cpp index bb946b5af..31826e53f 100644 --- a/src/V3LinkDot.cpp +++ b/src/V3LinkDot.cpp @@ -5148,8 +5148,10 @@ class LinkDotResolveVisitor final : public VNVisitor { return; } } - if (first && nodep->name() == "randomize" && VN_IS(m_modp, Class)) { + if (first && nodep->name() == "randomize" && VN_IS(m_modp, Class) + && !VN_IS(nodep->classOrPackagep(), Package)) { // need special handling to avoid falling back to std::randomize + // Skip if classOrPackagep is a Package (i.e. std::randomize resolved earlier) VMemberMap memberMap; AstFunc* const randFuncp = V3Randomize::newRandomizeFunc( memberMap, VN_AS(m_modp, Class), nodep->name(), true, true); @@ -5231,7 +5233,9 @@ class LinkDotResolveVisitor final : public VNVisitor { || nodep->name() == "get_randstate" || nodep->name() == "set_randstate" || nodep->name() == "rand_mode" || nodep->name() == "constraint_mode") { if (AstClass* const classp = VN_CAST(m_modp, Class)) { - nodep->classOrPackagep(classp); + // Don't overwrite if already resolved to a package (e.g. std::randomize) + if (!VN_IS(nodep->classOrPackagep(), Package)) + nodep->classOrPackagep(classp); } else if (nodep->name() == "randomize") { // A std::randomize resolved in V3Width nodep->classOrPackagep(v3Global.rootp()->stdPackagep()); diff --git a/test_regress/t/t_randomize_std_param_extends.py b/test_regress/t/t_randomize_std_param_extends.py new file mode 100755 index 000000000..db1adb3f9 --- /dev/null +++ b/test_regress/t/t_randomize_std_param_extends.py @@ -0,0 +1,21 @@ +#!/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') + +if not test.have_solver: + test.skip("No constraint solver installed") + +test.compile() + +test.execute() + +test.passes() diff --git a/test_regress/t/t_randomize_std_param_extends.v b/test_regress/t/t_randomize_std_param_extends.v new file mode 100644 index 000000000..bfbaf4deb --- /dev/null +++ b/test_regress/t/t_randomize_std_param_extends.v @@ -0,0 +1,102 @@ +// 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 + +// verilog_format: off +`define stop $stop +`define checkd(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0); +// verilog_format: on + +// Test std::randomize() inside classes extending parameterized base classes. +// Derived from issue #7409 (wsnyder's minimized reproduction). + +package my_pkg; + + class uvm_sequence_item; + endclass + + class uvm_sequence #( + parameter type T = int + ); + endclass + + // std::randomize in class extending parameterized base (the bug) + class foo_t extends uvm_sequence #(uvm_sequence_item); + task test_std_rand(); + int unsigned my_var; + int ok; + ok = std::randomize(my_var); + `checkd(ok, 1); + endtask + endclass + + // std::randomize with 'with' clause + class bar_t extends uvm_sequence #(uvm_sequence_item); + task test_std_rand_with(); + int unsigned v; + int ok; + ok = std::randomize(v) with { v inside {[1:100]}; }; + `checkd(ok, 1); + if (v < 1 || v > 100) begin + $write("%%Error: constraint violated: v=%0d\n", v); + `stop; + end + endtask + endclass + + // this.randomize() regression for parameterized-derived class + class rand_t extends uvm_sequence #(uvm_sequence_item); + rand int unsigned x; + constraint c_x { x inside {[1:50]}; } + endclass + +endpackage + +// std::randomize outside package, multi-level parameterized inheritance +class base_c #(type T = int); + T item; +endclass + +class mid_c extends base_c #(int); +endclass + +class leaf_c extends mid_c; + task test_std_rand(); + int unsigned v; + int ok; + ok = std::randomize(v); + `checkd(ok, 1); + endtask +endclass + +module t; + initial begin + automatic my_pkg::foo_t foo = new; + automatic my_pkg::bar_t bar = new; + automatic my_pkg::rand_t rt = new; + automatic leaf_c lc = new; + int ok; + + // Issue #7409 exact scenario: std::randomize in package class + foo.test_std_rand(); + + // std::randomize with 'with' clause + bar.test_std_rand_with(); + + // this.randomize() regression + ok = rt.randomize(); + `checkd(ok, 1); + if (rt.x < 1 || rt.x > 50) begin + $write("%%Error: constraint violated: x=%0d\n", rt.x); + `stop; + end + + // Multi-level parameterized inheritance outside package + lc.test_std_rand(); + + $write("*-* All Finished *-*\n"); + $finish; + end +endmodule