diff --git a/flow/scripts/memories/extract_memories.tcl b/flow/scripts/memories/extract_memories.tcl index 18e5c23717..478f0e2426 100644 --- a/flow/scripts/memories/extract_memories.tcl +++ b/flow/scripts/memories/extract_memories.tcl @@ -23,11 +23,42 @@ hierarchy -top $::env(DESIGN_NAME) # bare `proc`: yosys -import cannot shadow Tcl's proc keyword, so the # bare word would define a procedure instead of running the pass. yosys proc -memory -nomap -# Write netlist JSON containing inferred $mem_v2 primitives +# The memory passes, on the modules that hold memory cells only. Every +# pass in `memory -nomap` walks every selected module, and memory_dff +# builds its per-bit driver and consumer index (ModWalker) for a module +# before it asks whether the module has a memory at all; opt_mem_priority +# and opt_mem_feedback scan every module's cells the same way. On a design +# whose memories sit in small generated modules and whose logic sits in +# large ones -- a firtool core keeps every memory in its own ram_* module +# -- that index over the memory-less modules is nearly the whole cost of +# this step. Scoping the passes to the modules with memory cells changes +# nothing in the result: a pass finds nothing to do in a module without +# them. memory_bmux2rom runs first and unscoped, since it is what turns a +# module's constant muxes into a memory. t:$mem* takes every memory cell +# type: read_verilog leaves reads as $memrd until memory_collect. `%m` +# then selects every module that holds one, whole, without going through +# its name: an escaped module or memory name may carry a slash, a `$` or a +# glob character, and none of those survives being cut out of a +# `select -list` line and handed back to `select`. +memory_bmux2rom +set n_mem_cells [lindex [tee -q -s result.string select -count t:\$mem*] 0] set out_json "$::env(RESULTS_DIR)/memories_inferred.json" file mkdir [file dirname $out_json] -write_json $out_json +if { $n_mem_cells > 0 } { + yosys select t:\$mem* %m + set mem_modules [tee -q -s result.string select -list-mod %] + set n_mem_modules [llength [split [string trim $mem_modules] "\n"]] + log "extract_memories: $n_mem_modules modules hold $n_mem_cells memory cells" + memory -nomap + # The JSON is read by gen_memories.py's detector, which looks at the + # $mem_v2 cells of each module and nothing across modules, so the + # memory modules are all it needs; the rest of the design is most of + # the bytes. + write_json -selected $out_json + select -clear +} else { + write_json $out_json +} exit