Skip to content

Commit f60c229

Browse files
committed
[VPlan] Manage instruction medata in VPlan.
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. Add metadata on cloning.
1 parent 48478ba commit f60c229

File tree

8 files changed

+176
-112
lines changed

8 files changed

+176
-112
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8587,11 +8587,13 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
85878587
}
85888588
if (LoadInst *Load = dyn_cast<LoadInst>(I))
85898589
return new VPWidenLoadRecipe(*Load, Ptr, Mask, Consecutive, Reverse,
8590+
getMetadataToPropagate(Load),
85908591
I->getDebugLoc());
85918592

85928593
StoreInst *Store = cast<StoreInst>(I);
85938594
return new VPWidenStoreRecipe(*Store, Ptr, Operands[0], Mask, Consecutive,
8594-
Reverse, I->getDebugLoc());
8595+
Reverse, getMetadataToPropagate(Store),
8596+
I->getDebugLoc());
85958597
}
85968598

85978599
/// Creates a VPWidenIntOrFpInductionRecpipe for \p Phi. If needed, it will also
@@ -8733,6 +8735,7 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
87338735
Range);
87348736
if (ShouldUseVectorIntrinsic)
87358737
return new VPWidenIntrinsicRecipe(*CI, ID, Ops, CI->getType(),
8738+
getMetadataToPropagate(CI),
87368739
CI->getDebugLoc());
87378740

87388741
Function *Variant = nullptr;
@@ -8786,7 +8789,8 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
87868789
}
87878790

87888791
Ops.push_back(Operands.back());
8789-
return new VPWidenCallRecipe(CI, Variant, Ops, CI->getDebugLoc());
8792+
return new VPWidenCallRecipe(CI, Variant, Ops, getMetadataToPropagate(CI),
8793+
CI->getDebugLoc());
87908794
}
87918795

87928796
return nullptr;
@@ -8824,7 +8828,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88248828
Plan.getOrAddLiveIn(ConstantInt::get(I->getType(), 1u, false));
88258829
auto *SafeRHS = Builder.createSelect(Mask, Ops[1], One, I->getDebugLoc());
88268830
Ops[1] = SafeRHS;
8827-
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()));
8831+
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()),
8832+
getMetadataToPropagate(I));
88288833
}
88298834
[[fallthrough]];
88308835
}
@@ -8870,7 +8875,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88708875
// For other binops, the legacy cost model only checks the second operand.
88718876
NewOps[1] = GetConstantViaSCEV(NewOps[1]);
88728877
}
8873-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8878+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8879+
getMetadataToPropagate(I));
88748880
}
88758881
case Instruction::ExtractValue: {
88768882
SmallVector<VPValue *> NewOps(Operands);
@@ -8879,7 +8885,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88798885
assert(EVI->getNumIndices() == 1 && "Expected one extractvalue index");
88808886
unsigned Idx = EVI->getIndices()[0];
88818887
NewOps.push_back(Plan.getOrAddLiveIn(ConstantInt::get(I32Ty, Idx, false)));
8882-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8888+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8889+
getMetadataToPropagate(I));
88838890
}
88848891
};
88858892
}
@@ -9084,6 +9091,13 @@ bool VPRecipeBuilder::getScaledReductions(
90849091
return false;
90859092
}
90869093

9094+
SmallVector<std::pair<unsigned, MDNode *>>
9095+
VPRecipeBuilder::getMetadataToPropagate(Instruction *I) {
9096+
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
9097+
::getMetadataToPropagate(I, Metadata);
9098+
return Metadata;
9099+
}
9100+
90879101
VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
90889102
Instruction *Instr, ArrayRef<VPValue *> Operands, VFRange &Range) {
90899103
// First, check for specific widening recipes that deal with inductions, Phi
@@ -9156,13 +9170,14 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
91569170
make_range(Operands.begin(), Operands.end()));
91579171

91589172
if (auto *SI = dyn_cast<SelectInst>(Instr)) {
9159-
return new VPWidenSelectRecipe(
9160-
*SI, make_range(Operands.begin(), Operands.end()));
9173+
return new VPWidenSelectRecipe(*SI,
9174+
make_range(Operands.begin(), Operands.end()),
9175+
getMetadataToPropagate(SI));
91619176
}
91629177

91639178
if (auto *CI = dyn_cast<CastInst>(Instr)) {
91649179
return new VPWidenCastRecipe(CI->getOpcode(), Operands[0], CI->getType(),
9165-
*CI);
9180+
*CI, getMetadataToPropagate(CI));
91669181
}
91679182

91689183
return tryToWiden(Instr, Operands);
@@ -9188,7 +9203,8 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
91889203
SmallVector<VPValue *, 2> Ops;
91899204
Ops.push_back(Plan.getOrAddLiveIn(Zero));
91909205
Ops.push_back(BinOp);
9191-
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()));
9206+
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()),
9207+
getMetadataToPropagate(Reduction));
91929208
Builder.insert(BinOp->getDefiningRecipe());
91939209
ReductionOpcode = Instruction::Add;
91949210
}

llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class VPRecipeBuilder {
150150
Instruction *PHI, Instruction *RdxExitInstr, VFRange &Range,
151151
SmallVectorImpl<std::pair<PartialReductionChain, unsigned>> &Chains);
152152

153+
SmallVector<std::pair<unsigned, MDNode *>>
154+
getMetadataToPropagate(Instruction *I);
155+
153156
public:
154157
VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI,
155158
const TargetTransformInfo *TTI,

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -355,23 +355,13 @@ BasicBlock *VPTransformState::CFGState::getPreheaderBBFor(VPRecipeBase *R) {
355355
return VPBB2IRBB[LoopRegion->getPreheaderVPBB()];
356356
}
357357

358-
void VPTransformState::addNewMetadata(Instruction *To,
359-
const Instruction *Orig) {
358+
void VPTransformState::addNewMetadata(Value *To, const Instruction *Orig) {
359+
360360
// If the loop was versioned with memchecks, add the corresponding no-alias
361361
// metadata.
362-
if (LVer && isa<LoadInst, StoreInst>(Orig))
363-
LVer->annotateInstWithNoAlias(To, Orig);
364-
}
365-
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-
}
362+
Instruction *ToI = dyn_cast<Instruction>(To);
363+
if (ToI && LVer && isa<LoadInst, StoreInst>(Orig))
364+
LVer->annotateInstWithNoAlias(ToI, Orig);
375365
}
376366

377367
void VPTransformState::setDebugLocFrom(DebugLoc DL) {

0 commit comments

Comments
 (0)