Skip to content

Commit fd539f8

Browse files
committed
!fixup, address comments and using transferFlags() to copy nneg.
Only VPWidenCastRecipe with ZExt continas nneg flag. Using transferFlags() to prevent get the nneg flag from a not-nneg flags operands.
1 parent b32538f commit fd539f8

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,14 @@ class VPRecipeWithIRFlags : public VPSingleDefRecipe {
823823

824824
FastMathFlags getFastMathFlags() const;
825825

826-
bool isNonNeg() const { return NonNegFlags.NonNeg; }
826+
/// Returns true if the recipe has non-negative flag.
827+
bool hasNonNegFlag() const { return OpType == OperationType::NonNegOp; }
828+
829+
bool isNonNeg() const {
830+
assert(OpType == OperationType::NonNegOp &&
831+
"recipe doesn't have a NNEG flag");
832+
return NonNegFlags.NonNeg;
833+
}
827834

828835
bool hasNoUnsignedWrap() const {
829836
assert(OpType == OperationType::OverflowingBinOp &&
@@ -2374,18 +2381,18 @@ class VPReductionRecipe : public VPRecipeWithIRFlags {
23742381
}
23752382

23762383
/// For VPExtendedReductionRecipe.
2377-
/// Note that IsNonNeg flag and the debug location are from the extend.
2384+
/// Note that the debug location is from the extend.
23782385
VPReductionRecipe(const unsigned char SC, const RecurKind RdxKind,
23792386
ArrayRef<VPValue *> Operands, VPValue *CondOp,
2380-
bool IsOrdered, NonNegFlagsTy NonNeg, DebugLoc DL)
2381-
: VPRecipeWithIRFlags(SC, Operands, NonNeg, DL), RdxKind(RdxKind),
2387+
bool IsOrdered, DebugLoc DL)
2388+
: VPRecipeWithIRFlags(SC, Operands, DL), RdxKind(RdxKind),
23822389
IsOrdered(IsOrdered), IsConditional(CondOp) {
23832390
if (CondOp)
23842391
addOperand(CondOp);
23852392
}
23862393

23872394
/// For VPMulAccumulateReductionRecipe.
2388-
/// Note that the NUW/NSW and DL are from the Mul.
2395+
/// Note that the NUW/NSW flags and the debug location are from the Mul.
23892396
VPReductionRecipe(const unsigned char SC, const RecurKind RdxKind,
23902397
ArrayRef<VPValue *> Operands, VPValue *CondOp,
23912398
bool IsOrdered, WrapFlagsTy WrapFlags, DebugLoc DL)
@@ -2462,7 +2469,7 @@ class VPReductionRecipe : public VPRecipeWithIRFlags {
24622469
/// A recipe to represent inloop reduction operations with vector-predication
24632470
/// intrinsics, performing a reduction on a vector operand with the explicit
24642471
/// vector length (EVL) into a scalar value, and adding the result to a chain.
2465-
/// The Operands are {ChainOp, VecOp, EVL, [Condition]}.
2472+
/// The operands are {ChainOp, VecOp, EVL, [Condition]}.
24662473
class VPReductionEVLRecipe : public VPReductionRecipe {
24672474
public:
24682475
VPReductionEVLRecipe(VPReductionRecipe &R, VPValue &EVL, VPValue *CondOp,
@@ -2518,17 +2525,21 @@ class VPExtendedReductionRecipe : public VPReductionRecipe {
25182525
: VPReductionRecipe(
25192526
VPDef::VPExtendedReductionSC, ExtRed->getRecurrenceKind(),
25202527
{ExtRed->getChainOp(), ExtRed->getVecOp()}, ExtRed->getCondOp(),
2521-
ExtRed->isOrdered(), NonNegFlagsTy(ExtRed->isNonNeg()),
2522-
ExtRed->getDebugLoc()),
2523-
ExtOp(ExtRed->getExtOpcode()), ResultTy(ExtRed->getResultType()) {}
2528+
ExtRed->isOrdered(), ExtRed->getDebugLoc()),
2529+
ExtOp(ExtRed->getExtOpcode()), ResultTy(ExtRed->getResultType()) {
2530+
transferFlags(*ExtRed);
2531+
}
25242532

25252533
public:
25262534
VPExtendedReductionRecipe(VPReductionRecipe *R, VPWidenCastRecipe *Ext)
25272535
: VPReductionRecipe(VPDef::VPExtendedReductionSC, R->getRecurrenceKind(),
25282536
{R->getChainOp(), Ext->getOperand(0)}, R->getCondOp(),
2529-
R->isOrdered(), NonNegFlagsTy(Ext->isNonNeg()),
2530-
Ext->getDebugLoc()),
2531-
ExtOp(Ext->getOpcode()), ResultTy(Ext->getResultType()) {}
2537+
R->isOrdered(), Ext->getDebugLoc()),
2538+
ExtOp(Ext->getOpcode()), ResultTy(Ext->getResultType()) {
2539+
// Not all WidenCastRecipes contain nneg flag. Need to transfer flags from
2540+
// the original recipe to prevent setting wrong flags.
2541+
transferFlags(*Ext);
2542+
}
25322543

25332544
~VPExtendedReductionRecipe() override = default;
25342545

@@ -2555,21 +2566,21 @@ class VPExtendedReductionRecipe : public VPReductionRecipe {
25552566
VPSlotTracker &SlotTracker) const override;
25562567
#endif
25572568

2558-
/// The scalar type after extended.
2569+
/// The scalar type after extending.
25592570
Type *getResultType() const { return ResultTy; }
25602571

25612572
/// Is the extend ZExt?
25622573
bool isZExt() const { return getExtOpcode() == Instruction::ZExt; }
25632574

2564-
/// The Opcode of extend recipe.
2575+
/// The opcode of extend recipe.
25652576
Instruction::CastOps getExtOpcode() const { return ExtOp; }
25662577
};
25672578

25682579
/// A recipe to represent inloop MulAccumulateReduction operations, performing a
25692580
/// reduction.add on the result of vector operands (might be extended)
25702581
/// multiplication into a scalar value, and adding the result to a chain. This
25712582
/// recipe is abstract and needs to be lowered to concrete recipes before
2572-
/// codegen. The Operands are {ChainOp, VecOp1, VecOp2, [Condition]}.
2583+
/// codegen. The operands are {ChainOp, VecOp1, VecOp2, [Condition]}.
25732584
class VPMulAccumulateReductionRecipe : public VPReductionRecipe {
25742585
/// Opcode of the extend recipe.
25752586
Instruction::CastOps ExtOp;
@@ -2600,12 +2611,14 @@ class VPMulAccumulateReductionRecipe : public VPReductionRecipe {
26002611
R->getCondOp(), R->isOrdered(),
26012612
WrapFlagsTy(Mul->hasNoUnsignedWrap(), Mul->hasNoSignedWrap()),
26022613
R->getDebugLoc()),
2603-
ExtOp(Ext0->getOpcode()), IsNonNeg(Ext0->isNonNeg()),
2604-
ResultTy(ResultTy) {
2614+
ExtOp(Ext0->getOpcode()), ResultTy(ResultTy) {
26052615
assert(RecurrenceDescriptor::getOpcode(getRecurrenceKind()) ==
26062616
Instruction::Add &&
26072617
"The reduction instruction in MulAccumulateteReductionRecipe must "
26082618
"be Add");
2619+
// Only set the non-negative flag if the original recipe contains.
2620+
if (Ext0->hasNonNegFlag())
2621+
IsNonNeg = Ext0->isNonNeg();
26092622
}
26102623

26112624
VPMulAccumulateReductionRecipe(VPReductionRecipe *R, VPWidenRecipe *Mul)
@@ -2658,13 +2671,13 @@ class VPMulAccumulateReductionRecipe : public VPReductionRecipe {
26582671
VPValue *getVecOp0() const { return getOperand(1); }
26592672
VPValue *getVecOp1() const { return getOperand(2); }
26602673

2661-
/// Return if this MulAcc recipe contains extend instructions.
2674+
/// Return if this MulAcc recipe contains extended operands.
26622675
bool isExtended() const { return ExtOp != Instruction::CastOps::CastOpsEnd; }
26632676

2664-
/// Return the opcode of the underlying extend.
2677+
/// Return the opcode of the extends for the operands.
26652678
Instruction::CastOps getExtOpcode() const { return ExtOp; }
26662679

2667-
/// Return if the extend opcode is ZExt.
2680+
/// Return if the operands are zero extended.
26682681
bool isZExt() const { return ExtOp == Instruction::CastOps::ZExt; }
26692682

26702683
/// Return the non negative flag of the ext recipe.

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,8 @@ Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
272272
// TODO: Use info from interleave group.
273273
return V->getUnderlyingValue()->getType();
274274
})
275-
.Case<VPExtendedReductionRecipe>(
276-
[](const VPExtendedReductionRecipe *R) {
277-
return R->getResultType();
278-
})
279-
.Case<VPMulAccumulateReductionRecipe>(
280-
[](const VPMulAccumulateReductionRecipe *R) {
281-
return R->getResultType();
282-
})
275+
.Case<VPExtendedReductionRecipe, VPMulAccumulateReductionRecipe>(
276+
[](const auto *R) { return R->getResultType(); })
283277
.Case<VPExpandSCEVRecipe>([](const VPExpandSCEVRecipe *R) {
284278
return R->getSCEV()->getType();
285279
})

llvm/lib/Transforms/Vectorize/VPlanHelpers.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ struct VFRange {
7878
return End.getKnownMinValue() <= Start.getKnownMinValue();
7979
}
8080

81-
bool contains(const ElementCount &VF) const {
82-
return VF.getKnownMinValue() >= Start.getKnownMinValue() &&
83-
VF.getKnownMinValue() < End.getKnownMinValue();
84-
}
85-
8681
VFRange(const ElementCount &Start, const ElementCount &End)
8782
: Start(Start), End(End) {
8883
assert(Start.isScalable() == End.isScalable() &&

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ bool VPRecipeBase::mayHaveSideEffects() const {
155155
case VPBlendSC:
156156
case VPReductionEVLSC:
157157
case VPReductionSC:
158+
case VPExtendedReductionSC:
159+
case VPMulAccumulateReductionSC:
158160
case VPScalarIVStepsSC:
159161
case VPVectorPointerSC:
160162
case VPWidenCanonicalIVSC:

0 commit comments

Comments
 (0)