diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 626697e071a..32f3ffd87bb 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -3134,6 +3134,12 @@ struct OptimizeInstructions // time, it is the expressions that execute before, not after, the // fallthrough that can affect its value. while (true) { + if (right->is()) { + // Inside a loop, expressions after the fallthrough can execute in prior + // iterations and affect the fallthrough value. For simplicity, give up + // rather than tracking those effects. + return false; + } auto* next = Properties::getImmediateFallthrough( right, getPassOptions(), *getModule()); if (next == right) { diff --git a/test/lit/passes/optimize-instructions-mvp.wast b/test/lit/passes/optimize-instructions-mvp.wast index 13e6692182e..1995d8d98d7 100644 --- a/test/lit/passes/optimize-instructions-mvp.wast +++ b/test/lit/passes/optimize-instructions-mvp.wast @@ -19598,4 +19598,185 @@ ) ) ) + + ;; 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: (select + ;; CHECK-NEXT: (local.get $x) + ;; 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: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $select-loop-if-unreachable-else-noninterfering (param $x i32) (param $y i32) (result i32) + ;; TODO: The unreachable else arm modifies $y and branches back to $loop, + ;; which does not affect (local.get $x), so we could still optimize the + ;; select if we tracked loop effects instead of bailing out on loops. + (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: (select + ;; CHECK-NEXT: (local.get $x) + ;; 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: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $select-unnamed-loop-if-unreachable-else (param $x i32) (param $y i32) (result i32) + ;; TODO: An unnamed loop cannot be branched to, so the unreachable else arm + ;; still cannot execute before the then arm, and we could still optimize + ;; the select. + (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) + ) + ) ) diff --git a/test/lit/passes/optimize-instructions-struct-rmw.wast b/test/lit/passes/optimize-instructions-struct-rmw.wast index cec1320c45d..836c8f56490 100644 --- a/test/lit/passes/optimize-instructions-struct-rmw.wast +++ b/test/lit/passes/optimize-instructions-struct-rmw.wast @@ -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) + ) + ) + ) + ) + ) + ) )