[SLP] Put the extract for a catchswitch-fed PHI after all PHIs #247022
@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
<!--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:
````````````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>
<!--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:
````````````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>
if (auto *VecPHI = dyn_cast<PHINode>(VecI)) {
Builder.SetInsertPoint(
VecPHI->getParent()->getFirstInsertionPt());
} else {
Builder.SetInsertPoint(std::next(VecI->getIterator()));
}
LG with a nit
Thanks, done in 71db3db4e61c.
alexey-bataev
hamed7ir
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)