-
Notifications
You must be signed in to change notification settings - Fork 10
Add --verilator-hier-blocks option to emit hier_block annotations #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/emit-hierarchical-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac6ddfc
Initial plan
Copilot 9fdc7af
Add --verilator-hier-blocks option to emit hier_block annotations
Copilot 3d88f3e
Make module-declaration check in CLI test more precise
Copilot f20f8af
Assert non-exported modules omit verilator hier_block metacomment
Copilot d74f8b5
Parse modules on declaration boundaries for robustness
Copilot 0c833dd
Fix review feedback on hier_block placement and docs
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+5.57 KB
test/compiler/cli/__pycache__/check_verilator_hier_blocks.cpython-312.pyc
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| """ | ||
| Verify the output of a `--verilator-hier-blocks` compile. | ||
|
|
||
| Contract of the flag: the exported design (core) module should carry a | ||
| `/*verilator hier_block*/` metacomment, which enables hierarchical | ||
| Verilation. Verilator requires this metacomment to appear inside the | ||
| module body (after the `module name(...);` port list). The metacomment is | ||
| *selective*: it is only emitted on the core design module, not on the | ||
| non-exported helper modules that the compiler generates alongside it (the | ||
| ESI wrapper, the per-basic-block modules, etc.). | ||
|
|
||
| Checks: | ||
| 1. At least one .sv file exists. | ||
| 2. The `/*verilator hier_block*/` metacomment appears inside at least one | ||
| generated module body (i.e. after that module's `module name(...);` | ||
| declaration, as Verilator requires). | ||
| 3. At least one other generated module exists that does *not* contain the | ||
| metacomment, proving the annotation is confined to the exported design | ||
| module and is not blanket-applied to non-exported modules. | ||
|
|
||
| Exits non-zero on failure. | ||
| """ | ||
| import argparse | ||
| import re | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| HIER_BLOCK = '/*verilator hier_block*/' | ||
|
|
||
| # Matches a SystemVerilog module declaration, e.g. `module Foo (`, capturing | ||
| # the module name. | ||
| MODULE_DECL = re.compile(r'(?:^|\s)module\s+(\w+)', re.MULTILINE) | ||
|
|
||
|
|
||
| def collect_modules(sv_files): | ||
| """Return a list of (sv_name, module_name, body) for every module. | ||
|
|
||
| Modules do not nest in SystemVerilog, so each module body is the text | ||
| spanning from its `module name` declaration up to the next module | ||
| declaration (or end of file). Slicing on declaration boundaries avoids | ||
| relying on an `endmodule` token, which could otherwise appear inside a | ||
| comment or string and truncate the body prematurely. | ||
| """ | ||
| modules = [] | ||
| for sv in sv_files: | ||
| text = sv.read_text() | ||
| decls = list(MODULE_DECL.finditer(text)) | ||
| for i, decl in enumerate(decls): | ||
| end = decls[i + 1].start() if i + 1 < len(decls) else len(text) | ||
| modules.append((sv.name, decl.group(1), text[decl.start():end])) | ||
| return modules | ||
|
|
||
|
|
||
| def has_hier_block_after_port_list(module_body): | ||
| """Return True if HIER_BLOCK appears after the declaration port list.""" | ||
| port_list_end = module_body.find(');') | ||
| if port_list_end == -1: | ||
| return False | ||
| return module_body.find(HIER_BLOCK, port_list_end + 2) != -1 | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument('output_dir', help='Directory containing compiler outputs') | ||
| args = parser.parse_args() | ||
|
|
||
| output_dir = Path(args.output_dir) | ||
| if not output_dir.is_dir(): | ||
| print(f"Output directory does not exist: {output_dir}") | ||
| return 1 | ||
|
|
||
| sv_files = sorted(output_dir.glob('*.sv')) | ||
| if not sv_files: | ||
| print("--verilator-hier-blocks should produce a .sv file, but none was found.") | ||
| return 1 | ||
|
|
||
| modules = collect_modules(sv_files) | ||
| if not modules: | ||
| names = ', '.join(s.name for s in sv_files) | ||
| print(f"No SystemVerilog modules found in {names}.") | ||
| return 1 | ||
|
|
||
| with_meta = [m for m in modules if HIER_BLOCK in m[2]] | ||
| with_meta_after_ports = [m for m in modules if has_hier_block_after_port_list(m[2])] | ||
| without_meta = [m for m in modules if HIER_BLOCK not in m[2]] | ||
| module_names = ', '.join(m[1] for m in modules) | ||
|
|
||
| # Check 2: the metacomment must appear inside at least one module body. | ||
|
teqdruid marked this conversation as resolved.
|
||
| if not with_meta: | ||
| print( | ||
| f"--verilator-hier-blocks must emit {HIER_BLOCK!r} into a generated " | ||
| f"module, but none of these modules contain it: {module_names}." | ||
| ) | ||
| return 1 | ||
|
|
||
| if not with_meta_after_ports: | ||
| print( | ||
| f"--verilator-hier-blocks must emit {HIER_BLOCK!r} after a generated " | ||
| "module's port list (after `);`), but no module matches this placement " | ||
| f"requirement: {module_names}." | ||
| ) | ||
| return 1 | ||
|
|
||
| # Check 3: the metacomment must be selective. The compiler emits several | ||
| # non-exported helper modules (ESI wrapper, per-basic-block modules); these | ||
| # must not carry the metacomment. | ||
| if not without_meta: | ||
| print( | ||
| f"--verilator-hier-blocks should only annotate the exported design " | ||
| f"module, but every generated module contains {HIER_BLOCK!r}: " | ||
| f"{module_names}." | ||
| ) | ||
| return 1 | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| sys.exit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // Minimal program used by the --verilator-hier-blocks CLI test. | ||
| class VerilatorHierBlock | ||
| { | ||
| public: | ||
| uint32 TimesTwo(uint32 x) | ||
| { | ||
| return x + x; | ||
| } | ||
| } | ||
|
|
||
| export VerilatorHierBlock; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ratio of test code to implementation code is high. Maybe a more general way to test this would be better? There could be performance tests that measure verilator compilation time. Or there could be a general script that is used to verify many things like this (input=kanagawa code + regex to search for in generated RTL).