[llvm] [SPIRV] Avoid repeated hash lookups (NFC) (PR #129826)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 4 20:51:20 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-spir-v

Author: Kazu Hirata (kazutakahirata)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/129826.diff


1 Files Affected:

- (modified) llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp (+12-11) 


``````````diff
diff --git a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
index 6d7fe408f9a14..32f817866d421 100644
--- a/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
@@ -399,11 +399,11 @@ MCRegister SPIRVModuleAnalysis::handleFunctionOrParameter(
     F = dyn_cast<Argument>(GObj)->getParent();
   assert(F && "Expected a reference to a function or an argument");
   IsFunDef = !F->isDeclaration();
-  auto It = GlobalToGReg.find(GObj);
-  if (It != GlobalToGReg.end())
+  auto [It, Inserted] = GlobalToGReg.try_emplace(GObj);
+  if (!Inserted)
     return It->second;
   MCRegister GReg = MAI.getNextIDRegister();
-  GlobalToGReg[GObj] = GReg;
+  It->second = GReg;
   if (!IsFunDef)
     MAI.MS[SPIRV::MB_ExtFuncDecls].push_back(&MI);
   return GReg;
@@ -413,11 +413,11 @@ MCRegister
 SPIRVModuleAnalysis::handleTypeDeclOrConstant(const MachineInstr &MI,
                                               InstrGRegsMap &SignatureToGReg) {
   InstrSignature MISign = instrToSignature(MI, MAI, false);
-  auto It = SignatureToGReg.find(MISign);
-  if (It != SignatureToGReg.end())
+  auto [It, Inserted] = SignatureToGReg.try_emplace(MISign);
+  if (!Inserted)
     return It->second;
   MCRegister GReg = MAI.getNextIDRegister();
-  SignatureToGReg[MISign] = GReg;
+  It->second = GReg;
   MAI.MS[SPIRV::MB_TypeConstVars].push_back(&MI);
   return GReg;
 }
@@ -428,11 +428,11 @@ MCRegister SPIRVModuleAnalysis::handleVariable(
   MAI.GlobalVarList.push_back(&MI);
   const Value *GObj = GR->getGlobalObject(MF, MI.getOperand(0).getReg());
   assert(GObj && "Unregistered global definition");
-  auto It = GlobalToGReg.find(GObj);
-  if (It != GlobalToGReg.end())
+  auto [It, Inserted] = GlobalToGReg.try_emplace(GObj);
+  if (!Inserted)
     return It->second;
   MCRegister GReg = MAI.getNextIDRegister();
-  GlobalToGReg[GObj] = GReg;
+  It->second = GReg;
   MAI.MS[SPIRV::MB_TypeConstVars].push_back(&MI);
   return GReg;
 }
@@ -605,8 +605,9 @@ void SPIRVModuleAnalysis::numberRegistersGlobally(const Module &M) {
         if (MI.getOpcode() != SPIRV::OpExtInst)
           continue;
         auto Set = MI.getOperand(2).getImm();
-        if (!MAI.ExtInstSetMap.contains(Set))
-          MAI.ExtInstSetMap[Set] = MAI.getNextIDRegister();
+        auto [It, Inserted] = MAI.ExtInstSetMap.try_emplace(Set);
+        if (Inserted)
+          It->second = MAI.getNextIDRegister();
       }
     }
   }

``````````

</details>


https://github.com/llvm/llvm-project/pull/129826


More information about the llvm-commits mailing list