Skip to content

Add more let peeling and rewrapping helpers - #9319

Open
abadams wants to merge 3 commits into
mainfrom
abadams/peel_lets
Open

Add more let peeling and rewrapping helpers#9319
abadams wants to merge 3 commits into
mainfrom
abadams/peel_lets

Conversation

@abadams

@abadams abadams commented Aug 11, 2026

Copy link
Copy Markdown
Member

Peeling Lets or LetStmts into a vector of name/value pairs, doing
something to the body, and then rewrapping is a common pattern in the
compiler. Rewrapping conditionally was done by calling expr_uses_var on
the partially-rebuilt body once per let, which is quadratic.

Adds a Stmt overload of peel_lets, plus rewrap_used_lets and
rewrap_all_lets for Expr and Stmt. rewrap_used_lets gathers the names
the body mentions once and extends the set with the value of each let it
keeps, so it is n log(n) instead of n^2. It conservatively treats every name
mentioned as a possible reference to a peeled let, even where an inner
let shadows it.

There should be no functional changes here, but while working on it Claude
noticed that the peeled lets in the store collector in Deinterleave.cpp are
unused. It looks like it was supposed to take the lets by reference instead of
by value, but if you do that you get duplicates in the list. It is currently trying
to figure out if this is just dead code or if this masks a bug.

abadams and others added 3 commits August 11, 2026 11:29
Peeling Lets or LetStmts into a vector of name/value pairs, doing
something to the body, and then rewrapping is a common pattern in the
compiler. Rewrapping conditionally was done by calling expr_uses_var on
the partially-rebuilt body once per let, which is quadratic.

Adds a Stmt overload of peel_lets, plus rewrap_used_lets and
rewrap_all_lets for Expr and Stmt. rewrap_used_lets gathers the names
the body mentions once and extends the set with the value of each let it
keeps, so it is linearithmic. It conservatively treats every name
mentioned as a possible reference to a peeled let, even where an inner
let shadows it.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
IRGraphVisitor memoizes nodes with a refcount above one, so a name
removed from the set can't be re-added by a later traversal that reaches
it through a shared subexpression. Testing without removing keeps
rewrap_used_lets as conservative as its documentation claims.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Holding a vector of const LetStmt * only works while some handle keeps
the chain alive, which is an easy thing to get wrong for no measurable
gain here. Use the same name/value pairs as the rest of the compiler.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.54167% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.85%. Comparing base (03a18bb) to head (90c1c4b).

Files with missing lines Patch % Lines
src/IROperator.cpp 87.50% 5 Missing ⚠️
src/LowerWarpShuffles.cpp 0.00% 4 Missing ⚠️
src/CodeGen_ARM.cpp 50.00% 1 Missing ⚠️
src/SplitTuples.cpp 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9319      +/-   ##
==========================================
+ Coverage   69.83%   69.85%   +0.02%     
==========================================
  Files         258      258              
  Lines       78210    78165      -45     
  Branches    19037    19006      -31     
==========================================
- Hits        54617    54604      -13     
+ Misses      17832    17793      -39     
- Partials     5761     5768       +7     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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