Skip to content

Commit f2a577e

Browse files
committed
[VPlan] Fix broadcasted values using loop region during execution
I noticed this after updating llvm#118638 on top of llvm#117506, and seeing that some broadcasts were no longer being hoisted into the loop preheader. It was calling VPlan::getVectorPreheader(), which at this point now is dissolved and returns null. This fixes it by getting the header from vputils::getFirstLoopHeader and getting the preheader from its first successor. I've also added an assertion in getVectorPreheader() to make sure its not called after the regions are dissolved.
1 parent 95ea436 commit f2a577e

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,22 @@ Value *VPTransformState::get(const VPValue *Def, bool NeedsScalar) {
290290
return Data.VPV2Vector[Def];
291291

292292
auto GetBroadcastInstrs = [this, Def](Value *V) {
293+
VPBasicBlock *PreheaderVPBB = nullptr;
294+
if (auto *Header = vputils::getFirstLoopHeader(*Plan, VPDT))
295+
PreheaderVPBB = cast<VPBasicBlock>(Header->getPredecessors()[0]);
296+
293297
bool SafeToHoist =
294-
!Def->hasDefiningRecipe() ||
295-
VPDT.properlyDominates(Def->getDefiningRecipe()->getParent(),
296-
Plan->getVectorPreheader());
298+
PreheaderVPBB &&
299+
(!Def->hasDefiningRecipe() ||
300+
VPDT.properlyDominates(Def->getDefiningRecipe()->getParent(),
301+
PreheaderVPBB));
297302

298303
if (VF.isScalar())
299304
return V;
300305
// Place the code for broadcasting invariant variables in the new preheader.
301306
IRBuilder<>::InsertPointGuard Guard(Builder);
302307
if (SafeToHoist) {
303-
BasicBlock *LoopVectorPreHeader =
304-
CFG.VPBB2IRBB[Plan->getVectorPreheader()];
308+
BasicBlock *LoopVectorPreHeader = CFG.VPBB2IRBB[PreheaderVPBB];
305309
if (LoopVectorPreHeader)
306310
Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
307311
}

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,13 +3984,11 @@ class VPlan {
39843984
VPBasicBlock *getEntry() { return Entry; }
39853985
const VPBasicBlock *getEntry() const { return Entry; }
39863986

3987-
/// Returns the preheader of the vector loop region, if one exists, or null
3988-
/// otherwise.
3987+
/// Returns the preheader of the vector loop region, provided it exists.
39893988
VPBasicBlock *getVectorPreheader() {
39903989
VPRegionBlock *VectorRegion = getVectorLoopRegion();
3991-
return VectorRegion
3992-
? cast<VPBasicBlock>(VectorRegion->getSinglePredecessor())
3993-
: nullptr;
3990+
assert(VectorRegion && "vector loop region no longer exists?");
3991+
return cast<VPBasicBlock>(VectorRegion->getSinglePredecessor());
39943992
}
39953993

39963994
/// Returns the VPRegionBlock of the vector loop.

llvm/test/Transforms/LoopVectorize/X86/pr141968-instsimplifyfolder.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ define i8 @pr141968(i1 %cond, i8 %v) {
1414
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <16 x i1> poison, i1 [[COND]], i64 0
1515
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <16 x i1> [[BROADCAST_SPLATINSERT]], <16 x i1> poison, <16 x i32> zeroinitializer
1616
; CHECK-NEXT: [[TMP0:%.*]] = xor <16 x i1> [[BROADCAST_SPLAT]], splat (i1 true)
17+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT31:%.*]] = insertelement <16 x i8> poison, i8 [[V]], i64 0
18+
; CHECK-NEXT: [[BROADCAST_SPLAT32:%.*]] = shufflevector <16 x i8> [[BROADCAST_SPLATINSERT31]], <16 x i8> poison, <16 x i32> zeroinitializer
1719
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
1820
; CHECK: [[VECTOR_BODY]]:
1921
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_SDIV_CONTINUE30:.*]] ]
@@ -97,8 +99,6 @@ define i8 @pr141968(i1 %cond, i8 %v) {
9799
; CHECK: [[PRED_SDIV_IF29]]:
98100
; CHECK-NEXT: br label %[[PRED_SDIV_CONTINUE30]]
99101
; CHECK: [[PRED_SDIV_CONTINUE30]]:
100-
; CHECK-NEXT: [[BROADCAST_SPLATINSERT31:%.*]] = insertelement <16 x i8> poison, i8 [[V]], i64 0
101-
; CHECK-NEXT: [[BROADCAST_SPLAT32:%.*]] = shufflevector <16 x i8> [[BROADCAST_SPLATINSERT31]], <16 x i8> poison, <16 x i32> zeroinitializer
102102
; CHECK-NEXT: [[PREDPHI:%.*]] = select <16 x i1> [[BROADCAST_SPLAT]], <16 x i8> zeroinitializer, <16 x i8> [[BROADCAST_SPLAT32]]
103103
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 16
104104
; CHECK-NEXT: [[TMP17:%.*]] = icmp eq i32 [[INDEX_NEXT]], 256

0 commit comments

Comments
 (0)