[llvm] ef31295 - collectUsedGlobalVariables: migrate SmallPtrSetImpl overload to SmallVecImpl overload after D97128

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 23 16:09:17 PST 2021


Author: Fangrui Song
Date: 2021-02-23T16:09:06-08:00
New Revision: ef312951fd6b4a255baf3cff27439c9ed8751651

URL: https://github.com/llvm/llvm-project/commit/ef312951fd6b4a255baf3cff27439c9ed8751651
DIFF: https://github.com/llvm/llvm-project/commit/ef312951fd6b4a255baf3cff27439c9ed8751651.diff

LOG: collectUsedGlobalVariables: migrate SmallPtrSetImpl overload to SmallVecImpl overload after D97128

And delete the SmallPtrSetImpl overload.

While here, decrease inline element counts from 8 to 4. See D97128 for the choice.

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D97257

Added: 
    

Modified: 
    llvm/include/llvm/IR/Module.h
    llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h
    llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
    llvm/lib/IR/Module.cpp
    llvm/lib/Object/IRSymtab.cpp
    llvm/lib/Transforms/IPO/GlobalOpt.cpp
    llvm/lib/Transforms/IPO/Internalize.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 63d66c5fd63ae..a27f44ed9d312 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -888,11 +888,6 @@ class Module {
   void setPartialSampleProfileRatio(const ModuleSummaryIndex &Index);
 };
 
-/// Given "llvm.used" or "llvm.compiler.used" as a global name, collect
-/// the initializer elements of that global in Set and return the global itself.
-GlobalVariable *collectUsedGlobalVariables(const Module &M,
-                                           SmallPtrSetImpl<GlobalValue *> &Set,
-                                           bool CompilerUsed);
 /// Given "llvm.used" or "llvm.compiler.used" as a global name, collect the
 /// initializer elements of that global in a SmallVector and return the global
 /// itself.

diff  --git a/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h b/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h
index acdd8fffa1c10..024d84a7abc88 100644
--- a/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h
+++ b/llvm/include/llvm/Transforms/Utils/FunctionImportUtils.h
@@ -54,7 +54,7 @@ class FunctionImportGlobalProcessing {
 
   /// Set of llvm.*used values, in order to validate that we don't try
   /// to promote any non-renamable values.
-  SmallPtrSet<GlobalValue *, 8> Used;
+  SmallPtrSet<GlobalValue *, 4> Used;
 
   /// Keep track of any COMDATs that require renaming (because COMDAT
   /// leader was promoted and renamed). Maps from original COMDAT to one
@@ -111,10 +111,12 @@ class FunctionImportGlobalProcessing {
       HasExportedFunctions = ImportIndex.hasExportedFunctions(M);
 
 #ifndef NDEBUG
+    SmallVector<GlobalValue *, 4> Vec;
     // First collect those in the llvm.used set.
-    collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
+    collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/false);
     // Next collect those in the llvm.compiler.used set.
-    collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
+    collectUsedGlobalVariables(M, Vec, /*CompilerUsed=*/true);
+    Used = {Vec.begin(), Vec.end()};
 #endif
   }
 

diff  --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
index cd21f55c3ab15..c9afdadd7335c 100644
--- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
+++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
@@ -662,12 +662,12 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
   // promotion, but we may have opaque uses e.g. in inline asm. We collect them
   // here because we use this information to mark functions containing inline
   // assembly calls as not importable.
-  SmallPtrSet<GlobalValue *, 8> LocalsUsed;
-  SmallPtrSet<GlobalValue *, 8> Used;
+  SmallPtrSet<GlobalValue *, 4> LocalsUsed;
+  SmallVector<GlobalValue *, 4> Used;
   // First collect those in the llvm.used set.
-  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false);
+  collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/false);
   // Next collect those in the llvm.compiler.used set.
-  collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ true);
+  collectUsedGlobalVariables(M, Used, /*CompilerUsed=*/true);
   DenseSet<GlobalValue::GUID> CantBePromoted;
   for (auto *V : Used) {
     if (V->hasLocalLinkage()) {

diff  --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index 4c244611fbb3d..60056f142d8a6 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -673,21 +673,6 @@ GlobalVariable *llvm::collectUsedGlobalVariables(
   return GV;
 }
 
-GlobalVariable *llvm::collectUsedGlobalVariables(
-    const Module &M, SmallPtrSetImpl<GlobalValue *> &Set, bool CompilerUsed) {
-  const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
-  GlobalVariable *GV = M.getGlobalVariable(Name);
-  if (!GV || !GV->hasInitializer())
-    return GV;
-
-  const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
-  for (Value *Op : Init->operands()) {
-    GlobalValue *G = cast<GlobalValue>(Op->stripPointerCasts());
-    Set.insert(G);
-  }
-  return GV;
-}
-
 void Module::setPartialSampleProfileRatio(const ModuleSummaryIndex &Index) {
   if (auto *SummaryMD = getProfileSummary(/*IsCS*/ false)) {
     std::unique_ptr<ProfileSummary> ProfileSummary(

diff  --git a/llvm/lib/Object/IRSymtab.cpp b/llvm/lib/Object/IRSymtab.cpp
index 8e6b6a373a2e2..16c6433a20611 100644
--- a/llvm/lib/Object/IRSymtab.cpp
+++ b/llvm/lib/Object/IRSymtab.cpp
@@ -108,7 +108,7 @@ struct Builder {
 
   Error addModule(Module *M);
   Error addSymbol(const ModuleSymbolTable &Msymtab,
-                  const SmallPtrSet<GlobalValue *, 8> &Used,
+                  const SmallPtrSet<GlobalValue *, 4> &Used,
                   ModuleSymbolTable::Symbol Sym);
 
   Error build(ArrayRef<Module *> Mods);
@@ -119,8 +119,9 @@ Error Builder::addModule(Module *M) {
     return make_error<StringError>("input module has no datalayout",
                                    inconvertibleErrorCode());
 
-  SmallPtrSet<GlobalValue *, 8> Used;
-  collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false);
+  SmallVector<GlobalValue *, 4> UsedV;
+  collectUsedGlobalVariables(*M, UsedV, /*CompilerUsed=*/false);
+  SmallPtrSet<GlobalValue *, 4> Used(UsedV.begin(), UsedV.end());
 
   ModuleSymbolTable Msymtab;
   Msymtab.addModule(M);
@@ -193,7 +194,7 @@ Expected<int> Builder::getComdatIndex(const Comdat *C, const Module *M) {
 }
 
 Error Builder::addSymbol(const ModuleSymbolTable &Msymtab,
-                         const SmallPtrSet<GlobalValue *, 8> &Used,
+                         const SmallPtrSet<GlobalValue *, 4> &Used,
                          ModuleSymbolTable::Symbol Msym) {
   Syms.emplace_back();
   storage::Symbol &Sym = Syms.back();

diff  --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 223a05e8ea02b..f275f371c77ae 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -2788,18 +2788,22 @@ namespace {
 
 /// An easy to access representation of llvm.used and llvm.compiler.used.
 class LLVMUsed {
-  SmallPtrSet<GlobalValue *, 8> Used;
-  SmallPtrSet<GlobalValue *, 8> CompilerUsed;
+  SmallPtrSet<GlobalValue *, 4> Used;
+  SmallPtrSet<GlobalValue *, 4> CompilerUsed;
   GlobalVariable *UsedV;
   GlobalVariable *CompilerUsedV;
 
 public:
   LLVMUsed(Module &M) {
-    UsedV = collectUsedGlobalVariables(M, Used, false);
-    CompilerUsedV = collectUsedGlobalVariables(M, CompilerUsed, true);
+    SmallVector<GlobalValue *, 4> Vec;
+    UsedV = collectUsedGlobalVariables(M, Vec, false);
+    Used = {Vec.begin(), Vec.end()};
+    Vec.clear();
+    CompilerUsedV = collectUsedGlobalVariables(M, Vec, true);
+    CompilerUsed = {Vec.begin(), Vec.end()};
   }
 
-  using iterator = SmallPtrSet<GlobalValue *, 8>::iterator;
+  using iterator = SmallPtrSet<GlobalValue *, 4>::iterator;
   using used_iterator_range = iterator_range<iterator>;
 
   iterator usedBegin() { return Used.begin(); }

diff  --git a/llvm/lib/Transforms/IPO/Internalize.cpp b/llvm/lib/Transforms/IPO/Internalize.cpp
index e1644819af617..ede81f61e79df 100644
--- a/llvm/lib/Transforms/IPO/Internalize.cpp
+++ b/llvm/lib/Transforms/IPO/Internalize.cpp
@@ -151,7 +151,7 @@ bool InternalizePass::internalizeModule(Module &M, CallGraph *CG) {
   bool Changed = false;
   CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : nullptr;
 
-  SmallPtrSet<GlobalValue *, 8> Used;
+  SmallVector<GlobalValue *, 4> Used;
   collectUsedGlobalVariables(M, Used, false);
 
   // Collect comdat visiblity information for the module.


        


More information about the llvm-commits mailing list