egraph: peer through ireduce in brif (ctz/clz) skeleton fold#13458
Merged
cfallin merged 3 commits intoMay 23, 2026
Conversation
Extends the simplify_skeleton fold from bytecodealliance#13343 to peer through an intervening `ireduce`. The wasm pattern `i64.ctz; i32.wrap_i64; br_if` (and the `clz` analog) lowers to clif as `brif (ireduce (ctz X))`, which the existing 2-op rules didn't catch. `ireduce` is harmless on a ctz/clz result because the count is in [0, bitwidth] - always fits in the narrower type without loss. The new rules rewrite to the same bit-extract condition as the bare-form rules, using the wider source type for the band/sge. Source pattern: motoko/moc's EOP peephole emits exactly this shape for `(value & 1) == 0` as a branch condition. Test coverage: - cranelift filetest: brif_ireduce_ctz_i64 / brif_ireduce_clz_i64 - tests/disas: \$if_ctz_bare_i64 reblessed (testq \$1,%rdx; je) + new \$if_clz_bare_i64 (testq %rdx,%rdx; jge)
ireduce in brif (ctz/clz) skeleton fold
cfallin
approved these changes
May 22, 2026
Member
cfallin
left a comment
There was a problem hiding this comment.
Thanks! New rule looks good; just some style comments.
- Shorten the inline comment on the ireduce-peer rules. - Rename the bound value `X` -> `x` to match the project's lowercase binding style. - Drop the verbose justification comment on the new filetest functions; match the one-liner style used by the existing tests in the file. No behavioural change; filetest still passes.
Function name is self-explanatory; rest of the file leaves the analogous eq0/ne0/select variants uncommented.
cfallin
approved these changes
May 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extends #13343's
simplify_skeletonfold so the existingbrif (ctz X)/brif (clz X)rules also fire when anireduce(i.e.
i32.wrap_i64at the wasm level) sits between the count andthe branch.
Why this matters: non-Rust frontends targeting wasm64 often emit
i64.ctz; i32.wrap_i64; br_iffor boolean-context LSB tests. Thewrap is harmless (the count is in [0, 64]) but it currently blocks
the fold, so cranelift falls back to materializing the full count
before the branch. With this PR, the i64-wrap form lowers to the
same
testq $1, %rdx; jeshape as the i32 case.Test coverage mirrors the existing patterns:
cranelift/filetests/.../brif-cnt-cond.clifgets two newbrif (ireduce (ctz/clz X))functions.tests/disas/ctz-clz-bool-condition.watgets a new$if_clz_bare_i64and the existing$if_ctz_bare_i64is reblessed - both now lower to the optimal shape.