From 40fae4d8fcbd6224f16fdf3ba84a0d89b713e7d5 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 24 Dec 2021 19:01:50 -0800 Subject: [PATCH] [ELF] Optimize replaceCommonSymbols This decreases the 0.2% time (no debug info) to nearly no. --- lld/ELF/Driver.cpp | 22 +++++++++++++--------- lld/ELF/InputFiles.cpp | 1 + lld/ELF/InputFiles.h | 5 ++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 19266cb..6b689f5 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(sym); - if (!s) + for (ELFFileBase *file : objectFiles) { + if (!file->hasCommonSyms) continue; + for (Symbol *sym : file->getGlobalSymbols()) { + auto *s = dyn_cast(sym); + if (!s) + continue; - auto *bss = make("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("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 0badf2c..e321b0d 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1123,6 +1123,7 @@ template void ObjFile::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 f58e76e..d622390 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -205,12 +205,15 @@ protected: // Initializes this class's member variables. template 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. -- 2.7.4