[llvm] ddfd81b - [LoopVer] Add function to get the alias_scope/noalias metadata (NFC).

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 15 05:46:58 PDT 2025


Author: Florian Hahn
Date: 2025-04-15T14:46:28+02:00
New Revision: ddfd81bb76c05b095c8eca7a45f71547c3df2a89

URL: https://github.com/llvm/llvm-project/commit/ddfd81bb76c05b095c8eca7a45f71547c3df2a89
DIFF: https://github.com/llvm/llvm-project/commit/ddfd81bb76c05b095c8eca7a45f71547c3df2a89.diff

LOG: [LoopVer] Add function to get the alias_scope/noalias metadata (NFC).

Split out logic to get noalias/alias_scope metadata to separate function
in LoopVersioning. This will be used to migrate away from
annotateInstWithNoAlias in LV.

Added: 
    

Modified: 
    llvm/include/llvm/Transforms/Utils/LoopVersioning.h
    llvm/lib/Transforms/Utils/LoopVersioning.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Utils/LoopVersioning.h b/llvm/include/llvm/Transforms/Utils/LoopVersioning.h
index eeab98c56b66c..ea4fe27c90f5c 100644
--- a/llvm/include/llvm/Transforms/Utils/LoopVersioning.h
+++ b/llvm/include/llvm/Transforms/Utils/LoopVersioning.h
@@ -83,6 +83,11 @@ class LoopVersioning {
   /// annotateInstWithNoAlias on the instructions of the versioned loop.
   void annotateLoopWithNoAlias();
 
+  /// Returns a pair containing the alias_scope and noalias metadata nodes for
+  /// \p OrigInst, if they exists.
+  std::pair<MDNode *, MDNode *>
+  getNoAliasMetadataFor(const Instruction *OrigInst) const;
+
   /// Set up the aliasing scopes based on the memchecks.  This needs to
   /// be called before the first call to annotateInstWithNoAlias.
   void prepareNoAliasMetadata();

diff  --git a/llvm/lib/Transforms/Utils/LoopVersioning.cpp b/llvm/lib/Transforms/Utils/LoopVersioning.cpp
index 5ee551e6f0ccb..1711163fb9f59 100644
--- a/llvm/lib/Transforms/Utils/LoopVersioning.cpp
+++ b/llvm/lib/Transforms/Utils/LoopVersioning.cpp
@@ -225,34 +225,43 @@ void LoopVersioning::annotateLoopWithNoAlias() {
   }
 }
 
-void LoopVersioning::annotateInstWithNoAlias(Instruction *VersionedInst,
-                                             const Instruction *OrigInst) {
+std::pair<MDNode *, MDNode *>
+LoopVersioning::getNoAliasMetadataFor(const Instruction *OrigInst) const {
   if (!AnnotateNoAlias)
-    return;
+    return {nullptr, nullptr};
 
   LLVMContext &Context = VersionedLoop->getHeader()->getContext();
   const Value *Ptr = isa<LoadInst>(OrigInst)
                          ? cast<LoadInst>(OrigInst)->getPointerOperand()
                          : cast<StoreInst>(OrigInst)->getPointerOperand();
 
+  MDNode *AliasScope = nullptr;
+  MDNode *NoAlias = nullptr;
   // Find the group for the pointer and then add the scope metadata.
   auto Group = PtrToGroup.find(Ptr);
   if (Group != PtrToGroup.end()) {
-    VersionedInst->setMetadata(
-        LLVMContext::MD_alias_scope,
-        MDNode::concatenate(
-            VersionedInst->getMetadata(LLVMContext::MD_alias_scope),
-            MDNode::get(Context, GroupToScope[Group->second])));
+    AliasScope = MDNode::concatenate(
+        OrigInst->getMetadata(LLVMContext::MD_alias_scope),
+        MDNode::get(Context, GroupToScope.lookup(Group->second)));
 
     // Add the no-alias metadata.
     auto NonAliasingScopeList = GroupToNonAliasingScopeList.find(Group->second);
     if (NonAliasingScopeList != GroupToNonAliasingScopeList.end())
-      VersionedInst->setMetadata(
-          LLVMContext::MD_noalias,
-          MDNode::concatenate(
-              VersionedInst->getMetadata(LLVMContext::MD_noalias),
-              NonAliasingScopeList->second));
+      NoAlias =
+          MDNode::concatenate(OrigInst->getMetadata(LLVMContext::MD_noalias),
+                              NonAliasingScopeList->second);
   }
+  return {AliasScope, NoAlias};
+}
+
+void LoopVersioning::annotateInstWithNoAlias(Instruction *VersionedInst,
+                                             const Instruction *OrigInst) {
+  const auto &[AliasScopeMD, NoAliasMD] = getNoAliasMetadataFor(OrigInst);
+  if (AliasScopeMD)
+    VersionedInst->setMetadata(LLVMContext::MD_alias_scope, AliasScopeMD);
+
+  if (NoAliasMD)
+    VersionedInst->setMetadata(LLVMContext::MD_noalias, NoAliasMD);
 }
 
 namespace {


        


More information about the llvm-commits mailing list