Repository navigation
memories: a parameterized module as a macro #4783
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
oharboe
wants to merge
1
commit into
The-OpenROAD-Project:master
Choose a base branch
from
oharboe:auto-memories-macro-parameters
base: master
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # AUTO_MEMORIES: the instances of a parameterized memory module (Sram | ||
| # #(NUM_ENTRIES)) keep their parameters when the module becomes a black | ||
| # box. The netlist then writes them out (`Sram #(.NUM_ENTRIES(32)) u0`), | ||
| # and OpenROAD's Verilog reader stops on a parameterized instance of a | ||
| # macro (STA-0171). The macro is the module at the widths detection | ||
| # sized it: drop the parameters from its instances. Run after the | ||
| # sources are read; `names` is the blackboxes.txt list. | ||
| proc auto_memories_drop_parameters { names } { | ||
| foreach m $names { | ||
| set dump [tee -q -s result.string dump t:$m] | ||
| set params {} | ||
| foreach {- name} [regexp -all -inline -- {parameter \\(\S+)} $dump] { | ||
| lappend params $name | ||
| } | ||
| # once per name: dump lists it once per instance | ||
| foreach name [lsort -unique $params] { | ||
| setparam -unset $name t:$m | ||
| } | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env python3 | ||
| """AUTO_MEMORIES drops the parameters from the instances of a | ||
| parameterized memory module it blackboxes. | ||
|
|
||
| A black box keeps its instances' parameters, and the netlist writes them | ||
| out (`Sram #(.NUM_ENTRIES(32)) u0`), which OpenROAD's Verilog reader | ||
| refuses for a macro. After auto_memories_drop_parameters the instance is | ||
| written as a plain instance of the macro, its ports unchanged. | ||
|
|
||
| Skipped when no yosys with read_slang is available: YOSYS_EXE or yosys on | ||
| PATH, with the plugin from SLANG_PLUGIN_PATH when read_slang is not built | ||
| in. | ||
| """ | ||
|
|
||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| DROP_TCL = os.path.join( | ||
| os.path.dirname(os.path.abspath(__file__)), | ||
| "..", | ||
| "scripts", | ||
| "memories", | ||
| "drop_parameters.tcl", | ||
| ) | ||
|
|
||
| DESIGN = """\ | ||
| module Sram #(parameter NUM_ENTRIES = 64) ( | ||
| input clk, input we, input [$clog2(NUM_ENTRIES)-1:0] addr, | ||
| input [15:0] d, output logic [15:0] q); | ||
| logic [15:0] mem [0:NUM_ENTRIES-1]; | ||
| always_ff @(posedge clk) begin | ||
| if (we) mem[addr] <= d; | ||
| q <= mem[addr]; | ||
| end | ||
| endmodule | ||
| module top (input clk, input we, input [4:0] addr, input [15:0] d, | ||
| output [15:0] q0, q1); | ||
| Sram #(.NUM_ENTRIES(32)) u0 (.clk, .we, .addr, .d, .q(q0)); | ||
| Sram #(.NUM_ENTRIES(32)) u1 (.clk, .we, .addr, .d, .q(q1)); | ||
| endmodule | ||
| """ | ||
|
|
||
|
|
||
| def yosys_command(): | ||
| yosys = os.environ.get("YOSYS_EXE") or shutil.which("yosys") | ||
| if not yosys: | ||
| return None | ||
| cmd = [yosys, "-q"] | ||
| plugin = os.environ.get("SLANG_PLUGIN_PATH") | ||
| if plugin: | ||
| cmd += ["-m", plugin] | ||
| probe = subprocess.run(cmd + ["-p", "help read_slang"], capture_output=True) | ||
| return cmd if probe.returncode == 0 else None | ||
|
|
||
|
|
||
| class TestAutoMemoriesParameters(unittest.TestCase): | ||
| def netlist(self, drop): | ||
| cmd = yosys_command() | ||
| if cmd is None: | ||
| self.skipTest("no yosys with read_slang") | ||
| with tempfile.TemporaryDirectory() as tmp: | ||
| design = os.path.join(tmp, "top.sv") | ||
| with open(design, "w") as f: | ||
| f.write(DESIGN) | ||
| netlist = os.path.join(tmp, "out.v") | ||
| script = os.path.join(tmp, "drop.tcl") | ||
| with open(script, "w") as f: | ||
| f.write( | ||
| "yosys -import\n" | ||
| f"source {{{DROP_TCL}}}\n" | ||
| "yosys read_slang --keep-hierarchy --top top" | ||
| f" --blackboxed-module Sram {{{design}}}\n" | ||
| + ("auto_memories_drop_parameters [list Sram]\n" if drop else "") | ||
| + "hierarchy -top top\n" | ||
| f"write_verilog -noattr {{{netlist}}}\n" | ||
| ) | ||
| result = subprocess.run( | ||
| cmd + ["-c", script], capture_output=True, text=True | ||
| ) | ||
| self.assertEqual(result.returncode, 0, result.stdout + result.stderr) | ||
| with open(netlist) as f: | ||
| return f.read() | ||
|
|
||
| def test_black_box_keeps_parameters(self): | ||
| # The problem: without the proc the instance is parameterized. | ||
| self.assertIn("Sram #(", self.netlist(drop=False)) | ||
|
|
||
| def test_parameters_dropped(self): | ||
| netlist = self.netlist(drop=True) | ||
| self.assertNotIn("#(", netlist) | ||
| self.assertIn("Sram u0", netlist) | ||
| self.assertIn("Sram u1", netlist) | ||
| self.assertIn(".addr(addr)", netlist) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.
Uh oh!
There was an error while loading. Please reload this page.