[lld] 40fae4d - [ELF] Optimize replaceCommonSymbols

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 24 19:01:57 PST 2021


Author: Fangrui Song
Date: 2021-12-24T19:01:51-08:00
New Revision: 40fae4d8fcbd6224f16fdf3ba84a0d89b713e7d5

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

LOG: [ELF] Optimize replaceCommonSymbols

This decreases the 0.2% time (no debug info) to nearly no.

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/ELF/InputFiles.cpp
    lld/ELF/InputFiles.h

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 19266cb280b9..6b689f50cce7 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1821,17 +1821,21 @@ static void writeDependencyFile() {
 // symbols of type CommonSymbol.
 static void replaceCommonSymbols() {
   llvm::TimeTraceScope timeScope("Replace common symbols");
-  for (Symbol *sym : symtab->symbols()) {
-    auto *s = dyn_cast<CommonSymbol>(sym);
-    if (!s)
+  for (ELFFileBase *file : objectFiles) {
+    if (!file->hasCommonSyms)
       continue;
+    for (Symbol *sym : file->getGlobalSymbols()) {
+      auto *s = dyn_cast<CommonSymbol>(sym);
+      if (!s)
+        continue;
 
-    auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
-    bss->file = s->file;
-    bss->markDead();
-    inputSections.push_back(bss);
-    s->replace(Defined{s->file, s->getName(), s->binding, s->stOther, s->type,
-                       /*value=*/0, s->size, bss});
+      auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
+      bss->file = s->file;
+      bss->markDead();
+      inputSections.push_back(bss);
+      s->replace(Defined{s->file, s->getName(), s->binding, s->stOther, s->type,
+                         /*value=*/0, s->size, bss});
+    }
   }
 }
 

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 0badf2c55e5b..e321b0d82920 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -1123,6 +1123,7 @@ template <class ELFT> void ObjFile<ELFT>::initializeSymbols() {
       if (value == 0 || value >= UINT32_MAX)
         fatal(toString(this) + ": common symbol '" + name +
               "' has invalid alignment: " + Twine(value));
+      hasCommonSyms = true;
       sym->resolve(
           CommonSymbol{this, name, binding, stOther, type, value, size});
       continue;

diff  --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index f58e76e48433..d622390fcade 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -205,12 +205,15 @@ class ELFFileBase : public InputFile {
   // Initializes this class's member variables.
   template <typename ELFT> void init();
 
+  StringRef stringTable;
   const void *elfShdrs = nullptr;
   const void *elfSyms = nullptr;
   uint32_t numELFShdrs = 0;
   uint32_t numELFSyms = 0;
   uint32_t firstGlobal = 0;
-  StringRef stringTable;
+
+public:
+  bool hasCommonSyms = false;
 };
 
 // .o file.


        


More information about the llvm-commits mailing list