Skip to content

Commit 72b5b76

Browse files
committed
!fixup find OrigRdxResult (now MaxResult) by looking at users.
1 parent 84925e4 commit 72b5b76

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,20 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
670670
// Find the first index of with the maximum value. This is used to extract the
671671
// lane with the final max value and is needed to handle signed zeros and NaNs
672672
// in the input.
673-
auto *MiddleVPBB = Plan.getMiddleBlock();
674-
auto *OrigRdxResult = cast<VPSingleDefRecipe>(&MiddleVPBB->front());
675-
VPBuilder Builder(OrigRdxResult->getParent(),
676-
std::next(OrigRdxResult->getIterator()));
673+
auto *MaxResult = find_singleton<VPSingleDefRecipe>(
674+
RedPhiR->users(), [](VPUser *U, bool) -> VPSingleDefRecipe * {
675+
auto *VPI = dyn_cast<VPInstruction>(U);
676+
if (VPI && VPI->getOpcode() == VPInstruction::ComputeReductionResult)
677+
return VPI;
678+
return nullptr;
679+
});
680+
VPBuilder Builder(MaxResult->getParent(),
681+
std::next(MaxResult->getIterator()));
677682

678683
// Create mask for lanes that have the max value and use it to mask out
679684
// indices that don't contain maximum values.
680685
auto *MaskFinalMaxValue = Builder.createNaryOp(
681-
Instruction::FCmp, {OrigRdxResult->getOperand(1), OrigRdxResult},
686+
Instruction::FCmp, {MaxResult->getOperand(1), MaxResult},
682687
VPIRFlags(CmpInst::FCMP_OEQ));
683688
auto *IndicesWithMaxValue = Builder.createNaryOp(
684689
Instruction::Select, {MaskFinalMaxValue, MinIdxSel, UMinSentinel});
@@ -692,11 +697,10 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
692697
Builder.createNaryOp(Instruction::URem, {FirstMaxIdx, &Plan.getVFxUF()});
693698

694699
// Extract the final max value and update the users.
695-
auto *Res = Builder.createNaryOp(
696-
VPInstruction::ExtractLane, {FirstMaxLane, OrigRdxResult->getOperand(1)});
697-
OrigRdxResult->replaceUsesWithIf(Res,
698-
[MaskFinalMaxValue](VPUser &U, unsigned) {
699-
return &U != MaskFinalMaxValue;
700-
});
700+
auto *Res = Builder.createNaryOp(VPInstruction::ExtractLane,
701+
{FirstMaxLane, MaxResult->getOperand(1)});
702+
MaxResult->replaceUsesWithIf(Res, [MaskFinalMaxValue](VPUser &U, unsigned) {
703+
return &U != MaskFinalMaxValue;
704+
});
701705
return true;
702706
}

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ bool VPInstruction::canGenerateScalarForFirstLane() const {
505505
return true;
506506
switch (Opcode) {
507507
case Instruction::Freeze:
508+
case Instruction::FCmp:
508509
case Instruction::ICmp:
509510
case Instruction::PHI:
510511
case Instruction::Select:

0 commit comments

Comments
 (0)