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

Dhruv Chawla via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 14 06:50:57 PDT 2023


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

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.

>From 62b09786fff4d53aa0c75b64aea48de241e4a856 Mon Sep 17 00:00:00 2001
From: Dhruv Chawla <44582521+dc03 at users.noreply.github.com>
Date: Wed, 6 Sep 2023 18:04:55 +0530
Subject: [PATCH] [IRSymtab] Replace linear time lookup with DenseSet

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.
---
 llvm/lib/Object/IRSymtab.cpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

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<int> Builder::getComdatIndex(const Comdat *C, const Module *M) {
   return P.first->second;
 }
 
+static DenseSet<StringRef> buildPreservedSymbolsSet() {
+  return DenseSet<StringRef>(std::begin(PreservedSymbols),
+                             std::end(PreservedSymbols));
+}
+
 Error Builder::addSymbol(const ModuleSymbolTable &Msymtab,
                          const SmallPtrSet<GlobalValue *, 4> &Used,
                          ModuleSymbolTable::Symbol Msym) {
@@ -270,7 +275,9 @@ Error Builder::addSymbol(const ModuleSymbolTable &Msymtab,
 
   setStr(Sym.IRName, GV->getName());
 
-  bool IsPreservedSymbol = llvm::is_contained(PreservedSymbols, GV->getName());
+  static const DenseSet<StringRef> PreservedSymbolsSet =
+      buildPreservedSymbolsSet();
+  bool IsPreservedSymbol = PreservedSymbolsSet.count(GV->getName());
 
   if (Used.count(GV) || IsPreservedSymbol)
     Sym.Flags |= 1 << storage::Symbol::FB_used;



More information about the llvm-commits mailing list