Stream: git-wasmtime

Topic: wasmtime / PR #13332 cranelift: fold `ctz`/`clz` comparis...


view this post on Zulip Wasmtime GitHub notifications bot (May 09 2026 at 19:25):

ggreif edited PR #13332.

view this post on Zulip Wasmtime GitHub notifications bot (May 09 2026 at 22:59):

github-actions[bot] added the label cranelift on PR #13332.

view this post on Zulip Wasmtime GitHub notifications bot (May 09 2026 at 22:59):

github-actions[bot] added the label isle on PR #13332.

view this post on Zulip Wasmtime GitHub notifications bot (May 09 2026 at 23:00):

github-actions[bot] commented on PR #13332:

Subscribe to Label Action

cc @cfallin, @fitzgen

<details>
This issue or pull request has been labeled: "cranelift", "isle"

Thus the following users have been cc'd because of the following labels:

To subscribe or unsubscribe from this label, edit the <code>.github/subscribe-to-label.json</code> configuration file.

Learn more.
</details>

view this post on Zulip Wasmtime GitHub notifications bot (May 09 2026 at 23:51):

alexcrichton commented on PR #13332:

Thanks for this! Are the backend rules necessary with the mid-end egraph rules? I'd expect that the egraph rewrites would be sufficient and the backends largely wouldn't need to change, unless they need to emit a new instruction pattern which isn't currently recognized.

One thing you may also want to do is to add something in tests/disas/*.wat. That's a wasm-level test which ensures that the right assembly will be generated which is a bit of an end-to-end test. Not required, but if you're curious that'd be a good way to validate that this is all kicking in correctly.

view this post on Zulip Wasmtime GitHub notifications bot (May 10 2026 at 09:14):

ggreif updated PR #13332.

view this post on Zulip Wasmtime GitHub notifications bot (May 10 2026 at 09:14):

ggreif requested wasmtime-core-reviewers for a review on PR #13332.

view this post on Zulip Wasmtime GitHub notifications bot (May 10 2026 at 09:14):

ggreif commented on PR #13332:

Added tests/disas/ctz-clz-bool-condition.wat (commit 0519796) per your suggestion — covers if/select/eqz consumers over (ctz/clz X) eq/ne 0 for i32 and i64, plus a numeric-comparison negative test ((ctz X) == 4).

Empirical answer to your first question — the egraph rules in this PR are complete for the icmp-mediated case but don't catch the wasm-natural bare form:

icmp-mediated (this PR's target — collapses correctly):

;; if_ctz_eq0_i32:        testl $1, %edx; jne
;; select_ctz_eq0_i32:    testl $1, %edx; cmovne
;; eqz_ctz_eq0_i32:       testl $1, %edx; sete
;; if_clz_eq0_i32:        testl %edx, %edx; jl

bare if (ctz X) (no icmp — what wasm frontends like Motoko emit directly):

;; if_ctz_bare_i32:       bsfl %edx, %r9d; cmovel; testl %r9d, %r9d; jne
;; if_clz_bare_i32:       bsrl + cmovel + 0x1f-sub + test  ;; 5 insns

negative test (correctly not collapsed — confirms the rules don't over-fire on numeric comparisons):

;; if_ctz_eq4_i32:        bsfl %edx, %r9d; cmpl $4, %r9d; je

So the egraph rules cover their intended shape end-to-end. The bare form would need backend is_nonzero specializations as a follow-up — happy to land this PR with just the egraph rules and tackle the bare form separately, if that scoping makes sense to you.

view this post on Zulip Wasmtime GitHub notifications bot (May 11 2026 at 14:32):

:thumbs_up: alexcrichton submitted PR review:

Ah yes good point, and yeah sounds good to me. Thanks for your work here! Happy to review backend-specific changes as well

view this post on Zulip Wasmtime GitHub notifications bot (May 11 2026 at 14:32):

alexcrichton added PR #13332 cranelift: fold ctz/clz comparisons against zero into direct LSB / sign-bit tests to the merge queue.

view this post on Zulip Wasmtime GitHub notifications bot (May 11 2026 at 14:58):

:check: alexcrichton merged PR #13332.

view this post on Zulip Wasmtime GitHub notifications bot (May 11 2026 at 14:58):

alexcrichton removed PR #13332 cranelift: fold ctz/clz comparisons against zero into direct LSB / sign-bit tests from the merge queue.


Last updated: Jun 01 2026 at 09:49 UTC