Skip to content

Commit 4ca4b38

Browse files
authored
Merge pull request #4650 from oharboe/auto-memories-keep-cost
AUTO_MEMORIES: a macro's blackbox costs its generated view's area
2 parents 4137d73 + b59c1d1 commit 4ca4b38

5 files changed

Lines changed: 80 additions & 12 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: 53 additions & 4 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 { } {
@@ -248,4 +267,34 @@ proc convert_liberty_areas { } {
248267
set gate_eq [expr int($area / $found_cell_area)]
249268
rtlil::set_attr -mod -uint $box gate_cost_equivalent $gate_eq
250269
}
270+
271+
# An AUTO_MEMORIES macro is a blackbox from the RTL's own module, not a
272+
# Liberty cell: it costs its generated view's area, or
273+
# keep_hierarchy -min_cost refuses it ("Missing cost information on
274+
# instanced blackbox"). Its view is not read in its place, as its pins
275+
# need not be the module's (FakeRAM uses its own names).
276+
# One name per line, not a Tcl list: yosys lists an escaped module with
277+
# its leading backslash (`\1d_ram`), which list parsing reads as an
278+
# escape, and blackboxes.txt names it as read_slang takes it (`1d_ram`).
279+
set modules {}
280+
foreach line [split [tee -q -s result.string select -list-mod =*] "\n"] {
281+
set line [string trim $line]
282+
if { $line ne "" } {
283+
lappend modules [regsub {^\\} $line {}]
284+
}
285+
}
286+
dict for {m area} [auto_memories_areas] {
287+
if { [lsearch -exact $modules $m] < 0 } {
288+
# slang imports a blackboxed module only where it is instanced
289+
continue
290+
}
291+
if { ![rtlil::has_attr -mod $m blackbox] } {
292+
# e.g. slang's per-instance `<definition>$<instance path>`, which
293+
# --blackboxed-module does not match: the body was elaborated
294+
error "AUTO_MEMORIES macro $m is in the design but was not\
295+
blackboxed; its name does not match the module's definition"
296+
}
297+
rtlil::set_attr -mod -uint $m gate_cost_equivalent \
298+
[expr { int($area / $found_cell_area) }]
299+
}
251300
}

‎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)