[llvm] [NVPTX] Avoid repeated map lookups (NFC) (PR #127169)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 14 01:30:22 PST 2025


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/127169

>From c7600fcbef02c31cd1421d2af183cc0e6a001579 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 13 Feb 2025 09:43:50 -0800
Subject: [PATCH 1/2] [NVPTX] Avoid repeated map lookups (NFC)

Note that cacheAnnotationFromMD modifies AC.Cache.
---
 llvm/lib/Target/NVPTX/NVPTXUtilities.cpp | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp b/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
index 430502d85dfb4..f34d18160b6e0 100644
--- a/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
@@ -128,13 +128,16 @@ static std::optional<unsigned> findOneNVVMAnnotation(const GlobalValue *gv,
   auto &AC = getAnnotationCache();
   std::lock_guard<sys::Mutex> Guard(AC.Lock);
   const Module *m = gv->getParent();
-  if (AC.Cache.find(m) == AC.Cache.end())
+  auto ACIt = AC.Cache.find(m);
+  if (ACIt == AC.Cache.end())
     cacheAnnotationFromMD(m, gv);
-  else if (AC.Cache[m].find(gv) == AC.Cache[m].end())
+  else if (ACIt->second.find(gv) == ACIt->second.end())
     cacheAnnotationFromMD(m, gv);
-  if (AC.Cache[m][gv].find(prop) == AC.Cache[m][gv].end())
+  auto &KVP = AC.Cache[m][gv];
+  auto It = KVP.find(prop);
+  if (It == KVP.end())
     return std::nullopt;
-  return AC.Cache[m][gv][prop][0];
+  return It->second[0];
 }
 
 static bool findAllNVVMAnnotation(const GlobalValue *gv,
@@ -143,13 +146,16 @@ static bool findAllNVVMAnnotation(const GlobalValue *gv,
   auto &AC = getAnnotationCache();
   std::lock_guard<sys::Mutex> Guard(AC.Lock);
   const Module *m = gv->getParent();
-  if (AC.Cache.find(m) == AC.Cache.end())
+  auto ACIt = AC.Cache.find(m);
+  if (ACIt == AC.Cache.end())
     cacheAnnotationFromMD(m, gv);
-  else if (AC.Cache[m].find(gv) == AC.Cache[m].end())
+  else if (ACIt->second.find(gv) == ACIt->second.end())
     cacheAnnotationFromMD(m, gv);
-  if (AC.Cache[m][gv].find(prop) == AC.Cache[m][gv].end())
+  auto &KVP = AC.Cache[m][gv];
+  auto It = KVP.find(prop);
+  if (It == KVP.end())
     return false;
-  retval = AC.Cache[m][gv][prop];
+  retval = It->second;
   return true;
 }
 

>From e8d3dbf9ba99205cad1ec4f5314e8ed752e641ac Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 14 Feb 2025 01:29:57 -0800
Subject: [PATCH 2/2] Address a comment.

---
 llvm/lib/Target/NVPTX/NVPTXUtilities.cpp | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp b/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
index f34d18160b6e0..cdb0f559d78b4 100644
--- a/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
@@ -133,6 +133,8 @@ static std::optional<unsigned> findOneNVVMAnnotation(const GlobalValue *gv,
     cacheAnnotationFromMD(m, gv);
   else if (ACIt->second.find(gv) == ACIt->second.end())
     cacheAnnotationFromMD(m, gv);
+  // Look up AC.Cache[m][gv] again because cacheAnnotationFromMD may have
+  // inserted the entry.
   auto &KVP = AC.Cache[m][gv];
   auto It = KVP.find(prop);
   if (It == KVP.end())
@@ -151,6 +153,8 @@ static bool findAllNVVMAnnotation(const GlobalValue *gv,
     cacheAnnotationFromMD(m, gv);
   else if (ACIt->second.find(gv) == ACIt->second.end())
     cacheAnnotationFromMD(m, gv);
+  // Look up AC.Cache[m][gv] again because cacheAnnotationFromMD may have
+  // inserted the entry.
   auto &KVP = AC.Cache[m][gv];
   auto It = KVP.find(prop);
   if (It == KVP.end())



More information about the llvm-commits mailing list