ggreif edited PR #13332.
github-actions[bot] added the label cranelift on PR #13332.
github-actions[bot] added the label isle on PR #13332.
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:
- cfallin: isle
- fitzgen: isle
To subscribe or unsubscribe from this label, edit the <code>.github/subscribe-to-label.json</code> configuration file.
Learn more.
</details>
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.
ggreif updated PR #13332.
ggreif requested wasmtime-core-reviewers for a review on PR #13332.
ggreif commented on PR #13332:
Added tests/disas/ctz-clz-bool-condition.wat (commit 0519796) per your suggestion — covers
if/select/eqzconsumers over(ctz/clz X) eq/ne 0for 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; jlbare
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 insnsnegative test (correctly not collapsed — confirms the rules don't over-fire on numeric comparisons):
;; if_ctz_eq4_i32: bsfl %edx, %r9d; cmpl $4, %r9d; jeSo the egraph rules cover their intended shape end-to-end. The bare form would need backend
is_nonzerospecializations 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.
: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
alexcrichton added PR #13332 cranelift: fold ctz/clz comparisons against zero into direct LSB / sign-bit tests to the merge queue.
:check: alexcrichton merged PR #13332.
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