[mlir] Use *Map::try_emplace (NFC) (#143341)

- try_emplace(Key) is shorter than insert({Key, nullptr}).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
This commit is contained in:
Kazu Hirata
2025-06-09 07:18:26 -07:00
committed by GitHub
parent 939666380f
commit b3b8a097fe
6 changed files with 8 additions and 9 deletions

View File

@@ -48,7 +48,7 @@ public:
/// If a breakpoint already exists for the given tag, return the existing
/// instance.
TagBreakpoint *addBreakpoint(StringRef tag) {
auto result = breakpoints.insert({tag, nullptr});
auto result = breakpoints.try_emplace(tag);
auto &it = result.first;
if (result.second)
it->second = std::make_unique<TagBreakpoint>(tag.str());

View File

@@ -308,7 +308,7 @@ void IRNumberingState::computeGlobalNumberingState(Operation *rootOp) {
}
void IRNumberingState::number(Attribute attr) {
auto it = attrs.insert({attr, nullptr});
auto it = attrs.try_emplace(attr);
if (!it.second) {
++it.first->second->refCount;
return;
@@ -475,7 +475,7 @@ void IRNumberingState::number(OperationName opName) {
}
void IRNumberingState::number(Type type) {
auto it = types.insert({type, nullptr});
auto it = types.try_emplace(type);
if (!it.second) {
++it.first->second->refCount;
return;

View File

@@ -810,7 +810,7 @@ OperationName::OperationName(StringRef name, MLIRContext *context) {
// Acquire a writer-lock so that we can safely create the new instance.
ScopedWriterLock lock(ctxImpl.operationInfoMutex, isMultithreadingEnabled);
auto it = ctxImpl.operations.insert({name, nullptr});
auto it = ctxImpl.operations.try_emplace(name);
if (it.second) {
auto nameAttr = StringAttr::get(context, name);
it.first->second = std::make_unique<UnregisteredOpModel>(

View File

@@ -280,7 +280,7 @@ LoopAnnotationTranslation::translateLoopAnnotation(LoopAnnotationAttr attr,
llvm::MDNode *
LoopAnnotationTranslation::getAccessGroup(AccessGroupAttr accessGroupAttr) {
auto [result, inserted] =
accessGroupMetadataMapping.insert({accessGroupAttr, nullptr});
accessGroupMetadataMapping.try_emplace(accessGroupAttr);
if (inserted)
result->second = llvm::MDNode::getDistinct(llvmModule.getContext(), {});
return result->second;

View File

@@ -427,8 +427,7 @@ public:
/// region with an instance of `returnLikeOp`s kind.
void combineExit(Operation *returnLikeOp,
function_ref<Value(unsigned)> getSwitchValue) {
auto [iter, inserted] =
returnLikeToCombinedExit.insert({returnLikeOp, nullptr});
auto [iter, inserted] = returnLikeToCombinedExit.try_emplace(returnLikeOp);
if (!inserted && iter->first == returnLikeOp)
return;
@@ -1284,7 +1283,7 @@ FailureOr<bool> mlir::transformCFGToSCF(Region &region,
DenseMap<Type, Value> typedUndefCache;
auto getUndefValue = [&](Type type) {
auto [iter, inserted] = typedUndefCache.insert({type, nullptr});
auto [iter, inserted] = typedUndefCache.try_emplace(type);
if (!inserted)
return iter->second;

View File

@@ -45,7 +45,7 @@ static void walkReferencedSymbolNodes(
Operation *symbolTableOp = op->getParentOp();
for (const SymbolTable::SymbolUse &use : *symbolUses) {
auto refIt = resolvedRefs.insert({use.getSymbolRef(), nullptr});
auto refIt = resolvedRefs.try_emplace(use.getSymbolRef());
CallGraphNode *&node = refIt.first->second;
// If this is the first instance of this reference, try to resolve a