Skip to content

Commit f23246a

Browse files
committed
[LV] Directly add fast-math flags to select recipe (NFC).
Now that VPInstruction can manage fast math flags via VPRecipeWithIRFlags, use them directly to model the fast-math flags of the select created for the final reduction value instead of adding them late.
1 parent fe2f67e commit f23246a

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,9 +3832,6 @@ void InnerLoopVectorizer::fixReduction(VPReductionPHIRecipe *PhiR,
38323832
assert(Sel && "Reduction exit feeds no select");
38333833
State.reset(LoopExitInstDef, Sel, Part);
38343834

3835-
if (isa<FPMathOperator>(Sel))
3836-
Sel->setFastMathFlags(RdxDesc.getFastMathFlags());
3837-
38383835
// If the target can create a predicated operator for the reduction at no
38393836
// extra cost in the loop (for example a predicated vadd), it can be
38403837
// cheaper for the select to remain in the loop than be sunk out of it,
@@ -9162,12 +9159,19 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
91629159
VPReductionPHIRecipe *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
91639160
if (!PhiR || PhiR->isInLoop())
91649161
continue;
9162+
const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
91659163
VPValue *Cond =
91669164
RecipeBuilder.createBlockInMask(OrigLoop->getHeader(), *Plan);
91679165
VPValue *Red = PhiR->getBackedgeValue();
91689166
assert(Red->getDefiningRecipe()->getParent() != LatchVPBB &&
91699167
"reduction recipe must be defined before latch");
9170-
Builder.createNaryOp(Instruction::Select, {Cond, Red, PhiR});
9168+
FastMathFlags FMFs = RdxDesc.getFastMathFlags();
9169+
Type *PhiTy = PhiR->getOperand(0)->getLiveInIRValue()->getType();
9170+
auto *Select =
9171+
PhiTy->isFloatingPointTy()
9172+
? new VPInstruction(Instruction::Select, {Cond, Red, PhiR}, FMFs)
9173+
: new VPInstruction(Instruction::Select, {Cond, Red, PhiR});
9174+
Select->insertBefore(&*Builder.getInsertPoint());
91719175
}
91729176
}
91739177

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,15 @@ bool VPInstruction::isFPMathOp() const {
415415
return Opcode == Instruction::FAdd || Opcode == Instruction::FMul ||
416416
Opcode == Instruction::FNeg || Opcode == Instruction::FSub ||
417417
Opcode == Instruction::FDiv || Opcode == Instruction::FRem ||
418-
Opcode == Instruction::FCmp;
418+
Opcode == Instruction::FCmp || Opcode == Instruction::Select;
419419
}
420420
#endif
421421

422422
void VPInstruction::execute(VPTransformState &State) {
423423
assert(!State.Instance && "VPInstruction executing an Instance");
424424
IRBuilderBase::FastMathFlagGuard FMFGuard(State.Builder);
425-
assert(hasFastMathFlags() == isFPMathOp() &&
425+
assert((hasFastMathFlags() == isFPMathOp() ||
426+
getOpcode() == Instruction::Select) &&
426427
"Recipe not a FPMathOp but has fast-math flags?");
427428
if (hasFastMathFlags())
428429
State.Builder.setFastMathFlags(getFastMathFlags());

0 commit comments

Comments
 (0)