From 8455ee709164807da770a1805158e7ff8deda8c6 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 22 Aug 2020 20:04:02 -0400 Subject: [PATCH] Optimize one-statement fork/join into begin --- src/V3Width.cpp | 11 ++++++++--- test_regress/t/t_fork.out | 5 ----- test_regress/t/t_fork.pl | 9 ++++----- test_regress/t/t_fork.v | 8 ++++++++ test_regress/t/t_fork2.out | 5 +++++ test_regress/t/t_fork2.pl | 19 +++++++++++++++++++ test_regress/t/t_fork2.v | 21 +++++++++++++++++++++ 7 files changed, 65 insertions(+), 13 deletions(-) delete mode 100644 test_regress/t/t_fork.out create mode 100644 test_regress/t/t_fork2.out create mode 100755 test_regress/t/t_fork2.pl create mode 100644 test_regress/t/t_fork2.v diff --git a/src/V3Width.cpp b/src/V3Width.cpp index 465b22c81..3a47a1d56 100644 --- a/src/V3Width.cpp +++ b/src/V3Width.cpp @@ -562,9 +562,14 @@ private: VL_DO_DANGLING(pushDeletep(nodep->unlinkFrBack()), nodep); return; } - if (v3Global.opt.bboxUnsup()) { - AstBegin* newp - = new AstBegin(nodep->fileline(), nodep->name(), nodep->stmtsp()->unlinkFrBack()); + if (v3Global.opt.bboxUnsup() + // With no statements, begin is identical + || !nodep->stmtsp() + // With one statement, a begin block does as good as a fork/join or join_any + || (!nodep->stmtsp()->nextp() && !nodep->joinType().joinNone())) { + AstNode* stmtsp = nullptr; + if (nodep->stmtsp()) stmtsp = nodep->stmtsp()->unlinkFrBack(); + AstBegin* newp = new AstBegin{nodep->fileline(), nodep->name(), stmtsp}; nodep->replaceWith(newp); VL_DO_DANGLING(nodep->deleteTree(), nodep); } else { diff --git a/test_regress/t/t_fork.out b/test_regress/t/t_fork.out deleted file mode 100644 index 03e479d9f..000000000 --- a/test_regress/t/t_fork.out +++ /dev/null @@ -1,5 +0,0 @@ -%Error-UNSUPPORTED: t/t_fork.v:10:14: Unsupported: fork statements - : ... In instance t - 10 | fork : fblk - | ^~~~ -%Error: Exiting due to diff --git a/test_regress/t/t_fork.pl b/test_regress/t/t_fork.pl index a5846c699..1c4ba9485 100755 --- a/test_regress/t/t_fork.pl +++ b/test_regress/t/t_fork.pl @@ -8,12 +8,11 @@ if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); di # Version 2.0. # SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 -scenarios(vlt => 1); +scenarios(simulator => 1); -lint( - fails => 1, - expect_filename => $Self->{golden_filename}, - ); +compile(); + +execute(); ok(1); 1; diff --git a/test_regress/t/t_fork.v b/test_regress/t/t_fork.v index 06f9ed036..b92cf8830 100644 --- a/test_regress/t/t_fork.v +++ b/test_regress/t/t_fork.v @@ -7,6 +7,14 @@ module t (/*AUTOARG*/); initial begin + // With no statements this is a NOP + fork + join + fork + join_any + fork + join_none + // With one statement this is supported and optimized to a begin/end fork : fblk begin $write("*-* All Finished *-*\n"); diff --git a/test_regress/t/t_fork2.out b/test_regress/t/t_fork2.out new file mode 100644 index 000000000..e72b77c58 --- /dev/null +++ b/test_regress/t/t_fork2.out @@ -0,0 +1,5 @@ +%Error-UNSUPPORTED: t/t_fork2.v:10:14: Unsupported: fork statements + : ... In instance t + 10 | fork : fblk + | ^~~~ +%Error: Exiting due to diff --git a/test_regress/t/t_fork2.pl b/test_regress/t/t_fork2.pl new file mode 100755 index 000000000..a5846c699 --- /dev/null +++ b/test_regress/t/t_fork2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl +if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; } +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2003 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 + +scenarios(vlt => 1); + +lint( + fails => 1, + expect_filename => $Self->{golden_filename}, + ); + +ok(1); +1; diff --git a/test_regress/t/t_fork2.v b/test_regress/t/t_fork2.v new file mode 100644 index 000000000..33eb0b485 --- /dev/null +++ b/test_regress/t/t_fork2.v @@ -0,0 +1,21 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain, for +// any use, without warranty, 2003 by Wilson Snyder. +// SPDX-License-Identifier: CC0-1.0 + +module t (/*AUTOARG*/); + + initial begin + fork : fblk + begin + $write("Forked"); + end + begin + $write("*-* All Finished *-*\n"); + $finish; + end + join : fblk + end + +endmodule