cranelift(x64): lower bare ctz/clz boolean tests via test+CC#13334
Open
ggreif wants to merge 1 commit into
Open
cranelift(x64): lower bare ctz/clz boolean tests via test+CC#13334ggreif wants to merge 1 commit into
ctz/clz boolean tests via test+CC#13334ggreif wants to merge 1 commit 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>
ctz/clz boolean tests via test+CC
This was referenced May 11, 2026
ctz/clz boolean tests via test+CCctz/clz boolean tests via test+CC
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.
Summary
Follow-up to #13332. That PR added egraph rules collapsing
(eq (ctz X) 0)/(ne (ctz X) 0)/(eq (clz X) 0)/(ne (clz X) 0)to direct LSB / sign-bit tests — but only when the comparison is mediated by an expliciticmp. The wasm front-end translateswasm if (ctz X)tobrif (ireduce.i32 (ctz.i64 X))directly (noicmp), so the egraph rules don't fire on the wasm-natural shape.This PR closes the gap by specialising
is_nonzeroin the x64 backend — the helper that allbrif/select/trapiflowerings funnel through.Rules
In
cranelift/codegen/src/isa/x64/inst.isle:The
ireducevariant catches the wasm front-end'si32.wrap_i64over a 64-bitctz/clz— a no-op on values in [0, bitwidth].Test deltas (
tests/disas/ctz-clz-bool-condition.wat)if_ctz_bare_i32bsfl + cmovel + test + jne)testl $1, %edx; je)if_ctz_bare_i64bsfq + cmovq + test + jne)testq $1, %rdx; je)if_clz_bare_i32bsr + cmov + sub + test + jne)testl + jns)The icmp-mediated cases (collapsed by #13332's egraph rules) are unchanged. The numeric-comparison negative test (
(ctz X) == 4) stays untouched.Motivation
Motoko's
moccodegen emitsi64.ctz X; i32.wrap_i64; iffor compactness/sign tests in the EOP backend (see caffeinelabs/motoko#6103). Before this PR, that lowers to 5 native instructions per dispatch; after, 2.A concrete idiomatic example: in Motoko, the
let-elsepattern overResultdesugars to a 2-arm refutable variant match (
#okvs#err). The variant-tag hashes arehash("ok") = 0x611C(LSB 0) andhash("err") = 0x4D0765(LSB 1) — they differ exactly at the LSB. The planned variant-switchBitTestdispatch (caffeinelabs/motoko'sgabor/variant-switch) recognizes this and emits a single LSB-test for the dispatch; combined with this PR, the entire let-else lowers toload hash; testq $1, ...; jccon x64 — three instructions for a pattern match. EveryResult-returning API + everylet-else-style early return collapses to this shape.Aggregated across hot paths (variant-switch dispatch, GC compact/heap discriminator, sign tests, …) this is meaningful.
Follow-ups (not in this PR)
select-consumer variant —selectalready routes throughis_nonzero_cmp→is_nonzero, so this PR's rules cover it too without extra work.