Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Mar 5, 2025

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

Author: Kazu Hirata (kazutakahirata)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp (+12-11)
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();
       }
     }
   }

@kazutakahirata kazutakahirata merged commit efdd660 into llvm:main Mar 5, 2025
14 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_repeated_hash_lookups_llvm_SPIRV branch March 5, 2025 08:45
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants