khagankhan opened PR #11723 from khagankhan:rotate-mo to bytecodealliance:main:
This adds the missed rotate optimizations in cranelift mid-end opened in #11722
(module (func $main (export "main") (result i32) ;; Step 1: (i32.rotl (i32.const 1) (i32.const 1)) ;; 1 in binary (32-bit) = 0b...0001 ;; Rotate left by 1 bit = 0b...0010 = 2 (i32.rotl (i32.const 1) (i32.const 1)) ;; Step 2: (i32.rotr (i32.const 10) (i32.const 1)) ;; 10 in binary = 0b...1010 ;; Rotate right by 1 bit = 0b...0101 = 5 (i32.rotr (i32.const 10) (i32.const 1)) ;; Step 3: Add the results ;; 2 + 5 = 7 (i32.add) ) )Before PR:
;; Intermediate Representation of function <wasm[0]::function[0]::main>: function u0:0(i64 vmctx, i64) -> i32 tail { gv0 = vmctx gv1 = load.i64 notrap aligned readonly gv0+8 gv2 = load.i64 notrap aligned gv1+16 stack_limit = gv2 block0(v0: i64, v1: i64): @002d jump block1 block1: @0022 v3 = iconst.i32 1 @0026 v5 = rotl v3, v3 ; v3 = 1, v3 = 1 @0027 v6 = iconst.i32 10 @002b v8 = rotr v6, v3 ; v6 = 10, v3 = 1 @002c v9 = iadd v5, v8 @002d return v9 }After PR:
;; Intermediate Representation of function <wasm[0]::function[0]::main>: function u0:0(i64 vmctx, i64) -> i32 tail { gv0 = vmctx gv1 = load.i64 notrap aligned readonly gv0+8 gv2 = load.i64 notrap aligned gv1+16 stack_limit = gv2 block0(v0: i64, v1: i64): @002d jump block1 block1: v12 = iconst.i32 7 @002d return v12 ; v12 = 7 }
khagankhan requested alexcrichton for a review on PR #11723.
khagankhan requested wasmtime-compiler-reviewers for a review on PR #11723.
github-actions[bot] commented on PR #11723:
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 created PR review comment:
I'd recommend using
cast_unsignedhere as it avoid usingaswhich is something we try to avoid since it's by default a lossy cast.
alexcrichton created PR review comment:
It's ok to not test for 0 here because a 0-size integral type should cause a panic (which
% 0would do I believe). Handling it here would accidentally try to recover from what should otherwise be a panicking situation.
alexcrichton submitted PR review:
Thanks! Mind adding some tests for this too? They'd go in
cranelift/filetests/filetests/egraphfor testing the IR is updated appropriately andcranelift/filetests/filetests/runtestsfor ensuring that the behavior matches the interpreter.
alexcrichton created PR review comment:
The
minhere should be an assert of some form because ifty.bits()is>=64then that's an invalid state for this function to be in and it should panic.
alexcrichton created PR review comment:
For promotion back up to u64 I'd recommend using
u64::from((xv as u8).rotate_left(amt))to avoidas u64. Theas u8is still required, however, as this is intentionally truncating data.
khagankhan commented on PR #11723:
Sure thing! @alexcrichton Just made PR to get initial thoughts. I will address the comments
Last updated: Dec 06 2025 at 07:03 UTC