Skip to content

Commit e5cc6e7

Browse files
authored
Merge pull request #4659 from oharboe/fakeram-asap7-column-mux
FakeRAM asap7: pick a column mux factor instead of hardcoding 1
2 parents 3451ca1 + 7f37494 commit e5cc6e7

5 files changed

Lines changed: 109 additions & 4 deletions

File tree

‎flow/scripts/memories/gen_memories_test.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ def test_additional_memories_forces_conversion(self):
149149
self.assertTrue(tags["idiomatic"])
150150
self.assertIn("forced", tags["reason"])
151151

152+
def test_deep_memory_is_folded_square(self):
153+
# 512x17 with one bit line per column is 2.3 x 166.6 um, 73:1, a
154+
# sliver macro placement cannot fit; the column mux folds it. The
155+
# fold squares the bit-cell counts, and a bit cell is not square,
156+
# so the bound leaves room for that.
157+
netlist = json.loads(json.dumps(SAMPLE_YOSYS_NETLIST))
158+
mem = netlist["modules"].pop("\\mem_128x32")
159+
mem["cells"]["\\$mem_0"]["parameters"].update(SIZE=512, WIDTH=17)
160+
netlist["modules"] = {"\\mem_512x17": mem}
161+
d = Path(self.tmp.name)
162+
(d / "netlist.json").write_text(json.dumps(netlist))
163+
code = gen_memories.main(
164+
[
165+
"--platform",
166+
"asap7",
167+
"--out-dir",
168+
str(d / "memories"),
169+
"--json",
170+
str(d / "memories.json"),
171+
"--yosys-json",
172+
str(d / "netlist.json"),
173+
]
174+
)
175+
self.assertEqual(code, 0)
176+
lef = (d / "memories" / "mem_512x17.lef").read_text()
177+
size = re.search(r"SIZE\s+(\S+)\s+BY\s+(\S+)", lef)
178+
self.assertIsNotNone(size, "no SIZE in the generated LEF")
179+
width, height = map(float, size.groups())
180+
self.assertLessEqual(max(width, height) / min(width, height), 8.0)
181+
152182
def test_unsupported_platform_fails_clearly(self):
153183
code, _d = self.run_generator(platform="nangate45")
154184
self.assertEqual(code, 1)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"manufacturing_grid_nm": 1,
2121
"contacted_poly_pitch_nm": 54,
2222
"fin_pitch_nm": 27,
23-
"column_mux_factor": 1,
2423
"snap_width_nm": 190,
2524
"snap_height_nm": 1400,
2625
}

‎tools/FakeRAM2.0/test/macro_dim_test.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,39 @@ def test_macro_dim_bitcell_override(self):
109109
self.assertAlmostEqual(width, exp_width, delta=self._delta)
110110
self.assertAlmostEqual(height, exp_height, delta=self._delta)
111111

112+
def _dimensions(self, column_mux_factor, width, depth, banks):
113+
"""Macro dimensions, column_mux_factor None leaves the key out"""
114+
115+
process_data = TestUtils.get_base_process_data().copy()
116+
del process_data["column_mux_factor"]
117+
if column_mux_factor is not None:
118+
process_data["column_mux_factor"] = column_mux_factor
119+
return Process(process_data).get_macro_dimensions(width, depth, banks, 0)
120+
121+
def test_macro_dim_column_mux_picked(self):
122+
"""An absent column_mux_factor folds a deep memory square"""
123+
124+
# sqrt(512 / 17) = 5.5, the nearest power of two is 4
125+
picked = self._dimensions(None, 17, 512, 1)
126+
self.assertEqual(picked, self._dimensions(4, 17, 512, 1))
127+
width, height = picked
128+
self.assertLessEqual(max(width, height) / min(width, height), 8.0)
129+
130+
def test_macro_dim_column_mux_explicit(self):
131+
"""An explicit column_mux_factor is kept, even when it is a sliver"""
132+
133+
width, height = self._dimensions(1, 17, 512, 1)
134+
self.assertGreater(height / width, 50.0)
135+
136+
def test_macro_dim_column_mux_after_banks(self):
137+
"""The factor is picked for the array banking leaves"""
138+
139+
# 4 banks make 2048x39 a 512x156 array: sqrt(512 / 156) = 1.8,
140+
# so 2, where sqrt(2048 / 39) = 7.2 would have given 8
141+
self.assertEqual(
142+
self._dimensions(None, 39, 2048, 4), self._dimensions(2, 39, 2048, 4)
143+
)
144+
112145

113146
if __name__ == "__main__":
114147
unittest.main()

‎tools/FakeRAM2.0/test/process_test.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ def test_process_optional_snap(self):
114114
+ (process.get_pin_width_um() / 2.0),
115115
)
116116

117+
def test_process_optional_column_mux(self):
118+
"""Tests that column_mux_factor is left to be picked per memory"""
119+
120+
process_data = self._base_data.copy()
121+
del process_data["column_mux_factor"]
122+
process = Process(process_data)
123+
self.assertIsNone(process.get_column_mux_factor())
124+
117125
def test_process_misaligned_pin_mfg_grid_pitch(self):
118126
"""
119127
Tests detection that pin pitch is not a multiple of the metal track

‎tools/FakeRAM2.0/utils/class_process.py‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88
################################################################################
99

1010

11+
import math
12+
13+
14+
def pick_column_mux_factor(rows, bits):
15+
"""Fold a tall array into a square one.
16+
17+
The array is rows/factor cells tall and bits*factor cells wide, so
18+
the factor that squares it is sqrt(rows/bits). Rounded to a power of
19+
two because a column mux selects one of 2^n bit lines, and clamped
20+
at 1 so a wide, shallow memory is left alone.
21+
22+
Area does not change: this moves cells between the two dimensions
23+
rather than adding or removing any.
24+
"""
25+
if rows <= 0 or bits <= 0:
26+
return 1
27+
ideal = math.sqrt(float(rows) / float(bits))
28+
if ideal <= 1.0:
29+
return 1
30+
lo = 2 ** math.floor(math.log2(ideal))
31+
hi = lo * 2
32+
# Nearest in log space, which is what "closest to square" means when
33+
# the candidates are powers of two.
34+
return lo if (ideal / lo) <= (hi / ideal) else hi
35+
36+
1137
class Process:
1238
def __init__(self, json_data):
1339
"""Initialize from json_data imported from config file"""
@@ -23,9 +49,14 @@ def __init__(self, json_data):
2349
self.contacted_poly_pitch_nm = json_data.get("contacted_poly_pitch_nm", None)
2450
self.fin_pitch_nm = json_data.get("fin_pitch_nm", None)
2551
self.manufacturing_grid_nm = int(json_data["manufacturing_grid_nm"])
26-
self.column_mux_factor = int(json_data["column_mux_factor"])
2752

2853
# Optional keys
54+
# Absent means get_macro_dimensions picks one per memory
55+
self.column_mux_factor = (
56+
int(json_data["column_mux_factor"])
57+
if "column_mux_factor" in json_data
58+
else None
59+
)
2960
self.snap_width_nm = (
3061
int(json_data["snap_width_nm"]) if "snap_width_nm" in json_data else 1
3162
)
@@ -99,7 +130,6 @@ def get_macro_dimensions(self, width_in_bits, depth, num_banks, additional_heigh
99130
and process parameters
100131
"""
101132

102-
column_mux_factor = self.column_mux_factor
103133
bitcell_width, bitcell_height = self.get_bitcell_dimensions()
104134

105135
all_bitcell_height = bitcell_height * depth
@@ -111,6 +141,11 @@ def get_macro_dimensions(self, width_in_bits, depth, num_banks, additional_heigh
111141
elif num_banks != 1:
112142
raise Exception("Unsupported number of banks: {}".format(num_banks))
113143

144+
column_mux_factor = self.column_mux_factor
145+
if column_mux_factor is None:
146+
column_mux_factor = pick_column_mux_factor(
147+
depth / num_banks, width_in_bits * num_banks
148+
)
114149
all_bitcell_height = all_bitcell_height / column_mux_factor
115150
all_bitcell_width = all_bitcell_width * column_mux_factor
116151

@@ -202,7 +237,7 @@ def get_y_step(self):
202237
return self.y_step
203238

204239
def get_column_mux_factor(self):
205-
"""Returns the column mux factor"""
240+
"""Returns the column mux factor, None if picked per memory"""
206241
return self.column_mux_factor
207242

208243
def set_calc_dimensions(self, val):

0 commit comments

Comments
 (0)