Skip to content

Commit 719b8db

Browse files
committed
Fix infinite recursion due to recursive functions/tasks
Signed-off-by: Krzysztof Bieganski <kbieganski@antmicro.com>
1 parent fe6cb29 commit 719b8db

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

src/V3AstNodes.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ bool AstNodeFTaskRef::isPure() {
7676
// cached.
7777
return false;
7878
} else {
79-
if (!m_purity.isCached()) m_purity.set(this->getPurityRecurse());
79+
if (!m_purity.isCached()) {
80+
m_purity.set(true); // To prevent infinite recursion, set to true before getting
81+
// the actual purity. If there are impure statements in the
82+
// task/function, they'll taint this call anyway.
83+
m_purity.set(this->getPurityRecurse());
84+
}
8085
return m_purity.get();
8186
}
8287
}

src/V3Hasher.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class HasherVisitor final : public VNVisitorConst {
3838

3939
V3Hash hashNodeAndIterate(AstNode* nodep, bool hashDType, bool hashChildren,
4040
std::function<void()>&& f) {
41-
// See comments in visit(AstCFunc) about this breaking recursion
42-
if (m_cacheInUser4 && nodep->user4()) {
41+
// See comments in visit(AstCFunc/AstNodeFTask) about this breaking recursion
42+
if (nodep->user4()) {
4343
return V3Hash{nodep->user4()};
4444
} else {
4545
VL_RESTORER(m_hash);
@@ -470,7 +470,13 @@ class HasherVisitor final : public VNVisitorConst {
470470
}
471471
void visit(AstNodeFTask* nodep) override {
472472
m_hash += hashNodeAndIterate(nodep, HASH_DTYPE, HASH_CHILDREN, [this, nodep]() { //
473+
// We might be in a recursive function, if so on *second* call
474+
// here we need to break what would be an infinite loop.
473475
m_hash += nodep->name();
476+
nodep->user4(true); // Set this "first" call
477+
// So that a second call will then exit hashNodeAndIterate
478+
// Having a constant in the hash just means the recursion will
479+
// end, it shouldn't change the NodeFTask having a unique hash itself.
474480
});
475481
}
476482
void visit(AstModport* nodep) override {
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env perl
2+
if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; }
3+
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
4+
#
5+
# Copyright 2023 by Wilson Snyder. This program is free software; you
6+
# can redistribute it and/or modify it under the terms of either the GNU
7+
# Lesser General Public License Version 3 or the Perl Artistic License
8+
# Version 2.0.
9+
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
10+
11+
scenarios(vlt => 1);
12+
13+
lint(
14+
verilator_flags2 => ["--no-unlimited-stack -Wno-lint"],
15+
);
16+
17+
ok(1);
18+
1;

test_regress/t/t_infinite_recursion.v

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// DESCRIPTION: Verilator: Verilog Test module
2+
//
3+
// This file ONLY is placed under the Creative Commons Public Domain, for
4+
// any use, without warranty, 2024 by Antmicro.
5+
// SPDX-License-Identifier: CC0-1.0
6+
7+
class cls;
8+
task t; t; endtask
9+
task pre_randomize;
10+
t;
11+
endtask
12+
endclass
13+
module t;
14+
cls obj;
15+
task t;
16+
int _ = obj.randomize() with {1 == 1;};
17+
endtask
18+
endmodule

0 commit comments

Comments
 (0)