diff --git a/Changes b/Changes index 3cb14209e..5cae1fc1c 100644 --- a/Changes +++ b/Changes @@ -13,6 +13,8 @@ Verilator 4.227 devel **Minor:** +Fix crash in gate optimization of circular logic (#3543). [Bill Flynn] + Verilator 4.226 2022-08-31 ========================== diff --git a/src/V3Gate.cpp b/src/V3Gate.cpp index a4c72d5d4..e4b2e8853 100644 --- a/src/V3Gate.cpp +++ b/src/V3Gate.cpp @@ -985,11 +985,18 @@ static void eliminate(AstNode* logicp, const std::unordered_map& substitutions, GateDedupeVarVisitor* varVisp) { - const std::function visit - = [&substitutions, &visit, varVisp](AstNodeVarRef* nodep) -> void { + // Recursion filter holding already replaced variables + std::unordered_set replaced(substitutions.size() * 2); + + const std::function visit = [&, varVisp](AstNodeVarRef* nodep) -> void { // See if this variable has a substitution - const auto& it = substitutions.find(nodep->varScopep()); + AstVarScope* const vscp = nodep->varScopep(); + const auto& it = substitutions.find(vscp); if (it == substitutions.end()) return; + + // Do not substitute circular logic + if (!replaced.insert(vscp).second) return; + AstNode* const substp = it->second; // Substitute in the new tree @@ -1016,6 +1023,9 @@ static void eliminate(AstNode* logicp, VL_DO_DANGLING(nodep->deleteTree(), nodep); // Recursively substitute the new tree newp->foreach(visit); + + // Remove from recursion filter + replaced.erase(vscp); }; logicp->foreach(visit); diff --git a/test_regress/t/t_gate_loop.pl b/test_regress/t/t_gate_loop.pl new file mode 100755 index 000000000..1d9caab1c --- /dev/null +++ b/test_regress/t/t_gate_loop.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; } +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2022 by Geza Lore. 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 + +scenarios(simulator => 1); + +compile( + verilator_flags2 => ["-Wno-UNOPTFLAT"] + ); + +ok(1); +1; diff --git a/test_regress/t/t_gate_loop.v b/test_regress/t/t_gate_loop.v new file mode 100644 index 000000000..13ce7c416 --- /dev/null +++ b/test_regress/t/t_gate_loop.v @@ -0,0 +1,14 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain, for +// any use, without warranty, 2022 by Geza Lore. +// SPDX-License-Identifier: CC0-1.0 + +module t; + wire a; + wire b; + wire c; + assign a = b; + assign b = c; + assign c = a; +endmodule