Skip to content

Commit dfa488b

Browse files
authored
[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: #103410
1 parent ce8cb7c commit dfa488b

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

llvm/include/llvm/IR/Module.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
202202
///< ID and FunctionType maps to the extension that
203203
///< is used to make the intrinsic name unique.
204204

205+
/// llvm.module.flags metadata
206+
NamedMDNode *ModuleFlags = nullptr;
207+
205208
friend class Constant;
206209

207210
/// @}
@@ -528,7 +531,7 @@ class LLVM_EXTERNAL_VISIBILITY Module {
528531

529532
/// Returns the NamedMDNode in the module that represents module-level flags.
530533
/// This method returns null if there are no module-level flags.
531-
NamedMDNode *getModuleFlagsMetadata() const;
534+
NamedMDNode *getModuleFlagsMetadata() const { return ModuleFlags; }
532535

533536
/// Returns the NamedMDNode in the module that represents module-level flags.
534537
/// If module-level flags aren't found, it creates the named metadata that

llvm/lib/IR/Module.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
272272
NMD = new NamedMDNode(Name);
273273
NMD->setParent(this);
274274
insertNamedMDNode(NMD);
275+
if (Name == "llvm.module.flags")
276+
ModuleFlags = NMD;
275277
}
276278
return NMD;
277279
}
@@ -280,6 +282,8 @@ NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
280282
/// delete it.
281283
void Module::eraseNamedMetadata(NamedMDNode *NMD) {
282284
NamedMDSymTab.erase(NMD->getName());
285+
if (NMD == ModuleFlags)
286+
ModuleFlags = nullptr;
283287
eraseNamedMDNode(NMD);
284288
}
285289

@@ -323,17 +327,12 @@ Metadata *Module::getModuleFlag(StringRef Key) const {
323327
return nullptr;
324328
}
325329

326-
/// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
327-
/// represents module-level flags. This method returns null if there are no
328-
/// module-level flags.
329-
NamedMDNode *Module::getModuleFlagsMetadata() const {
330-
return getNamedMetadata("llvm.module.flags");
331-
}
332-
333330
/// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
334331
/// represents module-level flags. If module-level flags aren't found, it
335332
/// creates the named metadata that contains them.
336333
NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
334+
if (ModuleFlags)
335+
return ModuleFlags;
337336
return getOrInsertNamedMetadata("llvm.module.flags");
338337
}
339338

0 commit comments

Comments
 (0)