diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/binaryen.js/sideffects.js | 34 | ||||
-rw-r--r-- | test/lit/passes/simplify-locals-global.wast | 51 | ||||
-rw-r--r-- | test/passes/licm.txt | 33 | ||||
-rw-r--r-- | test/passes/licm.wast | 25 |
4 files changed, 133 insertions, 10 deletions
diff --git a/test/binaryen.js/sideffects.js b/test/binaryen.js/sideffects.js index 654c51608..e3187041d 100644 --- a/test/binaryen.js/sideffects.js +++ b/test/binaryen.js/sideffects.js @@ -19,28 +19,32 @@ console.log("SideEffects.Any=" + binaryen.SideEffects.Any); var module = new binaryen.Module(); assert( binaryen.getSideEffects( - module.i32.const(1) + module.i32.const(1), + module ) == binaryen.SideEffects.None ); assert( binaryen.getSideEffects( - module.br("test") + module.br("test"), + module ) == binaryen.SideEffects.Branches ); assert( binaryen.getSideEffects( - module.call("test", [], binaryen.i32) + module.call("test", [], binaryen.i32), + module ) == binaryen.SideEffects.Calls ); assert( binaryen.getSideEffects( - module.local.get("test", binaryen.i32) + module.local.get("test", binaryen.i32), + module ) == binaryen.SideEffects.ReadsLocal @@ -49,21 +53,28 @@ assert( binaryen.getSideEffects( module.local.set("test", module.i32.const(1) - ) + ), + module ) == binaryen.SideEffects.WritesLocal ); + +// Add a global for the test, as computing side effects will look for it. +module.addGlobal('test', binaryen.i32, true, module.i32.const(42)); + assert( binaryen.getSideEffects( - module.global.get("test", binaryen.i32) + module.global.get("test", binaryen.i32), + module ) == binaryen.SideEffects.ReadsGlobal ); assert( binaryen.getSideEffects( - module.global.set("test", module.i32.const(1)) + module.global.set("test", module.i32.const(1)), + module ) == binaryen.SideEffects.WritesGlobal @@ -72,7 +83,8 @@ assert( binaryen.getSideEffects( module.i32.load(0, 0, module.i32.const(0) - ) + ), + module ) == binaryen.SideEffects.ReadsMemory | binaryen.SideEffects.ImplicitTrap @@ -82,7 +94,8 @@ assert( module.i32.store(0, 0, module.i32.const(0), module.i32.const(1) - ) + ), + module ) == binaryen.SideEffects.WritesMemory | binaryen.SideEffects.ImplicitTrap @@ -92,7 +105,8 @@ assert( module.i32.div_s( module.i32.const(1), module.i32.const(0) - ) + ), + module ) == binaryen.SideEffects.ImplicitTrap diff --git a/test/lit/passes/simplify-locals-global.wast b/test/lit/passes/simplify-locals-global.wast new file mode 100644 index 000000000..215b32a3c --- /dev/null +++ b/test/lit/passes/simplify-locals-global.wast @@ -0,0 +1,51 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s --simplify-locals -S -o - | filecheck %s + +(module + ;; CHECK: (global $imm-glob i32 (i32.const 1234)) + (global $imm-glob i32 (i32.const 1234)) + + ;; CHECK: (global $mut-glob (mut i32) (i32.const 5678)) + (global $mut-glob (mut i32) (i32.const 5678)) + + ;; CHECK: (func $reorder-of-immmutable-global (result i32) + ;; CHECK-NEXT: (local $temp i32) + ;; CHECK-NEXT: (nop) + ;; CHECK-NEXT: (call $helper) + ;; CHECK-NEXT: (global.get $imm-glob) + ;; CHECK-NEXT: ) + (func $reorder-of-immmutable-global (result i32) + (local $temp i32) + ;; As the global is immutable, it cannot be modified in the call, and we can + ;; reorder here. + (local.set $temp + (global.get $imm-glob) + ) + (call $helper) + (local.get $temp) + ) + + ;; CHECK: (func $no-reorder-of-mutable-global (result i32) + ;; CHECK-NEXT: (local $temp i32) + ;; CHECK-NEXT: (local.set $temp + ;; CHECK-NEXT: (global.get $mut-glob) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (call $helper) + ;; CHECK-NEXT: (local.get $temp) + ;; CHECK-NEXT: ) + (func $no-reorder-of-mutable-global (result i32) + (local $temp i32) + ;; As the global is mutable, it can be modified in the call, and we cannot + ;; reorder here. + (local.set $temp + (global.get $mut-glob) + ) + (call $helper) + (local.get $temp) + ) + + ;; CHECK: (func $helper + ;; CHECK-NEXT: (nop) + ;; CHECK-NEXT: ) + (func $helper) +) diff --git a/test/passes/licm.txt b/test/passes/licm.txt index 956f6365e..0ad7af771 100644 --- a/test/passes/licm.txt +++ b/test/passes/licm.txt @@ -5,6 +5,7 @@ (type $i32_=>_none (func (param i32))) (type $i32_=>_i32 (func (param i32) (result i32))) (global $glob (mut i32) (i32.const 1)) + (global $glob-imm i32 (i32.const 1)) (memory $0 1) (func $loop1 (drop @@ -692,4 +693,36 @@ ) ) ) + (func $global-call + (local $x i32) + (loop $loop + (local.set $x + (global.get $glob) + ) + (call $global) + (drop + (local.get $x) + ) + (br_if $loop + (local.get $x) + ) + ) + ) + (func $global-call-immutable + (local $x i32) + (local.set $x + (global.get $glob-imm) + ) + (drop + (local.get $x) + ) + (loop $loop + (nop) + (call $global) + (nop) + (br_if $loop + (local.get $x) + ) + ) + ) ) diff --git a/test/passes/licm.wast b/test/passes/licm.wast index 6c5c747bd..c4fbfcd99 100644 --- a/test/passes/licm.wast +++ b/test/passes/licm.wast @@ -1,6 +1,7 @@ (module (memory 1) (global $glob (mut i32) (i32.const 1)) + (global $glob-imm i32 (i32.const 1)) (func $loop1 (loop $loop (drop (i32.const 10)) @@ -393,9 +394,33 @@ (func $global (local $x i32) (loop $loop + ;; Moving this global.get out of the loop is ok. It is mutable, but there + ;; is no possible place where it can be set in the loop. (local.set $x (global.get $glob)) (drop (local.get $x)) (br_if $loop (local.get $x)) ) ) + (func $global-call + (local $x i32) + (loop $loop + ;; As above, but now the loop does a call, which might in theory change + ;; mutable global state, so we cannot optimize. + (local.set $x (global.get $glob)) + (call $global) + (drop (local.get $x)) + (br_if $loop (local.get $x)) + ) + ) + (func $global-call-immutable + (local $x i32) + (loop $loop + ;; As above, but now the global is immutable, so the call does not + ;; prevent us from optimizing. + (local.set $x (global.get $glob-imm)) + (call $global) + (drop (local.get $x)) + (br_if $loop (local.get $x)) + ) + ) ) |