← Back to Pull requests

[SLP] Put the extract for a catchswitch-fed PHI after all PHIs #247022

Open
hamed7ir opened this pull request with 84 lines diff across 2 files
View on GitHub
hamed7ir hamed7ir commented on Nov 29, 2026 22:54 UTC
Author

When a vectorized scalar is used by a PHI whose incoming block ends in
a catchswitch, the extract can't go into that block, so it went right
after the vector value. If that value is a vector PHI followed by
another PHI, the extract ended up between the PHIs and the module was
invalid ("PHI nodes not grouped at top of basic block"). Insert it at
the first insertion point of the PHI's block instead.

Assisted-by: Claude Opus 5.5 (Anthropic)

Diff: 84 lines (+83 / -1) across 2 files Reviewer: alexey-bataev Review duration: 1365m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.36) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.36 · 84 lines diff across 2 files
0.36
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.93 · 5 substantive comment(s)
0.93
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
92%
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.266
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
7 items
hamed7ir hamed7ir commented on Nov 29, 2026 22:54 UTC
Author

@alexey-bataev we found this in a Notepad++ build with PGO instrumentation for 32-bit Windows on Arm: PGO's counter PHIs got vectorized, and clang then crashed in instruction selection on the invalid IR. cc @mstorsjo

llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 29, 2026 22:55 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-vectorizers

Author: Hamed (hamed7ir)

<details>
<summary>Changes</summary>

When a vectorized scalar is used by a PHI whose incoming block ends in
a catchswitch, the extract can't go into that block, so it went right
after the vector value. If that value is a vector PHI followed by
another PHI, the extract ended up between the PHIs and the module was
invalid ("PHI nodes not grouped at top of basic block"). Insert it at
the first insertion point of the PHI's block instead.

Assisted-by: Claude Opus 5.5 (Anthropic)

---
Full diff: https://github.com/llvm/llvm-project/pull/247022.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+6-1)

  • (added) llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll (+76)
  • ``````

    diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    index 0109b43d9a076..21d6d6df8af86 100644
    --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    @@ -28186,7 +28186,12 @@ BoUpSLP::vectorizeTree(const ExtraValueToDebugLocsMap &ExternallyUsedValues,
                 Instruction *IncomingTerminator =
                     PH->getIncomingBlock(I)->getTerminator();
                 if (isa<CatchSwitchInst>(IncomingTerminator)) {
    -              Builder.SetInsertPoint(std::next(VecI->getIterator()));
    +              // Other PHIs may follow a vector PHI, so go after all of them.
    +              if (auto *VecPHI = dyn_cast<PHINode>(VecI))
    +                Builder.SetInsertPoint(
    +                    VecPHI->getParent()->getFirstInsertionPt());
    +              else
    +                Builder.SetInsertPoint(std::next(VecI->getIterator()));
                 } else {
                   Builder.SetInsertPoint(PH->getIncomingBlock(I)->getTerminator());
                 }
    diff --git a/llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll b/llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll
    new file mode 100644
    index 0000000000000..fe69f3c429b72
    --- /dev/null
    +++ b/llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll
    @@ -0,0 +1,76 @@
    +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
    +; RUN: opt -S --passes=slp-vectorizer -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s
    +
    +; %b is used by a PHI in a cleanup whose predecessor is a catchswitch block,
    +; so its extract can't go there. It must go after all PHIs of the block of the
    +; vector PHI, not right after that PHI, where it would be between two vector
    +; PHIs.
    +
    +declare void @g()
    +declare i32 @__CxxFrameHandler3(...)
    +
    +define void @test(ptr %p) personality ptr @__CxxFrameHandler3 {
    +; CHECK-LABEL: define void @test(
    +; CHECK-SAME: ptr [[P:%.*]]) personality ptr @__CxxFrameHandler3 {
    +; CHECK-NEXT:  [[ENTRY:.*]]:
    +; CHECK-NEXT:    br label %[[LOOP:.*]]
    +; CHECK:       [[LOOP]]:
    +; CHECK-NEXT:    [[TMP0:%.*]] = phi <2 x i64> [ zeroinitializer, %[[LATCH:.*]] ], [ zeroinitializer, %[[ENTRY]] ]
    +; CHECK-NEXT:    [[TMP1:%.*]] = phi <2 x i64> [ zeroinitializer, %[[LATCH]] ], [ zeroinitializer, %[[ENTRY]] ]
    +; CHECK-NEXT:    [[TMP2:%.*]] = extractelement <2 x i64> [[TMP0]], i64 1
    +; CHECK-NEXT:    invoke void @g()
    +; CHECK-NEXT:            to label %[[LATCH]] unwind label %[[DISPATCH:.*]]
    +; CHECK:       [[DISPATCH]]:
    +; CHECK-NEXT:    [[CS:%.*]] = catchswitch within none [label %[[CATCH:.*]]] unwind label %[[CLEANUP:.*]]
    +; CHECK:       [[CATCH]]:
    +; CHECK-NEXT:    [[CP:%.*]] = catchpad within [[CS]] [ptr null, i32 0, ptr null]
    +; CHECK-NEXT:    catchret from [[CP]] to label %[[LATCH]]
    +; CHECK:       [[LATCH]]:
    +; CHECK-NEXT:    br i1 false, label %[[EXIT:.*]], label %[[LOOP]]
    +; CHECK:       [[CLEANUP]]:
    +; CHECK-NEXT:    [[E:%.*]] = phi i64 [ [[TMP2]], %[[DISPATCH]] ]
    +; CHECK-NEXT:    [[PAD:%.*]] = cleanuppad within none []
    +; CHECK-NEXT:    cleanupret from [[PAD]] unwind to caller
    +; CHECK:       [[EXIT]]:
    +; CHECK-NEXT:    [[TMP3:%.*]] = or <2 x i64> [[TMP0]], [[TMP1]]
    +; CHECK-NEXT:    [[P0:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i32 128
    +; CHECK-NEXT:    [[TMP4:%.*]] = or <2 x i64> [[TMP3]], zeroinitializer
    +; CHECK-NEXT:    store <2 x i64> [[TMP4]], ptr [[P0]], align 8
    +; CHECK-NEXT:    ret void
    +;
    +entry:
    +  br label %loop
    +
    +loop:
    +  %a = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  %b = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  %c = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  %d = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  invoke void @g() to label %latch unwind label %dispatch
    +
    +dispatch:
    +  %cs = catchswitch within none [label %catch] unwind label %cleanup
    +
    +catch:
    +  %cp = catchpad within %cs [ptr null, i32 0, ptr null]
    +  catchret from %cp to label %latch
    +
    +latch:
    +  br i1 false, label %exit, label %loop
    +
    +cleanup:
    +  %e = phi i64 [ %b, %dispatch ]
    +  %pad = cleanuppad within none []
    +  cleanupret from %pad unwind to caller
    +
    +exit:
    +  %or0 = or i64 %d, %c
    +  %or1 = or i64 %or0, 0
    +  %p0 = getelementptr inbounds nuw i8, ptr %p, i32 128
    +  store i64 %or1, ptr %p0, align 8
    +  %or2 = or i64 %b, %a
    +  %or3 = or i64 %or2, 0
    +  %p1 = getelementptr inbounds nuw i8, ptr %p, i32 136
    +  store i64 %or3, ptr %p1, align 8
    +  ret void
    +}
    
    
    ``````

    </details>

    llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Nov 29, 2026 22:55 UTC
    Bot

    <!--LLVM PR SUMMARY COMMENT-->

    @llvm/pr-subscribers-llvm-transforms

    Author: Hamed (hamed7ir)

    <details>
    <summary>Changes</summary>

    When a vectorized scalar is used by a PHI whose incoming block ends in
    a catchswitch, the extract can't go into that block, so it went right
    after the vector value. If that value is a vector PHI followed by
    another PHI, the extract ended up between the PHIs and the module was
    invalid ("PHI nodes not grouped at top of basic block"). Insert it at
    the first insertion point of the PHI's block instead.

    Assisted-by: Claude Opus 5.5 (Anthropic)

    ---
    Full diff: https://github.com/llvm/llvm-project/pull/247022.diff

    2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+6-1)

  • (added) llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll (+76)
  • ``````

    diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    index 0109b43d9a076..21d6d6df8af86 100644
    --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
    @@ -28186,7 +28186,12 @@ BoUpSLP::vectorizeTree(const ExtraValueToDebugLocsMap &ExternallyUsedValues,
                 Instruction *IncomingTerminator =
                     PH->getIncomingBlock(I)->getTerminator();
                 if (isa<CatchSwitchInst>(IncomingTerminator)) {
    -              Builder.SetInsertPoint(std::next(VecI->getIterator()));
    +              // Other PHIs may follow a vector PHI, so go after all of them.
    +              if (auto *VecPHI = dyn_cast<PHINode>(VecI))
    +                Builder.SetInsertPoint(
    +                    VecPHI->getParent()->getFirstInsertionPt());
    +              else
    +                Builder.SetInsertPoint(std::next(VecI->getIterator()));
                 } else {
                   Builder.SetInsertPoint(PH->getIncomingBlock(I)->getTerminator());
                 }
    diff --git a/llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll b/llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll
    new file mode 100644
    index 0000000000000..fe69f3c429b72
    --- /dev/null
    +++ b/llvm/test/Transforms/SLPVectorizer/X86/catchswitch-phi-extract.ll
    @@ -0,0 +1,76 @@
    +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
    +; RUN: opt -S --passes=slp-vectorizer -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s
    +
    +; %b is used by a PHI in a cleanup whose predecessor is a catchswitch block,
    +; so its extract can't go there. It must go after all PHIs of the block of the
    +; vector PHI, not right after that PHI, where it would be between two vector
    +; PHIs.
    +
    +declare void @g()
    +declare i32 @__CxxFrameHandler3(...)
    +
    +define void @test(ptr %p) personality ptr @__CxxFrameHandler3 {
    +; CHECK-LABEL: define void @test(
    +; CHECK-SAME: ptr [[P:%.*]]) personality ptr @__CxxFrameHandler3 {
    +; CHECK-NEXT:  [[ENTRY:.*]]:
    +; CHECK-NEXT:    br label %[[LOOP:.*]]
    +; CHECK:       [[LOOP]]:
    +; CHECK-NEXT:    [[TMP0:%.*]] = phi <2 x i64> [ zeroinitializer, %[[LATCH:.*]] ], [ zeroinitializer, %[[ENTRY]] ]
    +; CHECK-NEXT:    [[TMP1:%.*]] = phi <2 x i64> [ zeroinitializer, %[[LATCH]] ], [ zeroinitializer, %[[ENTRY]] ]
    +; CHECK-NEXT:    [[TMP2:%.*]] = extractelement <2 x i64> [[TMP0]], i64 1
    +; CHECK-NEXT:    invoke void @g()
    +; CHECK-NEXT:            to label %[[LATCH]] unwind label %[[DISPATCH:.*]]
    +; CHECK:       [[DISPATCH]]:
    +; CHECK-NEXT:    [[CS:%.*]] = catchswitch within none [label %[[CATCH:.*]]] unwind label %[[CLEANUP:.*]]
    +; CHECK:       [[CATCH]]:
    +; CHECK-NEXT:    [[CP:%.*]] = catchpad within [[CS]] [ptr null, i32 0, ptr null]
    +; CHECK-NEXT:    catchret from [[CP]] to label %[[LATCH]]
    +; CHECK:       [[LATCH]]:
    +; CHECK-NEXT:    br i1 false, label %[[EXIT:.*]], label %[[LOOP]]
    +; CHECK:       [[CLEANUP]]:
    +; CHECK-NEXT:    [[E:%.*]] = phi i64 [ [[TMP2]], %[[DISPATCH]] ]
    +; CHECK-NEXT:    [[PAD:%.*]] = cleanuppad within none []
    +; CHECK-NEXT:    cleanupret from [[PAD]] unwind to caller
    +; CHECK:       [[EXIT]]:
    +; CHECK-NEXT:    [[TMP3:%.*]] = or <2 x i64> [[TMP0]], [[TMP1]]
    +; CHECK-NEXT:    [[P0:%.*]] = getelementptr inbounds nuw i8, ptr [[P]], i32 128
    +; CHECK-NEXT:    [[TMP4:%.*]] = or <2 x i64> [[TMP3]], zeroinitializer
    +; CHECK-NEXT:    store <2 x i64> [[TMP4]], ptr [[P0]], align 8
    +; CHECK-NEXT:    ret void
    +;
    +entry:
    +  br label %loop
    +
    +loop:
    +  %a = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  %b = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  %c = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  %d = phi i64 [ 0, %latch ], [ 0, %entry ]
    +  invoke void @g() to label %latch unwind label %dispatch
    +
    +dispatch:
    +  %cs = catchswitch within none [label %catch] unwind label %cleanup
    +
    +catch:
    +  %cp = catchpad within %cs [ptr null, i32 0, ptr null]
    +  catchret from %cp to label %latch
    +
    +latch:
    +  br i1 false, label %exit, label %loop
    +
    +cleanup:
    +  %e = phi i64 [ %b, %dispatch ]
    +  %pad = cleanuppad within none []
    +  cleanupret from %pad unwind to caller
    +
    +exit:
    +  %or0 = or i64 %d, %c
    +  %or1 = or i64 %or0, 0
    +  %p0 = getelementptr inbounds nuw i8, ptr %p, i32 128
    +  store i64 %or1, ptr %p0, align 8
    +  %or2 = or i64 %b, %a
    +  %or3 = or i64 %or2, 0
    +  %p1 = getelementptr inbounds nuw i8, ptr %p, i32 136
    +  store i64 %or3, ptr %p1, align 8
    +  ret void
    +}
    
    
    ``````

    </details>

    alexey-bataev alexey-bataev commented on code on Nov 29, 2026 22:59 UTC
    Code Review
    llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:1
    @@ -28186,7 +28186,12 @@ BoUpSLP::vectorizeTree(const ExtraValueToDebugLocsMap &ExternallyUsedValues,
                 Instruction *IncomingTerminator =
                     PH->getIncomingBlock(I)->getTerminator();
                 if (isa<CatchSwitchInst>(IncomingTerminator)) {
    -              Builder.SetInsertPoint(std::next(VecI->getIterator()));
    +              // Other PHIs may follow a vector PHI, so go after all of them.
    +              if (auto *VecPHI = dyn_cast<PHINode>(VecI))
    +                Builder.SetInsertPoint(
    +                    VecPHI->getParent()->getFirstInsertionPt());
    +              else
    +                Builder.SetInsertPoint(std::next(VecI->getIterator()));

                  if (auto *VecPHI = dyn_cast<PHINode>(VecI)) {
                    Builder.SetInsertPoint(
                        VecPHI->getParent()->getFirstInsertionPt());
                  } else {
                    Builder.SetInsertPoint(std::next(VecI->getIterator()));
                  }
    

    alexey-bataev alexey-bataev approved these changes on Nov 29, 2026 22:59 UTC
    Approved Reviewer

    LG with a nit

    hamed7ir hamed7ir commented on code on Nov 29, 2026 23:24 UTC
    Code Review
    llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:1
    @@ -28186,7 +28186,12 @@ BoUpSLP::vectorizeTree(const ExtraValueToDebugLocsMap &ExternallyUsedValues,
                 Instruction *IncomingTerminator =
                     PH->getIncomingBlock(I)->getTerminator();
                 if (isa<CatchSwitchInst>(IncomingTerminator)) {
    -              Builder.SetInsertPoint(std::next(VecI->getIterator()));
    +              // Other PHIs may follow a vector PHI, so go after all of them.
    +              if (auto *VecPHI = dyn_cast<PHINode>(VecI))
    +                Builder.SetInsertPoint(
    +                    VecPHI->getParent()->getFirstInsertionPt());
    +              else
    +                Builder.SetInsertPoint(std::next(VecI->getIterator()));

    Thanks, done in 71db3db4e61c.

    Reviewers
    Assignees
    hamed7ir hamed7ir
    Labels
    risk: low confidence: 92% 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: 36% (MEDIUM) · Higher = larger blast radius
    36%
    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: 92% · Lower = rushed or shallow review
    92%
    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: 2.9% · Safe to merge
    2.9%