Skip to content

Commit 165e24a

Browse files
committed
[VPlan] Move DebugLoc to VPRecipeBase (NFCI).
Add a dedicated debug location to VPRecipeBase to remove another unneeded use of the underlying LLVM IR instruction and also consolidate various DL fields in sub classes. Each recipe can have debug location and it shouldn't rely on reference to the underlying LLVM IR instructions to retain it. See various recipes that had separate DL fields already.
1 parent 0f1507a commit 165e24a

File tree

2 files changed

+48
-52
lines changed

2 files changed

+48
-52
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -709,13 +709,18 @@ class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock>,
709709
/// Each VPRecipe belongs to a single VPBasicBlock.
710710
VPBasicBlock *Parent = nullptr;
711711

712+
/// The debug location for the recipe.
713+
DebugLoc DL;
714+
712715
public:
713-
VPRecipeBase(const unsigned char SC, ArrayRef<VPValue *> Operands)
714-
: VPDef(SC), VPUser(Operands, VPUser::VPUserID::Recipe) {}
716+
VPRecipeBase(const unsigned char SC, ArrayRef<VPValue *> Operands,
717+
DebugLoc DL = {})
718+
: VPDef(SC), VPUser(Operands, VPUser::VPUserID::Recipe), DL(DL) {}
715719

716720
template <typename IterT>
717-
VPRecipeBase(const unsigned char SC, iterator_range<IterT> Operands)
718-
: VPDef(SC), VPUser(Operands, VPUser::VPUserID::Recipe) {}
721+
VPRecipeBase(const unsigned char SC, iterator_range<IterT> Operands,
722+
DebugLoc DL = {})
723+
: VPDef(SC), VPUser(Operands, VPUser::VPUserID::Recipe), DL(DL) {}
719724
virtual ~VPRecipeBase() = default;
720725

721726
/// \return the VPBasicBlock which this VPRecipe belongs to.
@@ -792,6 +797,9 @@ class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock>,
792797
bool mayReadOrWriteMemory() const {
793798
return mayReadFromMemory() || mayWriteToMemory();
794799
}
800+
801+
/// Returns the debug location of the recipe.
802+
DebugLoc getDebugLoc() const { return DL; }
795803
};
796804

797805
// Helper macro to define common classof implementations for recipes.
@@ -862,15 +870,15 @@ class VPRecipeWithIRFlags : public VPRecipeBase {
862870

863871
public:
864872
template <typename IterT>
865-
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands)
866-
: VPRecipeBase(SC, Operands) {
873+
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands, DebugLoc DL = {})
874+
: VPRecipeBase(SC, Operands, DL) {
867875
OpType = OperationType::Other;
868876
AllFlags = 0;
869877
}
870878

871879
template <typename IterT>
872880
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands, Instruction &I)
873-
: VPRecipeWithIRFlags(SC, Operands) {
881+
: VPRecipeWithIRFlags(SC, Operands, I.getDebugLoc()) {
874882
if (auto *Op = dyn_cast<CmpInst>(&I)) {
875883
OpType = OperationType::Cmp;
876884
CmpPredicate = Op->getPredicate();
@@ -891,20 +899,20 @@ class VPRecipeWithIRFlags : public VPRecipeBase {
891899

892900
template <typename IterT>
893901
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands,
894-
CmpInst::Predicate Pred)
895-
: VPRecipeBase(SC, Operands), OpType(OperationType::Cmp),
902+
CmpInst::Predicate Pred, DebugLoc DL = {})
903+
: VPRecipeBase(SC, Operands, DL), OpType(OperationType::Cmp),
896904
CmpPredicate(Pred) {}
897905

898906
template <typename IterT>
899907
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands,
900-
WrapFlagsTy WrapFlags)
901-
: VPRecipeBase(SC, Operands), OpType(OperationType::OverflowingBinOp),
908+
WrapFlagsTy WrapFlags, DebugLoc DL = {})
909+
: VPRecipeBase(SC, Operands, DL), OpType(OperationType::OverflowingBinOp),
902910
WrapFlags(WrapFlags) {}
903911

904912
template <typename IterT>
905913
VPRecipeWithIRFlags(const unsigned char SC, IterT Operands,
906-
FastMathFlags FMFs)
907-
: VPRecipeBase(SC, Operands), OpType(OperationType::FPMathOp),
914+
FastMathFlags FMFs, DebugLoc DL = {})
915+
: VPRecipeBase(SC, Operands, DL), OpType(OperationType::FPMathOp),
908916
FMFs(FMFs) {}
909917

910918
static inline bool classof(const VPRecipeBase *R) {
@@ -1030,7 +1038,6 @@ class VPInstruction : public VPRecipeWithIRFlags, public VPValue {
10301038
private:
10311039
typedef unsigned char OpcodeTy;
10321040
OpcodeTy Opcode;
1033-
DebugLoc DL;
10341041

10351042
/// An optional name that can be used for the generated IR instruction.
10361043
const std::string Name;
@@ -1053,8 +1060,8 @@ class VPInstruction : public VPRecipeWithIRFlags, public VPValue {
10531060
public:
10541061
VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands, DebugLoc DL,
10551062
const Twine &Name = "")
1056-
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands), VPValue(this),
1057-
Opcode(Opcode), DL(DL), Name(Name.str()) {}
1063+
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, DL),
1064+
VPValue(this), Opcode(Opcode), Name(Name.str()) {}
10581065

10591066
VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands,
10601067
DebugLoc DL = {}, const Twine &Name = "")
@@ -1065,8 +1072,8 @@ class VPInstruction : public VPRecipeWithIRFlags, public VPValue {
10651072

10661073
VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands,
10671074
WrapFlagsTy WrapFlags, DebugLoc DL = {}, const Twine &Name = "")
1068-
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, WrapFlags),
1069-
VPValue(this), Opcode(Opcode), DL(DL), Name(Name.str()) {}
1075+
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, WrapFlags, DL),
1076+
VPValue(this), Opcode(Opcode), Name(Name.str()) {}
10701077

10711078
VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands,
10721079
FastMathFlags FMFs, DebugLoc DL = {}, const Twine &Name = "");
@@ -1238,7 +1245,8 @@ class VPWidenCallRecipe : public VPRecipeBase, public VPValue {
12381245
struct VPWidenSelectRecipe : public VPRecipeBase, public VPValue {
12391246
template <typename IterT>
12401247
VPWidenSelectRecipe(SelectInst &I, iterator_range<IterT> Operands)
1241-
: VPRecipeBase(VPDef::VPWidenSelectSC, Operands), VPValue(this, &I) {}
1248+
: VPRecipeBase(VPDef::VPWidenSelectSC, Operands, I.getDebugLoc()),
1249+
VPValue(this, &I) {}
12421250

12431251
~VPWidenSelectRecipe() override = default;
12441252

@@ -1324,8 +1332,8 @@ class VPWidenGEPRecipe : public VPRecipeWithIRFlags, public VPValue {
13241332
class VPHeaderPHIRecipe : public VPRecipeBase, public VPValue {
13251333
protected:
13261334
VPHeaderPHIRecipe(unsigned char VPDefID, Instruction *UnderlyingInstr,
1327-
VPValue *Start = nullptr)
1328-
: VPRecipeBase(VPDefID, {}), VPValue(this, UnderlyingInstr) {
1335+
VPValue *Start = nullptr, DebugLoc DL = {})
1336+
: VPRecipeBase(VPDefID, {}, DL), VPValue(this, UnderlyingInstr) {
13291337
if (Start)
13301338
addOperand(Start);
13311339
}
@@ -1607,14 +1615,13 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe {
16071615
/// A recipe for vectorizing a phi-node as a sequence of mask-based select
16081616
/// instructions.
16091617
class VPBlendRecipe : public VPRecipeBase, public VPValue {
1610-
PHINode *Phi;
1611-
16121618
public:
16131619
/// The blend operation is a User of the incoming values and of their
16141620
/// respective masks, ordered [I0, M0, I1, M1, ...]. Note that a single value
16151621
/// might be incoming with a full mask for which there is no VPValue.
16161622
VPBlendRecipe(PHINode *Phi, ArrayRef<VPValue *> Operands)
1617-
: VPRecipeBase(VPDef::VPBlendSC, Operands), VPValue(this, Phi), Phi(Phi) {
1623+
: VPRecipeBase(VPDef::VPBlendSC, Operands, Phi->getDebugLoc()),
1624+
VPValue(this, Phi) {
16181625
assert(Operands.size() > 0 &&
16191626
((Operands.size() == 1) || (Operands.size() % 2 == 0)) &&
16201627
"Expected either a single incoming value or a positive even number "
@@ -2047,11 +2054,9 @@ class VPExpandSCEVRecipe : public VPRecipeBase, public VPValue {
20472054
/// loop). VPWidenCanonicalIVRecipe represents the vector version of the
20482055
/// canonical induction variable.
20492056
class VPCanonicalIVPHIRecipe : public VPHeaderPHIRecipe {
2050-
DebugLoc DL;
2051-
20522057
public:
20532058
VPCanonicalIVPHIRecipe(VPValue *StartV, DebugLoc DL)
2054-
: VPHeaderPHIRecipe(VPDef::VPCanonicalIVPHISC, nullptr, StartV), DL(DL) {}
2059+
: VPHeaderPHIRecipe(VPDef::VPCanonicalIVPHISC, nullptr, StartV, DL) {}
20552060

20562061
~VPCanonicalIVPHIRecipe() override = default;
20572062

@@ -2094,12 +2099,10 @@ class VPCanonicalIVPHIRecipe : public VPHeaderPHIRecipe {
20942099
/// TODO: It would be good to use the existing VPWidenPHIRecipe instead and
20952100
/// remove VPActiveLaneMaskPHIRecipe.
20962101
class VPActiveLaneMaskPHIRecipe : public VPHeaderPHIRecipe {
2097-
DebugLoc DL;
2098-
20992102
public:
21002103
VPActiveLaneMaskPHIRecipe(VPValue *StartMask, DebugLoc DL)
2101-
: VPHeaderPHIRecipe(VPDef::VPActiveLaneMaskPHISC, nullptr, StartMask),
2102-
DL(DL) {}
2104+
: VPHeaderPHIRecipe(VPDef::VPActiveLaneMaskPHISC, nullptr, StartMask,
2105+
DL) {}
21032106

21042107
~VPActiveLaneMaskPHIRecipe() override = default;
21052108

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,25 @@ VPInstruction::VPInstruction(unsigned Opcode, CmpInst::Predicate Pred,
250250
VPValue *A, VPValue *B, DebugLoc DL,
251251
const Twine &Name)
252252
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, ArrayRef<VPValue *>({A, B}),
253-
Pred),
254-
VPValue(this), Opcode(Opcode), DL(DL), Name(Name.str()) {
253+
Pred, DL),
254+
VPValue(this), Opcode(Opcode), Name(Name.str()) {
255255
assert(Opcode == Instruction::ICmp &&
256256
"only ICmp predicates supported at the moment");
257257
}
258258

259259
VPInstruction::VPInstruction(unsigned Opcode,
260260
std::initializer_list<VPValue *> Operands,
261261
FastMathFlags FMFs, DebugLoc DL, const Twine &Name)
262-
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, FMFs),
263-
VPValue(this), Opcode(Opcode), DL(DL), Name(Name.str()) {
262+
: VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, FMFs, DL),
263+
VPValue(this), Opcode(Opcode), Name(Name.str()) {
264264
// Make sure the VPInstruction is a floating-point operation.
265265
assert(isFPMathOp() && "this op can't take fast-math flags");
266266
}
267267

268268
Value *VPInstruction::generateInstruction(VPTransformState &State,
269269
unsigned Part) {
270270
IRBuilderBase &Builder = State.Builder;
271-
Builder.SetCurrentDebugLocation(DL);
271+
Builder.SetCurrentDebugLocation(getDebugLoc());
272272

273273
if (Instruction::isBinaryOp(getOpcode())) {
274274
Value *A = State.get(getOperand(0), Part);
@@ -488,7 +488,7 @@ void VPInstruction::print(raw_ostream &O, const Twine &Indent,
488488
printFlags(O);
489489
printOperands(O, SlotTracker);
490490

491-
if (DL) {
491+
if (auto DL = getDebugLoc()) {
492492
O << ", !dbg ";
493493
DL.print(O);
494494
}
@@ -591,8 +591,7 @@ void VPWidenSelectRecipe::print(raw_ostream &O, const Twine &Indent,
591591
#endif
592592

593593
void VPWidenSelectRecipe::execute(VPTransformState &State) {
594-
auto &I = *cast<SelectInst>(getUnderlyingInstr());
595-
State.setDebugLocFrom(I.getDebugLoc());
594+
State.setDebugLocFrom(getDebugLoc());
596595

597596
// The condition can be loop invariant but still defined inside the
598597
// loop. This means that we can't just use the original 'cond' value.
@@ -607,7 +606,7 @@ void VPWidenSelectRecipe::execute(VPTransformState &State) {
607606
Value *Op1 = State.get(getOperand(2), Part);
608607
Value *Sel = State.Builder.CreateSelect(Cond, Op0, Op1);
609608
State.set(this, Sel, Part);
610-
State.addMetadata(Sel, &I);
609+
State.addMetadata(Sel, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
611610
}
612611
}
613612

@@ -654,6 +653,7 @@ void VPRecipeWithIRFlags::printFlags(raw_ostream &O) const {
654653
#endif
655654

656655
void VPWidenRecipe::execute(VPTransformState &State) {
656+
State.setDebugLocFrom(getDebugLoc());
657657
auto &I = *cast<Instruction>(getUnderlyingValue());
658658
auto &Builder = State.Builder;
659659
switch (I.getOpcode()) {
@@ -683,8 +683,6 @@ void VPWidenRecipe::execute(VPTransformState &State) {
683683
case Instruction::Or:
684684
case Instruction::Xor: {
685685
// Just widen unops and binops.
686-
State.setDebugLocFrom(I.getDebugLoc());
687-
688686
for (unsigned Part = 0; Part < State.UF; ++Part) {
689687
SmallVector<Value *, 2> Ops;
690688
for (VPValue *VPOp : operands())
@@ -703,8 +701,6 @@ void VPWidenRecipe::execute(VPTransformState &State) {
703701
break;
704702
}
705703
case Instruction::Freeze: {
706-
State.setDebugLocFrom(I.getDebugLoc());
707-
708704
for (unsigned Part = 0; Part < State.UF; ++Part) {
709705
Value *Op = State.get(getOperand(0), Part);
710706

@@ -718,7 +714,6 @@ void VPWidenRecipe::execute(VPTransformState &State) {
718714
// Widen compares. Generate vector compares.
719715
bool FCmp = (I.getOpcode() == Instruction::FCmp);
720716
auto *Cmp = cast<CmpInst>(&I);
721-
State.setDebugLocFrom(Cmp->getDebugLoc());
722717
for (unsigned Part = 0; Part < State.UF; ++Part) {
723718
Value *A = State.get(getOperand(0), Part);
724719
Value *B = State.get(getOperand(1), Part);
@@ -756,9 +751,7 @@ void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent,
756751
#endif
757752

758753
void VPWidenCastRecipe::execute(VPTransformState &State) {
759-
auto *I = cast_or_null<Instruction>(getUnderlyingValue());
760-
if (I)
761-
State.setDebugLocFrom(I->getDebugLoc());
754+
State.setDebugLocFrom(getDebugLoc());
762755
auto &Builder = State.Builder;
763756
/// Vectorize casts.
764757
assert(State.VF.isVector() && "Not vectorizing?");
@@ -768,7 +761,7 @@ void VPWidenCastRecipe::execute(VPTransformState &State) {
768761
Value *A = State.get(getOperand(0), Part);
769762
Value *Cast = Builder.CreateCast(Instruction::CastOps(Opcode), A, DestTy);
770763
State.set(this, Cast, Part);
771-
State.addMetadata(Cast, I);
764+
State.addMetadata(Cast, cast_or_null<Instruction>(getUnderlyingValue()));
772765
}
773766
}
774767

@@ -1193,7 +1186,7 @@ void VPWidenGEPRecipe::print(raw_ostream &O, const Twine &Indent,
11931186
#endif
11941187

11951188
void VPBlendRecipe::execute(VPTransformState &State) {
1196-
State.setDebugLocFrom(Phi->getDebugLoc());
1189+
State.setDebugLocFrom(getDebugLoc());
11971190
// We know that all PHIs in non-header blocks are converted into
11981191
// selects, so we don't have to worry about the insertion order and we
11991192
// can just use the builder.
@@ -1417,7 +1410,7 @@ void VPCanonicalIVPHIRecipe::execute(VPTransformState &State) {
14171410

14181411
BasicBlock *VectorPH = State.CFG.getPreheaderBBFor(this);
14191412
EntryPart->addIncoming(Start, VectorPH);
1420-
EntryPart->setDebugLoc(DL);
1413+
EntryPart->setDebugLoc(getDebugLoc());
14211414
for (unsigned Part = 0, UF = State.UF; Part < UF; ++Part)
14221415
State.set(this, EntryPart, Part);
14231416
}
@@ -1687,7 +1680,7 @@ void VPActiveLaneMaskPHIRecipe::execute(VPTransformState &State) {
16871680
PHINode *EntryPart =
16881681
State.Builder.CreatePHI(StartMask->getType(), 2, "active.lane.mask");
16891682
EntryPart->addIncoming(StartMask, VectorPH);
1690-
EntryPart->setDebugLoc(DL);
1683+
EntryPart->setDebugLoc(getDebugLoc());
16911684
State.set(this, EntryPart, Part);
16921685
}
16931686
}

0 commit comments

Comments
 (0)