[llvm] dfa488b - [IR] Cache llvm.module.flags metadata in Module

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 14 00:44:08 PDT 2024


Author: Alexis Engelke
Date: 2024-08-14T09:44:05+02:00
New Revision: dfa488baa2fad165e353ef54fde863a39f093333

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

LOG: [IR] Cache llvm.module.flags metadata in Module

This metadata is queried quite often, so avoiding frequent lookups in
the hash map is beneficial. Therefore, cache the metadata node directly
in the module.

Pull Request: https://github.com/llvm/llvm-project/pull/103410

Added: 
    

Modified: 
    llvm/include/llvm/IR/Module.h
    llvm/lib/IR/Module.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 7042a54a59e7d6..e8905f7ed393ca 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -202,6 +202,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
                              ///< ID and FunctionType maps to the extension that
                              ///< is used to make the intrinsic name unique.
 
+  /// llvm.module.flags metadata
+  NamedMDNode *ModuleFlags = nullptr;
+
   friend class Constant;
 
 /// @}
@@ -528,7 +531,7 @@ class LLVM_EXTERNAL_VISIBILITY Module {
 
   /// Returns the NamedMDNode in the module that represents module-level flags.
   /// This method returns null if there are no module-level flags.
-  NamedMDNode *getModuleFlagsMetadata() const;
+  NamedMDNode *getModuleFlagsMetadata() const { return ModuleFlags; }
 
   /// Returns the NamedMDNode in the module that represents module-level flags.
   /// If module-level flags aren't found, it creates the named metadata that

diff  --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index 4738ec7639f57a..7ba5b2784f5460 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -272,6 +272,8 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
     NMD = new NamedMDNode(Name);
     NMD->setParent(this);
     insertNamedMDNode(NMD);
+    if (Name == "llvm.module.flags")
+      ModuleFlags = NMD;
   }
   return NMD;
 }
@@ -280,6 +282,8 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
 /// delete it.
 void Module::eraseNamedMetadata(NamedMDNode *NMD) {
   NamedMDSymTab.erase(NMD->getName());
+  if (NMD == ModuleFlags)
+    ModuleFlags = nullptr;
   eraseNamedMDNode(NMD);
 }
 
@@ -323,17 +327,12 @@ Metadata *Module::getModuleFlag(StringRef Key) const {
   return nullptr;
 }
 
-/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
-/// represents module-level flags. This method returns null if there are no
-/// module-level flags.
-NamedMDNode *Module::getModuleFlagsMetadata() const {
-  return getNamedMetadata("llvm.module.flags");
-}
-
 /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
 /// represents module-level flags. If module-level flags aren't found, it
 /// creates the named metadata that contains them.
 NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
+  if (ModuleFlags)
+    return ModuleFlags;
   return getOrInsertNamedMetadata("llvm.module.flags");
 }
 


        


More information about the llvm-commits mailing list