Skip to content

memories: run the memory passes on the modules that hold memory cells - #4622

Merged
maliberty merged 3 commits into
The-OpenROAD-Project:masterfrom
oharboe:extract-memories-scoped
Oct 8, 2026
Merged

maliberty merged 3 commits into
The-OpenROAD-Project:masterfrom
oharboe:extract-memories-scoped

Conversation

@oharboe

@oharboe oharboe commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

AUTO_MEMORIES' extraction step runs memory -nomap over the whole design to find memories before synthesis. On a large design almost none of that time goes to the memories.

Each pass in memory -nomap walks every module. memory_dff builds its per-bit index for a module before it asks whether the module has a memory at all (YosysHQ/yosys#6282), and opt_mem_priority and opt_mem_feedback scan every module the same way. In a design whose memories sit in small generated modules, a firtool core for instance, indexing the memory-less modules is nearly the whole cost.

This PR runs the memory passes only on the modules that hold memory cells. memory_bmux2rom still runs first over the whole design, because it is what turns constant muxes into memories. The JSON now holds only those modules; the detector reads nothing else.

The public ORFS designs are small, so the step costs seconds there. Private designs built with ORFS are much larger, and that is where it costs minutes.

Measured

XiangShan's flat XSCore (1870 modules, 407 with memory cells, read through slang, yosys 0.68), memory passes alone:

time
master 721 s memory_dff 462, opt_mem_priority 67, opt_mem_feedback 52, two opt_clean 99, share 37
this PR 64 s memory_dff 24, opt_mem_priority 27

Both runs find the same 407 $mem_v2 cells. The whole extraction step went from about fifteen minutes to under four; the remaining four minutes are reading and proc.

On a small AUTO_MEMORIES test design (one convertible memory, one inline array that must stay flip-flops), memories.json is byte-identical with and without this change, and the extraction JSON shrinks from 1.99 MB to 9.5 KB.

Selecting the modules

t:$mem* %m selects every module that holds a memory cell, whole, without going through its name. An escaped module or memory name may hold a /, a $ or a glob character, and none of those survives being cut out of a select -list line and handed back to select. Tested with slang only, as yosys's own Verilog frontend is deprecated.

tclfmt and tclint are clean.

🤖 Generated with Claude Code

`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 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes the memory extraction process in extract_memories.tcl by scoping the memory -nomap pass only to modules that contain memory cells, which significantly improves performance on designs with large logic modules. Feedback suggests enhancing the robustness of this extraction by supporting older memory cell types and improving the parsing of the select -list output to handle potential whitespace and invalid module names safely.

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 <noreply@anthropic.com>
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
@oharboe
oharboe requested a review from maliberty October 4, 2026 15:24
@openroad-ci

openroad-ci commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

🔍 QoR check

Metrics reflect the PR merge build — i.e. what will land on the target branch.

Commit f5d2f9f · Jenkins build #4 · Baseline: build · View build on dashboard

62 design(s) checked — 0 with regression(s), 0 without a comparable baseline.

@maliberty

Copy link
Copy Markdown
Member

Codex:

Review finding — needs changes. At extract_memories.tcl:56, splitting a cell path at the first / truncates a valid escaped module name such as \ram/one. I reproduced this with Yosys 0.68: the new script selects ram and fails its assertion; the previous script completes and emits the $mem_v2 cell.

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 <noreply@anthropic.com>
Signed-off-by: Øyvind Harboe <oyvind.harboe@zylin.com>
@oharboe

oharboe commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks, confirmed: \ram/one cut at the first / selects ram, and the step stops. Fixed in 53f9445 by selecting t:$mem* %m instead of parsing cell names, which also covers a / in the memory's own name.

Tested with slang only, as yosys's own Verilog frontend is deprecated: slang's uniquified names (ram$top.x.a) and an escaped ram/one module give the same $mem_v2 cells as master's unscoped run, with no module left partially selected. The %m concern in the description did not reproduce and is removed from it.

@maliberty
maliberty merged commit 12eb8bd into The-OpenROAD-Project:master Oct 8, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants