Skip to content

Commit 5d136f9

Browse files
authored
[VPlan] Manage instruction metadata in VPlan. (llvm#135272)
Add a new helper to manage IR metadata that can be progated to generated instructions for recipes. This helps to remove a number of remaining uses of getUnderlyingInstr during VPlan execution. PR: llvm#135272
1 parent acc335b commit 5d136f9

File tree

4 files changed

+63
-50
lines changed

4 files changed

+63
-50
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,6 @@ void VPTransformState::addNewMetadata(Instruction *To,
363363
LVer->annotateInstWithNoAlias(To, Orig);
364364
}
365365

366-
void VPTransformState::addMetadata(Value *To, Instruction *From) {
367-
// No source instruction to transfer metadata from?
368-
if (!From)
369-
return;
370-
371-
if (Instruction *ToI = dyn_cast<Instruction>(To)) {
372-
propagateMetadata(ToI, From);
373-
addNewMetadata(ToI, From);
374-
}
375-
}
376-
377366
void VPTransformState::setDebugLocFrom(DebugLoc DL) {
378367
const DILocation *DIL = DL;
379368
// When a FSDiscriminator is enabled, we don't need to add the multiply

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,18 +1190,33 @@ struct VPIRPhi : public VPIRInstruction {
11901190
#endif
11911191
};
11921192

1193+
/// Helper to manage IR metadata for recipes. It filters out metadata that
1194+
/// cannot be propagated.
1195+
class VPIRMetadata {
1196+
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
1197+
1198+
protected:
1199+
VPIRMetadata() {}
1200+
VPIRMetadata(Instruction &I) { getMetadataToPropagate(&I, Metadata); }
1201+
1202+
public:
1203+
/// Add all metadata to \p I.
1204+
void applyMetadata(Instruction &I) const;
1205+
};
1206+
11931207
/// VPWidenRecipe is a recipe for producing a widened instruction using the
11941208
/// opcode and operands of the recipe. This recipe covers most of the
11951209
/// traditional vectorization cases where each recipe transforms into a
11961210
/// vectorized version of itself.
1197-
class VPWidenRecipe : public VPRecipeWithIRFlags {
1211+
class VPWidenRecipe : public VPRecipeWithIRFlags, public VPIRMetadata {
11981212
unsigned Opcode;
11991213

12001214
protected:
12011215
template <typename IterT>
12021216
VPWidenRecipe(unsigned VPDefOpcode, Instruction &I,
12031217
iterator_range<IterT> Operands)
1204-
: VPRecipeWithIRFlags(VPDefOpcode, Operands, I), Opcode(I.getOpcode()) {}
1218+
: VPRecipeWithIRFlags(VPDefOpcode, Operands, I), VPIRMetadata(I),
1219+
Opcode(I.getOpcode()) {}
12051220

12061221
public:
12071222
template <typename IterT>
@@ -1236,7 +1251,7 @@ class VPWidenRecipe : public VPRecipeWithIRFlags {
12361251
};
12371252

12381253
/// VPWidenCastRecipe is a recipe to create vector cast instructions.
1239-
class VPWidenCastRecipe : public VPRecipeWithIRFlags {
1254+
class VPWidenCastRecipe : public VPRecipeWithIRFlags, public VPIRMetadata {
12401255
/// Cast instruction opcode.
12411256
Instruction::CastOps Opcode;
12421257

@@ -1246,15 +1261,15 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
12461261
public:
12471262
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy,
12481263
CastInst &UI)
1249-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op, UI), Opcode(Opcode),
1250-
ResultTy(ResultTy) {
1264+
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op, UI), VPIRMetadata(UI),
1265+
Opcode(Opcode), ResultTy(ResultTy) {
12511266
assert(UI.getOpcode() == Opcode &&
12521267
"opcode of underlying cast doesn't match");
12531268
}
12541269

12551270
VPWidenCastRecipe(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy)
1256-
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op), Opcode(Opcode),
1257-
ResultTy(ResultTy) {}
1271+
: VPRecipeWithIRFlags(VPDef::VPWidenCastSC, Op), VPIRMetadata(),
1272+
Opcode(Opcode), ResultTy(ResultTy) {}
12581273

12591274
~VPWidenCastRecipe() override = default;
12601275

@@ -1288,7 +1303,7 @@ class VPWidenCastRecipe : public VPRecipeWithIRFlags {
12881303
};
12891304

12901305
/// A recipe for widening vector intrinsics.
1291-
class VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags {
1306+
class VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags, public VPIRMetadata {
12921307
/// ID of the vector intrinsic to widen.
12931308
Intrinsic::ID VectorIntrinsicID;
12941309

@@ -1309,7 +1324,7 @@ class VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags {
13091324
ArrayRef<VPValue *> CallArguments, Type *Ty,
13101325
DebugLoc DL = {})
13111326
: VPRecipeWithIRFlags(VPDef::VPWidenIntrinsicSC, CallArguments, CI),
1312-
VectorIntrinsicID(VectorIntrinsicID), ResultTy(Ty),
1327+
VPIRMetadata(CI), VectorIntrinsicID(VectorIntrinsicID), ResultTy(Ty),
13131328
MayReadFromMemory(CI.mayReadFromMemory()),
13141329
MayWriteToMemory(CI.mayWriteToMemory()),
13151330
MayHaveSideEffects(CI.mayHaveSideEffects()) {}
@@ -1318,7 +1333,7 @@ class VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags {
13181333
ArrayRef<VPValue *> CallArguments, Type *Ty,
13191334
DebugLoc DL = {})
13201335
: VPRecipeWithIRFlags(VPDef::VPWidenIntrinsicSC, CallArguments, DL),
1321-
VectorIntrinsicID(VectorIntrinsicID), ResultTy(Ty) {
1336+
VPIRMetadata(), VectorIntrinsicID(VectorIntrinsicID), ResultTy(Ty) {
13221337
LLVMContext &Ctx = Ty->getContext();
13231338
AttributeSet Attrs = Intrinsic::getFnAttributes(Ctx, VectorIntrinsicID);
13241339
MemoryEffects ME = Attrs.getMemoryEffects();
@@ -1374,7 +1389,7 @@ class VPWidenIntrinsicRecipe : public VPRecipeWithIRFlags {
13741389
};
13751390

13761391
/// A recipe for widening Call instructions using library calls.
1377-
class VPWidenCallRecipe : public VPRecipeWithIRFlags {
1392+
class VPWidenCallRecipe : public VPRecipeWithIRFlags, public VPIRMetadata {
13781393
/// Variant stores a pointer to the chosen function. There is a 1:1 mapping
13791394
/// between a given VF and the chosen vectorized variant, so there will be a
13801395
/// different VPlan for each VF with a valid variant.
@@ -1385,7 +1400,7 @@ class VPWidenCallRecipe : public VPRecipeWithIRFlags {
13851400
ArrayRef<VPValue *> CallArguments, DebugLoc DL = {})
13861401
: VPRecipeWithIRFlags(VPDef::VPWidenCallSC, CallArguments,
13871402
*cast<Instruction>(UV)),
1388-
Variant(Variant) {
1403+
VPIRMetadata(*cast<Instruction>(UV)), Variant(Variant) {
13891404
assert(
13901405
isa<Function>(getOperand(getNumOperands() - 1)->getLiveInIRValue()) &&
13911406
"last operand must be the called function");
@@ -1471,10 +1486,11 @@ class VPHistogramRecipe : public VPRecipeBase {
14711486
};
14721487

14731488
/// A recipe for widening select instructions.
1474-
struct VPWidenSelectRecipe : public VPRecipeWithIRFlags {
1489+
struct VPWidenSelectRecipe : public VPRecipeWithIRFlags, public VPIRMetadata {
14751490
template <typename IterT>
14761491
VPWidenSelectRecipe(SelectInst &I, iterator_range<IterT> Operands)
1477-
: VPRecipeWithIRFlags(VPDef::VPWidenSelectSC, Operands, I) {}
1492+
: VPRecipeWithIRFlags(VPDef::VPWidenSelectSC, Operands, I),
1493+
VPIRMetadata(I) {}
14781494

14791495
~VPWidenSelectRecipe() override = default;
14801496

@@ -2602,7 +2618,7 @@ class VPPredInstPHIRecipe : public VPSingleDefRecipe {
26022618

26032619
/// A common base class for widening memory operations. An optional mask can be
26042620
/// provided as the last operand.
2605-
class VPWidenMemoryRecipe : public VPRecipeBase {
2621+
class VPWidenMemoryRecipe : public VPRecipeBase, public VPIRMetadata {
26062622
protected:
26072623
Instruction &Ingredient;
26082624

@@ -2626,8 +2642,8 @@ class VPWidenMemoryRecipe : public VPRecipeBase {
26262642
VPWidenMemoryRecipe(const char unsigned SC, Instruction &I,
26272643
std::initializer_list<VPValue *> Operands,
26282644
bool Consecutive, bool Reverse, DebugLoc DL)
2629-
: VPRecipeBase(SC, Operands, DL), Ingredient(I), Consecutive(Consecutive),
2630-
Reverse(Reverse) {
2645+
: VPRecipeBase(SC, Operands, DL), VPIRMetadata(I), Ingredient(I),
2646+
Consecutive(Consecutive), Reverse(Reverse) {
26312647
assert((Consecutive || !Reverse) && "Reverse implies consecutive");
26322648
}
26332649

llvm/lib/Transforms/Vectorize/VPlanHelpers.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,6 @@ struct VPTransformState {
290290
/// vector loop.
291291
void addNewMetadata(Instruction *To, const Instruction *Orig);
292292

293-
/// Add metadata from one instruction to another.
294-
///
295-
/// This includes both the original MDs from \p From and additional ones (\see
296-
/// addNewMetadata). Use this for *newly created* instructions in the vector
297-
/// loop.
298-
void addMetadata(Value *To, Instruction *From);
299-
300293
/// Set the debug location in the builder using the debug location \p DL.
301294
void setDebugLocFrom(DebugLoc DL);
302295

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,11 @@ void VPIRPhi::print(raw_ostream &O, const Twine &Indent,
12051205
}
12061206
#endif
12071207

1208+
void VPIRMetadata::applyMetadata(Instruction &I) const {
1209+
for (const auto &[Kind, Node] : Metadata)
1210+
I.setMetadata(Kind, Node);
1211+
}
1212+
12081213
void VPWidenCallRecipe::execute(VPTransformState &State) {
12091214
assert(State.VF.isVector() && "not widening");
12101215
assert(Variant != nullptr && "Can't create vector function.");
@@ -1231,11 +1236,11 @@ void VPWidenCallRecipe::execute(VPTransformState &State) {
12311236

12321237
CallInst *V = State.Builder.CreateCall(Variant, Args, OpBundles);
12331238
applyFlags(*V);
1239+
applyMetadata(*V);
12341240
V->setCallingConv(Variant->getCallingConv());
12351241

12361242
if (!V->getType()->isVoidTy())
12371243
State.set(this, V);
1238-
State.addMetadata(V, CI);
12391244
}
12401245

12411246
InstructionCost VPWidenCallRecipe::computeCost(ElementCount VF,
@@ -1311,10 +1316,10 @@ void VPWidenIntrinsicRecipe::execute(VPTransformState &State) {
13111316
CallInst *V = State.Builder.CreateCall(VectorF, Args, OpBundles);
13121317

13131318
applyFlags(*V);
1319+
applyMetadata(*V);
13141320

13151321
if (!V->getType()->isVoidTy())
13161322
State.set(this, V);
1317-
State.addMetadata(V, CI);
13181323
}
13191324

13201325
InstructionCost VPWidenIntrinsicRecipe::computeCost(ElementCount VF,
@@ -1509,9 +1514,11 @@ void VPWidenSelectRecipe::execute(VPTransformState &State) {
15091514
Value *Op1 = State.get(getOperand(2));
15101515
Value *Sel = State.Builder.CreateSelect(Cond, Op0, Op1);
15111516
State.set(this, Sel);
1512-
if (isa<FPMathOperator>(Sel))
1513-
applyFlags(*cast<Instruction>(Sel));
1514-
State.addMetadata(Sel, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
1517+
if (auto *I = dyn_cast<Instruction>(Sel)) {
1518+
if (isa<FPMathOperator>(I))
1519+
applyFlags(*I);
1520+
applyMetadata(*I);
1521+
}
15151522
}
15161523

15171524
InstructionCost VPWidenSelectRecipe::computeCost(ElementCount VF,
@@ -1642,12 +1649,13 @@ void VPWidenRecipe::execute(VPTransformState &State) {
16421649

16431650
Value *V = Builder.CreateNAryOp(Opcode, Ops);
16441651

1645-
if (auto *VecOp = dyn_cast<Instruction>(V))
1652+
if (auto *VecOp = dyn_cast<Instruction>(V)) {
16461653
applyFlags(*VecOp);
1654+
applyMetadata(*VecOp);
1655+
}
16471656

16481657
// Use this vector value for all users of the original instruction.
16491658
State.set(this, V);
1650-
State.addMetadata(V, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
16511659
break;
16521660
}
16531661
case Instruction::ExtractValue: {
@@ -1679,8 +1687,9 @@ void VPWidenRecipe::execute(VPTransformState &State) {
16791687
} else {
16801688
C = Builder.CreateICmp(getPredicate(), A, B);
16811689
}
1690+
if (auto *I = dyn_cast<Instruction>(C))
1691+
applyMetadata(*I);
16821692
State.set(this, C);
1683-
State.addMetadata(C, dyn_cast_or_null<Instruction>(getUnderlyingValue()));
16841693
break;
16851694
}
16861695
default:
@@ -1796,9 +1805,10 @@ void VPWidenCastRecipe::execute(VPTransformState &State) {
17961805
Value *A = State.get(Op);
17971806
Value *Cast = Builder.CreateCast(Instruction::CastOps(Opcode), A, DestTy);
17981807
State.set(this, Cast);
1799-
State.addMetadata(Cast, cast_or_null<Instruction>(getUnderlyingValue()));
1800-
if (auto *CastOp = dyn_cast<Instruction>(Cast))
1808+
if (auto *CastOp = dyn_cast<Instruction>(Cast)) {
18011809
applyFlags(*CastOp);
1810+
applyMetadata(*CastOp);
1811+
}
18021812
}
18031813

18041814
InstructionCost VPWidenCastRecipe::computeCost(ElementCount VF,
@@ -2750,8 +2760,10 @@ void VPWidenLoadRecipe::execute(VPTransformState &State) {
27502760
} else {
27512761
NewLI = Builder.CreateAlignedLoad(DataTy, Addr, Alignment, "wide.load");
27522762
}
2753-
// Add metadata to the load, but setVectorValue to the reverse shuffle.
2754-
State.addMetadata(NewLI, cast<LoadInst>(&Ingredient));
2763+
// Add metadata to the load, but set the result to the reverse shuffle, if
2764+
// needed.
2765+
State.addNewMetadata(cast<Instruction>(NewLI), &Ingredient);
2766+
applyMetadata(*cast<Instruction>(NewLI));
27552767
if (Reverse)
27562768
NewLI = Builder.CreateVectorReverse(NewLI, "reverse");
27572769
State.set(this, NewLI);
@@ -2810,7 +2822,8 @@ void VPWidenLoadEVLRecipe::execute(VPTransformState &State) {
28102822
}
28112823
NewLI->addParamAttr(
28122824
0, Attribute::getWithAlignment(NewLI->getContext(), Alignment));
2813-
State.addMetadata(NewLI, cast<LoadInst>(&Ingredient));
2825+
State.addNewMetadata(NewLI, &Ingredient);
2826+
applyMetadata(*NewLI);
28142827
Instruction *Res = NewLI;
28152828
if (isReverse())
28162829
Res = createReverseEVL(Builder, Res, EVL, "vp.reverse");
@@ -2884,7 +2897,8 @@ void VPWidenStoreRecipe::execute(VPTransformState &State) {
28842897
NewSI = Builder.CreateMaskedStore(StoredVal, Addr, Alignment, Mask);
28852898
else
28862899
NewSI = Builder.CreateAlignedStore(StoredVal, Addr, Alignment);
2887-
State.addMetadata(NewSI, cast<StoreInst>(&Ingredient));
2900+
State.addNewMetadata(NewSI, &Ingredient);
2901+
applyMetadata(*NewSI);
28882902
}
28892903

28902904
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
@@ -2930,7 +2944,8 @@ void VPWidenStoreEVLRecipe::execute(VPTransformState &State) {
29302944
}
29312945
NewSI->addParamAttr(
29322946
1, Attribute::getWithAlignment(NewSI->getContext(), Alignment));
2933-
State.addMetadata(NewSI, cast<StoreInst>(&Ingredient));
2947+
State.addNewMetadata(NewSI, &Ingredient);
2948+
applyMetadata(*NewSI);
29342949
}
29352950

29362951
InstructionCost VPWidenStoreEVLRecipe::computeCost(ElementCount VF,

0 commit comments

Comments
 (0)