Reduce the number of allocators.

We used to have one allocator per file, which reduces the advantage of
using an allocator in the first place.

This is a small speed up is most cases. The largest speedup was in
1.014X in chromium no-gc. The largest slowdown was scylla at 1.003X.

llvm-svn: 285205
This commit is contained in:
Rafael Espindola 2016-10-26 15:34:24 +00:00
parent 81e8b771d7
commit 5da1d88492
8 changed files with 43 additions and 37 deletions

View File

@ -137,7 +137,7 @@ void LinkerDriver::addFile(StringRef Path) {
case file_magic::archive:
if (InWholeArchive) {
for (MemoryBufferRef MB : getArchiveMembers(MBRef))
Files.push_back(createObjectFile(MB, Path));
Files.push_back(createObjectFile(Alloc, MB, Path));
return;
}
Files.push_back(new ArchiveFile(MBRef));
@ -147,13 +147,13 @@ void LinkerDriver::addFile(StringRef Path) {
error("attempted static link of dynamic object " + Path);
return;
}
Files.push_back(createSharedFile(MBRef));
Files.push_back(createSharedFile(Alloc, MBRef));
return;
default:
if (InLib)
Files.push_back(new LazyObjectFile(MBRef));
else
Files.push_back(createObjectFile(MBRef));
Files.push_back(createObjectFile(Alloc, MBRef));
}
}

View File

@ -140,8 +140,8 @@ template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
}
template <class ELFT>
elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
: ELFFileBase<ELFT>(Base::ObjectKind, M) {}
elf::ObjectFile<ELFT>::ObjectFile(BumpPtrAllocator &Alloc, MemoryBufferRef M)
: ELFFileBase<ELFT>(Base::ObjectKind, M), Alloc(Alloc) {}
template <class ELFT>
ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
@ -547,7 +547,7 @@ ArchiveFile::getMember(const Archive::Symbol *Sym) {
}
template <class ELFT>
SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
SharedFile<ELFT>::SharedFile(BumpPtrAllocator &Alloc, MemoryBufferRef M)
: ELFFileBase<ELFT>(Base::SharedKind, M), AsNeeded(Config->AsNeeded) {}
template <class ELFT>
@ -793,7 +793,7 @@ void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
}
template <template <class> class T>
static InputFile *createELFFile(MemoryBufferRef MB) {
static InputFile *createELFFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB) {
unsigned char Size;
unsigned char Endian;
std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
@ -802,13 +802,13 @@ static InputFile *createELFFile(MemoryBufferRef MB) {
InputFile *Obj;
if (Size == ELFCLASS32 && Endian == ELFDATA2LSB)
Obj = new T<ELF32LE>(MB);
Obj = new T<ELF32LE>(Alloc, MB);
else if (Size == ELFCLASS32 && Endian == ELFDATA2MSB)
Obj = new T<ELF32BE>(MB);
Obj = new T<ELF32BE>(Alloc, MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2LSB)
Obj = new T<ELF64LE>(MB);
Obj = new T<ELF64LE>(Alloc, MB);
else if (Size == ELFCLASS64 && Endian == ELFDATA2MSB)
Obj = new T<ELF64BE>(MB);
Obj = new T<ELF64BE>(Alloc, MB);
else
fatal("invalid file class: " + MB.getBufferIdentifier());
@ -825,7 +825,7 @@ template <class ELFT> InputFile *BinaryFile::createELF() {
Buffer = wrapBinaryWithElfHeader<ELFT>(Blob, Filename);
return createELFFile<ObjectFile>(
MemoryBufferRef(toStringRef(Buffer), Filename));
Alloc, MemoryBufferRef(toStringRef(Buffer), Filename));
}
static bool isBitcode(MemoryBufferRef MB) {
@ -833,17 +833,18 @@ static bool isBitcode(MemoryBufferRef MB) {
return identify_magic(MB.getBuffer()) == file_magic::bitcode;
}
InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
InputFile *elf::createObjectFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB,
StringRef ArchiveName,
uint64_t OffsetInArchive) {
InputFile *F =
isBitcode(MB) ? new BitcodeFile(MB) : createELFFile<ObjectFile>(MB);
InputFile *F = isBitcode(MB) ? new BitcodeFile(MB)
: createELFFile<ObjectFile>(Alloc, MB);
F->ArchiveName = ArchiveName;
F->OffsetInArchive = OffsetInArchive;
return F;
}
InputFile *elf::createSharedFile(MemoryBufferRef MB) {
return createELFFile<SharedFile>(MB);
InputFile *elf::createSharedFile(BumpPtrAllocator &Alloc, MemoryBufferRef MB) {
return createELFFile<SharedFile>(Alloc, MB);
}
MemoryBufferRef LazyObjectFile::getBuffer() {

View File

@ -167,7 +167,7 @@ public:
ArrayRef<SymbolBody *> getLocalSymbols();
ArrayRef<SymbolBody *> getNonLocalSymbols();
explicit ObjectFile(MemoryBufferRef M);
explicit ObjectFile(llvm::BumpPtrAllocator &Alloc, MemoryBufferRef M);
void parse(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
ArrayRef<InputSectionBase<ELFT> *> getSections() const { return Sections; }
@ -202,7 +202,7 @@ public:
// SymbolBodies and Thunks for sections in this file are allocated
// using this buffer.
llvm::BumpPtrAllocator Alloc;
llvm::BumpPtrAllocator &Alloc;
// Name of source file obtained from STT_FILE symbol value,
// or empty string if there is no such symbol in object file
@ -324,7 +324,7 @@ public:
return F->kind() == Base::SharedKind;
}
explicit SharedFile(MemoryBufferRef M);
explicit SharedFile(llvm::BumpPtrAllocator &Alloc, MemoryBufferRef M);
void parseSoName();
void parseRest();
@ -356,11 +356,13 @@ public:
private:
std::vector<uint8_t> Buffer;
llvm::BumpPtrAllocator Alloc;
};
InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "",
InputFile *createObjectFile(llvm::BumpPtrAllocator &Alloc, MemoryBufferRef MB,
StringRef ArchiveName = "",
uint64_t OffsetInArchive = 0);
InputFile *createSharedFile(MemoryBufferRef MB);
InputFile *createSharedFile(llvm::BumpPtrAllocator &Alloc, MemoryBufferRef MB);
} // namespace elf
} // namespace lld

View File

@ -137,7 +137,8 @@ std::vector<InputFile *> BitcodeCompiler::compile() {
else
saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o");
}
InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp"));
InputFile *Obj =
createObjectFile(Alloc, MemoryBufferRef(Buff[I], "lto.tmp"));
Ret.push_back(Obj);
}
return Ret;

View File

@ -49,6 +49,7 @@ public:
private:
std::unique_ptr<llvm::lto::LTO> LtoObj;
std::vector<SmallString<0>> Buff;
llvm::BumpPtrAllocator Alloc;
};
}
}

View File

@ -293,7 +293,7 @@ Symbol *SymbolTable<ELFT>::addUndefined(StringRef Name, uint8_t Binding,
// its type. See also comment in addLazyArchive.
if (S->isWeak())
L->Type = Type;
else if (InputFile *F = L->fetch())
else if (InputFile *F = L->fetch(Alloc))
addFile(F);
}
return S;
@ -510,7 +510,7 @@ void SymbolTable<ELFT>::addLazyArchive(ArchiveFile *F,
}
std::pair<MemoryBufferRef, uint64_t> MBInfo = F->getMember(&Sym);
if (!MBInfo.first.getBuffer().empty())
addFile(createObjectFile(MBInfo.first, F->getName(), MBInfo.second));
addFile(createObjectFile(Alloc, MBInfo.first, F->getName(), MBInfo.second));
}
template <class ELFT>
@ -531,7 +531,7 @@ void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
} else {
MemoryBufferRef MBRef = Obj.getBuffer();
if (!MBRef.getBuffer().empty())
addFile(createObjectFile(MBRef));
addFile(createObjectFile(Alloc, MBRef));
}
}
@ -539,7 +539,7 @@ void SymbolTable<ELFT>::addLazyObject(StringRef Name, LazyObjectFile &Obj) {
template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
for (StringRef S : Config->Undefined)
if (auto *L = dyn_cast_or_null<Lazy>(find(S)))
if (InputFile *File = L->fetch())
if (InputFile *File = L->fetch(Alloc))
addFile(File);
}

View File

@ -227,10 +227,10 @@ DefinedCommon::DefinedCommon(StringRef N, uint64_t Size, uint64_t Alignment,
this->File = File;
}
InputFile *Lazy::fetch() {
InputFile *Lazy::fetch(BumpPtrAllocator &Alloc) {
if (auto *S = dyn_cast<LazyArchive>(this))
return S->fetch();
return cast<LazyObject>(this)->fetch();
return S->fetch(Alloc);
return cast<LazyObject>(this)->fetch(Alloc);
}
LazyArchive::LazyArchive(ArchiveFile &File,
@ -244,21 +244,22 @@ LazyObject::LazyObject(StringRef Name, LazyObjectFile &File, uint8_t Type)
this->File = &File;
}
InputFile *LazyArchive::fetch() {
InputFile *LazyArchive::fetch(BumpPtrAllocator &Alloc) {
std::pair<MemoryBufferRef, uint64_t> MBInfo = file()->getMember(&Sym);
// getMember returns an empty buffer if the member was already
// read from the library.
if (MBInfo.first.getBuffer().empty())
return nullptr;
return createObjectFile(MBInfo.first, file()->getName(), MBInfo.second);
return createObjectFile(Alloc, MBInfo.first, file()->getName(),
MBInfo.second);
}
InputFile *LazyObject::fetch() {
InputFile *LazyObject::fetch(BumpPtrAllocator &Alloc) {
MemoryBufferRef MBRef = file()->getBuffer();
if (MBRef.getBuffer().empty())
return nullptr;
return createObjectFile(MBRef);
return createObjectFile(Alloc, MBRef);
}
bool Symbol::includeInDynsym() const {

View File

@ -328,7 +328,7 @@ public:
// Returns an object file for this symbol, or a nullptr if the file
// was already returned.
InputFile *fetch();
InputFile *fetch(llvm::BumpPtrAllocator &Alloc);
protected:
Lazy(SymbolBody::Kind K, StringRef Name, uint8_t Type)
@ -346,7 +346,7 @@ public:
}
ArchiveFile *file() { return (ArchiveFile *)this->File; }
InputFile *fetch();
InputFile *fetch(llvm::BumpPtrAllocator &Alloc);
private:
const llvm::object::Archive::Symbol Sym;
@ -363,7 +363,7 @@ public:
}
LazyObjectFile *file() { return (LazyObjectFile *)this->File; }
InputFile *fetch();
InputFile *fetch(llvm::BumpPtrAllocator &Alloc);
};
// Some linker-generated symbols need to be created as