Skip to content

Conversation

@will-keen
Copy link
Contributor

  • Add unit test for hierarchical verilation
  • Add verilator_includes to expose header files required for hierarchical verilation, use as data for verilator_bin target

Diff from 5.036.bcr.2:

diff --git a/5.036.bcr.2/MODULE.bazel b/5.036.bcr.3/MODULE.bazel
index 71382212..498f2f67 100644
--- a/5.036.bcr.2/MODULE.bazel
+++ b/5.036.bcr.3/MODULE.bazel
@@ -2,7 +2,7 @@
 
 module(
     name = "verilator",
-    version = "5.036.bcr.2",
+    version = "5.036.bcr.3",
     bazel_compatibility = [">=7.2.1"],
 )
 
diff --git a/5.036.bcr.2/overlay/BUILD.bazel b/5.036.bcr.3/overlay/BUILD.bazel
index bb7d5ddc..50d4f66d 100644
--- a/5.036.bcr.2/overlay/BUILD.bazel
+++ b/5.036.bcr.3/overlay/BUILD.bazel
@@ -338,9 +338,16 @@ cc_library(
     visibility = ["//visibility:public"],
 )
 
+filegroup(
+    name = "verilator_include",
+    srcs = glob(["include/**"]),
+    visibility = ["//visibility:public"],
+)
+
 cc_binary(
     name = "bin/verilator",
     srcs = ["src/Verilator.cpp"],
+    data = ["verilator_include"],
     copts = select({
         "@platforms//os:windows": [],
         "//conditions:default": [
diff --git a/5.036.bcr.2/overlay/private/verilator_utils.bzl b/5.036.bcr.3/overlay/private/verilator_utils.bzl
index 581ecc1c..e8a82d3d 100644
--- a/5.036.bcr.2/overlay/private/verilator_utils.bzl
+++ b/5.036.bcr.3/overlay/private/verilator_utils.bzl
@@ -311,3 +311,73 @@ verilator_build_template = rule(
         ),
     },
 )
+
+def _verilator_test_impl(ctx):
+    script = ctx.actions.declare_file(ctx.label.name + ".sh")
+
+    # Get runfiles path to verilator binary
+    repo = ctx.executable.verilator.owner.workspace_name or ctx.workspace_name
+    verilator_rf = "{}/{}".format(repo, ctx.executable.verilator.short_path)
+
+    # Build runfiles paths for sources
+    def _rf(ctx, f):
+        repo = f.owner.workspace_name or ctx.workspace_name
+        return "{}/{}".format(repo, f.short_path)
+    src_rfs = [_rf(ctx, f) for f in ctx.files.srcs]
+    src_args = " ".join(['"${TEST_SRCDIR}/%s"' % s for s in src_rfs])
+
+    # Unique include dirs based on sources (also runfiles paths)
+    def _rf_dir(ctx, f):
+        repo = f.owner.workspace_name or ctx.workspace_name
+        return repo if not f.dirname else "%s/%s" % (repo, f.dirname)
+    seen = {}
+    inc_dirs = []
+    for f in ctx.files.srcs:
+        d = _rf_dir(ctx, f)
+        if d not in seen:
+            seen[d] = True
+            inc_dirs.append(d)
+    inc_flags = " ".join(['-I"${TEST_SRCDIR}/%s"' % d for d in inc_dirs])
+
+    # Arguments to verilator
+    args = " ".join([a for a in ctx.attr.verilator_args])
+
+    # Always specify top module
+    top = "--top-module {}".format(ctx.attr.top_module)
+
+    content = "\n".join([
+        "#!/usr/bin/env bash",
+        "set -euo pipefail",
+        # Get correct path to binary, set VERILATOR_ROOT accordingly
+        'VERILATOR="${TEST_SRCDIR}/%s"' % verilator_rf,
+        'export VERILATOR_ROOT="$(dirname "$(dirname "$VERILATOR")")"',
+        # Setup working directory and output directory
+        'WORKDIR="${TEST_TMPDIR}"',
+        'mkdir -p "${WORKDIR}"',
+        'cd "${WORKDIR}"',
+        'OUTDIR=obj_dir',
+        'mkdir "$OUTDIR"',
+        '"${VERILATOR}" --Mdir "${OUTDIR}" -I${OUTDIR} ' + args + " " + inc_flags + " " + src_args + " " + top,
+    ])
+
+    ctx.actions.write(output = script, content = content, is_executable = True)
+
+    # Ensure the executable and its runfiles are present in the test’s runfiles.
+    runfiles = ctx.runfiles(files = ctx.files.srcs + [ctx.executable.verilator])
+    runfiles = runfiles.merge(ctx.attr.verilator[DefaultInfo].default_runfiles)
+
+    return DefaultInfo(
+        executable = script,
+        runfiles = runfiles
+    )
+
+verilator_test = rule(
+    implementation = _verilator_test_impl,
+    test = True,
+    attrs = {
+        "verilator": attr.label(executable = True, cfg = "target", default = "//:verilator_bin"),
+        "srcs": attr.label_list(allow_files = [".sv", ".svh", ".v"]),
+        "top_module": attr.string(doc = "The top module name"),
+        "verilator_args": attr.string_list(),
+    },
+)
diff --git a/5.036.bcr.3/overlay/tests/BUILD.bazel b/5.036.bcr.3/overlay/tests/BUILD.bazel
new file mode 100644
index 00000000..3587d2cf
--- /dev/null
+++ b/5.036.bcr.3/overlay/tests/BUILD.bazel
@@ -0,0 +1,9 @@
+load("//private:verilator_utils.bzl", "verilator_test")
+
+verilator_test(
+    # Demonstrate hermetic build works with `--hierarchical` switch
+    name = "test_hierarchical",
+    srcs = ["child.sv", "parent.sv"],
+    top_module = "parent",
+    verilator_args = ["--cc", "--hierarchical"],
+)
diff --git a/5.036.bcr.3/overlay/tests/child.sv b/5.036.bcr.3/overlay/tests/child.sv
new file mode 100644
index 00000000..5b25ecf6
--- /dev/null
+++ b/5.036.bcr.3/overlay/tests/child.sv
@@ -0,0 +1,19 @@
+module child (
+    input  logic clk,
+    input  logic rst_n,
+    input  logic in,
+    output logic out
+);
+    /*verilator hier_block*/
+
+    logic in_reg;
+
+    always_ff @(posedge clk or negedge rst_n)
+        if (!rst_n)
+            in_reg <= 1'b0;
+        else
+            in_reg <= in;
+
+    assign out = in_reg;
+
+endmodule
diff --git a/5.036.bcr.3/overlay/tests/parent.sv b/5.036.bcr.3/overlay/tests/parent.sv
new file mode 100644
index 00000000..4a25ad53
--- /dev/null
+++ b/5.036.bcr.3/overlay/tests/parent.sv
@@ -0,0 +1,19 @@
+module parent #(
+    parameter int NUM_CHILDREN = 8
+) (
+    input  logic                    clk,
+    input  logic                    rst_n,
+    input  logic [NUM_CHILDREN-1:0] in,
+    output logic [NUM_CHILDREN-1:0] out
+);
+
+    for (genvar i=0; i<NUM_CHILDREN; i++) begin: g_children
+        child u_child(
+            .clk(clk),
+            .rst_n(rst_n),
+            .in(in[i]),
+            .out(out[i])
+        );
+    end
+
+endmodule
\ No newline at end of file
diff --git a/5.036.bcr.2/presubmit.yml b/5.036.bcr.3/presubmit.yml
index 882cf0ee..32b4ebf1 100644
--- a/5.036.bcr.2/presubmit.yml
+++ b/5.036.bcr.3/presubmit.yml
@@ -10,3 +10,5 @@ bcr_test_module:
       bazel: ${{ bazel }}
       build_targets:
       - "//..."
+      test_targets:
+      - "//..."
diff --git a/5.036.bcr.2/source.json b/5.036.bcr.3/source.json
index 868d8055..4c6e5538 100644
--- a/5.036.bcr.2/source.json
+++ b/5.036.bcr.3/source.json
@@ -4,14 +4,17 @@
     "strip_prefix": "verilator-5.036",
     "overlay": {
         ".bazelignore": "sha256-4Yz6+7DIt5CedRfM7s3dxN7HstNIP9KBMBXro1MaVu0=",
-        "BUILD.bazel": "sha256-ohpdBx+dn0egYfEDf8bP6sSt8bydqD0wFhMM8P4zGjQ=",
-        "MODULE.bazel": "sha256-WNpu950cevXY1hodO6FJUIxwTWRHWjCNOSDTR3URb8c=",
+        "BUILD.bazel": "sha256-Bbz54cFplT9cqiquPz3Ortv3uGt/g0TjT2h8+syMs4I=",
+        "MODULE.bazel": "sha256-Gf98apEz9AQVew1Jn/jJO/x+mOfo11abUeUPeaWjPRs=",
         "private/BUILD.bazel": "sha256-um1vB3qYV9jNLMhTue172PCrJbZf/4Nc9gFiwpXqWYY=",
         "private/verilator_astgen.py": "sha256-aBHwsUzSKEEEseMUDdXS2XTT8X0jHfAJw7Yu8qvKqPA=",
         "private/verilator_bisonpre.py": "sha256-G92zsCtKeJSK2WtjbM47gwwKY19i5QolU6eKp6yUnr0=",
         "private/verilator_build_template.py": "sha256-fmqPyHGZcYsgM4mpJX8mHDYMSMPQXBpCTLYZ/NFTv9o=",
         "private/verilator_flexfix.py": "sha256-eMDxDSMiytb//P2Ej8KfCTwsblf+/WEsGa1gk2GkfK8=",
-        "private/verilator_utils.bzl": "sha256-0nDLkhOTQfvlyjg+B57KXhru4e2unIBOr87YcCbCzq8=",
-        "private/verilator_version.py": "sha256-GOQW7JAmkikFKdk7KzNQOiAt8Wk2WZvpC6V2UdLV36U="
+        "private/verilator_utils.bzl": "sha256-OvXHXIQEItxL+cdZuMkAuPC3ypR9+F1fqsJzFkrz4jw=",
+        "private/verilator_version.py": "sha256-GOQW7JAmkikFKdk7KzNQOiAt8Wk2WZvpC6V2UdLV36U=",
+        "tests/BUILD.bazel": "sha256-9++clrqKhaK1T+mkJReBRtHWm+OxAGx9U5VqDOZYZX4=",
+        "tests/child.sv": "sha256-dFhxCaZZ75jbuI/sYVdqm4k5m/Xa4NKduCXkHITS7r4=",
+        "tests/parent.sv": "sha256-rbIzpMk1t+0QYPIJlShSW3mFyJWAfmrfuEi/D/3r50s="
     }
 }

@bazel-io
Copy link
Member

Hello @UebelAndre, modules you maintain (verilator) have been updated in this PR.
Please review the changes. You can view a diff against the previous version in the "Generate module diff" check.

@will-keen
Copy link
Contributor Author

@bazel-io skip_check unstable_url

@bazel-io bazel-io added the skip-url-stability-check Skip the URL stability check for the PR label Oct 17, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new version of the verilator module (5.036.bcr.3) to the Bazel Central Registry. It includes a unit test for hierarchical verilation, adds verilator_includes to expose header files required for hierarchical verilation, and uses it as data for the verilator_bin target. The changes also involve updates to the MODULE.bazel, BUILD.bazel, presubmit.yml, and source.json files, as well as the addition of new files for the unit test and related utilities. I have identified a critical issue related to the filegroup naming convention in BUILD.bazel.

Copy link
Contributor

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! A couple of questions

Copy link
Contributor

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thank you!

Copy link
Member

@bazel-io bazel-io left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All modules in this PR have been approved by their maintainers. This PR will be merged if all presubmit checks pass.

@will-keen
Copy link
Contributor Author

Hi @UebelAndre, thanks for all the reviews and the approval! Do you know how it gets merged from here? Is there another step required from me?

@UebelAndre
Copy link
Contributor

UebelAndre commented Oct 21, 2025

@meteorcloudy @Wyverald are either of you able to start CI for this PR?

As far as I know I did all I can.

@Wyverald Wyverald added the presubmit-auto-run Presubmit jobs will be triggered for new changes automatically without reviewer's approval label Oct 21, 2025
@bazel-io bazel-io merged commit 5b8104c into bazelbuild:main Oct 21, 2025
14 checks passed
@bazel-io bazel-io added the auto-merged This PR is automatically merged by the BCR reviewer bot. label Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-merged This PR is automatically merged by the BCR reviewer bot. presubmit-auto-run Presubmit jobs will be triggered for new changes automatically without reviewer's approval skip-url-stability-check Skip the URL stability check for the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants