Skip to content

opt_shift: add -expand-keep-arith to keep a variable barrel off an add tree - #298

Merged
akashlevy merged 1 commit into
mainfrom
qor/gated-shift-path8
Aug 28, 2026
Merged

opt_shift: add -expand-keep-arith to keep a variable barrel off an add tree#298
akashlevy merged 1 commit into
mainfrom
qor/gated-shift-path8

Conversation

@akashlevy

@akashlevy akashlevy commented Aug 27, 2026

Copy link
Copy Markdown

Summary

-expand rewrites (a OP b) << c into (a << c) OP (b << c) for every OP it
handles, without looking at what the amount costs.

  • A constant amount is free rewiring, so splitting it in two costs nothing.
  • A bitwise OP is one gate level, so the amount's cone barely notices moving ahead of it.
  • A variable amount on $add/$sub is a barrel. Expanding duplicates it and
    lands it ahead of the carry chain, so the amount's own cone gains the adder's whole
    depth instead of bypassing it. On an N-operand add tree the rewrite cascades down
    every link, ending with N barrels in front of the tree rather than one behind it.

-expand-keep-arith restricts -expand to reject exactly that last case. It
defaults off, so -expand is bit-exact unchanged without it and this PR is a
no-op to merge on its own.

Motivating shape

A gated accumulate under a variable barrel: six enabled terms sum into a 40-bit
accumulator, then the sum is shifted by bw + bh. -expand fired six times down the
tree, so the shift-amount input drove six barrels and then the entire carry-save tree
and its 39-bit final adder, instead of driving one barrel hanging off the adder output.

Measured in Preqorsor on that design: lol 29.0 -> 28.25, clk 449.3 -> 425.0,
area 295.0 -> 168.7 (the six duplicated barrels were most of the area).

Test plan

  • tests/silimate/opt_expand_shifts.ys — 5 new cases, each equiv_opt -assert:
    • variable amount on $add is held (1 $add, 1 $shl)
    • variable amount on $sub is held
    • constant amount still expands
    • bitwise ops untouched by the guard (still 2 $shl)
    • four-operand tree comes out with 1 barrel, not 4
  • The pre-existing cases in that file all use plain -expand and still pass,
    including "SHL across ADD" asserting 2 $shl — the default really is unchanged.
  • Siblings green: opt_combine_shifts, opt_chain_shifts, opt_descale_shifts,
    opt_fuse_shifts, opt_sink_shifts, opt_expand.
  • Preqorsor A/B over its 38 shift/arithmetic regressions with the flag on: 36
    bit-identical, 2 small area wins, 0 regressions.

Preqorsor side that turns it on: Silimate/preqorsor#2284.

Made with Cursor

…d tree

-expand rewrites (a OP b) << c into (a << c) OP (b << c) for every OP it
handles, without looking at what the amount costs. That is right for a constant
amount, which is free rewiring, and for the bitwise ops, where the amount's cone
gains a single gate level by moving ahead of OP. It is wrong for a variable
amount on $add/$sub: the amount is a barrel, so the rewrite duplicates it, and
it lands ahead of the carry chain, so the amount's own cone gains the adder's
whole depth instead of bypassing it. On an N-operand add tree the rewrite
cascades down every link, ending with N barrels in front of the tree rather than
one behind it.

-expand-keep-arith restricts -expand to reject exactly that case. It defaults
off, so -expand is bit-exact unchanged without it.

The motivating shape is a gated accumulate under a variable barrel: six enabled
terms summed into a 40-bit accumulator, then shifted by bw + bh. -expand fired
six times down the tree, so the shift-amount input drove six barrels and then
the whole carry-save tree and its final 39-bit adder, instead of driving one
barrel hanging off the adder output. The guard puts the barrel back behind the
tree.

Tests cover both halves of the condition -- a variable amount on $add and on
$sub is held, a constant amount still expands, bitwise ops are untouched -- plus
a four-operand tree that has to come out with one barrel rather than four.

Co-authored-by: Cursor <cursoragent@cursor.com>
@greptile-apps

greptile-apps Bot commented Aug 27, 2026

Copy link
Copy Markdown

Greptile Summary

The PR adds an opt-in restriction that prevents variable shifts from being expanded across addition and subtraction while preserving existing default behavior.

  • Adds and documents the -expand-keep-arith option.
  • Propagates the option into the expansion pattern after each PMG setup.
  • Rejects variable-amount $add/$sub expansion while retaining constant and bitwise expansion.
  • Adds equivalence and structural tests for addition, subtraction, constants, bitwise operations, and add trees.

Confidence Score: 5/5

The PR appears safe to merge with no actionable correctness, security, or compatibility failures identified.

The option defaults off, its state is assigned after each PMG reset, and the guarded pattern preserves the documented constant, bitwise, addition, and subtraction behavior with focused equivalence tests.

Important Files Changed

Filename Overview
passes/silimate/opt_shift.cc Adds option help, parsing, and correctly restores the PMG udata flag after each setup.
passes/silimate/peepopt_expand_shifts.pmg Adds a narrowly scoped guard that retains variable shifts after $add and $sub cells.
tests/silimate/opt_expand_shifts.ys Adds equivalence and cell-count coverage for all principal branches of the new option.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Match shifted binary operation] --> B{keep_arith enabled?}
    B -- No --> E[Expand shift across operation]
    B -- Yes --> C{Operation is add or sub?}
    C -- No --> E
    C -- Yes --> D{Shift amount fully constant?}
    D -- Yes --> E
    D -- No --> F[Reject rewrite and retain one output shifter]
Loading

Reviews (1): Last reviewed commit: "opt_shift: add -expand-keep-arith to kee..." | Re-trigger Greptile

@akashlevy
akashlevy merged commit 6fd649b into main Aug 28, 2026
11 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.

1 participant