Skip to content

Commit caae126

Browse files
committed
!fixup find OrigRdxResult (now MaxResult) by looking at users.
1 parent ec473e5 commit caae126

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
@@ -709,15 +709,20 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
709709
// Find the first index of with the maximum value. This is used to extract the
710710
// lane with the final max value and is needed to handle signed zeros and NaNs
711711
// in the input.
712-
auto *MiddleVPBB = Plan.getMiddleBlock();
713-
auto *OrigRdxResult = cast<VPSingleDefRecipe>(&MiddleVPBB->front());
714-
VPBuilder Builder(OrigRdxResult->getParent(),
715-
std::next(OrigRdxResult->getIterator()));
712+
auto *MaxResult = find_singleton<VPSingleDefRecipe>(
713+
RedPhiR->users(), [](VPUser *U, bool) -> VPSingleDefRecipe * {
714+
auto *VPI = dyn_cast<VPInstruction>(U);
715+
if (VPI && VPI->getOpcode() == VPInstruction::ComputeReductionResult)
716+
return VPI;
717+
return nullptr;
718+
});
719+
VPBuilder Builder(MaxResult->getParent(),
720+
std::next(MaxResult->getIterator()));
716721

717722
// Create mask for lanes that have the max value and use it to mask out
718723
// indices that don't contain maximum values.
719724
auto *MaskFinalMaxValue = Builder.createNaryOp(
720-
Instruction::FCmp, {OrigRdxResult->getOperand(1), OrigRdxResult},
725+
Instruction::FCmp, {MaxResult->getOperand(1), MaxResult},
721726
VPIRFlags(CmpInst::FCMP_OEQ));
722727
auto *IndicesWithMaxValue = Builder.createNaryOp(
723728
Instruction::Select, {MaskFinalMaxValue, MinIdxSel, UMinSentinel});
@@ -731,11 +736,10 @@ bool VPlanTransforms::handleFMaxReductionsWithoutFastMath(VPlan &Plan) {
731736
Builder.createNaryOp(Instruction::URem, {FirstMaxIdx, &Plan.getVFxUF()});
732737

733738
// Extract the final max value and update the users.
734-
auto *Res = Builder.createNaryOp(
735-
VPInstruction::ExtractLane, {FirstMaxLane, OrigRdxResult->getOperand(1)});
736-
OrigRdxResult->replaceUsesWithIf(Res,
737-
[MaskFinalMaxValue](VPUser &U, unsigned) {
738-
return &U != MaskFinalMaxValue;
739-
});
739+
auto *Res = Builder.createNaryOp(VPInstruction::ExtractLane,
740+
{FirstMaxLane, MaxResult->getOperand(1)});
741+
MaxResult->replaceUsesWithIf(Res, [MaskFinalMaxValue](VPUser &U, unsigned) {
742+
return &U != MaskFinalMaxValue;
743+
});
740744
return true;
741745
}

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)