From 75229cc03de85acde1a3c198b12a4cc46ec19e5b Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Thu, 26 Jun 2025 18:34:20 -0400 Subject: [PATCH] Fix `pre_randomize`/`post_randomize` when no randomize (#6122). --- Changes | 1 + src/V3LinkDot.cpp | 1 + test_regress/t/t_randomize_prepost.v | 90 +++++++++++++-------- test_regress/t/t_randomize_prepost_alone.py | 16 ++++ test_regress/t/t_randomize_prepost_alone.v | 22 +++++ 5 files changed, 95 insertions(+), 35 deletions(-) create mode 100755 test_regress/t/t_randomize_prepost_alone.py create mode 100644 test_regress/t/t_randomize_prepost_alone.v diff --git a/Changes b/Changes index 2da5b8239..991815d20 100644 --- a/Changes +++ b/Changes @@ -77,6 +77,7 @@ Verilator 5.037 devel * Fix DFG binToOneHot table index missing driver (#6100). [Geza Lore] * Fix decoding octal string escapes with 1-2 digits (#6108). * Fix colon-divide operator without space (#6121). [Alex Solomatnikov] +* Fix `pre_randomize`/`post_randomize` when no randomize (#6122). [Alex Solomatnikov] * Fix variables declared in fork after taskify (#6126). [Kamil Rakoczy, Antmicro Ltd.] * Fix method calls without parenthesis (#6127). [Alex Solomatnikov] diff --git a/src/V3LinkDot.cpp b/src/V3LinkDot.cpp index 9a06cb1b4..969f510dd 100644 --- a/src/V3LinkDot.cpp +++ b/src/V3LinkDot.cpp @@ -3957,6 +3957,7 @@ class LinkDotResolveVisitor final : public VNVisitor { // Resolved in V3Width } else if (nodep->name() == "randomize" || nodep->name() == "srandom" || nodep->name() == "get_randstate" || nodep->name() == "set_randstate" + || nodep->name() == "pre_randomize" || nodep->name() == "post_randomize" || nodep->name() == "rand_mode" || nodep->name() == "constraint_mode") { if (AstClass* const classp = VN_CAST(m_modp, Class)) { nodep->classOrPackagep(classp); diff --git a/test_regress/t/t_randomize_prepost.v b/test_regress/t/t_randomize_prepost.v index 07b749c43..204881f52 100644 --- a/test_regress/t/t_randomize_prepost.v +++ b/test_regress/t/t_randomize_prepost.v @@ -4,52 +4,72 @@ // any use, without warranty, 2022 by Wilson Snyder. // SPDX-License-Identifier: CC0-1.0 +`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); + typedef enum int { RANDOMIZED = 20 } enumed_t; -class Cls; - int pre; - rand enumed_t r; - int post; +class Base; + int m_pre; + rand enumed_t r; + int m_post; - function void pre_randomize; - if (pre != 0) $stop; - $display("%d", r); - if (r != enumed_t'(0)) $stop; - if (post != 0) $stop; - pre = 10; - endfunction + function void pre_randomize; + `checkd(m_pre, 0); + `checkd(r, enumed_t'(0)); + `checkd(m_post, 0); + m_pre = 10; + endfunction - function void post_randomize; - if (pre != 10) $stop; - if (r != RANDOMIZED) $stop; - if (post != 0) $stop; - post = 30; - endfunction + function void post_randomize; + `checkd(m_pre, 10); + `checkd(r, RANDOMIZED); + `checkd(m_post, 0); + m_post = 30; + endfunction +endclass + +class Cls extends Base; + int m_cpre; + int m_cpost; + function void pre_randomize; + m_cpre = 111; + super.pre_randomize(); + endfunction + + function void post_randomize; + m_cpost = 222; + super.post_randomize(); + endfunction endclass module t (/*AUTOARG*/); - initial begin - Cls c; - int rand_result; + initial begin + Cls c; + int rand_result; - c = new; - rand_result = c.randomize(); - if (rand_result != 1) $stop; - if (c.pre != 10) $stop; - if (c.r != RANDOMIZED) $stop; - if (c.post != 30) $stop; + c = new; + rand_result = c.randomize(); + `checkd(rand_result, 1); + `checkd(c.m_pre, 10); + `checkd(c.m_cpre, 111); + `checkd(c.r, RANDOMIZED); + `checkd(c.m_post, 30); + `checkd(c.m_cpost, 222); - c = new; - rand_result = c.randomize() with { r == RANDOMIZED; }; - if (rand_result != 1) $stop; - if (c.pre != 10) $stop; - if (c.r != RANDOMIZED) $stop; - if (c.post != 30) $stop; + c = new; + rand_result = c.randomize() with { r == RANDOMIZED; }; + `checkd(rand_result, 1); + `checkd(c.m_pre, 10); + `checkd(c.m_cpre, 111); + `checkd(c.r, RANDOMIZED); + `checkd(c.m_post, 30); + `checkd(c.m_cpost, 222); - $write("*-* All Finished *-*\n"); - $finish; - end + $write("*-* All Finished *-*\n"); + $finish; + end endmodule diff --git a/test_regress/t/t_randomize_prepost_alone.py b/test_regress/t/t_randomize_prepost_alone.py new file mode 100755 index 000000000..cca4c9e73 --- /dev/null +++ b/test_regress/t/t_randomize_prepost_alone.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2025 by Wilson Snyder. 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-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +import vltest_bootstrap + +test.scenarios('linter') + +test.lint() + +test.passes() diff --git a/test_regress/t/t_randomize_prepost_alone.v b/test_regress/t/t_randomize_prepost_alone.v new file mode 100644 index 000000000..b6bdfd06a --- /dev/null +++ b/test_regress/t/t_randomize_prepost_alone.v @@ -0,0 +1,22 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain, for +// any use, without warranty, 2025 by Wilson Snyder. +// SPDX-License-Identifier: CC0-1.0 + +package uvm_pkg; + class uvm_sequence_item; + endclass +endpackage + +package tb_cpu_pkg; + import uvm_pkg::*; + class tb_cpu_seq_item extends uvm_sequence_item; + function void pre_randomize(); + super.pre_randomize(); + endfunction + function void post_randomize(); + super.post_randomize(); + endfunction + endclass +endpackage