Skip to content

Commit e4daf84

Browse files
authored
Merge pull request #4389 from oharboe/auto-memories
flow: AUTO_MEMORIES generated memory macros via FakeRAM2.0 (experimental)
2 parents abbaff4 + 5e45ad1 commit e4daf84

27 files changed

Lines changed: 1846 additions & 19 deletions

‎MODULE.bazel‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ git_override(
131131
remote = "https://github.com/povik/yosys-slang.git",
132132
)
133133

134+
bazel_dep(name = "fakeram", dev_dependency = True)
135+
git_override(
136+
module_name = "fakeram",
137+
commit = "6f62c655952b683cda679d80850cb557cc3c262e",
138+
patch_strip = 1,
139+
patches = [
140+
"//patches/fakeram:0001-Add-ORFS-ASAP7-backend.patch",
141+
],
142+
remote = "https://github.com/The-OpenROAD-Project/FakeRAM2.0.git",
143+
)
144+
134145
# --- Extensions ---
135146

136147
register_toolchains(

‎docs/toc.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ entries:
1919
title: Build Using WSL
2020
- file: user/FlowVariables.md
2121
title: Flow Variables
22+
- file: user/AutoMemories.md
23+
title: Generated Memory Macros (AUTO_MEMORIES)
2224
- file: contrib/Metrics.md
2325
title: Metrics
2426
- file: user/InstructionsForAutoTuner.md

‎docs/user/AutoMemories.md‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# AUTO_MEMORIES: generated memory macros
2+
3+
**Experimental.** AUTO_MEMORIES detects memories in a design's RTL
4+
before synthesis and generates abstract `.lib`/`.lef` macro views for
5+
them, so an existing design gets macro-based synthesis and physical
6+
design results — including RTL-MP macro placement — without a memory
7+
compiler or hand-maintained fakeram files.
8+
9+
The intended audience is flows built on top of ORFS (for example
10+
bazel-orfs based in-house flows) that need early, reasonable physical
11+
results on designs whose memories exist only as behavioral RTL. Support
12+
posture: if it works for you, great; if it needs fixing, the report is
13+
itself a welcome signal that someone is using it.
14+
15+
## What it does
16+
17+
With `AUTO_MEMORIES=1`, a pre-synthesis step runs
18+
`scripts/memories/gen_memories.py` over `VERILOG_FILES` and writes:
19+
20+
| File | Content |
21+
| --- | --- |
22+
| `$(RESULTS_DIR)/memories.json` | Inventory of every detected memory: geometry, full pin list with pin functions, behavioral model, and whether it was converted (`idiomatic`) with a reason. |
23+
| `$(RESULTS_DIR)/memories/<m>.lib` | Generated Liberty view per converted memory. |
24+
| `$(RESULTS_DIR)/memories/<m>_pre_layout.lib` | Ideal-clock (zero clock-tree insertion) variant for pre-CTS consumers that select lib files themselves. The Makefile flow uses `<m>.lib` throughout. |
25+
| `$(RESULTS_DIR)/memories/<m>.lef` | Abstract LEF per converted memory. |
26+
| `$(RESULTS_DIR)/memories/blackboxes.txt` | Names of the converted modules — what synthesis blackboxes. |
27+
28+
Synthesis (canonicalization) blackboxes the converted modules so the
29+
liberty view wins over their behavioral bodies; floorplan through final
30+
read the generated `.lib`/`.lef` alongside `ADDITIONAL_LIBS`/
31+
`ADDITIONAL_LEFS`. Everything downstream keys off the files above —
32+
nothing else passes between the generator and the flow. That file-based
33+
handoff is what lets build systems (e.g. bazel-orfs) declare the
34+
generated artifacts as ordinary stage outputs and transitive
35+
dependencies of the remaining stages.
36+
37+
Memories that are *not* converted stay in the netlist and synthesize to
38+
flip-flops, like any other RTL.
39+
40+
## How memories are detected
41+
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).
49+
50+
Two consequences, documented as deliberate scope:
51+
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.
59+
- **No banking.** Each detected memory maps to exactly one macro. A
60+
memory too wide, too deep, or too ported for a single sensible macro
61+
is not decomposed across several macros — a future extension.
62+
63+
## The idiomatic gate
64+
65+
Not every detected memory should be a macro. `scripts/memories/
66+
idiomatic.py` applies simple floors (minimum depth 16, minimum capacity
67+
256 bits, at most 4 ports); memories below them are cheaper as
68+
flip-flops than as a macro paying the fixed control/decode/sense-amp
69+
floor. Rejected memories are kept in `memories.json` with
70+
`"idiomatic": false` and a reason.
71+
72+
To overrule the gate, list a `.memories` file in `ADDITIONAL_MEMORIES`:
73+
74+
```json
75+
{
76+
"version": 1,
77+
"memories": [
78+
{
79+
"name": "tag_array",
80+
"idiomatic": true,
81+
"reason": "forced: the RTL provides no behavioral fallback"
82+
}
83+
]
84+
}
85+
```
86+
87+
Entries merge by name onto the detected set: fields the override
88+
carries win, everything else (geometry, pins) is kept from detection. A
89+
`.memories` entry naming a module the scanner never found is taken
90+
whole — it must then describe its pins itself. The
91+
`designs/asap7/tinyRocket` design demonstrates the forced-conversion
92+
case: its `tag_array` wrapper is 4 entries deep (rejected by the gate)
93+
but instantiates a module the sources never define, so flops are not an
94+
option and the design forces conversion.
95+
96+
## Generated views
97+
98+
The `.lib` mirrors the structural shape OpenROAD's abstract writer
99+
produces for hardened blocks: `bus()` groups **with per-bit `pin()`
100+
records** (a bus without per-bit siblings makes yosys silently drop bit
101+
connections at parent instances), per-port clock pins with
102+
`min/max_clock_tree_path` arcs, setup/hold constraints on inputs,
103+
clock-to-out arcs on outputs, and `internal_power()` records under a
104+
`power_lut_template` so SAIF-driven power reporting is non-zero.
105+
106+
The `.lef` is an abstract following the conventions of the platform's
107+
fakeram abstracts: `CLASS BLOCK`, per-bit signal pin pads stacked along
108+
the macro edge, interleaved horizontal power/ground straps the
109+
platform's PDN macro grid connects to, and a full-footprint multi-layer
110+
`OBS`.
111+
112+
Timing and area come from simple parametric models
113+
(`scripts/memories/liberty.py`, `scripts/memories/sram_area_model.py`):
114+
log2(rows) decode depth and √bits bit-line scaling for timing; an area
115+
model anchored to published 7 nm SP-SRAM figures (Suzuki et al., ISSCC
116+
2018). These are budgetary models — good enough to make floorplanning,
117+
placement, and timing behave representatively; not sign-off numbers.
118+
119+
## Platform support
120+
121+
**asap7 only.** The emitters are split into general structure
122+
(`liberty.py`, `lef.py`, parameterized by a `PdkParams`) and platform
123+
constants (`pdk_asap7.py`: pins and power straps on M4 — where the
124+
platform's PDN macro grid connects — pin pad/pitch, strap geometry,
125+
OBS layers, nominal voltage). Generalizing to other PDKs means
126+
providing their `PdkParams` — the code seam exists, the calibration
127+
work does not. `AUTO_MEMORIES=1` on any other platform fails with a
128+
clear error.
129+
130+
## Trying it
131+
132+
```shell
133+
# Unit tests (fast, no EDA tools):
134+
bazelisk test //flow:memories_tests
135+
136+
# The demo design:
137+
make DESIGN_CONFIG=designs/asap7/tinyRocket/config.mk synth floorplan
138+
```
139+
140+
The generator can also be run standalone to inspect what it would do:
141+
142+
```shell
143+
python3 flow/scripts/memories/gen_memories.py \
144+
--platform asap7 --out-dir /tmp/mems --json /tmp/memories.json \
145+
--verilog flow/designs/src/tinyRocket/freechips.rocketchip.system.TinyConfig.v
146+
```
147+
148+
## Consuming from bazel-orfs
149+
150+
Everything downstream keys off generated files, so a build system can
151+
declare them as ordinary stage outputs and transitive dependencies. In
152+
bazel-orfs each stage runs in a sandbox where only declared outputs
153+
survive, so it additionally needs to declare `memories.json` plus the
154+
`memories/` directory (a directory artifact — the per-memory file
155+
names are only known at run time) as canonicalize outputs and stage
156+
them into every downstream stage's sandbox. The bazel-orfs change that
157+
does this is carried alongside this feature as
158+
`flow/scripts/memories/bazel-orfs-auto-memories.patch`, to be
159+
upstreamed to bazel-orfs once the feature lands here.
160+
161+
## Variables
162+
163+
- [AUTO_MEMORIES](FlowVariables.md#AUTO_MEMORIES)
164+
- [ADDITIONAL_MEMORIES](FlowVariables.md#ADDITIONAL_MEMORIES)

‎docs/user/FlowVariables.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ configuration file.
100100
| <a name="ADDITIONAL_GDS"></a>ADDITIONAL_GDS| Hardened macro GDS files listed here.| |
101101
| <a name="ADDITIONAL_LEFS"></a>ADDITIONAL_LEFS| Hardened macro LEF view files listed here. The LEF information of the macros is immutable and used throughout all stages. Stored in the .odb file.| |
102102
| <a name="ADDITIONAL_LIBS"></a>ADDITIONAL_LIBS| Hardened macro library files listed here. The library information is immutable and used throughout all stages. Not stored in the .odb file.| |
103+
| <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.| |
103104
| <a name="ADDITIONAL_SITES"></a>ADDITIONAL_SITES| Passed as -additional_sites to initialize_floorplan.| |
104105
| <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|
105107
| <a name="BALANCE_ROWS"></a>BALANCE_ROWS| Balance rows during placement.| 0|
106108
| <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.| |
107109
| <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.| |
@@ -340,6 +342,7 @@ configuration file.
340342
- [ABC_DRIVER_CELL](#ABC_DRIVER_CELL)
341343
- [ABC_LOAD_IN_FF](#ABC_LOAD_IN_FF)
342344
- [ADDER_MAP_FILE](#ADDER_MAP_FILE)
345+
- [ADDITIONAL_MEMORIES](#ADDITIONAL_MEMORIES)
343346
- [CACHED_REPORTS](#CACHED_REPORTS)
344347
- [CLKGATE_MAP_FILE](#CLKGATE_MAP_FILE)
345348
- [DFF_LIB_FILE](#DFF_LIB_FILE)
@@ -627,6 +630,7 @@ configuration file.
627630
- [ADDITIONAL_FILES](#ADDITIONAL_FILES)
628631
- [ADDITIONAL_LEFS](#ADDITIONAL_LEFS)
629632
- [ADDITIONAL_LIBS](#ADDITIONAL_LIBS)
633+
- [AUTO_MEMORIES](#AUTO_MEMORIES)
630634
- [BLOCKS](#BLOCKS)
631635
- [CAP_MARGIN](#CAP_MARGIN)
632636
- [CDL_FILES](#CDL_FILES)

‎flow/BUILD‎

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel-orfs//:openroad.bzl", "orfs_pdk")
2+
load("@rules_python//python:defs.bzl", "py_library", "py_test")
23

34
# Expose every individual file under platforms/ as a public source-file
45
# target, so designs in other packages can refer to e.g.
@@ -23,37 +24,39 @@ exports_files(
2324
# instead of vendoring its own (drifting) copy at the bazel-orfs repo
2425
# root. See bazel-orfs `private/rules.bzl`.
2526
exports_files(
26-
[
27-
"scripts/synth.tcl",
28-
# Expose variables.yaml as an addressable source label so the
29-
# orfs-vars MCP server (//tools/orfs_mcp) can data-dep the ORFS
30-
# variable catalogue pinned to this commit.
31-
"scripts/variables.yaml",
32-
],
27+
["scripts/synth.tcl"],
3328
visibility = ["//visibility:public"],
3429
)
3530

3631
# files shared between scripts/synth.sh and scripts/flow.sh steps
3732
MAKEFILE_SHARED = [
3833
"scripts/variables.json",
3934
"scripts/*.py",
35+
"scripts/memories/*.py",
4036
"scripts/*.sh",
4137
"scripts/*.yaml",
4238
"scripts/*.mk",
4339
]
4440

41+
MAKEFILE_SHARED_EXCLUDE = [
42+
"scripts/memories/*_test.py",
43+
]
44+
4545
# makefile and scripts used by script/synth.sh steps
4646
filegroup(
4747
name = "makefile_yosys",
4848
srcs = ["Makefile"],
49-
data = glob(MAKEFILE_SHARED + [
50-
"scripts/*.script",
51-
"scripts/*.v",
52-
"scripts/util.tcl",
53-
"scripts/synth*.tcl",
54-
"scripts/synth*.v",
55-
"platforms/common/**/*.v",
56-
]) + [
49+
data = glob(
50+
MAKEFILE_SHARED + [
51+
"scripts/*.script",
52+
"scripts/*.v",
53+
"scripts/util.tcl",
54+
"scripts/synth*.tcl",
55+
"scripts/synth*.v",
56+
"platforms/common/**/*.v",
57+
],
58+
exclude = MAKEFILE_SHARED_EXCLUDE,
59+
) + [
5760
"//flow/util:makefile_yosys",
5861
],
5962
visibility = ["//visibility:public"],
@@ -63,10 +66,13 @@ filegroup(
6366
filegroup(
6467
name = "makefile",
6568
srcs = ["Makefile"],
66-
data = glob(MAKEFILE_SHARED + [
67-
"scripts/*.tcl",
68-
"platforms/common/**/*.v",
69-
]) + [
69+
data = glob(
70+
MAKEFILE_SHARED + [
71+
"scripts/*.tcl",
72+
"platforms/common/**/*.v",
73+
],
74+
exclude = MAKEFILE_SHARED_EXCLUDE,
75+
) + [
7076
"//flow/util:makefile",
7177
],
7278
visibility = ["//visibility:public"],
@@ -179,3 +185,47 @@ filegroup(
179185
"sky130hd",
180186
"sky130hs",
181187
]]
188+
189+
# AUTO_MEMORIES: pre-synthesis memory detection and .lib/.lef macro view
190+
# generation. See docs/user/AutoMemories.md. The unit tests are the
191+
# feature's test coverage — run with `bazelisk test //flow:memories_tests`.
192+
py_library(
193+
name = "memories",
194+
srcs = glob(
195+
["scripts/memories/*.py"],
196+
exclude = ["scripts/memories/*_test.py"],
197+
),
198+
imports = ["scripts/memories"],
199+
data = ["@fakeram//:all_files"],
200+
)
201+
202+
# Test fixtures (Verilog snippets) live in detect_test.py and are
203+
# imported by the other tests.
204+
py_library(
205+
name = "memories_test_fixtures",
206+
srcs = ["scripts/memories/detect_test.py"],
207+
imports = ["scripts/memories"],
208+
deps = [":memories"],
209+
)
210+
211+
MEMORIES_TESTS = [
212+
"detect_test",
213+
"gen_memories_test",
214+
"idiomatic_test",
215+
"schema_test",
216+
]
217+
218+
[py_test(
219+
name = "memories_" + test,
220+
srcs = ["scripts/memories/{}.py".format(test)],
221+
main = "scripts/memories/{}.py".format(test),
222+
deps = [
223+
":memories",
224+
":memories_test_fixtures",
225+
],
226+
) for test in MEMORIES_TESTS]
227+
228+
test_suite(
229+
name = "memories_tests",
230+
tests = ["memories_" + test for test in MEMORIES_TESTS],
231+
)

‎flow/Makefile‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,33 @@ $(SDC_FILE_CLOCK_PERIOD): $(SDC_FILE)
261261
.PHONY: yosys-dependencies
262262
yosys-dependencies: $(YOSYS_DEPENDENCIES)
263263

264+
# AUTO_MEMORIES: detect memories in the RTL pre-synthesis and generate
265+
# .lib/.lef macro views for them. Experimental; see
266+
# docs/user/AutoMemories.md.
267+
ifeq ($(AUTO_MEMORIES),1)
268+
$(RESULTS_DIR)/memories_inferred.json: $(VERILOG_FILES) $(SCRIPTS_DIR)/memories/extract_memories.tcl
269+
mkdir -p $(RESULTS_DIR) $(LOG_DIR)
270+
$(SCRIPTS_DIR)/synth.sh $(SCRIPTS_DIR)/memories/extract_memories.tcl $(LOG_DIR)/memories_extract.log
271+
272+
.PHONY: do-auto-memories
273+
do-auto-memories: $(RESULTS_DIR)/memories_inferred.json
274+
mkdir -p $(RESULTS_DIR)
275+
$(PYTHON_EXE) $(SCRIPTS_DIR)/memories/gen_memories.py \
276+
--platform $(PLATFORM) \
277+
--yosys-json $(RESULTS_DIR)/memories_inferred.json \
278+
--out-dir $(RESULTS_DIR)/memories \
279+
--json $(RESULTS_DIR)/memories.json \
280+
$(foreach file,$(VERILOG_FILES),--verilog $(file)) \
281+
$(foreach file,$(ADDITIONAL_MEMORIES),--memories $(file))
282+
283+
$(RESULTS_DIR)/memories.json: $(RESULTS_DIR)/memories_inferred.json $(ADDITIONAL_MEMORIES) \
284+
$(wildcard $(SCRIPTS_DIR)/memories/*.py)
285+
$(UNSET_AND_MAKE) do-auto-memories
286+
287+
yosys-dependencies: $(RESULTS_DIR)/memories.json
288+
$(RESULTS_DIR)/1_1_yosys_canonicalize.rtlil: $(RESULTS_DIR)/memories.json
289+
endif
290+
264291
.PHONY: do-yosys
265292
do-yosys: yosys-dependencies
266293
$(SCRIPTS_DIR)/synth.sh $(SYNTH_SCRIPT) $(LOG_DIR)/1_2_yosys.log
@@ -281,6 +308,7 @@ $(RESULTS_DIR)/1_2_yosys.v: $(RESULTS_DIR)/1_1_yosys_canonicalize.rtlil
281308
.PHONY: clean_synth
282309
clean_synth:
283310
rm -f $(RESULTS_DIR)/1_* $(RESULTS_DIR)/mem*.json
311+
rm -rf $(RESULTS_DIR)/memories
284312
rm -f $(REPORTS_DIR)/synth_*
285313
rm -f $(LOG_DIR)/1_*
286314
rm -f $(SYNTH_STATS)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
load("//flow/designs:design.bzl", "design")
2+
3+
design(config = "config.mk")

0 commit comments

Comments
 (0)