Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7356,8 +7356,10 @@ static void fixReductionScalarResumeWhenVectorizingEpilog(
VPlanPatternMatch::m_Select(VPlanPatternMatch::m_VPValue(),
VPlanPatternMatch::m_VPValue(BackedgeVal),
VPlanPatternMatch::m_VPValue()));
EpiRedHeaderPhi = cast<VPReductionPHIRecipe>(
EpiRedHeaderPhi = cast_if_present<VPReductionPHIRecipe>(
vputils::findRecipe(BackedgeVal, IsaPred<VPReductionPHIRecipe>));
if (!EpiRedHeaderPhi)
return;
}

Value *MainResumeValue;
Expand Down Expand Up @@ -9361,12 +9363,22 @@ static void fixScalarResumeValuesFromBypass(BasicBlock *BypassBlock, Loop *L,
// Fix induction resume values from the additional bypass block.
IRBuilder<> BypassBuilder(BypassBlock, BypassBlock->getFirstInsertionPt());
for (const auto &[IVPhi, II] : LVL.getInductionVars()) {
auto *Inc = cast<PHINode>(IVPhi->getIncomingValueForBlock(PH));
Value *IncomingFromPH = IVPhi->getIncomingValueForBlock(PH);
Value *V = createInductionAdditionalBypassValues(
IVPhi, II, BypassBuilder, ExpandedSCEVs, MainVectorTripCount,
LVL.getPrimaryInduction());
// TODO: Directly add as extra operand to the VPResumePHI recipe.
Inc->setIncomingValueForBlock(BypassBlock, V);

if (auto *Inc = dyn_cast<PHINode>(IncomingFromPH)) {
// TODO: Directly add as extra operand to the VPResumePHI recipe.
Inc->setIncomingValueForBlock(BypassBlock, V);
} else {
// If the incoming value from preheader is not a PHI node (e.g., a
// constant in functions with GC statepoint), directly add an incoming
// value from BypassBlock to IVPhi. The incoming value from PH remains
// unchanged (IncomingFromPH).
if (IVPhi->getBasicBlockIndex(BypassBlock) == -1)
IVPhi->addIncoming(V, BypassBlock);
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions llvm/test/Transforms/LoopVectorize/pr179407-gc-statepoint.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
; RUN: opt -passes=loop-vectorize -enable-epilogue-vectorization -force-vector-width=2 -epilogue-vectorization-force-VF=2 -S < %s | FileCheck %s
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you generate the full IR for the test, using update_test_checks.py?

Is it possible that we end up with a trivial reduction? I think we expect them to be folded away

;
; Test case for issue #179407: LoopVectorize should not crash when
; vectorizing loops in functions with GC statepoint.
;
; The issue was that fixScalarResumeValuesFromBypass assumed
; IVPhi->getIncomingValueForBlock(PH) always returns a PHINode, but in
; functions with GC statepoint, it could return a constant or other value.
;
; This test verifies that epilogue vectorization works correctly with
; GC statepoint by checking that the loop is vectorized and the
; bypass blocks are properly created.

Comment on lines +10 to +13
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is related to statepoints?

; CHECK: define void @wombat
; CHECK-SAME: gc "statepoint-example"
; CHECK: vector.body:
; CHECK: vec.epilog.vector.body:
; CHECK: vec.epilog.scalar.ph:
; CHECK: bb1:

define void @wombat(i64 %arg) gc "statepoint-example" {
bb:
br label %bb1

bb1: ; preds = %bb1, %bb
%phi = phi i64 [ 0, %bb ], [ %add, %bb1 ]
%phi2 = phi i32 [ 0, %bb ], [ %or, %bb1 ]
%phi3 = phi i32 [ 0, %bb ], [ %select, %bb1 ]
%icmp = icmp eq i32 0, 0
%select = select i1 %icmp, i32 0, i32 %phi3
%or = or i32 %phi2, 0
%add = add i64 %phi, 1
%icmp4 = icmp ult i64 %phi, %arg
br i1 %icmp4, label %bb1, label %bb5

bb5: ; preds = %bb1
%phi6 = phi i32 [ %select, %bb1 ]
%phi7 = phi i32 [ %or, %bb1 ]
ret void
}