Skip to content

Commit 24a2d1c

Browse files
authored
Merge pull request #4609 from oharboe/memories-yosys-only
memories: drop the Verilog scanner leftovers, detection is yosys-only
2 parents f81e15b + cc5c30d commit 24a2d1c

8 files changed

Lines changed: 54 additions & 55 deletions

File tree

‎docs/user/AutoMemories.md‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ itself a welcome signal that someone is using it.
1414

1515
## What it does
1616

17-
With `AUTO_MEMORIES=1`, a pre-synthesis step runs
18-
`scripts/memories/gen_memories.py` over `VERILOG_FILES` and writes:
17+
With `AUTO_MEMORIES=1`, a pre-synthesis yosys pass
18+
(`scripts/memories/extract_memories.tcl`) elaborates `VERILOG_FILES`
19+
into `$(RESULTS_DIR)/memories_inferred.json`, and
20+
`scripts/memories/gen_memories.py` reads that netlist and writes:
1921

2022
| File | Content |
2123
| --- | --- |
@@ -39,23 +41,26 @@ flip-flops, like any other RTL.
3941

4042
## How memories are detected
4143

42-
Detection is a fast Python scan (`scripts/memories/detect.py`) for
43-
modules whose entire port list follows the firtool (CIRCT) memory port
44-
convention: every port named `<R|W|RW><n>_<function>`, e.g. `R0_addr`,
45-
`W0_en`, `RW0_wdata`, including the subword-split forms `RW0_wdata_3` /
46-
`W0_mask_2`. This is what Chisel/firtool emits for module-separated
47-
memories, and what the rocket-chip generation of Chisel emitted (the
48-
in-tree tinyRocket design).
44+
Yosys infers the memories. `scripts/memories/extract_memories.tcl`
45+
reads the design sources with the design's frontend, runs `hierarchy`,
46+
`proc` and `memory -nomap`, and writes the netlist as JSON.
47+
`scripts/memories/detect.py` then takes each `$mem_v2` cell in it:
48+
49+
- depth and width from `SIZE` and `WIDTH`, read and write port counts
50+
from `RD_PORTS` and `WR_PORTS`, and write-mask lanes from the width
51+
of `WR_EN`. Read-write ports are not inferred; a `.memories` override
52+
can describe them (see below).
53+
- pins named `R<n>_clk/addr/en/data` and `W<n>_clk/addr/en/data`, plus
54+
`W<n>_mask` when there are mask lanes.
55+
- the module's name when the `$mem_v2` is the only cell in its module,
56+
otherwise the cell's name.
4957

5058
Two consequences, documented as deliberate scope:
5159

52-
- **Module boundary only.** A memory embedded inside a larger module
53-
(a bare `reg [7:0] mem [0:255]` next to other logic) is not detected.
54-
Yosys's memory-inference pass sees those; FPGA tools extract them
55-
into block RAMs. Wiring yosys up as the detector — or growing such a
56-
pass in OpenROAD SYN, which currently has no memory inference and
57-
therefore cannot be leaned on here either — is future work; this
58-
feature punts on it with the simple scanner.
60+
- **Module boundary only.** Synthesis blackboxes converted memories by
61+
module name, so only a memory alone in its module is replaced by a
62+
macro. A memory inferred next to other logic is named after its
63+
cell, which matches no module.
5964
- **No banking.** Each detected memory maps to exactly one macro. A
6065
memory too wide, too deep, or too ported for a single sensible macro
6166
is not decomposed across several macros — a future extension.
@@ -133,12 +138,14 @@ clear error.
133138
make DESIGN_CONFIG=designs/asap7/tinyRocket/config.mk synth floorplan
134139
```
135140

136-
The generator can also be run standalone to inspect what it would do:
141+
The generator can also be run standalone, on the
142+
`memories_inferred.json` that run leaves in the results directory, to
143+
inspect what it would do:
137144

138145
```shell
139146
python3 flow/scripts/memories/gen_memories.py \
140147
--platform asap7 --out-dir /tmp/mems --json /tmp/memories.json \
141-
--verilog flow/designs/src/tinyRocket/freechips.rocketchip.system.TinyConfig.v
148+
--yosys-json flow/results/asap7/tinyRocket/base/memories_inferred.json
142149
```
143150

144151
## Consuming from bazel-orfs

‎docs/user/FlowVariables.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ configuration file.
103103
| <a name="ADDITIONAL_MEMORIES"></a>ADDITIONAL_MEMORIES| Experimental. Space-separated list of user-supplied `.memories` files (the memories.json JSON schema) merged onto the set AUTO_MEMORIES detects — to force-convert a memory the idiomatic gate rejects, or to describe one the scanner cannot find. See docs/user/AutoMemories.md.| |
104104
| <a name="ADDITIONAL_SITES"></a>ADDITIONAL_SITES| Passed as -additional_sites to initialize_floorplan.| |
105105
| <a name="ASAP7_USE_VT"></a>ASAP7_USE_VT| A space separated list of VT options to use with the ASAP7 standard cell library: RVT, LVT, SLVT.| RVT|
106-
| <a name="AUTO_MEMORIES"></a>AUTO_MEMORIES| Experimental. When set to 1, memory-shaped modules in the RTL (firtool/CIRCT `R*/W*/RW*_` port convention, module boundary only) are detected pre-synthesis, inventoried in `$(RESULTS_DIR)/memories.json`, and — when idiomatic as an SRAM macro — given generated .lib/.lef views under `$(RESULTS_DIR)/memories/`. Synthesis blackboxes those modules and all later stages read the generated views, so an existing design gets macro-based results without a memory compiler. Supported for the asap7 platform only. See docs/user/AutoMemories.md.| 0|
106+
| <a name="AUTO_MEMORIES"></a>AUTO_MEMORIES| Experimental. When set to 1, memories that yosys infers in the RTL (`$mem_v2` cells, module boundary only) are detected pre-synthesis, inventoried in `$(RESULTS_DIR)/memories.json`, and — when idiomatic as an SRAM macro — given generated .lib/.lef views under `$(RESULTS_DIR)/memories/`. Synthesis blackboxes those modules and all later stages read the generated views, so an existing design gets macro-based results without a memory compiler. Supported for the asap7 platform only. See docs/user/AutoMemories.md.| 0|
107107
| <a name="BALANCE_ROWS"></a>BALANCE_ROWS| Balance rows during placement.| 0|
108108
| <a name="BLOCKS"></a>BLOCKS| Blocks used as hard macros in a hierarchical flow. Do note that you have to specify block-specific inputs file in the directory mentioned by Makefile.| |
109109
| <a name="BUFFER_PORTS_ARGS"></a>BUFFER_PORTS_ARGS| Specify arguments to the buffer_ports call during placement. Only used if DONT_BUFFER_PORTS=0.| |

‎flow/Makefile‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ do-auto-memories: $(RESULTS_DIR)/memories_inferred.json
285285
--yosys-json $(RESULTS_DIR)/memories_inferred.json \
286286
--out-dir $(RESULTS_DIR)/memories \
287287
--json $(RESULTS_DIR)/memories.json \
288-
$(foreach file,$(VERILOG_FILES),--verilog $(file)) \
289288
$(foreach file,$(ADDITIONAL_MEMORIES),--memories $(file))
290289

291290
$(RESULTS_DIR)/memories.json: $(RESULTS_DIR)/memories_inferred.json $(ADDITIONAL_MEMORIES) \

‎flow/scripts/memories/detect.py‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ def parse_param_int(val) -> int:
3232
def scan_yosys_json(data: dict | str | Path) -> list[schema.Memory]:
3333
"""Extract memory modules from a Yosys netlist JSON output ($mem_v2 cells)."""
3434
if isinstance(data, (str, Path)):
35-
p = Path(data)
36-
if not p.is_file():
37-
return []
38-
data = json.loads(p.read_text())
35+
data = json.loads(Path(data).read_text())
3936

4037
out: list[schema.Memory] = []
4138
modules = data.get("modules", {})
@@ -176,12 +173,3 @@ def scan_yosys_json(data: dict | str | Path) -> list[schema.Memory]:
176173
out.append(mem)
177174

178175
return out
179-
180-
181-
def scan_files(paths: list[Path]) -> list[schema.Memory]:
182-
"""Scan Yosys netlist JSON files; later definitions of a name win."""
183-
found: dict[str, schema.Memory] = {}
184-
for path in paths:
185-
for memory in scan_yosys_json(path):
186-
found[memory.name] = memory
187-
return list(found.values())

‎flow/scripts/memories/gen_memories.py‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env python3
22
"""AUTO_MEMORIES driver: detect memories, judge them, emit macro views.
33
4-
Run pre-synthesis (before canonicalization). Scans the design's Verilog
5-
for memory-shaped modules, merges user-supplied `.memories` files
4+
Run pre-synthesis (before canonicalization). Reads the netlist JSON
5+
that extract_memories.tcl writes after yosys `proc; memory -nomap`,
6+
collects its $mem_v2 cells, merges user-supplied `.memories` files
67
(ADDITIONAL_MEMORIES), applies the idiomatic-macro gate, then writes:
78
89
<json> full inventory, converted or not (memories.json)
@@ -45,21 +46,17 @@ def find_fakeram_run() -> str | None:
4546

4647

4748
def run(
48-
verilog: list[Path],
49+
yosys_json: Path,
4950
memories_files: list[Path],
5051
platform: str,
5152
out_dir: Path,
5253
json_path: Path,
53-
yosys_json: Path | None = None,
5454
) -> int:
5555
if platform != "asap7":
5656
sys.stderr.write(f"gen_memories: unsupported platform {platform}\n")
5757
return 1
5858

59-
if yosys_json and yosys_json.is_file():
60-
found = detect.scan_yosys_json(yosys_json)
61-
else:
62-
found = detect.scan_files(verilog)
59+
found = detect.scan_yosys_json(yosys_json)
6360

6461
idiomatic.apply(found)
6562

@@ -108,17 +105,10 @@ def run(
108105

109106
def main(argv: list[str] | None = None) -> int:
110107
p = argparse.ArgumentParser(description=__doc__)
111-
p.add_argument(
112-
"--verilog",
113-
action="append",
114-
default=[],
115-
type=Path,
116-
help="Verilog source file to scan (repeatable).",
117-
)
118108
p.add_argument(
119109
"--yosys-json",
110+
required=True,
120111
type=Path,
121-
default=None,
122112
help="Yosys netlist JSON file containing $mem_v2 primitives.",
123113
)
124114
p.add_argument(
@@ -144,12 +134,11 @@ def main(argv: list[str] | None = None) -> int:
144134
)
145135
args = p.parse_args(argv)
146136
return run(
147-
args.verilog,
137+
args.yosys_json,
148138
args.memories,
149139
args.platform,
150140
args.out_dir,
151141
args.json,
152-
args.yosys_json,
153142
)
154143

155144

‎flow/scripts/memories/gen_memories_test.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@ def test_bad_memories_file_fails(self):
163163
with self.assertRaises(json.JSONDecodeError):
164164
self.run_generator(memories_files=[bad])
165165

166+
def test_missing_yosys_json_fails(self):
167+
d = Path(self.tmp.name)
168+
with self.assertRaises(FileNotFoundError):
169+
gen_memories.main(
170+
[
171+
"--platform",
172+
"asap7",
173+
"--out-dir",
174+
str(d / "memories"),
175+
"--json",
176+
str(d / "memories.json"),
177+
"--yosys-json",
178+
str(d / "missing.json"),
179+
]
180+
)
181+
166182
def test_no_memories_still_writes_contract_files(self):
167183
d = Path(self.tmp.name)
168184
empty_netlist = d / "empty.json"

‎flow/scripts/variables.json‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎flow/scripts/variables.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ SYNTH_USE_SYN:
192192
default: 0
193193
AUTO_MEMORIES:
194194
description: >
195-
Experimental. When set to 1, memory-shaped modules in the RTL
196-
(firtool/CIRCT `R*/W*/RW*_` port convention, module boundary only)
197-
are detected pre-synthesis, inventoried in
195+
Experimental. When set to 1, memories that yosys infers in the RTL
196+
(`$mem_v2` cells, module boundary only) are detected
197+
pre-synthesis, inventoried in
198198
`$(RESULTS_DIR)/memories.json`, and — when idiomatic as an SRAM
199199
macro — given generated .lib/.lef views under
200200
`$(RESULTS_DIR)/memories/`. Synthesis blackboxes those modules and

0 commit comments

Comments
 (0)