Skip to content

Commit 302dabb

Browse files
authored
Merge pull request #4744 from oharboe/rom-not-counted
synth: a ROM is not counted against SYNTH_MEMORY_MAX_BITS
2 parents f21a580 + fc2166d commit 302dabb

5 files changed

Lines changed: 97 additions & 7 deletions

File tree

‎docs/user/FlowVariables.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ configuration file.
308308
| <a name="SYNTH_INSBUF"></a>SYNTH_INSBUF| Insert input buffers on top-level input ports during synthesis. Useful to disable when doing parallel synthesis and concatenating netlists later as we're generating netlists of submodules.| 1|
309309
| <a name="SYNTH_KEEP_MOCKED_MEMORIES"></a>SYNTH_KEEP_MOCKED_MEMORIES| When `SYNTH_MOCK_LARGE_MEMORIES=1`, setting this to 1, will keep mocked memories (not flattening them). This preserves some of the access logic complexity and avoids optimizations outside of the mocked memory.| 1|
310310
| <a name="SYNTH_KEEP_MODULES"></a>SYNTH_KEEP_MODULES| Mark modules to keep from getting removed in flattening. A name that matches no module of the design stops synthesis.| |
311-
| <a name="SYNTH_MEMORY_MAX_BITS"></a>SYNTH_MEMORY_MAX_BITS| Maximum number of bits for memory synthesis. Ideally, real RAM or realistic fakeram should be used for RAMs much larger than 1024 bits. To temporarily ignore the RAM concerns and investigate other aspects of the design, consider setting `SYNTH_MOCK_LARGE_MEMORIES=1`, or adjusting `SYNTH_MEMORY_MAX_BITS`.| 4096|
311+
| <a name="SYNTH_MEMORY_MAX_BITS"></a>SYNTH_MEMORY_MAX_BITS| Maximum number of bits for memory synthesis. Ideally, real RAM or realistic fakeram should be used for RAMs much larger than 1024 bits. To temporarily ignore the RAM concerns and investigate other aspects of the design, consider setting `SYNTH_MOCK_LARGE_MEMORIES=1`, or adjusting `SYNTH_MEMORY_MAX_BITS`. ROMs, inferred memories with no write port, are not counted: a ROM holds constants that synthesis maps to logic like any other, and it goes through the normal flow. The limit is there so that a read/write memory, which belongs in a macro (`AUTO_MEMORIES`, fakeram) or a mock, does not go undetected into flip-flops; raising it to admit a large ROM would let read/write memories of that size through too.| 4096|
312312
| <a name="SYNTH_MINIMUM_KEEP_SIZE"></a>SYNTH_MINIMUM_KEEP_SIZE| For hierarchical synthesis, we keep modules of larger area than given by this variable and flatten smaller modules. The area unit used is the size of a basic nand2 gate from the platform's standard cell library. The default value is platform specific.| 0|
313313
| <a name="SYNTH_MOCK_LARGE_MEMORIES"></a>SYNTH_MOCK_LARGE_MEMORIES| Reduce Yosys inferred memories larger than SYNTH_MEMORY_MAX_BITS to 1 row. Yosys will generally infer memories from behavioral Verilog code, whether the memories are in standalone modules or instantiated within some larger module. fakeram and empty Verilog memories(blackboxes) of memories will not be inferred memories by Yosys and are therefore not affected by this variable. This is useful and convenient to separate the concern of instantiating and placing memories from investigating other issues with a design, though it comes at the expense of the increased accuracy that using realistic fakemem would provide. Memories with a single 1 row will of course have unrealistically good timing and area characteristics, but timing will still correctly terminate in a register. Large port memories, typically register files, will still have the retain a lot of the port logic that can be useful to investigate issues. This can be especially useful during development of designs where the behavioral model comes first and suitable memories are matched up when the design RTL is stable. A typical use case would be Chisel which will generate a behavioral model for a memories with the required clocks, ports, etc. in addition to a computer readable file with the specification of the memories that is used to [automatically](https://chipyard.readthedocs.io/en/stable/Tools/Barstools.html/) match up suitable memory macros later in the flow. During an architectural screening study, a large range of memory configurations can be investigated quickly with this option, without getting bogged down in the concern of how to realize the memories in silicon for emphemral RTL configurations that exist only long enough to run through the ORFS flow to create a table of some characteristics of a design configuration.| 0|
314314
| <a name="SYNTH_NETLIST_FILES"></a>SYNTH_NETLIST_FILES| Skips synthesis and uses the supplied netlist files. If the netlist files contains duplicate modules, which can happen when using hierarchical synthesis on indvidual netlist files and combining here, subsequent modules are silently ignored and only the first module is used.| |

‎flow/scripts/mem_dump.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,29 @@ def format_ram_table_from_json(data, max_bits=None):
6969
instance_bits = size * width
7070
bits = instance_bits * len(instances)
7171

72-
entries.append((size, width, bits, module_name, ", ".join(instances)))
72+
# A ROM has no write port: its contents are constants that
73+
# synthesis maps to logic, so it is not storage a RAM would
74+
# replace and SYNTH_MEMORY_MAX_BITS does not apply to it.
75+
rom = int(parameters["WR_PORTS"], 2) == 0
7376

74-
if max_bits is not None and instance_bits > max_bits:
77+
entries.append((size, width, bits, module_name, ", ".join(instances), rom))
78+
79+
if max_bits is not None and instance_bits > max_bits and not rom:
7580
max_ok = False
7681

7782
entries.sort(key=lambda x: x[2], reverse=True)
7883

79-
for entry in entries:
80-
table += formatting.format(*entry)
84+
for size, width, bits, module, instances, rom in entries:
85+
kind = "ROM " if rom else ""
86+
table += formatting.format(size, width, bits, kind + module, instances)
8187

8288
# ---- Summary statistics ----
8389
total_bits = sum(e[2] for e in entries)
8490

8591
largest_instance_bits = 0
8692
largest_instance_module = None
8793

88-
for size, width, _, module, _ in entries:
94+
for size, width, _, module, _, _ in entries:
8995
instance_bits = size * width
9096
if instance_bits > largest_instance_bits:
9197
largest_instance_bits = instance_bits

‎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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ SYNTH_MEMORY_MAX_BITS:
264264
To temporarily ignore the RAM concerns and investigate other
265265
aspects of the design, consider setting `SYNTH_MOCK_LARGE_MEMORIES=1`,
266266
or adjusting `SYNTH_MEMORY_MAX_BITS`.
267+
ROMs, inferred memories with no write port, are not counted: a ROM
268+
holds constants that synthesis maps to logic like any other, and it
269+
goes through the normal flow. The limit is there so that a
270+
read/write memory, which belongs in a macro (`AUTO_MEMORIES`,
271+
fakeram) or a mock, does not go undetected into flip-flops; raising
272+
it to admit a large ROM would let read/write memories of that size
273+
through too.
267274
default: 4096
268275
stages:
269276
- synth

‎flow/test/test_mem_dump.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
"""SYNTH_MEMORY_MAX_BITS stops synthesis on a read/write memory over the
3+
limit, and lets a ROM of the same size through.
4+
5+
mem_dump.py reads the memories yosys inferred (mem.json) and exits with
6+
an error when one is over the limit. A ROM, a $mem_v2 with no write
7+
port, holds constants that synthesis maps to logic: it is listed, marked
8+
as a ROM, and not counted.
9+
"""
10+
11+
import json
12+
import os
13+
import subprocess
14+
import sys
15+
import tempfile
16+
import unittest
17+
18+
MEM_DUMP = os.path.join(
19+
os.path.dirname(os.path.abspath(__file__)), "..", "scripts", "mem_dump.py"
20+
)
21+
22+
23+
def mem_json(wr_ports, size=64, width=128):
24+
def b(n):
25+
return format(n, "032b")
26+
27+
return {
28+
"modules": {
29+
"top": {
30+
"attributes": {},
31+
"cells": {
32+
"mem": {
33+
"type": "$mem_v2",
34+
"attributes": {},
35+
"parameters": {
36+
"SIZE": b(size),
37+
"WIDTH": b(width),
38+
"WR_PORTS": b(wr_ports),
39+
},
40+
}
41+
},
42+
}
43+
}
44+
}
45+
46+
47+
class TestMemDump(unittest.TestCase):
48+
def run_mem_dump(self, data, max_bits):
49+
with tempfile.TemporaryDirectory() as tmp:
50+
path = os.path.join(tmp, "mem.json")
51+
with open(path, "w") as f:
52+
json.dump(data, f)
53+
return subprocess.run(
54+
[sys.executable, MEM_DUMP, "--max-bits", str(max_bits), path],
55+
capture_output=True,
56+
text=True,
57+
)
58+
59+
def test_ram_over_limit_stops(self):
60+
# 64 x 128 = 8192 bits, one write port
61+
result = self.run_mem_dump(mem_json(wr_ports=1), 4096)
62+
self.assertNotEqual(result.returncode, 0, result.stdout)
63+
self.assertIn("exceeds SYNTH_MEMORY_MAX_BITS", result.stderr)
64+
65+
def test_rom_over_limit_passes(self):
66+
# the same 8192 bits with no write port
67+
result = self.run_mem_dump(mem_json(wr_ports=0), 4096)
68+
self.assertEqual(result.returncode, 0, result.stderr)
69+
self.assertIn("ROM top", result.stdout)
70+
71+
def test_ram_under_limit_passes(self):
72+
result = self.run_mem_dump(mem_json(wr_ports=1), 8192)
73+
self.assertEqual(result.returncode, 0, result.stderr)
74+
75+
76+
if __name__ == "__main__":
77+
unittest.main()

0 commit comments

Comments
 (0)