Skip to content

Search equivalent NOR netlists with an e-graph - #63

Closed
rollrat wants to merge 2 commits into
claude/construct-switch-ownerfrom
claude/egraph-search
Closed

rollrat wants to merge 2 commits into
claude/construct-switch-ownerfrom
claude/egraph-search

Conversation

@rollrat

@rollrat rollrat commented Oct 5, 2026

Copy link
Copy Markdown
Member

Middle of a stack: the switch fix below, OR nets for wide NORs above.

Why

The exact placer builds whatever NOR netlist it is given. A hand-written netlist (the full adder's nor9) therefore fixes the torch count and the logic depth before placement starts. This layer searches the equivalent netlists with an e-graph and measures whether placement gains from them.

What

  • exact/egraph.rs (egg 0.9):
    • Rewrites: De Morgan, distribution and factoring both ways, absorption, XOR and XNOR expansions.
    • Every e-class carries its truth table, and classes with equal tables are unioned in modify.
    • The full adder reaches 230 of the 256 three-input functions in 73 ms.
  • Exact extraction (exact/egraph_extract.rsdsl):
    • Grounded with rsdsl and solved with the placer's CaDiCaL binding.
    • Picks one node per needed class: a NOT is a torch (one tick), an OR is free, outputs are torches.
    • Options: a depth bound, a cost per OR node, and two-input NORs only.
    • Each extraction is proven optimal for its e-graph in 0.1-10 s.
    • Greedy extraction is kept, but it misses shared NORs (13 gates against nor9's 9).
  • Placing the results: ExactLocalPlacer::from_netlist, CIRCUIT=egraph-full-adder with EGRAPH_DEPTH, EGRAPH_OR_COST and EGRAPH_BINARY, and explore_egraph_netlists for the sweep.

Findings (full adder, docs/egraph_netlists.md)

Netlist Gates Depth cout / s Max fan-in
nor9 9 5 / 6 2
fewest gates 8 3 / 4 4
depth ≤ 3 9 2 / 3 4
or_cost 1 9 4 / 5 3
two-input NORs only 9: nor9, proven optimal 5 / 6 2
  • Every gain over nor9 uses NORs of 3-4 signals, and construction struggles with those.
  • The 8-gate and depth-3 netlists found no layout in 3 and 5 restarts.
  • The or_cost 1 netlist took 15-16 minutes to construct, against 1-1.5 for nor9. Once built, its two seeds gave 88 blocks and 11 ticks, and 121 blocks and 19 ticks.

Test plan

  • egraph_netlists_compute_the_full_adder: the extractions compute the full adder; 8 gates, proven; depth 3 with 9 gates; nothing at depth 2.
  • cargo test --release --lib exact::: all pass.

🤖 Generated with Claude Code

rollrat and others added 2 commits October 5, 2026 16:21
The exact placer builds whatever NOR netlist it is handed, so a
hand-written netlist such as the full adder's nor9 fixes its torch count
and logic depth before placement starts. egraph.rs explores the
equivalent netlists with egg and extracts ones worth placing.

Saturation runs De Morgan, double negation, commutativity and
associativity, distribution and factoring both ways, absorption, and XOR
and XNOR expansions. Each e-class carries its truth table, and classes
with equal tables are unioned in modify, so different forms of one
function always meet. The full adder reaches 230 of the 256 three-input
functions in 73 ms.

Greedy extraction decides class by class and missed the shared NORs (13
gates against nor9's 9), so extraction is exact: egraph_extract.rsdsl,
grounded with rsdsl and solved with the placer's CaDiCaL binding, picks
one node per needed class (outputs are torches, a NOT adds a tick, an OR
none) and minimizes gates, plus or_cost per OR node since every NOR input
is a signal to route, within an optional depth bound. Each extraction is
proven optimal for its e-graph in 0.1-10 s. On the full adder:
- fewest gates: 8 (nor9 has 9), depth 3/4, but NORs of up to 4 inputs;
- depth 3: 9 gates, depth 2/3;
- or_cost 1: 9 gates, depth 4/5 against nor9's 5/6, fan-in 3, and the
  same 4 nets alive at once as nor9.

The circuit harness places them (CIRCUIT=egraph-full-adder with
EGRAPH_DEPTH and EGRAPH_OR_COST); explore_egraph_netlists sweeps depth
bounds and OR costs. ExactLocalPlacer::from_netlist takes a netlist built
elsewhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The e-graph finds full adders with fewer gates (8) or less depth than
nor9, so the question was whether placement gains from them. Placed with
the same settings, the wide NORs they rely on are what construction
cannot handle: steps with a 3- or 4-input NOR ran out of their 60 s
repeatedly. The 8-gate and depth-3 netlists found no layout in 3 and 5
restarts; the or_cost 1 netlist (9 gates, depths 4/5, fan-in 3) took
15-16 minutes to construct against nor9's 1-1.5, and then compacted to
the best cell of the runs on one seed (88 blocks, 11 ticks) and a poor
one on the other.

A `binary` extraction option allows only two-input NORs (an OR may not
join classes built as ORs). Under it the minimum is nor9 itself, 9 gates
at depth 5/6, proven optimal for the e-graph, so every gain over nor9
needs wider NORs and therefore a construction that can build them.
docs/egraph_netlists.md records the method, the extractions, the
placements, and that as the next step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rollrat
rollrat added this pull request to stack #64 October 5, 2026 08:26
@rollrat
rollrat removed this pull request from stack #64 October 5, 2026 08:30
@rollrat

rollrat commented Oct 5, 2026

Copy link
Copy Markdown
Member Author

Opened by mistake as a separate stack; the work stays in #61 on top of #59.

@rollrat rollrat closed this Oct 5, 2026
@rollrat
rollrat deleted the claude/egraph-search branch October 5, 2026 08:30
@rollrat
rollrat restored the claude/egraph-search branch October 5, 2026 08:31
@rollrat
rollrat deleted the claude/egraph-search branch October 5, 2026 08:31
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.

1 participant