Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ir/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ class EffectAnalyzer {
bool invalidates(const EffectAnalyzer& other) {
if ((transfersControlFlow() && other.hasSideEffects()) ||
(other.transfersControlFlow() && hasSideEffects()) ||
(mayNotReturn && other.writesGlobalState()) ||
(other.mayNotReturn && writesGlobalState()) ||
((writesMemory || calls) && other.accessesMemory()) ||
((other.writesMemory || other.calls) && accessesMemory()) ||
((writesTable || calls) && other.accessesTable()) ||
Expand Down
120 changes: 120 additions & 0 deletions test/lit/passes/optimize-instructions-maynotreturn.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited.

;; RUN: wasm-opt %s -all --optimize-instructions -S -o - | filecheck %s

;; Test that mayNotReturn (infinite loops) prevents reordering with side effects
;; in select arm optimizations.

(module
(memory 1)
;; CHECK: (global $g (mut i32) (i32.const 0))
(global $g (mut i32) (i32.const 0))

;; A select with an infinite loop in one arm and a side-effecting global.set
;; in the other should not have its arms swapped, because the loop may not
;; return and the global.set should not be moved before it.
;; CHECK: (func $select-loop-globalset (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (select
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (loop $L
;; CHECK-NEXT: (br_if $L
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (global.set $g
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.eqz
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-loop-globalset (param $x i32) (result i32)
(select
(block (result i32)
(loop $L
(br_if $L (local.get $x))
)
(i32.const 1)
)
(block (result i32)
(global.set $g (i32.const 42))
(i32.const 2)
)
(i32.eqz (local.get $x))
)
)

;; When neither arm has side effects, reordering is still fine even with a
;; loop.
;; CHECK: (func $select-loop-no-sideeffects (type $0) (param $x i32) (result i32)
;; CHECK-NEXT: (select
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (loop $L
;; CHECK-NEXT: (br_if $L
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-loop-no-sideeffects (param $x i32) (result i32)
(select
(block (result i32)
(loop $L
(br_if $L (local.get $x))
)
(i32.const 1)
)
(i32.const 2)
(i32.eqz (local.get $x))
)
)

;; A loop arm with a memory store in the other arm should not be swapped.
;; CHECK: (func $select-loop-store (type $1) (param $x i32) (param $p i32) (result i32)
;; CHECK-NEXT: (select
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (loop $L
;; CHECK-NEXT: (br_if $L
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (i32.store
;; CHECK-NEXT: (local.get $p)
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 2)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.eqz
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-loop-store (param $x i32) (param $p i32) (result i32)
(select
(block (result i32)
(loop $L
(br_if $L (local.get $x))
)
(i32.const 1)
)
(block (result i32)
(i32.store (local.get $p) (i32.const 42))
(i32.const 2)
)
(i32.eqz (local.get $x))
)
)
)
Loading