Skip to content

[VectorUtils] Add helper to get list of metadata to propagate (NFC). #135003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Apr 9, 2025

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.

@llvmbot
Copy link
Member

llvmbot commented Apr 9, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Florian Hahn (fhahn)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/135003.diff

1 Files Affected:

  • (modified) llvm/lib/Analysis/VectorUtils.cpp (+28-9)
diff --git a/llvm/lib/Analysis/VectorUtils.cpp b/llvm/lib/Analysis/VectorUtils.cpp
index 46f588f4c6705..1c0ea87990a3a 100644
--- a/llvm/lib/Analysis/VectorUtils.cpp
+++ b/llvm/lib/Analysis/VectorUtils.cpp
@@ -984,19 +984,38 @@ MDNode *llvm::intersectAccessGroups(const Instruction *Inst1,
   return MDNode::get(Ctx, Intersection);
 }
 
+/// Add metadata from \p Inst to \p Metadata, if it can be preserved after
+/// vectorization.
+static void getMetadataToPropagate(
+    Instruction *Inst,
+    SmallVectorImpl<std::pair<unsigned, MDNode *>> &Metadata) {
+  Inst->getAllMetadataOtherThanDebugLoc(Metadata);
+  unsigned SupportedIDs[] = {
+      LLVMContext::MD_tbaa,         LLVMContext::MD_alias_scope,
+      LLVMContext::MD_noalias,      LLVMContext::MD_fpmath,
+      LLVMContext::MD_nontemporal,  LLVMContext::MD_invariant_load,
+      LLVMContext::MD_access_group, LLVMContext::MD_mmra};
+
+  // Remove any unsupported metadata kinds from Metadata.
+  for (unsigned Idx = 0; Idx != Metadata.size();) {
+    if (is_contained(SupportedIDs, Metadata[Idx].first))
+      Idx++;
+    else {
+      // Swap element to end and remove it.
+      std::swap(Metadata[Idx], Metadata.back());
+      Metadata.pop_back();
+    }
+  }
+}
+
 /// \returns \p I after propagating metadata from \p VL.
 Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) {
   if (VL.empty())
     return Inst;
-  Instruction *I0 = cast<Instruction>(VL[0]);
-  SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
-  I0->getAllMetadataOtherThanDebugLoc(Metadata);
-
-  for (auto Kind : {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
-                    LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
-                    LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
-                    LLVMContext::MD_access_group, LLVMContext::MD_mmra}) {
-    MDNode *MD = I0->getMetadata(Kind);
+  SmallVector<std::pair<unsigned, MDNode *>> Metadata;
+  getMetadataToPropagate(cast<Instruction>(VL[0]), Metadata);
+
+  for (auto &[Kind, MD] : Metadata) {
     for (int J = 1, E = VL.size(); MD && J != E; ++J) {
       const Instruction *IJ = cast<Instruction>(VL[J]);
       MDNode *IMD = IJ->getMetadata(Kind);

Comment on lines 1001 to 1003
if (is_contained(SupportedIDs, Metadata[Idx].first))
Idx++;
else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if (is_contained(SupportedIDs, Metadata[Idx].first)) {
      Idx++;
    } else {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks

fhahn added 2 commits April 9, 2025 15:53
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.
@fhahn fhahn force-pushed the vector-utils-md-to-prop branch from 5b32fe4 to f44451d Compare April 9, 2025 15:02
Copy link
Member

@alexey-bataev alexey-bataev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG with a nit

// Remove any unsupported metadata kinds from Metadata.
for (unsigned Idx = 0; Idx != Metadata.size();) {
if (is_contained(SupportedIDs, Metadata[Idx].first)) {
Idx++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Idx++;
++Idx;

Instruction *Inst,
SmallVectorImpl<std::pair<unsigned, MDNode *>> &Metadata) {
Inst->getAllMetadataOtherThanDebugLoc(Metadata);
unsigned SupportedIDs[] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static const?

@fhahn fhahn merged commit 0fd81e5 into llvm:main Apr 9, 2025
8 of 10 checks passed
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Apr 9, 2025
…ate (NFC). (#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/llvm-project#135003
AllinLeeYL pushed a commit to AllinLeeYL/llvm-project that referenced this pull request Apr 10, 2025
…lvm#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
@fhahn fhahn deleted the vector-utils-md-to-prop branch April 11, 2025 09:11
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…lvm#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants