[llvm] [Support][IR] Modernize map insertions using try_emplace (PR #204977)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 30 05:49:33 PDT 2026
https://github.com/Saravanan-Codez updated https://github.com/llvm/llvm-project/pull/204977
>From 6255afd2989f703c6583929d60dd1086992ea16b Mon Sep 17 00:00:00 2001
From: saravanan-codez <assfsaravanan at gmail.com>
Date: Sun, 21 Jun 2026 18:16:46 +0530
Subject: [PATCH] [Support][IR] Modernize map insertions using try_emplace
Replace legacy .insert(std::make_pair(Key, Value)) map insertion calls with C++17-idiomatic .try_emplace(Key, Value) across LLVM Core libraries (Support and IR). Using try_emplace avoids constructing a temporary std::pair object and avoids constructing the mapped value altogether if the key is already present.
---
llvm/lib/IR/AsmWriter.cpp | 2 +-
llvm/lib/IR/LLVMContextImpl.cpp | 4 ++--
llvm/lib/IR/Metadata.cpp | 4 ++--
llvm/lib/IR/PassRegistry.cpp | 2 +-
llvm/lib/IR/Type.cpp | 6 +++---
llvm/lib/IR/ValueSymbolTable.cpp | 4 ++--
llvm/lib/IR/Verifier.cpp | 2 +-
llvm/lib/Support/CommandLine.cpp | 6 +++---
llvm/lib/Support/ELFAttrParserCompact.cpp | 4 ++--
llvm/lib/Support/Mustache.cpp | 2 +-
llvm/lib/Support/Signposts.cpp | 2 +-
11 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 52ed28f71f615..0dfeba9cbfb21 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -1424,7 +1424,7 @@ void SlotTracker::CreateMetadataSlot(const MDNode *N) {
return;
unsigned DestSlot = mdnNext;
- if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
+ if (!mdnMap.try_emplace(N, DestSlot).second)
return;
++mdnNext;
diff --git a/llvm/lib/IR/LLVMContextImpl.cpp b/llvm/lib/IR/LLVMContextImpl.cpp
index 90afa09f73abe..b1f37d0c76359 100644
--- a/llvm/lib/IR/LLVMContextImpl.cpp
+++ b/llvm/lib/IR/LLVMContextImpl.cpp
@@ -188,7 +188,7 @@ unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
uint32_t NewIdx = BundleTagCache.size();
- return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
+ return &*(BundleTagCache.try_emplace(Tag, NewIdx).first);
}
void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
@@ -207,7 +207,7 @@ SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
auto NewSSID = SSC.size();
assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
"Hit the maximum number of synchronization scopes allowed!");
- return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
+ return SSC.try_emplace(SSN, SyncScope::ID(NewSSID)).first->second;
}
void LLVMContextImpl::getSyncScopeNames(
diff --git a/llvm/lib/IR/Metadata.cpp b/llvm/lib/IR/Metadata.cpp
index 491c788fc4445..882fbe177109a 100644
--- a/llvm/lib/IR/Metadata.cpp
+++ b/llvm/lib/IR/Metadata.cpp
@@ -302,7 +302,7 @@ ReplaceableMetadataImpl::getAllDbgVariableRecordUsers() {
void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
bool WasInserted =
- UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
+ UseMap.try_emplace(Ref, Owner, NextIndex)
.second;
(void)WasInserted;
assert(WasInserted && "Expected to add a reference");
@@ -323,7 +323,7 @@ void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
assert(I != UseMap.end() && "Expected to move a reference");
auto OwnerAndIndex = I->second;
UseMap.erase(I);
- bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
+ bool WasInserted = UseMap.try_emplace(New, OwnerAndIndex).second;
(void)WasInserted;
assert(WasInserted && "Expected to add a reference");
diff --git a/llvm/lib/IR/PassRegistry.cpp b/llvm/lib/IR/PassRegistry.cpp
index a91bb563af4bb..eb1c6d2ffb95b 100644
--- a/llvm/lib/IR/PassRegistry.cpp
+++ b/llvm/lib/IR/PassRegistry.cpp
@@ -48,7 +48,7 @@ const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const {
void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) {
sys::SmartScopedWriter<true> Guard(Lock);
bool Inserted =
- PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second;
+ PassInfoMap.try_emplace(PI.getTypeInfo(), &PI).second;
assert(Inserted && "Pass registered multiple times!");
(void)Inserted;
PassInfoStringMap[PI.getPassArgument()] = &PI;
diff --git a/llvm/lib/IR/Type.cpp b/llvm/lib/IR/Type.cpp
index 47b230d44285b..5fcdfbc0ba8f7 100644
--- a/llvm/lib/IR/Type.cpp
+++ b/llvm/lib/IR/Type.cpp
@@ -655,7 +655,7 @@ void StructType::setName(StringRef Name) {
// Look up the entry for the name.
auto IterBool =
- getContext().pImpl->NamedStructTypes.insert(std::make_pair(Name, this));
+ getContext().pImpl->NamedStructTypes.try_emplace(Name, this);
// While we have a name collision, try a random rename.
if (!IterBool.second) {
@@ -668,8 +668,8 @@ void StructType::setName(StringRef Name) {
TempStr.resize(NameSize + 1);
TmpStream << getContext().pImpl->NamedStructTypesUniqueID++;
- IterBool = getContext().pImpl->NamedStructTypes.insert(
- std::make_pair(TmpStream.str(), this));
+ IterBool = getContext().pImpl->NamedStructTypes.try_emplace(
+ TmpStream.str(), this);
} while (!IterBool.second);
}
diff --git a/llvm/lib/IR/ValueSymbolTable.cpp b/llvm/lib/IR/ValueSymbolTable.cpp
index 3bf52f6ef024e..d062f69af4549 100644
--- a/llvm/lib/IR/ValueSymbolTable.cpp
+++ b/llvm/lib/IR/ValueSymbolTable.cpp
@@ -71,7 +71,7 @@ ValueName *ValueSymbolTable::makeUniqueName(Value *V,
continue;
}
// Try insert the vmap entry with this suffix.
- auto IterBool = vmap.insert(std::make_pair(UniqueName.str(), V));
+ auto IterBool = vmap.try_emplace(UniqueName.str(), V);
if (IterBool.second)
return &*IterBool.first;
}
@@ -114,7 +114,7 @@ ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
// In the common case, the name is not already in the symbol table.
- auto IterBool = vmap.insert(std::make_pair(Name, V));
+ auto IterBool = vmap.try_emplace(Name, V);
if (IterBool.second) {
// LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
// << *V << "\n");
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 8207e60857eba..6a5a3d9e3f698 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2121,7 +2121,7 @@ Verifier::visitModuleFlag(const MDNode *Op,
// Unless this is a "requires" flag, check the ID is unique.
if (MFB != Module::Require) {
- bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
+ bool Inserted = SeenIDs.try_emplace(ID, Op).second;
Check(Inserted,
"module flag identifiers must be unique (or of 'require' type)", ID);
}
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index a9102fe30efe3..f9049805d1586 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -217,7 +217,7 @@ class CommandLineParser {
void addLiteralOption(Option &Opt, SubCommand *SC, StringRef Name) {
if (Opt.hasArgStr())
return;
- if (!SC->OptionsMap.insert(std::make_pair(Name, &Opt)).second) {
+ if (!SC->OptionsMap.try_emplace(Name, &Opt).second) {
errs() << ProgramName << ": CommandLine Error: Option '" << Name
<< "' registered more than once!\n";
report_fatal_error("inconsistency in registered CommandLine options");
@@ -237,7 +237,7 @@ class CommandLineParser {
return;
// Add argument to the argument map!
- if (!SC->OptionsMap.insert(std::make_pair(O->ArgStr, O)).second) {
+ if (!SC->OptionsMap.try_emplace(O->ArgStr, O).second) {
errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
<< "' registered more than once!\n";
HadErrors = true;
@@ -335,7 +335,7 @@ class CommandLineParser {
void updateArgStr(Option *O, StringRef NewName, SubCommand *SC) {
SubCommand &Sub = *SC;
- if (!Sub.OptionsMap.insert(std::make_pair(NewName, O)).second) {
+ if (!Sub.OptionsMap.try_emplace(NewName, O).second) {
errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
<< "' registered more than once!\n";
report_fatal_error("inconsistency in registered CommandLine options");
diff --git a/llvm/lib/Support/ELFAttrParserCompact.cpp b/llvm/lib/Support/ELFAttrParserCompact.cpp
index 9665a82374113..681317349b7a7 100644
--- a/llvm/lib/Support/ELFAttrParserCompact.cpp
+++ b/llvm/lib/Support/ELFAttrParserCompact.cpp
@@ -40,7 +40,7 @@ Error ELFCompactAttrParser::integerAttribute(unsigned tag) {
StringRef tagName =
ELFAttrs::attrTypeAsString(tag, tagToStringMap, /*hasTagPrefix=*/false);
uint64_t value = de.getULEB128(cursor);
- attributes.insert(std::make_pair(tag, value));
+ attributes.try_emplace(tag, value);
if (sw) {
DictScope scope(*sw, "Attribute");
@@ -70,7 +70,7 @@ Error ELFCompactAttrParser::stringAttribute(unsigned tag) {
void ELFCompactAttrParser::printAttribute(unsigned tag, unsigned value,
StringRef valueDesc) {
- attributes.insert(std::make_pair(tag, value));
+ attributes.try_emplace(tag, value);
if (sw) {
StringRef tagName = ELFAttrs::attrTypeAsString(tag, tagToStringMap,
diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index e02a31639d695..49d23eaa9a4b9 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -894,7 +894,7 @@ void Template::registerPartial(std::string Name, std::string Partial) {
StringRef SavedPartial = Ctx.Saver.save(Partial);
Parser P(SavedPartial, Ctx);
AstPtr PartialTree = P.parse();
- Ctx.Partials.insert(std::make_pair(Name, PartialTree));
+ Ctx.Partials.try_emplace(Name, PartialTree);
}
void Template::registerLambda(std::string Name, Lambda L) {
diff --git a/llvm/lib/Support/Signposts.cpp b/llvm/lib/Support/Signposts.cpp
index 232b84e965a03..f65451a7d9fd7 100644
--- a/llvm/lib/Support/Signposts.cpp
+++ b/llvm/lib/Support/Signposts.cpp
@@ -55,7 +55,7 @@ class SignpostEmitterImpl {
if (SIGNPOSTS_AVAILABLE()) {
ID = os_signpost_id_make_with_pointer(getLogger(), O);
}
- const auto &Inserted = Signposts.insert(std::make_pair(O, ID));
+ const auto &Inserted = Signposts.try_emplace(O, ID);
return Inserted.first->second;
}
More information about the llvm-commits
mailing list