← Back to Pull requests

[CFP][GSI] Fix catch pops after optimizing reads #9289

Open
tlively opened this pull request with 427 lines diff across 6 files
View on GitHub
tlively tlively commented on Oct 20, 2026 20:34 UTC
Author

When ConstantFieldPropagation or GlobalStructInference optimizes a struct.get or ref.get_desc, it either wraps the reference operand in a Block or places it into the condition (third child) of a Select. Any pop instruction inside the reference from a legacy exception-handling catch body then becomes either nested inside a block or displaced out of the catch body's first-descendant chain. Update EHUtils::handleBlockNestedPop to also find and lift pops that have been moved out of the first-descendant chain, and call EHUtils::handleBlockNestedPops in both passes when reads are optimized.

Diff: 427 lines (+345 / -82) across 6 files Reviewer: tlively Review duration: 4009m
All Vouch review confidence checks passed
Residual risk 0.09 = Change Risk (0.61) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.61 · 427 lines diff across 6 files
0.61
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.94 · 5 substantive comment(s)
0.94
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
84%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.363
Self Review self_review
0.140
Real PR Conversation Timeline
24 items
tlively tlively commented on code on Oct 20, 2026 20:35 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -119,6 +141,16 @@ void handleBlockNestedPop(Try* try_, Function* func, Module& wasm) {
     bool isPopNested = false;
     Expression** popPtr = nullptr;
     Expression* pop = getFirstPop(catchBody, isPopNested, popPtr);
+    if (!pop) {
+      // The pop may have been moved out of the first-descendant line, e.g.
+      // into the condition of a select. Find it and fix it up just like a
+      // block-nested pop.

What do we think about this solution? The alternative would be to inhibit these optimizations in the presence of pops.

gemini-code-assist[bot] gemini-code-assist[bot] commented on code on Oct 20, 2026 20:36 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -106,6 +106,28 @@ bool containsValidDanglingPop(Expression* catchBody) {
   return pop != nullptr && !isPopNested;
 }
 
+static Expression** findPopPtr(Expression*& expr) {
+  SmallVector<Expression**, 8> work;
+  work.push_back(&expr);
+  while (!work.empty()) {
+    auto* currPtr = work.back();
+    work.pop_back();
+    auto* curr = *currPtr;
+    if (curr->is<Pop>()) {

!critical

The curr pointer can be nullptr for optional AST children (such as the ifFalse branch of an If expression). Calling curr->is<Pop>() without a null check will result in a segmentation fault. We should add a null check before checking the expression type.

    auto* curr = *currPtr;
    if (!curr) {
      continue;
    }
    if (curr->is<Pop>()) {

gemini-code-assist[bot] gemini-code-assist[bot] commented on Oct 20, 2026 20:36 UTC
Reviewer

Code Review

This pull request introduces a helper function findPopPtr in eh-utils.cpp to locate nested Pop expressions and fix them up when they are moved out of the first-descendant line (e.g., into select conditions or blocks). This fix-up logic is integrated into the ConstantFieldPropagation and GlobalStructInference passes, with accompanying tests. A critical issue was identified in the new findPopPtr function where curr can be nullptr for optional AST children, which would lead to a segmentation fault when calling curr->is<Pop>(). A null check should be added to prevent this.

tlively tlively commented on code on Oct 20, 2026 21:41 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -106,6 +106,28 @@ bool containsValidDanglingPop(Expression* catchBody) {
   return pop != nullptr && !isPopNested;
 }
 
+static Expression** findPopPtr(Expression*& expr) {
+  SmallVector<Expression**, 8> work;
+  work.push_back(&expr);
+  while (!work.empty()) {
+    auto* currPtr = work.back();
+    work.pop_back();
+    auto* curr = *currPtr;
+    if (curr->is<Pop>()) {

This is not the case because ChildIterator knows about optional children and skips them when they're null.

kripken kripken commented on code on Oct 20, 2026 22:13 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -119,6 +141,16 @@ void handleBlockNestedPop(Try* try_, Function* func, Module& wasm) {
     bool isPopNested = false;
     Expression** popPtr = nullptr;
     Expression* pop = getFirstPop(catchBody, isPopNested, popPtr);
+    if (!pop) {
+      // The pop may have been moved out of the first-descendant line, e.g.
+      // into the condition of a select. Find it and fix it up just like a
+      // block-nested pop.
+      popPtr = findPopPtr(catchBody);
+      if (popPtr) {
+        pop = *popPtr;
+        isPopNested = true;
+      }
+    }

Can we separate this fix from adding calls to the two passes?

tlively tlively commented on code on Oct 20, 2026 22:22 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -119,6 +141,16 @@ void handleBlockNestedPop(Try* try_, Function* func, Module& wasm) {
     bool isPopNested = false;
     Expression** popPtr = nullptr;
     Expression* pop = getFirstPop(catchBody, isPopNested, popPtr);
+    if (!pop) {
+      // The pop may have been moved out of the first-descendant line, e.g.
+      // into the condition of a select. Find it and fix it up just like a
+      // block-nested pop.
+      popPtr = findPopPtr(catchBody);
+      if (popPtr) {
+        pop = *popPtr;
+        isPopNested = true;
+      }
+    }

The reason this fix goes along with these two passes is that these passes are the only ones (that we know about) that can move the pop out of that first-child position. We could of course split it up in principle, but I think it makes sense to keep them together.

kripken kripken commented on code on Oct 21, 2026 00:00 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -119,6 +141,16 @@ void handleBlockNestedPop(Try* try_, Function* func, Module& wasm) {
     bool isPopNested = false;
     Expression** popPtr = nullptr;
     Expression* pop = getFirstPop(catchBody, isPopNested, popPtr);
+    if (!pop) {
+      // The pop may have been moved out of the first-descendant line, e.g.
+      // into the condition of a select. Find it and fix it up just like a
+      // block-nested pop.
+      popPtr = findPopPtr(catchBody);
+      if (popPtr) {
+        pop = *popPtr;
+        isPopNested = true;
+      }
+    }

Ok, then let me try to understand this change more. How does getFirstPop differ from findPopPtr? The new function isn't documented and I'm not sure exactly what it is doing yet.

tlively tlively commented on code on Oct 21, 2026 00:18 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -119,6 +141,16 @@ void handleBlockNestedPop(Try* try_, Function* func, Module& wasm) {
     bool isPopNested = false;
     Expression** popPtr = nullptr;
     Expression* pop = getFirstPop(catchBody, isPopNested, popPtr);
+    if (!pop) {
+      // The pop may have been moved out of the first-descendant line, e.g.
+      // into the condition of a select. Find it and fix it up just like a
+      // block-nested pop.
+      popPtr = findPopPtr(catchBody);
+      if (popPtr) {
+        pop = *popPtr;
+        isPopNested = true;
+      }
+    }

getFirstPop only looks at the leftmost leaf in the expression tree, i.e. where pop is supposed to be found. findPopPtr looks for a pop in the entire expression tree, except inside nested trys.

aheejin aheejin commented on code on Oct 21, 2026 00:24 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -119,6 +141,16 @@ void handleBlockNestedPop(Try* try_, Function* func, Module& wasm) {
     bool isPopNested = false;
     Expression** popPtr = nullptr;
     Expression* pop = getFirstPop(catchBody, isPopNested, popPtr);
+    if (!pop) {
+      // The pop may have been moved out of the first-descendant line, e.g.
+      // into the condition of a select. Find it and fix it up just like a
+      // block-nested pop.

If we are gonna ignore getFirstPop's "no", we probably should remove it and replace it with this? There's no point in calling and keeping getFirstPop at this point.

aheejin aheejin commented on Oct 21, 2026 00:30 UTC
Reviewer

I think the reason I only allowed pops in first-descendant lines in #4348 was because otherwise the code was invalid, and all optimizations that needed the fixup so far were only nesting pops into a block and nothing more. (@kripken Is this right? You are who added almost all fixup code in various passes. Does this reflect your experience, or you saw cases where pops were displaced out of first-descendant lines but you just gave up optimizing them?)

Anyway, if we are to allow pops to be hoisted from anywhere within a catch block, I think we should just replace it with the existing first-descendant searching (because we are ignoring its result anyway). I'm also wondering, should we go into loops, where pops can run multiple times, and if's true/false bodies, where pops run conditionally? Or should we just avoid going into nested catches?

tlively tlively commented on Oct 21, 2026 00:35 UTC
Author

I'm also wondering, should we go into loops, where pops can run multiple times, and if's true/false bodies, where pops run conditionally? Or should we just avoid going into nested catches?

I think it would be harmless to search in ifs and loops. It's hard to imagine a case where an optimization would be able to move a pop into an if or a loop, and maybe that never makes sense. But if it _did_ make sense, then we would definitely need to fix it up. I don't think there's much downside to searching in ifs or loops, either. I doubt it makes much of a performance difference.

kripken kripken commented on code on Oct 21, 2026 17:40 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -119,6 +141,16 @@ void handleBlockNestedPop(Try* try_, Function* func, Module& wasm) {
     bool isPopNested = false;
     Expression** popPtr = nullptr;
     Expression* pop = getFirstPop(catchBody, isPopNested, popPtr);
+    if (!pop) {
+      // The pop may have been moved out of the first-descendant line, e.g.
+      // into the condition of a select. Find it and fix it up just like a
+      // block-nested pop.
+      popPtr = findPopPtr(catchBody);
+      if (popPtr) {
+        pop = *popPtr;
+        isPopNested = true;
+      }
+    }

Thanks, though now I'm a little fuzzy on why we need both - the motivation/background is not clear to me?

kripken kripken commented on Oct 21, 2026 17:42 UTC
Contributor

@aheejin

and all optimizations that needed the fixup so far were only nesting pops into a block and nothing more. (@kripken Is this right? You are who added almost all fixup code in various passes. Does this reflect your experience,

Yes, that is all I have seen. Actually I was just thinking that since that is the common case, maybe there is a simpler solution to all this. Yesterday I started Gemini on an attempt to allow multiple unnamed blocks in validation - will report back when it finishes.

kripken kripken commented on Oct 21, 2026 23:14 UTC
Contributor

Ok, #9292 is up for feedback. If that direction looks good maybe we don't need these CFP/GSI changes?

tlively tlively commented on Oct 21, 2026 23:36 UTC
Author

We still need these changes because pops can only ever validate in the leftmost leaf position, and the problem here is that CFP and GSI do not preserve this.

kripken kripken commented on code on Oct 21, 2026 23:39 UTC
Code Review
src/ir/eh-utils.cpp:1
@@ -106,6 +106,28 @@ bool containsValidDanglingPop(Expression* catchBody) {
   return pop != nullptr && !isPopNested;
 }
 
+static Expression** findPopPtr(Expression*& expr) {

Maybe add a comment on this function?

kripken kripken commented on Oct 21, 2026 23:40 UTC
Reviewer

Makes sense, happy to review this.

tlively tlively commented on Oct 22, 2026 00:39 UTC
Author

Ok, I've pushed a new implementation that 1) replaces getFirstPop entirely, and 2) uses a PostWalker with a custom scan to be as efficient as possible. It also includes the simplification around unnamed blocks from #9292, so that will need to land before the tests pass here.

tlively tlively commented on code on Oct 22, 2026 00:39 UTC
Code Review
src/passes/ConstantFieldPropagation.cpp:1
@@ -453,9 +454,10 @@ struct FunctionOptimizer : public WalkerPass<PostWalker<FunctionOptimizer>> {
     WalkerPass<PostWalker<FunctionOptimizer>>::doWalkFunction(func);
 
     // If we changed anything, we need to update parent types as types may have
-    // changed.
+    // changed, and fix up any pops that we enclosed in a block.

Comment needs updating after #9292.

tlively tlively commented on code on Oct 22, 2026 00:40 UTC
Code Review
src/passes/GlobalStructInference.cpp:1
@@ -465,6 +467,7 @@ struct GlobalStructInference : public Pass {
           // to synchronize with in the first place.
           ref = builder.makeSequence(
             builder.makeDrop(builder.makeRefAs(RefAsNonNull, ref)), get);
+          needEHFixups = true;

Unnecessary after #9292.

tlively tlively commented on code on Oct 22, 2026 00:40 UTC
Code Review
src/passes/GlobalStructInference.cpp:1
@@ -514,6 +517,7 @@ struct GlobalStructInference : public Pass {
           replaceCurrent(builder.makeSequence(
             builder.makeDrop(builder.makeRefAs(RefAsNonNull, ref)),
             getReadValue(values[0], fieldIndex, field, curr)));
+          needEHFixups = true;

Unnecessary after #9292.

tlively tlively commented on code on Oct 22, 2026 00:42 UTC
Code Review
test/lit/passes/cfp.wast:1
@@ -3366,3 +3366,49 @@
     )
   )
 )
+
+(module
+  ;; CHECK:      (type $struct (struct (field i32)))
+  (type $struct (struct (field i32)))
+  ;; CHECK:      (type $1 (func (param (ref $struct))))
+
+  ;; CHECK:      (type $2 (func))
+
+  ;; CHECK:      (tag $e (type $1) (param (ref $struct)))
+  (tag $e (param (ref $struct)))
+
+  ;; CHECK:      (func $catch-pop (type $2)
+  ;; CHECK-NEXT:  (local $0 (ref $struct))
+  ;; CHECK-NEXT:  (try
+  ;; CHECK-NEXT:   (do
+  ;; CHECK-NEXT:   )
+  ;; CHECK-NEXT:   (catch $e
+  ;; CHECK-NEXT:    (local.set $0
+  ;; CHECK-NEXT:     (pop (ref $struct))
+  ;; CHECK-NEXT:    )
+  ;; CHECK-NEXT:    (drop
+  ;; CHECK-NEXT:     (block
+  ;; CHECK-NEXT:      (drop
+  ;; CHECK-NEXT:       (local.get $0)
+  ;; CHECK-NEXT:      )
+  ;; CHECK-NEXT:      (unreachable)
+  ;; CHECK-NEXT:     )
+  ;; CHECK-NEXT:    )
+  ;; CHECK-NEXT:   )
+  ;; CHECK-NEXT:  )
+  ;; CHECK-NEXT: )
+  (func $catch-pop
+    ;; Optimizing this struct.get wraps its reference in a block, requiring the
+    ;; enclosed pop to be fixed up.

Unnecessary after #9292. We can probably just remove the test, or alternatively keep it as a test showing that we need no fixup.

tlively tlively commented on code on Oct 22, 2026 00:43 UTC
Code Review
test/lit/passes/gsi.wast:1
@@ -2415,3 +2415,110 @@
     )
   )
 )
+
+(module
+  ;; CHECK:      (type $one (struct (field i32)))
+  (type $one (struct (field i32)))
+  ;; CHECK:      (type $two (struct (field i64)))
+  (type $two (struct (field i64)))
+  ;; CHECK:      (type $2 (func))
+
+  ;; CHECK:      (type $3 (func (param (ref $one))))
+
+  ;; CHECK:      (type $4 (func (param (ref $two))))
+
+  ;; CHECK:      (global $one (ref $one) (struct.new $one
+  ;; CHECK-NEXT:  (i32.const 10)
+  ;; CHECK-NEXT: ))
+  (global $one (ref $one) (struct.new $one (i32.const 10)))
+  ;; CHECK:      (global $two-a (ref $two) (struct.new $two
+  ;; CHECK-NEXT:  (i64.const 10)
+  ;; CHECK-NEXT: ))
+  (global $two-a (ref $two) (struct.new $two (i64.const 10)))
+  ;; CHECK:      (global $two-b (ref $two) (struct.new $two
+  ;; CHECK-NEXT:  (i64.const 20)
+  ;; CHECK-NEXT: ))
+  (global $two-b (ref $two) (struct.new $two (i64.const 20)))
+  ;; CHECK:      (tag $e-one (type $3) (param (ref $one)))
+  (tag $e-one (param (ref $one)))
+  ;; CHECK:      (tag $e-two (type $4) (param (ref $two)))
+  (tag $e-two (param (ref $two)))
+
+  ;; CHECK:      (func $catch-pop-block (type $2)
+  ;; CHECK-NEXT:  (local $0 (ref $one))
+  ;; CHECK-NEXT:  (try
+  ;; CHECK-NEXT:   (do
+  ;; CHECK-NEXT:   )
+  ;; CHECK-NEXT:   (catch $e-one
+  ;; CHECK-NEXT:    (local.set $0
+  ;; CHECK-NEXT:     (pop (ref $one))
+  ;; CHECK-NEXT:    )
+  ;; CHECK-NEXT:    (drop
+  ;; CHECK-NEXT:     (struct.get $one 0
+  ;; CHECK-NEXT:      (block (result (ref $one))
+  ;; CHECK-NEXT:       (drop
+  ;; CHECK-NEXT:        (ref.as_non_null
+  ;; CHECK-NEXT:         (local.get $0)
+  ;; CHECK-NEXT:        )
+  ;; CHECK-NEXT:       )
+  ;; CHECK-NEXT:       (global.get $one)
+  ;; CHECK-NEXT:      )
+  ;; CHECK-NEXT:     )
+  ;; CHECK-NEXT:    )
+  ;; CHECK-NEXT:   )
+  ;; CHECK-NEXT:  )
+  ;; CHECK-NEXT: )
+  (func $catch-pop-block
+    ;; Optimizing this struct.get wraps its reference in a block, requiring the
+    ;; enclosed pop to be fixed up.

Unnecessary after #9292.

Reviewers
Assignees
tlively tlively
Labels
risk: low confidence: 84.9% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 60.6% (MEDIUM) · Higher = larger blast radius
60.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 84.9% · Lower = rushed or shallow review
84.9%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 9.1% · Safe to merge
9.1%