Skip to content

Commit 38bf1af

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. !fixup address latest comments, thanks
1 parent 0314755 commit 38bf1af

File tree

8 files changed

+168
-113
lines changed

8 files changed

+168
-113
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8599,11 +8599,13 @@ VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
85998599
}
86008600
if (LoadInst *Load = dyn_cast<LoadInst>(I))
86018601
return new VPWidenLoadRecipe(*Load, Ptr, Mask, Consecutive, Reverse,
8602+
getMetadataToPropagate(Load),
86028603
I->getDebugLoc());
86038604

86048605
StoreInst *Store = cast<StoreInst>(I);
86058606
return new VPWidenStoreRecipe(*Store, Ptr, Operands[0], Mask, Consecutive,
8606-
Reverse, I->getDebugLoc());
8607+
Reverse, getMetadataToPropagate(Store),
8608+
I->getDebugLoc());
86078609
}
86088610

86098611
/// Creates a VPWidenIntOrFpInductionRecpipe for \p Phi. If needed, it will also
@@ -8745,6 +8747,7 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
87458747
Range);
87468748
if (ShouldUseVectorIntrinsic)
87478749
return new VPWidenIntrinsicRecipe(*CI, ID, Ops, CI->getType(),
8750+
getMetadataToPropagate(CI),
87488751
CI->getDebugLoc());
87498752

87508753
Function *Variant = nullptr;
@@ -8798,7 +8801,8 @@ VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
87988801
}
87998802

88008803
Ops.push_back(Operands.back());
8801-
return new VPWidenCallRecipe(CI, Variant, Ops, CI->getDebugLoc());
8804+
return new VPWidenCallRecipe(CI, Variant, Ops, getMetadataToPropagate(CI),
8805+
CI->getDebugLoc());
88028806
}
88038807

88048808
return nullptr;
@@ -8836,7 +8840,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88368840
Plan.getOrAddLiveIn(ConstantInt::get(I->getType(), 1u, false));
88378841
auto *SafeRHS = Builder.createSelect(Mask, Ops[1], One, I->getDebugLoc());
88388842
Ops[1] = SafeRHS;
8839-
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()));
8843+
return new VPWidenRecipe(*I, make_range(Ops.begin(), Ops.end()),
8844+
getMetadataToPropagate(I));
88408845
}
88418846
[[fallthrough]];
88428847
}
@@ -8882,7 +8887,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88828887
// For other binops, the legacy cost model only checks the second operand.
88838888
NewOps[1] = GetConstantViaSCEV(NewOps[1]);
88848889
}
8885-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8890+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8891+
getMetadataToPropagate(I));
88868892
}
88878893
case Instruction::ExtractValue: {
88888894
SmallVector<VPValue *> NewOps(Operands);
@@ -8891,7 +8897,8 @@ VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
88918897
assert(EVI->getNumIndices() == 1 && "Expected one extractvalue index");
88928898
unsigned Idx = EVI->getIndices()[0];
88938899
NewOps.push_back(Plan.getOrAddLiveIn(ConstantInt::get(I32Ty, Idx, false)));
8894-
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()));
8900+
return new VPWidenRecipe(*I, make_range(NewOps.begin(), NewOps.end()),
8901+
getMetadataToPropagate(I));
88958902
}
88968903
};
88978904
}
@@ -9096,6 +9103,13 @@ bool VPRecipeBuilder::getScaledReductions(
90969103
return false;
90979104
}
90989105

9106+
SmallVector<std::pair<unsigned, MDNode *>>
9107+
VPRecipeBuilder::getMetadataToPropagate(Instruction *I) {
9108+
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
9109+
::getMetadataToPropagate(I, Metadata);
9110+
return Metadata;
9111+
}
9112+
90999113
VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
91009114
Instruction *Instr, ArrayRef<VPValue *> Operands, VFRange &Range) {
91019115
// First, check for specific widening recipes that deal with inductions, Phi
@@ -9168,13 +9182,14 @@ VPRecipeBase *VPRecipeBuilder::tryToCreateWidenRecipe(
91689182
make_range(Operands.begin(), Operands.end()));
91699183

91709184
if (auto *SI = dyn_cast<SelectInst>(Instr)) {
9171-
return new VPWidenSelectRecipe(
9172-
*SI, make_range(Operands.begin(), Operands.end()));
9185+
return new VPWidenSelectRecipe(*SI,
9186+
make_range(Operands.begin(), Operands.end()),
9187+
getMetadataToPropagate(SI));
91739188
}
91749189

91759190
if (auto *CI = dyn_cast<CastInst>(Instr)) {
91769191
return new VPWidenCastRecipe(CI->getOpcode(), Operands[0], CI->getType(),
9177-
*CI);
9192+
*CI, getMetadataToPropagate(CI));
91789193
}
91799194

91809195
return tryToWiden(Instr, Operands);
@@ -9200,7 +9215,8 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
92009215
SmallVector<VPValue *, 2> Ops;
92019216
Ops.push_back(Plan.getOrAddLiveIn(Zero));
92029217
Ops.push_back(BinOp);
9203-
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()));
9218+
BinOp = new VPWidenRecipe(*Reduction, make_range(Ops.begin(), Ops.end()),
9219+
getMetadataToPropagate(Reduction));
92049220
Builder.insert(BinOp->getDefiningRecipe());
92059221
ReductionOpcode = Instruction::Add;
92069222
}

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+
static 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)