wasmtime icon indicating copy to clipboard operation
wasmtime copied to clipboard

Add missing rotates for cranelift

Open khagankhan opened this issue 4 months ago • 2 comments

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 avatar Sep 19 '25 23:09 khagankhan

Subscribe to Label Action

cc @cfallin, @fitzgen

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 .github/subscribe-to-label.json configuration file.

Learn more.

github-actions[bot] avatar Sep 20 '25 02:09 github-actions[bot]

Sure thing! @alexcrichton Just made PR to get initial thoughts. I will address the comments

khagankhan avatar Sep 20 '25 20:09 khagankhan