From 168fa42682daeed3ff60c2b65097014c03c522c9 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Sun, 22 Nov 2026 09:41:33 -0800 Subject: [PATCH 1/2] [OptimizeInstructions] Fix loop effects in areConsecutiveInputsEqual When walking the right-hand input's fallthrough chain in `OptimizeInstructions::areConsecutiveInputsEqual`, breaking out of the `ChildIterator` loop upon seeing the immediate fallthrough child skipped any subsequent children (such as the unreachable `ifFalse` arm of an `If`, a `Break` condition, or a `RefCast`/`BrOn` descriptor). While those children execute after (or instead of) the fallthrough expression when there is no enclosing loop, once the right-hand traversal enters a named `Loop`, control flow can cycle back so those skipped children execute on an earlier iteration before a subsequent evaluation of the fallthrough expression. For example, a `select` whose second arm was a `loop` containing an `if` with an unreachable `else` modifying a local and branching back to the loop falsely compared equal to the first arm and dropped the first arm. Track whether the right-hand fallthrough traversal has entered a named `Loop`, and once inside one, continue walking all remaining non-fallthrough children into `interferingEffects` rather than breaking at the immediate fallthrough child. --- src/passes/OptimizeInstructions.cpp | 17 +- .../lit/passes/optimize-instructions-mvp.wast | 183 ++++++++++++++++++ .../optimize-instructions-struct-rmw.wast | 53 +++++ 3 files changed, 250 insertions(+), 3 deletions(-) diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 626697e071a..7b97dba4fa6 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -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()); @@ -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->name) { + inLoop = true; + } right = next; } diff --git a/test/lit/passes/optimize-instructions-mvp.wast b/test/lit/passes/optimize-instructions-mvp.wast index 13e6692182e..136f9ca2a98 100644 --- a/test/lit/passes/optimize-instructions-mvp.wast +++ b/test/lit/passes/optimize-instructions-mvp.wast @@ -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) + ) + ) ) 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) + ) + ) + ) + ) + ) + ) ) From bae49e05c64fed12b0509c8eea5131963b7c012b Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Mon, 30 Nov 2026 13:23:12 -0800 Subject: [PATCH 2/2] simplify --- src/passes/OptimizeInstructions.cpp | 23 +++---- .../lit/passes/optimize-instructions-mvp.wast | 66 +++++++++---------- 2 files changed, 41 insertions(+), 48 deletions(-) diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 7b97dba4fa6..32f3ffd87bb 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -3132,11 +3132,14 @@ 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. 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; + // 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) { @@ -3147,20 +3150,12 @@ struct OptimizeInstructions // container. for (auto* child : ChildIterator(right)) { if (child == next) { - 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. + // Skip children that execute after the fallthrough value, such as + // br_if conditions. break; } interferingEffects.walk(child); } - if (auto* loop = right->dynCast(); loop && loop->name) { - inLoop = true; - } right = next; } diff --git a/test/lit/passes/optimize-instructions-mvp.wast b/test/lit/passes/optimize-instructions-mvp.wast index 136f9ca2a98..1995d8d98d7 100644 --- a/test/lit/passes/optimize-instructions-mvp.wast +++ b/test/lit/passes/optimize-instructions-mvp.wast @@ -19646,30 +19646,29 @@ ) ;; CHECK: (func $select-loop-if-unreachable-else-noninterfering (param $x i32) (param $y i32) (result i32) - ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (select ;; 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: (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: (br $loop) ;; 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) - ;; 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. + ;; 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) @@ -19736,30 +19735,29 @@ ) ;; CHECK: (func $select-unnamed-loop-if-unreachable-else (param $x i32) (param $y i32) (result i32) - ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (select ;; 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: (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: (unreachable) ;; 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) - ;; An unnamed loop cannot be branched to, so the unreachable else arm still - ;; cannot execute before the then arm. + ;; 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)