Stream: git-wasmtime

Topic: wasmtime / PR #11723 Add missing rotates for cranelift


view this post on Zulip Wasmtime GitHub notifications bot (Sep 19 2025 at 23:56):

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
}

view this post on Zulip Wasmtime GitHub notifications bot (Sep 19 2025 at 23:56):

khagankhan requested alexcrichton for a review on PR #11723.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 19 2025 at 23:56):

khagankhan requested wasmtime-compiler-reviewers for a review on PR #11723.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 20 2025 at 02:09):

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:

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 (Sep 20 2025 at 19:55):

alexcrichton created PR review comment:

I'd recommend using cast_unsigned here as it avoid using as which is something we try to avoid since it's by default a lossy cast.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 20 2025 at 19:55):

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 % 0 would do I believe). Handling it here would accidentally try to recover from what should otherwise be a panicking situation.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 20 2025 at 19:55):

alexcrichton submitted PR review:

Thanks! Mind adding some tests for this too? They'd go in cranelift/filetests/filetests/egraph for testing the IR is updated appropriately and cranelift/filetests/filetests/runtests for ensuring that the behavior matches the interpreter.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 20 2025 at 19:55):

alexcrichton created PR review comment:

The min here should be an assert of some form because if ty.bits() is >=64 then that's an invalid state for this function to be in and it should panic.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 20 2025 at 19:55):

alexcrichton created PR review comment:

For promotion back up to u64 I'd recommend using u64::from((xv as u8).rotate_left(amt)) to avoid as u64. The as u8 is still required, however, as this is intentionally truncating data.

view this post on Zulip Wasmtime GitHub notifications bot (Sep 20 2025 at 20:00):

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