From: Rui Ueyama Date: Wed, 14 Sep 2016 00:05:51 +0000 (+0000) Subject: Simplify InputFile ownership management. X-Git-Tag: llvmorg-4.0.0-rc1~9857 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=38dbd3eea9943696166467a407cf45159d8c0f3f;p=platform%2Fupstream%2Fllvm.git Simplify InputFile ownership management. Previously, all input files were owned by the symbol table. Files were created at various places, such as the Driver, the lazy symbols, or the bitcode compiler, and the ownership of new files was transferred to the symbol table using std::unique_ptr. All input files were then free'd when the symbol table is freed which is on program exit. I think we don't have to transfer ownership just to free all instance at once on exit. In this patch, all instances are automatically collected to a vector and freed on exit. In this way, we no longer have to use std::unique_ptr. Differential Revision: https://reviews.llvm.org/D24493 llvm-svn: 281425 --- diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 9c81bc2..a741b67 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -50,6 +50,7 @@ bool elf::link(ArrayRef Args, raw_ostream &Error) { ScriptConfig = ≻ Driver->main(Args); + InputFile::freePool(); return !HasError; } @@ -127,7 +128,7 @@ void LinkerDriver::addFile(StringRef Path, bool KnownScript) { MemoryBufferRef MBRef = *Buffer; if (Config->Binary && !KnownScript) { - Files.push_back(make_unique(MBRef)); + Files.push_back(new BinaryFile(MBRef)); return; } @@ -141,7 +142,7 @@ void LinkerDriver::addFile(StringRef Path, bool KnownScript) { Files.push_back(createObjectFile(MB, Path)); return; } - Files.push_back(make_unique(MBRef)); + Files.push_back(new ArchiveFile(MBRef)); return; case file_magic::elf_shared_object: if (Config->Relocatable) { @@ -152,7 +153,7 @@ void LinkerDriver::addFile(StringRef Path, bool KnownScript) { return; default: if (InLib) - Files.push_back(make_unique(MBRef)); + Files.push_back(new LazyObjectFile(MBRef)); else Files.push_back(createObjectFile(MBRef)); } @@ -570,7 +571,7 @@ void LinkerDriver::createFiles(opt::InputArgList &Args) { // If -m was not given, infer it from object files. if (Config->EKind == ELFNoneKind) { - for (std::unique_ptr &F : Files) { + for (InputFile *F : Files) { if (F->EKind == ELFNoneKind) continue; Config->EKind = F->EKind; @@ -616,8 +617,8 @@ template void LinkerDriver::link(opt::InputArgList &Args) { // Add all files to the symbol table. After this, the symbol table // contains all known names except a few linker-synthesized symbols. - for (std::unique_ptr &F : Files) - Symtab.addFile(std::move(F)); + for (InputFile *F : Files) + Symtab.addFile(F); // Add the start symbol. // It initializes either Config->Entry or Config->EntryAddr. @@ -658,8 +659,7 @@ template void LinkerDriver::link(opt::InputArgList &Args) { // MergeInputSection::splitIntoPieces needs to be called before // any call of MergeInputSection::getOffset. Do that. - for (const std::unique_ptr> &F : - Symtab.getObjectFiles()) + for (elf::ObjectFile *F : Symtab.getObjectFiles()) { for (InputSectionBase *S : F->getSections()) { if (!S || S == &InputSection::Discarded || !S->Live) continue; @@ -668,6 +668,7 @@ template void LinkerDriver::link(opt::InputArgList &Args) { if (auto *MS = dyn_cast>(S)) MS->splitIntoPieces(); } + } // Write the result to the file. writeResult(); diff --git a/lld/ELF/Driver.h b/lld/ELF/Driver.h index b16244f..cdb19f5 100644 --- a/lld/ELF/Driver.h +++ b/lld/ELF/Driver.h @@ -46,7 +46,7 @@ private: bool InLib = false; llvm::BumpPtrAllocator Alloc; - std::vector> Files; + std::vector Files; std::vector> OwningMBs; }; diff --git a/lld/ELF/ICF.cpp b/lld/ELF/ICF.cpp index 52d7cba..eb403b1 100644 --- a/lld/ELF/ICF.cpp +++ b/lld/ELF/ICF.cpp @@ -148,8 +148,7 @@ template bool ICF::isEligible(InputSectionBase *Sec) { template std::vector *> ICF::getSections() { std::vector *> V; - for (const std::unique_ptr> &F : - Symtab::X->getObjectFiles()) + for (ObjectFile *F : Symtab::X->getObjectFiles()) for (InputSectionBase *S : F->getSections()) if (isEligible(S)) V.push_back(cast>(S)); diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index e86b48b..4857d57 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -32,6 +32,17 @@ using namespace llvm::sys::fs; using namespace lld; using namespace lld::elf; +std::vector InputFile::Pool; + +// Deletes all InputFile instances created so far. +void InputFile::freePool() { + // Files are freed in reverse order so that files created + // from other files (e.g. object files extracted from archives) + // are freed in the proper order. + for (int I = Pool.size() - 1; I >= 0; --I) + delete Pool[I]; +} + // Returns "(internal)", "foo.a(bar.o)" or "baz.o". std::string elf::getFilename(const InputFile *F) { if (!F) @@ -700,31 +711,31 @@ void BitcodeFile::parse(DenseSet &ComdatGroups) { } template