cranelift(aarch64): lower bare ctz/clz boolean tests via tst/cmp+Cond#13336
Draft
ggreif wants to merge 2 commits into
Draft
cranelift(aarch64): lower bare ctz/clz boolean tests via tst/cmp+Cond#13336ggreif wants to merge 2 commits into
ctz/clz boolean tests via tst/cmp+Cond#13336ggreif wants to merge 2 commits into
Conversation
Follow-up to bytecodealliance#13332. That PR added egraph rules collapsing `(eq (ctz X) 0)` / `(ne (ctz X) 0)` / clz analogues to direct LSB / sign-bit tests — but only when the comparison is mediated by an explicit `icmp`. The wasm front-end translates `wasm if (ctz X)` to `brif (ireduce.i32 (ctz.i64 X))` directly (no `icmp`), so the egraph rules don't fire on the wasm-natural shape. This commit closes the gap by specialising `is_nonzero` in the x64 backend — the helper that all `brif`/`select`/`trapif` lowerings funnel through. Four rules: `ctz`/`clz` × bare/`ireduce`-wrapped. The `ireduce` variant catches the wasm front-end's `i32.wrap_i64` over a 64-bit `ctz`/`clz` — a no-op on values in [0, bitwidth]. Test deltas (tests/disas/ctz-clz-bool-condition.wat): if_ctz_bare_i32: 5 insns -> 2 (testl $1, %edx; je) if_ctz_bare_i64: 5 insns -> 2 (testq $1, %rdx; je) if_clz_bare_i32: 7 insns -> 2 (testl %edx, %edx; jns) The icmp-mediated cases (collapsed by bytecodealliance#13332's egraph rules) are unchanged. The numeric-comparison negative test stays untouched. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
aarch64 analogue of the x64 follow-up. Specialises `is_nonzero (ctz X)` and `is_nonzero (clz X)` (plus their `ireduce`-wrapped variants) so the wasm-natural `brif (ireduce.i32 (ctz.i64 X))` shape lowers to a single bit-test instead of `rbit; clz; cmp; b.cond`. ctz: `tst Xn, #1` + `Cond.Eq` — branches when LSB is clear. clz: `cmp Xn, #0` + `Cond.Pl` — branches when sign bit is clear. Test deltas (tests/disas/aarch64-ctz-clz-bool-condition.wat): if_ctz_bare_i32: `tst w4, #1; b.eq` if_ctz_bare_i64: `tst x4, #1; b.eq` if_clz_bare_i32: `cmp w4, #0; b.pl` Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ctz/clz boolean tests via tst/cmp+Cond
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.
aarch64 analogue of #13334; egraph counterpart in #13332.
Same shape as the x64 follow-up: specialise
is_nonzero (ctz X)/is_nonzero (clz X)(and theirireduce-wrapped variants) incranelift/codegen/src/isa/aarch64/inst.isle, so the wasm-naturalbrif (ireduce.i32 (ctz.i64 X))shape lowers to a single bit-test instead ofrbit; clz; cmp; b.cond.aarch64-specific instructions used:
ctz:tst Xn, #1(logical AND with immediate, flags only) +Cond.Eq— branches when LSB is clear.clz:cmp Xn, #0+Cond.Pl— branches when sign bit (N flag) is clear, i.e. X is signed-non-negative.Test deltas (
tests/disas/aarch64-ctz-clz-bool-condition.wat, newly added):if_ctz_bare_i32rbit + clz + ...)tst w4, #1; b.eq)if_ctz_bare_i64tst x4, #1; b.eq)if_clz_bare_i32clz + ...)cmp w4, #0; b.pl)Negative test (
(ctz X) == 4) correctly untouched. Same motivation as #13334 — closes the gap for non-Rust wasm frontends like Motoko'smoc.riscv64 and s390x to follow.