Skip to content

Commit 09f12f7

Browse files
fhahnYilin Li
authored and
Yilin Li
committed
[VectorUtils] Add helper to get list of metadata to propagate (NFC). (llvm#135003)
Split off the logic to filter out metadata that should not be propagated to a helper that populates a list with metadata kinds that should be preserved. The current version just uses is_contained on an array to check if a metadata kind is supported. Given that most instructions will only have a small number of metadata kinds to start with, this shouldn't be worse than iterating over all kinds once and querying the instruction for individual metadata kinds, which involves quite a bit of indirection. I plan ot use this utility in a follow-up patch in other places as well. PR: llvm#135003
1 parent adce96f commit 09f12f7

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

llvm/lib/Analysis/VectorUtils.cpp

+28-9
Original file line numberDiff line numberDiff line change
@@ -984,19 +984,38 @@ MDNode *llvm::intersectAccessGroups(const Instruction *Inst1,
984984
return MDNode::get(Ctx, Intersection);
985985
}
986986

987+
/// Add metadata from \p Inst to \p Metadata, if it can be preserved after
988+
/// vectorization.
989+
static void getMetadataToPropagate(
990+
Instruction *Inst,
991+
SmallVectorImpl<std::pair<unsigned, MDNode *>> &Metadata) {
992+
Inst->getAllMetadataOtherThanDebugLoc(Metadata);
993+
static const unsigned SupportedIDs[] = {
994+
LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
995+
LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
996+
LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
997+
LLVMContext::MD_access_group, LLVMContext::MD_mmra};
998+
999+
// Remove any unsupported metadata kinds from Metadata.
1000+
for (unsigned Idx = 0; Idx != Metadata.size();) {
1001+
if (is_contained(SupportedIDs, Metadata[Idx].first)) {
1002+
++Idx;
1003+
} else {
1004+
// Swap element to end and remove it.
1005+
std::swap(Metadata[Idx], Metadata.back());
1006+
Metadata.pop_back();
1007+
}
1008+
}
1009+
}
1010+
9871011
/// \returns \p I after propagating metadata from \p VL.
9881012
Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) {
9891013
if (VL.empty())
9901014
return Inst;
991-
Instruction *I0 = cast<Instruction>(VL[0]);
992-
SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
993-
I0->getAllMetadataOtherThanDebugLoc(Metadata);
994-
995-
for (auto Kind : {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
996-
LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
997-
LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
998-
LLVMContext::MD_access_group, LLVMContext::MD_mmra}) {
999-
MDNode *MD = I0->getMetadata(Kind);
1015+
SmallVector<std::pair<unsigned, MDNode *>> Metadata;
1016+
getMetadataToPropagate(cast<Instruction>(VL[0]), Metadata);
1017+
1018+
for (auto &[Kind, MD] : Metadata) {
10001019
for (int J = 1, E = VL.size(); MD && J != E; ++J) {
10011020
const Instruction *IJ = cast<Instruction>(VL[J]);
10021021
MDNode *IMD = IJ->getMetadata(Kind);

0 commit comments

Comments
 (0)