Skip to content
Open
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
17 changes: 14 additions & 3 deletions src/passes/OptimizeInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3132,7 +3132,10 @@ struct OptimizeInstructions

// We similarly want to find the fallthrough expression of `right`. But this
// time, it is the expressions that execute before, not after, the
// fallthrough that can affect its value.
// fallthrough that can affect its value. The exception is when we're in a
// named loop; in that case, expressions after the fallthrough can also
// affect its value because they execute in prior loop iterations.
bool inLoop = false;
while (true) {
auto* next = Properties::getImmediateFallthrough(
right, getPassOptions(), *getModule());
Expand All @@ -3144,12 +3147,20 @@ struct OptimizeInstructions
// container.
for (auto* child : ChildIterator(right)) {
if (child == next) {
// Skip children that execute after the fallthrough value, such as
// br_if conditions.
if (inLoop) {
// Once we have entered a loop, later children can interfere as
// well.
continue;
}
// Outside a loop, children after the fallthrough cannot execute
// before it, so we can skip them.
break;
}
interferingEffects.walk(child);
}
if (auto* loop = right->dynCast<Loop>(); loop && loop->name) {
inLoop = true;
}
right = next;
}

Expand Down
183 changes: 183 additions & 0 deletions test/lit/passes/optimize-instructions-mvp.wast
Original file line number Diff line number Diff line change
Expand Up @@ -19598,4 +19598,187 @@
)
)
)

;; CHECK: (func $select-loop-if-unreachable-else-interfering (param $x i32) (result i32)
;; CHECK-NEXT: (select
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (loop $loop (result i32)
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $loop)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-loop-if-unreachable-else-interfering (param $x i32) (result i32)
;; The unreachable else arm modifies $x and branches back to $loop, so it
;; can execute before the then arm evaluates (local.get $x) on a later
;; iteration. We must not drop the first select arm.
(select
(local.get $x)
(loop $loop (result i32)
(if (result i32)
(local.get $x)
(then
(local.get $x)
)
(else
(block
(local.set $x
(i32.const 42)
)
(br $loop)
)
)
)
)
(i32.const 1)
)
)

;; CHECK: (func $select-loop-if-unreachable-else-noninterfering (param $x i32) (param $y i32) (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (loop $loop (result i32)
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (local.set $y
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (br $loop)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-loop-if-unreachable-else-noninterfering (param $x i32) (param $y i32) (result i32)
;; The unreachable else arm modifies $y and branches back to $loop, which
;; does not affect (local.get $x), so we can still optimize the select.
(select
(local.get $x)
(loop $loop (result i32)
(if (result i32)
(local.get $y)
(then
(local.get $x)
)
(else
(block
(local.set $y
(i32.const 42)
)
(br $loop)
)
)
)
)
(i32.const 1)
)
)

;; CHECK: (func $select-if-unreachable-else-no-loop (param $x i32) (param $y i32) (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-if-unreachable-else-no-loop (param $x i32) (param $y i32) (result i32)
;; Without an enclosing loop, the unreachable else arm cannot execute before
;; the then arm, so its modification to $x does not block optimization.
(select
(local.get $x)
(if (result i32)
(local.get $y)
(then
(local.get $x)
)
(else
(block
(local.set $x
(i32.const 42)
)
(unreachable)
)
)
)
(i32.const 1)
)
)

;; CHECK: (func $select-unnamed-loop-if-unreachable-else (param $x i32) (param $y i32) (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (i32.const 1)
;; CHECK-NEXT: )
;; CHECK-NEXT: (loop (result i32)
;; CHECK-NEXT: (if (result i32)
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: (then
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: )
;; CHECK-NEXT: (else
;; CHECK-NEXT: (local.set $x
;; CHECK-NEXT: (i32.const 42)
;; CHECK-NEXT: )
;; CHECK-NEXT: (unreachable)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $select-unnamed-loop-if-unreachable-else (param $x i32) (param $y i32) (result i32)
;; An unnamed loop cannot be branched to, so the unreachable else arm still
;; cannot execute before the then arm.
(select
(local.get $x)
(loop (result i32)
(if (result i32)
(local.get $y)
(then
(local.get $x)
)
(else
(block
(local.set $x
(i32.const 42)
)
(unreachable)
)
)
)
)
(i32.const 1)
)
)
)
53 changes: 53 additions & 0 deletions test/lit/passes/optimize-instructions-struct-rmw.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2150,4 +2150,57 @@
)
)
)

;; CHECK: (func $loop-br_if-acquire (type $2) (param $struct (ref $struct)) (param $array (ref $array)) (result i32)
;; CHECK-NEXT: (local $flag i32)
;; CHECK-NEXT: (block $l (result i32)
;; CHECK-NEXT: (struct.atomic.rmw.cmpxchg acqrel acqrel $struct 0
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: (struct.get $struct 0
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (loop $loop (result i32)
;; CHECK-NEXT: (br_if $l
;; CHECK-NEXT: (struct.get $struct 0
;; CHECK-NEXT: (local.get $struct)
;; CHECK-NEXT: )
;; CHECK-NEXT: (block (result i32)
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (array.atomic.get acqrel $array
;; CHECK-NEXT: (local.get $array)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: (br_if $loop
;; CHECK-NEXT: (local.get $flag)
;; CHECK-NEXT: )
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
(func $loop-br_if-acquire (param $struct (ref $struct)) (param $array (ref $array)) (result i32)
(local $flag i32)
(block $l (result i32)
(struct.atomic.rmw.cmpxchg acqrel acqrel $struct 0
(local.get $struct)
(struct.get $struct 0 (local.get $struct))
(loop $loop (result i32)
(br_if $l
(struct.get $struct 0 (local.get $struct))
;; Inside a loop, the br_if condition can execute on an earlier
;; iteration before looping back to re-evaluate the struct.get, so
;; its acquire load must block optimization.
(block (result i32)
(drop (array.atomic.get acqrel $array (local.get $array) (i32.const 0)))
(br_if $loop (local.get $flag))
(i32.const 0)
)
)
)
)
)
)
)
Loading