Skip to content

Commit b57fc68

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 fb00fa5 commit b57fc68

File tree

8 files changed

+179
-112
lines changed

8 files changed

+179
-112
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8612,11 +8612,13 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
86128612
}
86138613
if (LoadInst *Load = dyn_cast<LoadInst>(I))
86148614
return new VPWidenLoadRecipe(*Load, Ptr, Mask, Consecutive, Reverse,
8615+
getMetadataToPropagate(Load),
86158616
I->getDebugLoc());
86168617

86178618
StoreInst *Store = cast<StoreInst>(I);
86188619
return new VPWidenStoreRecipe(*Store, Ptr, Operands[0], Mask, Consecutive,
8619-
Reverse, I->getDebugLoc());
8620+
Reverse, getMetadataToPropagate(Store),
8621+
I->getDebugLoc());
86208622
}
86218623

86228624
/// Creates a VPWidenIntOrFpInductionRecpipe for \p Phi. If needed, it will also
@@ -8758,6 +8760,7 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
87588760
Range);
87598761
if (ShouldUseVectorIntrinsic)
87608762
return new VPWidenIntrinsicRecipe(*CI, ID, Ops, CI->getType(),
8763+
getMetadataToPropagate(CI),
87618764
CI->getDebugLoc());
87628765

87638766
Function *Variant = nullptr;
@@ -8811,7 +8814,8 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
88118814
}
88128815

88138816
Ops.push_back(Operands.back());
8814-
return new VPWidenCallRecipe(CI, Variant, Ops, CI->getDebugLoc());
8817+
return new VPWidenCallRecipe(CI, Variant, Ops, getMetadataToPropagate(CI),
8818+
CI->getDebugLoc());
88158819
}
88168820

88178821
return nullptr;
@@ -8849,7 +8853,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88498853
Plan.getOrAddLiveIn(ConstantInt::get(I->getType(), 1u, false));
88508854
auto *SafeRHS = Builder.createSelect(Mask, Ops[1], One, I->getDebugLoc());
88518855
Ops[1] = SafeRHS;
8852-
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()));
8856+
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()),
8857+
getMetadataToPropagate(I));
88538858
}
88548859
[[fallthrough]];
88558860
}
@@ -8895,7 +8900,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88958900
// For other binops, the legacy cost model only checks the second operand.
88968901
NewOps[1] = GetConstantViaSCEV(NewOps[1]);
88978902
}
8898-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8903+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8904+
getMetadataToPropagate(I));
88998905
}
89008906
case Instruction::ExtractValue: {
89018907
SmallVector<VPValue *> NewOps(Operands);
@@ -8904,7 +8910,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
89048910
assert(EVI->getNumIndices() == 1 && "Expected one extractvalue index");
89058911
unsigned Idx = EVI->getIndices()[0];
89068912
NewOps.push_back(Plan.getOrAddLiveIn(ConstantInt::get(I32Ty, Idx, false)));
8907-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8913+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8914+
getMetadataToPropagate(I));
89088915
}
89098916
};
89108917
}
@@ -9109,6 +9116,13 @@ bool VPRecipeBuilder::getScaledReductions(
91099116
return false;
91109117
}
91119118

9119+
SmallVector<std::pair<unsigned, MDNode *>>
9120+
VPRecipeBuilder::getMetadataToPropagate(Instruction *I) {
9121+
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
9122+
::getMetadataToPropagate(I, Metadata);
9123+
return Metadata;
9124+
}
9125+
91129126
VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
91139127
Instruction *Instr, ArrayRef<VPValue *> Operands, VFRange &Range) {
91149128
// First, check for specific widening recipes that deal with inductions, Phi
@@ -9181,13 +9195,14 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
91819195
make_range(Operands.begin(), Operands.end()));
91829196

91839197
if (auto *SI = dyn_cast<SelectInst>(Instr)) {
9184-
return new VPWidenSelectRecipe(
9185-
*SI, make_range(Operands.begin(), Operands.end()));
9198+
return new VPWidenSelectRecipe(*SI,
9199+
make_range(Operands.begin(), Operands.end()),
9200+
getMetadataToPropagate(SI));
91869201
}
91879202

91889203
if (auto *CI = dyn_cast<CastInst>(Instr)) {
91899204
return new VPWidenCastRecipe(CI->getOpcode(), Operands[0], CI->getType(),
9190-
*CI);
9205+
*CI, getMetadataToPropagate(CI));
91919206
}
91929207

91939208
return tryToWiden(Instr, Operands);
@@ -9213,7 +9228,8 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
92139228
SmallVector<VPValue *, 2> Ops;
92149229
Ops.push_back(Plan.getOrAddLiveIn(Zero));
92159230
Ops.push_back(BinOp);
9216-
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()));
9231+
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()),
9232+
getMetadataToPropagate(Reduction));
92179233
Builder.insert(BinOp->getDefiningRecipe());
92189234
ReductionOpcode = Instruction::Add;
92199235
}

llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ class VPRecipeBuilder {
233233
}
234234
return Plan.getOrAddLiveIn(V);
235235
}
236+
237+
/// Returns the metatadata that can be preserved from the original instruction
238+
/// \p I, including noalias metadata guaranteed by runtime checks.
239+
SmallVector<std::pair<unsigned, MDNode *>>
240+
getMetadataToPropagate(Instruction *I);
236241
};
237242
} // end namespace llvm
238243

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)