Skip to content

Commit a3305f1

Browse files
oharboeclaude
andcommitted
AUTO_MEMORIES: blackboxes.txt carries each macro's area
The cost loop read a macro's area out of its .lib with a regular expression that matched only because FakeRAM writes `area :` first in the cell group. FakeRAM knows the area when it writes the view, so it now writes it beside the module name, `<module> <area>` in um^2 per line, and synthesis reads it from there. A line of any other shape is an error. gen_memories_test.py checks the area against the one the .lib states. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
1 parent 8411e9c commit a3305f1

5 files changed

Lines changed: 52 additions & 30 deletions

File tree

‎docs/user/AutoMemories.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ into `$(RESULTS_DIR)/memories_inferred.json`, and
2525
| `$(RESULTS_DIR)/memories/<m>.lib` | Generated Liberty view per converted memory. |
2626
| `$(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. |
2727
| `$(RESULTS_DIR)/memories/<m>.lef` | Abstract LEF per converted memory. |
28-
| `$(RESULTS_DIR)/memories/blackboxes.txt` | Names of the converted modules — what synthesis blackboxes. |
28+
| `$(RESULTS_DIR)/memories/blackboxes.txt` | `<module> <area>` per converted module — what synthesis blackboxes, and the area in um² its `.lib` states, which `SYNTH_MINIMUM_KEEP_SIZE` costs it by. |
2929

3030
Synthesis (canonicalization) blackboxes the converted modules so the
3131
liberty view wins over their behavioral bodies; floorplan through final

‎flow/scripts/memories/gen_memories.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
<out-dir>/<m>.lib Liberty view per converted memory
1111
<out-dir>/<m>_pre_layout.lib ideal-clock variant for pre-CTS consumers
1212
<out-dir>/<m>.lef abstract LEF per converted memory
13-
<out-dir>/blackboxes.txt converted module names, one per line —
14-
what synthesis blackboxes
13+
<out-dir>/blackboxes.txt `<module> <area>` per line, area in um^2 as
14+
the .lib states it — what synthesis
15+
blackboxes and the cost it gives each
1516
1617
Everything downstream consumes these files; nothing else is passed
1718
between the generator and the flow.

‎flow/scripts/memories/gen_memories_test.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22
import json
3+
import re
34
import sys
45
import tempfile
56
import unittest
@@ -118,14 +119,28 @@ def test_flow_contract(self):
118119
"mem_128x32_pre_layout.lib",
119120
],
120121
)
121-
self.assertEqual((mems_dir / "blackboxes.txt").read_text(), "mem_128x32\n")
122+
# one `<module> <area>` line; the area is the one the .lib states,
123+
# which synthesis costs the blackbox by
124+
[line] = (mems_dir / "blackboxes.txt").read_text().splitlines()
125+
name, area = line.split()
126+
self.assertEqual(name, "mem_128x32")
127+
lib_area = re.search(
128+
r"^\s*area\s*:\s*([0-9.]+)\s*;",
129+
(mems_dir / "mem_128x32.lib").read_text(),
130+
re.M,
131+
).group(1)
132+
self.assertEqual(float(area), float(lib_area))
133+
self.assertGreater(float(area), 0)
122134

123135
def test_additional_memories_forces_conversion(self):
124136
force = Path(self.tmp.name) / "force.memories"
125137
force.write_text(FORCE_TAGS)
126138
code, d = self.run_generator(memories_files=[force])
127139
self.assertEqual(code, 0)
128-
blackboxes = (d / "memories" / "blackboxes.txt").read_text().split()
140+
blackboxes = [
141+
line.split()[0]
142+
for line in (d / "memories" / "blackboxes.txt").read_text().splitlines()
143+
]
129144
self.assertEqual(sorted(blackboxes), ["cache_tags", "mem_128x32"])
130145
self.assertTrue((d / "memories" / "cache_tags.lib").exists())
131146
self.assertTrue((d / "memories" / "cache_tags.lef").exists())

‎flow/scripts/synth_preamble.tcl‎

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ proc read_checkpoint { file } {
4444
}
4545

4646
# AUTO_MEMORIES: memory modules whose generated liberty view replaces
47-
# their behavioral body during synthesis. The list is produced by
48-
# scripts/memories/gen_memories.py before canonicalization; see
47+
# their behavioral body during synthesis, each with that view's area in
48+
# um^2. The list is produced by scripts/memories/gen_memories.py before
49+
# canonicalization, one `<module> <area>` per line; see
4950
# docs/user/AutoMemories.md.
50-
proc auto_memories_blackboxes { } {
51+
proc auto_memories_areas { } {
5152
if { ![env_var_equals AUTO_MEMORIES 1] } {
5253
return {}
5354
}
@@ -59,7 +60,25 @@ proc auto_memories_blackboxes { } {
5960
set fh [open $f r]
6061
set content [string map {\r ""} [read $fh]]
6162
close $fh
62-
return [regexp -all -inline {\S+} $content]
63+
set areas [dict create]
64+
foreach line [split $content "\n"] {
65+
if { [string trim $line] == "" } {
66+
continue
67+
}
68+
set fields [regexp -all -inline {\S+} $line]
69+
if {
70+
[llength $fields] != 2
71+
|| ![string is double -strict [lindex $fields 1]]
72+
} {
73+
error "$f: expected `<module> <area>`, got `$line`"
74+
}
75+
dict set areas {*}$fields
76+
}
77+
return $areas
78+
}
79+
80+
proc auto_memories_blackboxes { } {
81+
return [dict keys [auto_memories_areas]]
6382
}
6483

6584
proc read_design_sources { } {
@@ -250,12 +269,12 @@ proc convert_liberty_areas { } {
250269
}
251270

252271
# An AUTO_MEMORIES macro is a blackbox from the RTL's own module, not a
253-
# Liberty cell: it costs what its generated view's area says, or
272+
# Liberty cell: it costs its generated view's area, or
254273
# keep_hierarchy -min_cost refuses it ("Missing cost information on
255274
# instanced blackbox"). Its view is not read in its place, as its pins
256275
# need not be the module's (FakeRAM uses its own names).
257276
set modules [tee -q -s result.string select -list-mod =*]
258-
foreach m [auto_memories_blackboxes] {
277+
dict for {m area} [auto_memories_areas] {
259278
if { [lsearch -exact $modules $m] < 0 } {
260279
# slang imports a blackboxed module only where it is instanced
261280
continue
@@ -266,22 +285,6 @@ proc convert_liberty_areas { } {
266285
error "AUTO_MEMORIES macro $m is in the design but was not\
267286
blackboxed; its name does not match the module's definition"
268287
}
269-
set lib "$::env(RESULTS_DIR)/memories/$m.lib"
270-
if { ![file exists $lib] } {
271-
error "AUTO_MEMORIES macro $m has no $lib"
272-
}
273-
set fh [open $lib r]
274-
set text [read $fh]
275-
close $fh
276-
# the cell group of <m> and its area; the brace characters spelled as
277-
# hex escapes, which keep the brace count of this body balanced. The
278-
# module name is escaped whole: yosys and slang names carry `$`, `\`,
279-
# `[` and `.`, each a regular-expression metacharacter.
280-
set name [regsub -all {[][\x7b\x7d()*+?.\\^$|]} $m {\\&}]
281-
set pat "cell\\s*\\(\\s*\"?$name\"?\\s*\\)\\s*\\\x7b\[^\x7d\]*?area\\s*:\\s*(\[0-9.eE+-\]+)"
282-
if { ![regexp $pat $text -> area] } {
283-
error "AUTO_MEMORIES macro $m: no area for cell $m in $lib"
284-
}
285288
rtlil::set_attr -mod -uint $m gate_cost_equivalent \
286289
[expr { int($area / $found_cell_area) }]
287290
}

‎tools/FakeRAM2.0/orfs_asap7/generate.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def run_orfs_asap7(platform: str, out_dir: Path, json_path: Path):
5454
process = Process(ASAP7_PROCESS_CONFIG)
5555
timing_data = TimingData(ASAP7_TIMING_CONFIG)
5656

57+
blackboxes = []
5758
for m in converted:
5859
name = m["name"]
5960
bits = m.get("bits", 32)
@@ -82,7 +83,9 @@ def run_orfs_asap7(platform: str, out_dir: Path, json_path: Path):
8283
with open(lef_path, "w") as f:
8384
LefExporter(ram).export(f)
8485

85-
(out_dir / "blackboxes.txt").write_text(
86-
"".join(f"{m['name']}\n" for m in converted)
87-
)
86+
# the area the .lib states, beside the name: synthesis costs the
87+
# blackbox by it
88+
blackboxes.append(f"{name} {ram.physical.get_area(False):.6f}\n")
89+
90+
(out_dir / "blackboxes.txt").write_text("".join(blackboxes))
8891
return 0

0 commit comments

Comments
 (0)