[llvm] [IRSymtab] Replace linear time lookup with DenseSet (PR #66376)

via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 06:52:00 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-binary-utilities
            
<details>
<summary>Changes</summary>
There is an inefficiency in the IRSymtab Builder where it does a lookup of PreservedSymbols when calling addSymbol. This lookup is linear in time, so it tends to be quite slow. Replacing it with DenseSet gives a 0.1% speedup:
https://llvm-compile-time-tracker.com/compare.php?from=e7d8290f3be0f46b4766f1365a572019525f7a2b&to=132fc72e10dbd7cc0cf73ad02e2097972d39dd9e&stat=instructions:u

This change is quite similar to https://reviews.llvm.org/D157951.
--
Full diff: https://github.com/llvm/llvm-project/pull/66376.diff

1 Files Affected:

- (modified) llvm/lib/Object/IRSymtab.cpp (+8-1) 


<pre>
diff --git a/llvm/lib/Object/IRSymtab.cpp b/llvm/lib/Object/IRSymtab.cpp
index 14db7a10f31097e..85905a62cb9c76e 100644
--- a/llvm/lib/Object/IRSymtab.cpp
+++ b/llvm/lib/Object/IRSymtab.cpp
@@ -215,6 +215,11 @@ Expected&lt;int&gt; Builder::getComdatIndex(const Comdat *C, const Module *M) {
   return P.first-&gt;second;
 }
 
+static DenseSet&lt;StringRef&gt; buildPreservedSymbolsSet() {
+  return DenseSet&lt;StringRef&gt;(std::begin(PreservedSymbols),
+                             std::end(PreservedSymbols));
+}
+
 Error Builder::addSymbol(const ModuleSymbolTable &amp;Msymtab,
                          const SmallPtrSet&lt;GlobalValue *, 4&gt; &amp;Used,
                          ModuleSymbolTable::Symbol Msym) {
@@ -270,7 +275,9 @@ Error Builder::addSymbol(const ModuleSymbolTable &amp;Msymtab,
 
   setStr(Sym.IRName, GV-&gt;getName());
 
-  bool IsPreservedSymbol = llvm::is_contained(PreservedSymbols, GV-&gt;getName());
+  static const DenseSet&lt;StringRef&gt; PreservedSymbolsSet =
+      buildPreservedSymbolsSet();
+  bool IsPreservedSymbol = PreservedSymbolsSet.count(GV-&gt;getName());
 
   if (Used.count(GV) || IsPreservedSymbol)
     Sym.Flags |= 1 &lt;&lt; storage::Symbol::FB_used;
</pre>
</details>


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


More information about the llvm-commits mailing list