Skip to content

Commit c45aa04

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 e6e56f5 commit c45aa04

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
@@ -8585,11 +8585,13 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
85858585
}
85868586
if (LoadInst *Load = dyn_cast<LoadInst>(I))
85878587
return new VPWidenLoadRecipe(*Load, Ptr, Mask, Consecutive, Reverse,
8588+
getMetadataToPropagate(Load),
85888589
I->getDebugLoc());
85898590

85908591
StoreInst *Store = cast<StoreInst>(I);
85918592
return new VPWidenStoreRecipe(*Store, Ptr, Operands[0], Mask, Consecutive,
8592-
Reverse, I->getDebugLoc());
8593+
Reverse, getMetadataToPropagate(Store),
8594+
I->getDebugLoc());
85938595
}
85948596

85958597
/// Creates a VPWidenIntOrFpInductionRecpipe for \p Phi. If needed, it will also
@@ -8731,6 +8733,7 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
87318733
Range);
87328734
if (ShouldUseVectorIntrinsic)
87338735
return new VPWidenIntrinsicRecipe(*CI, ID, Ops, CI->getType(),
8736+
getMetadataToPropagate(CI),
87348737
CI->getDebugLoc());
87358738

87368739
Function *Variant = nullptr;
@@ -8784,7 +8787,8 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
87848787
}
87858788

87868789
Ops.push_back(Operands.back());
8787-
return new VPWidenCallRecipe(CI, Variant, Ops, CI->getDebugLoc());
8790+
return new VPWidenCallRecipe(CI, Variant, Ops, getMetadataToPropagate(CI),
8791+
CI->getDebugLoc());
87888792
}
87898793

87908794
return nullptr;
@@ -8822,7 +8826,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88228826
Plan.getOrAddLiveIn(ConstantInt::get(I->getType(), 1u, false));
88238827
auto *SafeRHS = Builder.createSelect(Mask, Ops[1], One, I->getDebugLoc());
88248828
Ops[1] = SafeRHS;
8825-
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()));
8829+
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()),
8830+
getMetadataToPropagate(I));
88268831
}
88278832
[[fallthrough]];
88288833
}
@@ -8868,7 +8873,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88688873
// For other binops, the legacy cost model only checks the second operand.
88698874
NewOps[1] = GetConstantViaSCEV(NewOps[1]);
88708875
}
8871-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8876+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8877+
getMetadataToPropagate(I));
88728878
}
88738879
case Instruction::ExtractValue: {
88748880
SmallVector<VPValue *> NewOps(Operands);
@@ -8877,7 +8883,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88778883
assert(EVI->getNumIndices() == 1 && "Expected one extractvalue index");
88788884
unsigned Idx = EVI->getIndices()[0];
88798885
NewOps.push_back(Plan.getOrAddLiveIn(ConstantInt::get(I32Ty, Idx, false)));
8880-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8886+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8887+
getMetadataToPropagate(I));
88818888
}
88828889
};
88838890
}
@@ -9082,6 +9089,13 @@ bool VPRecipeBuilder::getScaledReductions(
90829089
return false;
90839090
}
90849091

9092+
SmallVector<std::pair<unsigned, MDNode *>>
9093+
VPRecipeBuilder::getMetadataToPropagate(Instruction *I) {
9094+
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
9095+
::getMetadataToPropagate(I, Metadata);
9096+
return Metadata;
9097+
}
9098+
90859099
VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
90869100
Instruction *Instr, ArrayRef<VPValue *> Operands, VFRange &Range) {
90879101
// First, check for specific widening recipes that deal with inductions, Phi
@@ -9154,13 +9168,14 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
91549168
make_range(Operands.begin(), Operands.end()));
91559169

91569170
if (auto *SI = dyn_cast<SelectInst>(Instr)) {
9157-
return new VPWidenSelectRecipe(
9158-
*SI, make_range(Operands.begin(), Operands.end()));
9171+
return new VPWidenSelectRecipe(*SI,
9172+
make_range(Operands.begin(), Operands.end()),
9173+
getMetadataToPropagate(SI));
91599174
}
91609175

91619176
if (auto *CI = dyn_cast<CastInst>(Instr)) {
91629177
return new VPWidenCastRecipe(CI->getOpcode(), Operands[0], CI->getType(),
9163-
*CI);
9178+
*CI, getMetadataToPropagate(CI));
91649179
}
91659180

91669181
return tryToWiden(Instr, Operands);
@@ -9186,7 +9201,8 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
91869201
SmallVector<VPValue *, 2> Ops;
91879202
Ops.push_back(Plan.getOrAddLiveIn(Zero));
91889203
Ops.push_back(BinOp);
9189-
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()));
9204+
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()),
9205+
getMetadataToPropagate(Reduction));
91909206
Builder.insert(BinOp->getDefiningRecipe());
91919207
ReductionOpcode = Instruction::Add;
91929208
}

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)