[lld] r281425 - Simplify InputFile ownership management.

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 19 09:56:54 PDT 2016


Is there a particular advantage to not using unique_ptr you're gaining
here? (performance? readability)

Using unique_ptr makes the code easier to read by making the ownership
explicit so leaks are less likely to slip through - granted in this case
the intention is to keep them alive until the end of the program, but
that's not always obvious to readers, or some uses might not have the same
lifetime in the future, etc.

On Tue, Sep 13, 2016 at 5:14 PM Rui Ueyama via llvm-commits <
llvm-commits at lists.llvm.org> wrote:

> Author: ruiu
> Date: Tue Sep 13 19:05:51 2016
> New Revision: 281425
>
> URL: http://llvm.org/viewvc/llvm-project?rev=281425&view=rev
> Log:
> 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
>
> Modified:
>     lld/trunk/ELF/Driver.cpp
>     lld/trunk/ELF/Driver.h
>     lld/trunk/ELF/ICF.cpp
>     lld/trunk/ELF/InputFiles.cpp
>     lld/trunk/ELF/InputFiles.h
>     lld/trunk/ELF/LTO.cpp
>     lld/trunk/ELF/LTO.h
>     lld/trunk/ELF/LinkerScript.cpp
>     lld/trunk/ELF/MarkLive.cpp
>     lld/trunk/ELF/Mips.cpp
>     lld/trunk/ELF/OutputSections.cpp
>     lld/trunk/ELF/SymbolTable.cpp
>     lld/trunk/ELF/SymbolTable.h
>     lld/trunk/ELF/Symbols.cpp
>     lld/trunk/ELF/Symbols.h
>     lld/trunk/ELF/Writer.cpp
>
> Modified: lld/trunk/ELF/Driver.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Driver.cpp (original)
> +++ lld/trunk/ELF/Driver.cpp Tue Sep 13 19:05:51 2016
> @@ -50,6 +50,7 @@ bool elf::link(ArrayRef<const char *> Ar
>    ScriptConfig = &SC;
>
>    Driver->main(Args);
> +  InputFile::freePool();
>    return !HasError;
>  }
>
> @@ -127,7 +128,7 @@ void LinkerDriver::addFile(StringRef Pat
>    MemoryBufferRef MBRef = *Buffer;
>
>    if (Config->Binary && !KnownScript) {
> -    Files.push_back(make_unique<BinaryFile>(MBRef));
> +    Files.push_back(new BinaryFile(MBRef));
>      return;
>    }
>
> @@ -141,7 +142,7 @@ void LinkerDriver::addFile(StringRef Pat
>          Files.push_back(createObjectFile(MB, Path));
>        return;
>      }
> -    Files.push_back(make_unique<ArchiveFile>(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 Pat
>      return;
>    default:
>      if (InLib)
> -      Files.push_back(make_unique<LazyObjectFile>(MBRef));
> +      Files.push_back(new LazyObjectFile(MBRef));
>      else
>        Files.push_back(createObjectFile(MBRef));
>    }
> @@ -570,7 +571,7 @@ void LinkerDriver::createFiles(opt::Inpu
>
>    // If -m <machine_type> was not given, infer it from object files.
>    if (Config->EKind == ELFNoneKind) {
> -    for (std::unique_ptr<InputFile> &F : Files) {
> +    for (InputFile *F : Files) {
>        if (F->EKind == ELFNoneKind)
>          continue;
>        Config->EKind = F->EKind;
> @@ -616,8 +617,8 @@ template <class ELFT> void LinkerDriver:
>
>    // 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<InputFile> &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 <class ELFT> void LinkerDriver:
>
>    // MergeInputSection::splitIntoPieces needs to be called before
>    // any call of MergeInputSection::getOffset. Do that.
> -  for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
> -       Symtab.getObjectFiles())
> +  for (elf::ObjectFile<ELFT> *F : Symtab.getObjectFiles()) {
>      for (InputSectionBase<ELFT> *S : F->getSections()) {
>        if (!S || S == &InputSection<ELFT>::Discarded || !S->Live)
>          continue;
> @@ -668,6 +668,7 @@ template <class ELFT> void LinkerDriver:
>        if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(S))
>          MS->splitIntoPieces();
>      }
> +  }
>
>    // Write the result to the file.
>    writeResult<ELFT>();
>
> Modified: lld/trunk/ELF/Driver.h
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.h?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Driver.h (original)
> +++ lld/trunk/ELF/Driver.h Tue Sep 13 19:05:51 2016
> @@ -46,7 +46,7 @@ private:
>    bool InLib = false;
>
>    llvm::BumpPtrAllocator Alloc;
> -  std::vector<std::unique_ptr<InputFile>> Files;
> +  std::vector<InputFile *> Files;
>    std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
>  };
>
>
> Modified: lld/trunk/ELF/ICF.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ICF.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/ICF.cpp (original)
> +++ lld/trunk/ELF/ICF.cpp Tue Sep 13 19:05:51 2016
> @@ -148,8 +148,7 @@ template <class ELFT> bool ICF<ELFT>::is
>  template <class ELFT>
>  std::vector<InputSection<ELFT> *> ICF<ELFT>::getSections() {
>    std::vector<InputSection<ELFT> *> V;
> -  for (const std::unique_ptr<ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles())
> +  for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
>      for (InputSectionBase<ELFT> *S : F->getSections())
>        if (isEligible(S))
>          V.push_back(cast<InputSection<ELFT>>(S));
>
> Modified: lld/trunk/ELF/InputFiles.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/InputFiles.cpp (original)
> +++ lld/trunk/ELF/InputFiles.cpp Tue Sep 13 19:05:51 2016
> @@ -32,6 +32,17 @@ using namespace llvm::sys::fs;
>  using namespace lld;
>  using namespace lld::elf;
>
> +std::vector<InputFile *> 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<StringR
>  }
>
>  template <template <class> class T>
> -static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
> +static InputFile *createELFFile(MemoryBufferRef MB) {
>    unsigned char Size;
>    unsigned char Endian;
>    std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
>    if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
>      fatal("invalid data encoding: " + MB.getBufferIdentifier());
>
> -  std::unique_ptr<InputFile> Obj;
> +  InputFile *Obj;
>    if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
> -    Obj.reset(new T<ELF32LE>(MB));
> +    Obj = new T<ELF32LE>(MB);
>    else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
> -    Obj.reset(new T<ELF32BE>(MB));
> +    Obj = new T<ELF32BE>(MB);
>    else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
> -    Obj.reset(new T<ELF64LE>(MB));
> +    Obj = new T<ELF64LE>(MB);
>    else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
> -    Obj.reset(new T<ELF64BE>(MB));
> +    Obj = new T<ELF64BE>(MB);
>    else
>      fatal("invalid file class: " + MB.getBufferIdentifier());
>
>    if (!Config->FirstElf)
> -    Config->FirstElf = Obj.get();
> +    Config->FirstElf = Obj;
>    return Obj;
>  }
>
> -template <class ELFT> std::unique_ptr<InputFile> BinaryFile::createELF() {
> +template <class ELFT> InputFile *BinaryFile::createELF() {
>    // Wrap the binary blob with an ELF header and footer
>    // so that we can link it as a regular ELF file.
>    ELFCreator<ELFT> ELF(ET_REL, Config->EMachine);
> @@ -773,18 +784,14 @@ static bool isBitcode(MemoryBufferRef MB
>    return identify_magic(MB.getBuffer()) == file_magic::bitcode;
>  }
>
> -std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
> -                                                 StringRef ArchiveName) {
> -  std::unique_ptr<InputFile> F;
> -  if (isBitcode(MB))
> -    F.reset(new BitcodeFile(MB));
> -  else
> -    F = createELFFile<ObjectFile>(MB);
> +InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef
> ArchiveName) {
> +  InputFile *F =
> +      isBitcode(MB) ? new BitcodeFile(MB) : createELFFile<ObjectFile>(MB);
>    F->ArchiveName = ArchiveName;
>    return F;
>  }
>
> -std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
> +InputFile *elf::createSharedFile(MemoryBufferRef MB) {
>    return createELFFile<SharedFile>(MB);
>  }
>
> @@ -889,7 +896,7 @@ template class elf::SharedFile<ELF32BE>;
>  template class elf::SharedFile<ELF64LE>;
>  template class elf::SharedFile<ELF64BE>;
>
> -template std::unique_ptr<InputFile> BinaryFile::createELF<ELF32LE>();
> -template std::unique_ptr<InputFile> BinaryFile::createELF<ELF32BE>();
> -template std::unique_ptr<InputFile> BinaryFile::createELF<ELF64LE>();
> -template std::unique_ptr<InputFile> BinaryFile::createELF<ELF64BE>();
> +template InputFile *BinaryFile::createELF<ELF32LE>();
> +template InputFile *BinaryFile::createELF<ELF32BE>();
> +template InputFile *BinaryFile::createELF<ELF64LE>();
> +template InputFile *BinaryFile::createELF<ELF64BE>();
>
> Modified: lld/trunk/ELF/InputFiles.h
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/InputFiles.h (original)
> +++ lld/trunk/ELF/InputFiles.h Tue Sep 13 19:05:51 2016
> @@ -39,6 +39,8 @@ class SymbolBody;
>  // The root class of input files.
>  class InputFile {
>  public:
> +  virtual ~InputFile() = default;
> +
>    enum Kind {
>      ObjectKind,
>      SharedKind,
> @@ -63,11 +65,19 @@ public:
>    ELFKind EKind = ELFNoneKind;
>    uint16_t EMachine = llvm::ELF::EM_NONE;
>
> +  static void freePool();
> +
>  protected:
> -  InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
> +  InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {
> +    Pool.push_back(this);
> +  }
>
>  private:
>    const Kind FileKind;
> +
> +  // All InputFile instances are added to the pool
> +  // and freed all at once on exit by freePool().
> +  static std::vector<InputFile *> Pool;
>  };
>
>  // Returns "(internal)", "foo.a(bar.o)" or "baz.o".
> @@ -304,15 +314,14 @@ public:
>
>    static bool classof(const InputFile *F) { return F->kind() ==
> BinaryKind; }
>
> -  template <class ELFT> std::unique_ptr<InputFile> createELF();
> +  template <class ELFT> InputFile *createELF();
>
>  private:
>    std::vector<uint8_t> ELFData;
>  };
>
> -std::unique_ptr<InputFile> createObjectFile(MemoryBufferRef MB,
> -                                            StringRef ArchiveName = "");
> -std::unique_ptr<InputFile> createSharedFile(MemoryBufferRef MB);
> +InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName =
> "");
> +InputFile *createSharedFile(MemoryBufferRef MB);
>
>  } // namespace elf
>  } // namespace lld
>
> Modified: lld/trunk/ELF/LTO.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/LTO.cpp (original)
> +++ lld/trunk/ELF/LTO.cpp Tue Sep 13 19:05:51 2016
> @@ -259,7 +259,7 @@ static void internalize(GlobalValue &GV)
>    GV.setLinkage(GlobalValue::InternalLinkage);
>  }
>
> -std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::runSplitCodegen(
> +std::vector<InputFile *> BitcodeCompiler::runSplitCodegen(
>      const std::function<std::unique_ptr<TargetMachine>()> &TMFactory) {
>    unsigned NumThreads = Config->LtoJobs;
>    OwningData.resize(NumThreads);
> @@ -273,7 +273,7 @@ std::vector<std::unique_ptr<InputFile>>
>
>    splitCodeGen(std::move(Combined), OSPtrs, {}, TMFactory);
>
> -  std::vector<std::unique_ptr<InputFile>> ObjFiles;
> +  std::vector<InputFile *> ObjFiles;
>    for (SmallString<0> &Obj : OwningData)
>      ObjFiles.push_back(createObjectFile(
>          MemoryBufferRef(Obj, "LLD-INTERNAL-combined-lto-object")));
> @@ -294,7 +294,7 @@ std::vector<std::unique_ptr<InputFile>>
>
>  // Merge all the bitcode files we have seen, codegen the result
>  // and return the resulting ObjectFile.
> -std::vector<std::unique_ptr<InputFile>> BitcodeCompiler::compile() {
> +std::vector<InputFile *> BitcodeCompiler::compile() {
>    for (const auto &Name : InternalizedSyms) {
>      GlobalValue *GV = Combined->getNamedValue(Name.first());
>      assert(GV);
>
> Modified: lld/trunk/ELF/LTO.h
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LTO.h?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/LTO.h (original)
> +++ lld/trunk/ELF/LTO.h Tue Sep 13 19:05:51 2016
> @@ -37,10 +37,10 @@ class BitcodeCompiler {
>  public:
>    BitcodeCompiler();
>    void add(BitcodeFile &F);
> -  std::vector<std::unique_ptr<InputFile>> compile();
> +  std::vector<InputFile *> compile();
>
>  private:
> -  std::vector<std::unique_ptr<InputFile>> runSplitCodegen(
> +  std::vector<InputFile *> runSplitCodegen(
>        const std::function<std::unique_ptr<llvm::TargetMachine>()>
> &TMFactory);
>
>    std::unique_ptr<llvm::Module> Combined;
>
> Modified: lld/trunk/ELF/LinkerScript.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/LinkerScript.cpp (original)
> +++ lld/trunk/ELF/LinkerScript.cpp Tue Sep 13 19:05:51 2016
> @@ -116,14 +116,12 @@ std::vector<InputSectionBase<ELFT> *>
>  LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
>    const Regex &Re = I->SectionRe;
>    std::vector<InputSectionBase<ELFT> *> Ret;
> -  for (const std::unique_ptr<ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles()) {
> +  for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
>      if (fileMatches(I, sys::path::filename(F->getName())))
>        for (InputSectionBase<ELFT> *S : F->getSections())
>          if (!isDiscarded(S) && !S->OutSec &&
>              const_cast<Regex &>(Re).match(S->Name))
>            Ret.push_back(S);
> -  }
>
>    if (const_cast<Regex &>(Re).match("COMMON"))
>      Ret.push_back(CommonInputSection<ELFT>::X);
> @@ -286,8 +284,7 @@ void LinkerScript<ELFT>::createSections(
>    }
>
>    // Add orphan sections.
> -  for (const std::unique_ptr<ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles()) {
> +  for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
>      for (InputSectionBase<ELFT> *S : F->getSections()) {
>        if (isDiscarded(S) || S->OutSec)
>          continue;
>
> Modified: lld/trunk/ELF/MarkLive.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MarkLive.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/MarkLive.cpp (original)
> +++ lld/trunk/ELF/MarkLive.cpp Tue Sep 13 19:05:51 2016
> @@ -226,9 +226,8 @@ template <class ELFT> void elf::markLive
>
>    // Preserve special sections and those which are specified in linker
>    // script KEEP command.
> -  for (const std::unique_ptr<ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles())
> -    for (InputSectionBase<ELFT> *Sec : F->getSections())
> +  for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
> +    for (InputSectionBase<ELFT> *Sec : F->getSections()) {
>        if (Sec && Sec != &InputSection<ELFT>::Discarded) {
>          // .eh_frame is always marked as live now, but also it can
> reference to
>          // sections that contain personality. We preserve all non-text
> sections
> @@ -238,6 +237,8 @@ template <class ELFT> void elf::markLive
>          if (isReserved(Sec) || Script<ELFT>::X->shouldKeep(Sec))
>            Enqueue({Sec, 0});
>        }
> +    }
> +  }
>
>    // Mark all reachable sections.
>    while (!Q.empty())
>
> Modified: lld/trunk/ELF/Mips.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Mips.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Mips.cpp (original)
> +++ lld/trunk/ELF/Mips.cpp Tue Sep 13 19:05:51 2016
> @@ -283,10 +283,8 @@ static uint32_t getArchFlags(ArrayRef<Fi
>
>  template <class ELFT> uint32_t elf::getMipsEFlags() {
>    std::vector<FileFlags> V;
> -  for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles())
> +  for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
>      V.push_back({F->getName(), F->getObj().getHeader()->e_flags});
> -
>    checkFlags(V);
>    return getMiscFlags(V) | getPicFlags(V) | getArchFlags(V);
>  }
>
> Modified: lld/trunk/ELF/OutputSections.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/OutputSections.cpp (original)
> +++ lld/trunk/ELF/OutputSections.cpp Tue Sep 13 19:05:51 2016
> @@ -685,8 +685,7 @@ template <class ELFT> void DynamicSectio
>    if (!Config->RPath.empty())
>      Add({Config->EnableNewDtags ? DT_RUNPATH : DT_RPATH,
>           Out<ELFT>::DynStrTab->addString(Config->RPath)});
> -  for (const std::unique_ptr<SharedFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getSharedFiles())
> +  for (SharedFile<ELFT> *F : Symtab<ELFT>::X->getSharedFiles())
>      if (F->isNeeded())
>        Add({DT_NEEDED, Out<ELFT>::DynStrTab->addString(F->getSoName())});
>    if (!Config->SoName.empty())
> @@ -1431,8 +1430,7 @@ template <class ELFT>
>  void SymbolTableSection<ELFT>::writeLocalSymbols(uint8_t *&Buf) {
>    // Iterate over all input object files to copy their local symbols
>    // to the output symbol table pointed by Buf.
> -  for (const std::unique_ptr<ObjectFile<ELFT>> &File :
> -       Symtab<ELFT>::X->getObjectFiles()) {
> +  for (ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
>      for (const std::pair<const DefinedRegular<ELFT> *, size_t> &P :
>           File->KeptLocalSyms) {
>        const DefinedRegular<ELFT> &Body = *P.first;
>
> Modified: lld/trunk/ELF/SymbolTable.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/SymbolTable.cpp (original)
> +++ lld/trunk/ELF/SymbolTable.cpp Tue Sep 13 19:05:51 2016
> @@ -47,58 +47,52 @@ template <class ELFT> static bool isComp
>  }
>
>  // Add symbols in File to the symbol table.
> -template <class ELFT>
> -void SymbolTable<ELFT>::addFile(std::unique_ptr<InputFile> File) {
> -  InputFile *FileP = File.get();
> -  if (!isCompatible<ELFT>(FileP))
> +template <class ELFT> void SymbolTable<ELFT>::addFile(InputFile *File) {
> +  if (!isCompatible<ELFT>(File))
>      return;
>
>    // Binary file
> -  if (auto *F = dyn_cast<BinaryFile>(FileP)) {
> -    BinaryFiles.emplace_back(cast<BinaryFile>(File.release()));
> +  if (auto *F = dyn_cast<BinaryFile>(File)) {
>      addFile(F->createELF<ELFT>());
>      return;
>    }
>
>    // .a file
> -  if (auto *F = dyn_cast<ArchiveFile>(FileP)) {
> -    ArchiveFiles.emplace_back(cast<ArchiveFile>(File.release()));
> +  if (auto *F = dyn_cast<ArchiveFile>(File)) {
>      F->parse<ELFT>();
>      return;
>    }
>
>    // Lazy object file
> -  if (auto *F = dyn_cast<LazyObjectFile>(FileP)) {
> -    LazyObjectFiles.emplace_back(cast<LazyObjectFile>(File.release()));
> +  if (auto *F = dyn_cast<LazyObjectFile>(File)) {
>      F->parse<ELFT>();
>      return;
>    }
>
>    if (Config->Trace)
> -    outs() << getFilename(FileP) << "\n";
> +    outs() << getFilename(File) << "\n";
>
>    // .so file
> -  if (auto *F = dyn_cast<SharedFile<ELFT>>(FileP)) {
> +  if (auto *F = dyn_cast<SharedFile<ELFT>>(File)) {
>      // DSOs are uniquified not by filename but by soname.
>      F->parseSoName();
>      if (!SoNames.insert(F->getSoName()).second)
>        return;
> -
> -    SharedFiles.emplace_back(cast<SharedFile<ELFT>>(File.release()));
> +    SharedFiles.push_back(F);
>      F->parseRest();
>      return;
>    }
>
>    // LLVM bitcode file
> -  if (auto *F = dyn_cast<BitcodeFile>(FileP)) {
> -    BitcodeFiles.emplace_back(cast<BitcodeFile>(File.release()));
> +  if (auto *F = dyn_cast<BitcodeFile>(File)) {
> +    BitcodeFiles.push_back(F);
>      F->parse<ELFT>(ComdatGroups);
>      return;
>    }
>
>    // Regular object file
> -  auto *F = cast<ObjectFile<ELFT>>(FileP);
> -  ObjectFiles.emplace_back(cast<ObjectFile<ELFT>>(File.release()));
> +  auto *F = cast<ObjectFile<ELFT>>(File);
> +  ObjectFiles.push_back(F);
>    F->parse(ComdatGroups);
>  }
>
> @@ -113,19 +107,16 @@ template <class ELFT> void SymbolTable<E
>    if (BitcodeFiles.empty())
>      return;
>
> -  // Compile bitcode files.
> +  // Compile bitcode files and replace bitcode symbols.
>    Lto.reset(new BitcodeCompiler);
> -  for (const std::unique_ptr<BitcodeFile> &F : BitcodeFiles)
> +  for (BitcodeFile *F : BitcodeFiles)
>      Lto->add(*F);
> -  std::vector<std::unique_ptr<InputFile>> IFs = Lto->compile();
> -
> -  // Replace bitcode symbols.
> -  for (auto &IF : IFs) {
> -    ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(IF.release());
>
> +  for (InputFile *File : Lto->compile()) {
> +    ObjectFile<ELFT> *Obj = cast<ObjectFile<ELFT>>(File);
>      DenseSet<StringRef> DummyGroups;
>      Obj->parse(DummyGroups);
> -    ObjectFiles.emplace_back(Obj);
> +    ObjectFiles.push_back(Obj);
>    }
>  }
>
> @@ -307,8 +298,8 @@ Symbol *SymbolTable<ELFT>::addUndefined(
>      // its type. See also comment in addLazyArchive.
>      if (S->isWeak())
>        L->Type = Type;
> -    else if (auto F = L->fetch())
> -      addFile(std::move(F));
> +    else if (InputFile *F = L->fetch())
> +      addFile(F);
>    }
>    return S;
>  }
> @@ -558,8 +549,8 @@ void SymbolTable<ELFT>::addLazyObject(St
>  template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
>    for (StringRef S : Config->Undefined)
>      if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
> -      if (std::unique_ptr<InputFile> File = L->fetch())
> -        addFile(std::move(File));
> +      if (InputFile *File = L->fetch())
> +        addFile(File);
>  }
>
>  // This function takes care of the case in which shared libraries depend
> on
> @@ -570,7 +561,7 @@ template <class ELFT> void SymbolTable<E
>  // shared libraries can find them.
>  // Except this, we ignore undefined symbols in DSOs.
>  template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
> -  for (std::unique_ptr<SharedFile<ELFT>> &File : SharedFiles)
> +  for (SharedFile<ELFT> *File : SharedFiles)
>      for (StringRef U : File->getUndefinedSymbols())
>        if (SymbolBody *Sym = find(U))
>          if (Sym->isDefined())
>
> Modified: lld/trunk/ELF/SymbolTable.h
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.h?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/SymbolTable.h (original)
> +++ lld/trunk/ELF/SymbolTable.h Tue Sep 13 19:05:51 2016
> @@ -40,16 +40,16 @@ template <class ELFT> class SymbolTable
>    typedef typename ELFT::uint uintX_t;
>
>  public:
> -  void addFile(std::unique_ptr<InputFile> File);
> +  void addFile(InputFile *File);
>    void addCombinedLtoObject();
>
>    llvm::ArrayRef<Symbol *> getSymbols() const { return SymVector; }
>
> -  const std::vector<std::unique_ptr<ObjectFile<ELFT>>> &getObjectFiles()
> const {
> +  const std::vector<ObjectFile<ELFT> *> &getObjectFiles() const {
>      return ObjectFiles;
>    }
>
> -  const std::vector<std::unique_ptr<SharedFile<ELFT>>> &getSharedFiles()
> const {
> +  const std::vector<SharedFile<ELFT> *> &getSharedFiles() const {
>      return SharedFiles;
>    }
>
> @@ -126,13 +126,9 @@ private:
>    // is used to uniquify them.
>    llvm::DenseSet<StringRef> ComdatGroups;
>
> -  // The symbol table owns all file objects.
> -  std::vector<std::unique_ptr<ArchiveFile>> ArchiveFiles;
> -  std::vector<std::unique_ptr<BinaryFile>> BinaryFiles;
> -  std::vector<std::unique_ptr<ObjectFile<ELFT>>> ObjectFiles;
> -  std::vector<std::unique_ptr<LazyObjectFile>> LazyObjectFiles;
> -  std::vector<std::unique_ptr<SharedFile<ELFT>>> SharedFiles;
> -  std::vector<std::unique_ptr<BitcodeFile>> BitcodeFiles;
> +  std::vector<ObjectFile<ELFT> *> ObjectFiles;
> +  std::vector<SharedFile<ELFT> *> SharedFiles;
> +  std::vector<BitcodeFile *> BitcodeFiles;
>
>    // Set of .so files to not link the same shared object file more than
> once.
>    llvm::DenseSet<StringRef> SoNames;
>
> Modified: lld/trunk/ELF/Symbols.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Symbols.cpp (original)
> +++ lld/trunk/ELF/Symbols.cpp Tue Sep 13 19:05:51 2016
> @@ -215,7 +215,7 @@ DefinedCommon::DefinedCommon(StringRef N
>    this->File = File;
>  }
>
> -std::unique_ptr<InputFile> Lazy::fetch() {
> +InputFile *Lazy::fetch() {
>    if (auto *S = dyn_cast<LazyArchive>(this))
>      return S->fetch();
>    return cast<LazyObject>(this)->fetch();
> @@ -232,20 +232,20 @@ LazyObject::LazyObject(StringRef Name, L
>    this->File = &File;
>  }
>
> -std::unique_ptr<InputFile> LazyArchive::fetch() {
> +InputFile *LazyArchive::fetch() {
>    MemoryBufferRef MBRef = file()->getMember(&Sym);
>
>    // getMember returns an empty buffer if the member was already
>    // read from the library.
>    if (MBRef.getBuffer().empty())
> -    return std::unique_ptr<InputFile>(nullptr);
> +    return nullptr;
>    return createObjectFile(MBRef, file()->getName());
>  }
>
> -std::unique_ptr<InputFile> LazyObject::fetch() {
> +InputFile *LazyObject::fetch() {
>    MemoryBufferRef MBRef = file()->getBuffer();
>    if (MBRef.getBuffer().empty())
> -    return std::unique_ptr<InputFile>(nullptr);
> +    return nullptr;
>    return createObjectFile(MBRef);
>  }
>
>
> Modified: lld/trunk/ELF/Symbols.h
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Symbols.h?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Symbols.h (original)
> +++ lld/trunk/ELF/Symbols.h Tue Sep 13 19:05:51 2016
> @@ -322,7 +322,7 @@ public:
>
>    // Returns an object file for this symbol, or a nullptr if the file
>    // was already returned.
> -  std::unique_ptr<InputFile> fetch();
> +  InputFile *fetch();
>
>  protected:
>    Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
> @@ -340,7 +340,7 @@ public:
>    }
>
>    ArchiveFile *file() { return (ArchiveFile *)this->File; }
> -  std::unique_ptr<InputFile> fetch();
> +  InputFile *fetch();
>
>  private:
>    const llvm::object::Archive::Symbol Sym;
> @@ -357,7 +357,7 @@ public:
>    }
>
>    LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
> -  std::unique_ptr<InputFile> fetch();
> +  InputFile *fetch();
>  };
>
>  // Some linker-generated symbols need to be created as
>
> Modified: lld/trunk/ELF/Writer.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=281425&r1=281424&r2=281425&view=diff
>
> ==============================================================================
> --- lld/trunk/ELF/Writer.cpp (original)
> +++ lld/trunk/ELF/Writer.cpp Tue Sep 13 19:05:51 2016
> @@ -370,8 +370,7 @@ template <class ELFT> static bool includ
>  template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
>    if (!Out<ELFT>::SymTab)
>      return;
> -  for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles()) {
> +  for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
>      const char *StrTab = F->getStringTable().data();
>      for (SymbolBody *B : F->getLocalSymbols()) {
>        auto *DR = dyn_cast<DefinedRegular<ELFT>>(B);
> @@ -651,8 +650,7 @@ template <class ELFT>
>  void Writer<ELFT>::forEachRelSec(
>      std::function<void(InputSectionBase<ELFT> &, const typename
> ELFT::Shdr &)>
>          Fn) {
> -  for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles()) {
> +  for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
>      for (InputSectionBase<ELFT> *C : F->getSections()) {
>        if (isDiscarded(C))
>          continue;
> @@ -676,8 +674,7 @@ void Writer<ELFT>::forEachRelSec(
>  }
>
>  template <class ELFT> void Writer<ELFT>::createSections() {
> -  for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
> -       Symtab<ELFT>::X->getObjectFiles()) {
> +  for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
>      for (InputSectionBase<ELFT> *C : F->getSections()) {
>        if (isDiscarded(C)) {
>          reportDiscarded(C);
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160919/d2fbe5b1/attachment.html>


More information about the llvm-commits mailing list