diff --git a/src/V3Life.cpp b/src/V3Life.cpp index 573afc79a..818f69799 100644 --- a/src/V3Life.cpp +++ b/src/V3Life.cpp @@ -171,7 +171,8 @@ public: entr.init(false); } else { if (AstConst* const constp = entr.constNodep()) { - if (!varrefp->varp()->isSigPublic() && !varrefp->varp()->isVirtIface()) { + if (!varrefp->varp()->isSigPublic() && !varrefp->varp()->isWrittenByDpi() + && !varrefp->varp()->isVirtIface()) { // Aha, variable is constant; substitute in. // We'll later constant propagate UINFO(4, " replaceconst: " << varrefp); diff --git a/test_regress/t/t_opt_life_dpi_written.cpp b/test_regress/t/t_opt_life_dpi_written.cpp new file mode 100644 index 000000000..274344eec --- /dev/null +++ b/test_regress/t/t_opt_life_dpi_written.cpp @@ -0,0 +1,21 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// +// 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-2026 Wilson Snyder +// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 +// +//************************************************************************* + +#include "svdpi.h" + +#include + +extern "C" void setDpi(int value); + +extern "C" void setViaDpi(int value) { + std::cout << "setViaDpi " << value << std::endl; + setDpi(value); +} diff --git a/test_regress/t/t_opt_life_dpi_written.py b/test_regress/t/t_opt_life_dpi_written.py new file mode 100755 index 000000000..ea2bf11a3 --- /dev/null +++ b/test_regress/t/t_opt_life_dpi_written.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('vlt_all') + +test.compile(verilator_flags2=["--binary", "--stats", test.top_filename.replace(".v", ".cpp")]) + +test.execute() + +test.file_grep_not(test.stats, r'Optimizations, Lifetime constant prop') +test.file_grep_not(test.stats, r'Optimizations, Lifetime assign deletions') + +test.passes() diff --git a/test_regress/t/t_opt_life_dpi_written.v b/test_regress/t/t_opt_life_dpi_written.v new file mode 100644 index 000000000..7b599f415 --- /dev/null +++ b/test_regress/t/t_opt_life_dpi_written.v @@ -0,0 +1,31 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain. +// SPDX-FileCopyrightText: 2026 Wilson Snyder +// SPDX-License-Identifier: CC0-1.0 + +`define stop $stop +`define check(got ,exp) do if ((got) !== (exp)) begin $write("%%Error: %s:%0d: got='h%x exp='h%x\n", `__FILE__,`__LINE__, (got), (exp)); `stop; end while(0) + +module t; + + int dpiSet = 0; + function automatic void setDpi(int value); + dpiSet = value; + endfunction + export "DPI-C" function setDpi; + import "DPI-C" context function void setViaDpi(int value); // calls setDpi(value) + + initial begin + dpiSet = 13; + setViaDpi(14); + `check(dpiSet, 14); + dpiSet = 15; + setViaDpi(16); + `check(dpiSet, 16); + dpiSet = 17; + setViaDpi(18); + `check(dpiSet, 18); + end + +endmodule