From 28dd90e92df7c97235f54762381f1e91fad72c61 Mon Sep 17 00:00:00 2001 From: Igor Zaworski Date: Wed, 29 Oct 2025 19:27:31 +0100 Subject: [PATCH] Fix side effects for improved function/task/process purity (#6559) --- include/verilated_std.sv | 2 +- src/V3Const.cpp | 4 ++++ test_regress/t/t_func_purification.py | 18 ++++++++++++++++++ test_regress/t/t_func_purification.v | 23 +++++++++++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100755 test_regress/t/t_func_purification.py create mode 100644 test_regress/t/t_func_purification.v diff --git a/include/verilated_std.sv b/include/verilated_std.sv index 64ceff8e3..57468a393 100644 --- a/include/verilated_std.sv +++ b/include/verilated_std.sv @@ -151,7 +151,7 @@ package std; function state status(); `ifdef VERILATOR_TIMING - return state'($c(m_process, "->state()")); + return state'($cpure(m_process, "->state()")); `else return RUNNING; `endif diff --git a/src/V3Const.cpp b/src/V3Const.cpp index eeddc76f8..073997556 100644 --- a/src/V3Const.cpp +++ b/src/V3Const.cpp @@ -3373,6 +3373,10 @@ class ConstVisitor final : public VNVisitor { nodep->unlinkFrBack(); } VL_DO_DANGLING(pushDeletep(nodep), nodep); + + // Removed branch could contain only impurity within a function and the + // function could be purified + VIsCached::clearCacheTree(); } else if (!AstNode::afterCommentp(nodep->thensp()) && !AstNode::afterCommentp(nodep->elsesp())) { if (!nodep->condp()->isPure()) { diff --git a/test_regress/t/t_func_purification.py b/test_regress/t/t_func_purification.py new file mode 100755 index 000000000..f989a35fb --- /dev/null +++ b/test_regress/t/t_func_purification.py @@ -0,0 +1,18 @@ +#!/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('simulator') + +test.compile() + +test.execute() + +test.passes() diff --git a/test_regress/t/t_func_purification.v b/test_regress/t/t_func_purification.v new file mode 100644 index 000000000..cef807bc8 --- /dev/null +++ b/test_regress/t/t_func_purification.v @@ -0,0 +1,23 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain, for +// any use, without warranty, 2025 by Antmicro. +// SPDX-License-Identifier: CC0-1.0 + +module t; + initial begin + if (0 & func(1)) $stop; + $write("*-* All Finished *-*\n"); + $finish; + end + function bit func(bit x); + if (x) begin + if (x) begin + return 1; + end else begin + $c(""); + end + return 0; + end + endfunction +endmodule