From 8a8218384f29e96859c9b5d497c0fcfca238bf23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Sun, 4 Oct 2026 17:17:41 +0200 Subject: [PATCH 1/3] memories: run the memory passes on the modules that hold memory cells MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `memory -nomap` in extract_memories.tcl walks every module of the design in each of its passes, and memory_dff builds its per-bit driver and consumer index (ModWalker::setup, memory_dff.cc:225) for a module before run() asks whether the module has a memory; opt_mem_priority and opt_mem_feedback scan every module's cells the same way. On a design that keeps its memories in small generated modules and its logic in large ones -- a firtool core has every memory in its own ram_* module -- the index over the memory-less modules is nearly the whole cost of the step. Measured on XiangShan's flat XSCore (1870 modules, 407 with memory cells, read through slang, yosys 0.68), the memory passes alone: unscoped 721 s memory_dff 462, opt_mem_priority 67, opt_mem_feedback 52, two opt_clean 99, share 37 scoped 64 s memory_dff 24, opt_mem_priority 27 Both produce the same 407 $mem_v2 cells. The extraction step as a whole went from about fifteen minutes to under four, and the four remaining minutes are reading and proc. memory_bmux2rom still runs first and unscoped, since it is what turns a module's constant muxes into a memory. The module list comes from the memory cells' names (the module before the first slash) rather than the selection's `%m` expansion: `%m` leaves a module partially selected when its name carries a `$`, as slang's uniquified names do, and every pass then skips the module with "Ignoring partially selected module" and no memory is collected. write_json -selected writes the memory modules only; gen_memories.py's detector reads each module's $mem_v2 cells and nothing across modules. Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Øyvind Harboe --- flow/scripts/memories/extract_memories.tcl | 41 ++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/flow/scripts/memories/extract_memories.tcl b/flow/scripts/memories/extract_memories.tcl index 18e5c23717..53b6b90298 100644 --- a/flow/scripts/memories/extract_memories.tcl +++ b/flow/scripts/memories/extract_memories.tcl @@ -23,11 +23,46 @@ 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. The module list comes from the +# cells' names (module before the first slash) rather than the selection's +# `%m` expansion, which leaves a module partially selected when its name +# carries a `$`, as slang's uniquified names do, and the passes then skip it. +memory_bmux2rom +set mem_modules [dict create] +set mem_cells {t:$mem_v2 t:$memrd_v2 t:$memwr_v2 t:$meminit_v2} +foreach line [tee -q -s result.string select -list {*}$mem_cells] { + set line [string trim $line] + if { $line ne "" } { + dict set mem_modules [lindex [split $line "/"] 0] 1 + } +} +set mem_modules [dict keys $mem_modules] +log "extract_memories: [llength $mem_modules] modules hold memory cells" set out_json "$::env(RESULTS_DIR)/memories_inferred.json" file mkdir [file dirname $out_json] -write_json $out_json +if { [llength $mem_modules] > 0 } { + yosys select {*}$mem_modules + 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 From 13a2cf8a6db88ac3f57c18b7c9ab316cdae1faeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Sun, 4 Oct 2026 17:23:45 +0200 Subject: [PATCH 2/3] memories: select every memory cell type, stop if one is left out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit read_verilog leaves memory reads as $memrd until memory_collect, so the _v2-only list found a memory's module through its write or init cells alone. t:$mem* takes every memory cell type. The module name is the cell path up to its first slash. An escaped name holding a slash is cut short, select warns that it matched nothing, and that module's memories would be skipped without an error. The selection must now hold every memory cell, or the step stops. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Øyvind Harboe --- flow/scripts/memories/extract_memories.tcl | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/flow/scripts/memories/extract_memories.tcl b/flow/scripts/memories/extract_memories.tcl index 53b6b90298..91847741cf 100644 --- a/flow/scripts/memories/extract_memories.tcl +++ b/flow/scripts/memories/extract_memories.tcl @@ -35,25 +35,33 @@ yosys proc # 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. The module list comes from the -# cells' names (module before the first slash) rather than the selection's -# `%m` expansion, which leaves a module partially selected when its name -# carries a `$`, as slang's uniquified names do, and the passes then skip it. +# module's constant muxes into a memory. t:$mem* takes every memory cell +# type: read_verilog leaves reads as $memrd until memory_collect. The +# module list comes from the cells' names (module before the first slash) +# rather than the selection's `%m` expansion, which leaves a module +# partially selected when its name carries a `$`, as slang's uniquified +# names do, and the passes then skip it. A module name that itself holds a +# slash would be cut short and select nothing, so the selection must hold +# every memory cell. memory_bmux2rom -set mem_modules [dict create] -set mem_cells {t:$mem_v2 t:$memrd_v2 t:$memwr_v2 t:$meminit_v2} -foreach line [tee -q -s result.string select -list {*}$mem_cells] { +set mem_cells {} +foreach line [tee -q -s result.string select -list t:\$mem*] { set line [string trim $line] if { $line ne "" } { - dict set mem_modules [lindex [split $line "/"] 0] 1 + lappend mem_cells $line } } +set mem_modules [dict create] +foreach cell $mem_cells { + dict set mem_modules [lindex [split $cell "/"] 0] 1 +} set mem_modules [dict keys $mem_modules] -log "extract_memories: [llength $mem_modules] modules hold memory cells" +log "extract_memories: [llength $mem_modules] modules hold [llength $mem_cells] memory cells" set out_json "$::env(RESULTS_DIR)/memories_inferred.json" file mkdir [file dirname $out_json] if { [llength $mem_modules] > 0 } { yosys select {*}$mem_modules + select -assert-count [llength $mem_cells] % t:\$mem* %i 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 From 53f9445fa7851218e6d723204ddc7976690cb761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Harboe?= Date: Mon, 5 Oct 2026 20:21:35 +0200 Subject: [PATCH 3/3] memories: select the memory modules with %m, not by name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The module list was cut out of `select -list` lines at the first slash and handed back to `select`. An escaped module name may itself hold a slash, as in `\ram/one`: the cut name matched nothing and the step stopped. A memory's own name may hold one too, so no cut point is right. `t:$mem* %m` selects every module that holds a memory cell, whole, and never goes through a name. Checked with slang on yosys 0.68 against master's unscoped run: slang's uniquified `ram$top.x.a` and the escaped `ram/one$top.z` give the same $mem_v2 cells, and no module is left partially selected. Tested with slang only: yosys's own Verilog frontend is deprecated. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Øyvind Harboe --- flow/scripts/memories/extract_memories.tcl | 34 +++++++--------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/flow/scripts/memories/extract_memories.tcl b/flow/scripts/memories/extract_memories.tcl index 91847741cf..478f0e2426 100644 --- a/flow/scripts/memories/extract_memories.tcl +++ b/flow/scripts/memories/extract_memories.tcl @@ -36,32 +36,20 @@ yosys proc # 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. The -# module list comes from the cells' names (module before the first slash) -# rather than the selection's `%m` expansion, which leaves a module -# partially selected when its name carries a `$`, as slang's uniquified -# names do, and the passes then skip it. A module name that itself holds a -# slash would be cut short and select nothing, so the selection must hold -# 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 mem_cells {} -foreach line [tee -q -s result.string select -list t:\$mem*] { - set line [string trim $line] - if { $line ne "" } { - lappend mem_cells $line - } -} -set mem_modules [dict create] -foreach cell $mem_cells { - dict set mem_modules [lindex [split $cell "/"] 0] 1 -} -set mem_modules [dict keys $mem_modules] -log "extract_memories: [llength $mem_modules] modules hold [llength $mem_cells] memory cells" +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] -if { [llength $mem_modules] > 0 } { - yosys select {*}$mem_modules - select -assert-count [llength $mem_cells] % t:\$mem* %i +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