diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2016-12-13 19:43:49 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2016-12-13 19:43:49 +0000 |
commit | ad90369a940539e3f09acafb661b070ebfd1a559 (patch) | |
tree | db21c522c88b1d284f2d3c9abeaa4e4983ae0c96 /llvm/lib/LTO/LTO.cpp | |
parent | 77c5eaaedac7dde060b72a67514c41d2fa9df466 (diff) | |
download | bcm5719-llvm-ad90369a940539e3f09acafb661b070ebfd1a559.tar.gz bcm5719-llvm-ad90369a940539e3f09acafb661b070ebfd1a559.zip |
LTO: Port the new LTO API to ModuleSymbolTable.
Differential Revision: https://reviews.llvm.org/D27077
llvm-svn: 289574
Diffstat (limited to 'llvm/lib/LTO/LTO.cpp')
-rw-r--r-- | llvm/lib/LTO/LTO.cpp | 92 |
1 files changed, 48 insertions, 44 deletions
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp index 1fd2ccf7deb..8a68025121c 100644 --- a/llvm/lib/LTO/LTO.cpp +++ b/llvm/lib/LTO/LTO.cpp @@ -217,13 +217,22 @@ void llvm::thinLTOInternalizeAndPromoteInIndex( Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) { std::unique_ptr<InputFile> File(new InputFile); - Expected<std::unique_ptr<object::IRObjectFile>> IRObj = - IRObjectFile::create(Object, File->Ctx); - if (!IRObj) - return IRObj.takeError(); - File->Obj = std::move(*IRObj); - - for (const auto &C : File->Obj->getModule().getComdatSymbolTable()) { + ErrorOr<MemoryBufferRef> BCOrErr = + IRObjectFile::findBitcodeInMemBuffer(Object); + if (!BCOrErr) + return errorCodeToError(BCOrErr.getError()); + File->MBRef = *BCOrErr; + + Expected<std::unique_ptr<Module>> MOrErr = + getLazyBitcodeModule(*BCOrErr, File->Ctx, + /*ShouldLazyLoadMetadata*/ true); + if (!MOrErr) + return MOrErr.takeError(); + + File->Mod = std::move(*MOrErr); + File->SymTab.addModule(File->Mod.get()); + + for (const auto &C : File->Mod->getComdatSymbolTable()) { auto P = File->ComdatMap.insert(std::make_pair(&C.second, File->Comdats.size())); assert(P.second); @@ -235,17 +244,12 @@ Expected<std::unique_ptr<InputFile>> InputFile::create(MemoryBufferRef Object) { } Expected<int> InputFile::Symbol::getComdatIndex() const { - if (!GV) + if (!isGV()) return -1; - const GlobalObject *GO; - if (auto *GA = dyn_cast<GlobalAlias>(GV)) { - GO = GA->getBaseObject(); - if (!GO) - return make_error<StringError>("Unable to determine comdat of alias!", - inconvertibleErrorCode()); - } else { - GO = cast<GlobalObject>(GV); - } + const GlobalObject *GO = getGV()->getBaseObject(); + if (!GO) + return make_error<StringError>("Unable to determine comdat of alias!", + inconvertibleErrorCode()); if (const Comdat *C = GO->getComdat()) { auto I = File->ComdatMap.find(C); assert(I != File->ComdatMap.end()); @@ -272,11 +276,10 @@ LTO::LTO(Config Conf, ThinBackend Backend, ThinLTO(std::move(Backend)) {} // Add the given symbol to the GlobalResolutions map, and resolve its partition. -void LTO::addSymbolToGlobalRes(IRObjectFile *Obj, - SmallPtrSet<GlobalValue *, 8> &Used, +void LTO::addSymbolToGlobalRes(SmallPtrSet<GlobalValue *, 8> &Used, const InputFile::Symbol &Sym, SymbolResolution Res, unsigned Partition) { - GlobalValue *GV = Obj->getSymbolGV(Sym.I->getRawDataRefImpl()); + GlobalValue *GV = Sym.isGV() ? Sym.getGV() : nullptr; auto &GlobalRes = GlobalResolutions[Sym.getName()]; if (GV) { @@ -321,14 +324,13 @@ Error LTO::add(std::unique_ptr<InputFile> Input, writeToResolutionFile(*Conf.ResolutionFile, Input.get(), Res); // FIXME: move to backend - Module &M = Input->Obj->getModule(); + Module &M = *Input->Mod; if (!Conf.OverrideTriple.empty()) M.setTargetTriple(Conf.OverrideTriple); else if (M.getTargetTriple().empty()) M.setTargetTriple(Conf.DefaultTriple); - MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef(); - Expected<bool> HasThinLTOSummary = hasGlobalValueSummary(MBRef); + Expected<bool> HasThinLTOSummary = hasGlobalValueSummary(Input->MBRef); if (!HasThinLTOSummary) return HasThinLTOSummary.takeError(); @@ -346,17 +348,20 @@ Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input, llvm::make_unique<Module>("ld-temp.o", RegularLTO.Ctx); RegularLTO.Mover = llvm::make_unique<IRMover>(*RegularLTO.CombinedModule); } - Expected<std::unique_ptr<object::IRObjectFile>> ObjOrErr = - IRObjectFile::create(Input->Obj->getMemoryBufferRef(), RegularLTO.Ctx); - if (!ObjOrErr) - return ObjOrErr.takeError(); - std::unique_ptr<object::IRObjectFile> Obj = std::move(*ObjOrErr); + Expected<std::unique_ptr<Module>> MOrErr = + getLazyBitcodeModule(Input->MBRef, RegularLTO.Ctx, + /*ShouldLazyLoadMetadata*/ true); + if (!MOrErr) + return MOrErr.takeError(); - Module &M = Obj->getModule(); + Module &M = **MOrErr; if (Error Err = M.materializeMetadata()) return Err; UpgradeDebugInfo(M); + ModuleSymbolTable SymTab; + SymTab.addModule(&M); + SmallPtrSet<GlobalValue *, 8> Used; collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); @@ -368,16 +373,18 @@ Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input, auto ResI = Res.begin(); for (const InputFile::Symbol &Sym : - make_range(InputFile::symbol_iterator(Obj->symbol_begin(), nullptr), - InputFile::symbol_iterator(Obj->symbol_end(), nullptr))) { + make_range(InputFile::symbol_iterator(SymTab.symbols().begin(), SymTab, + nullptr), + InputFile::symbol_iterator(SymTab.symbols().end(), SymTab, + nullptr))) { assert(ResI != Res.end()); SymbolResolution Res = *ResI++; - addSymbolToGlobalRes(Obj.get(), Used, Sym, Res, 0); + addSymbolToGlobalRes(Used, Sym, Res, 0); - GlobalValue *GV = Obj->getSymbolGV(Sym.I->getRawDataRefImpl()); if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) continue; - if (Res.Prevailing && GV) { + if (Res.Prevailing && Sym.isGV()) { + GlobalValue *GV = Sym.getGV(); Keep.push_back(GV); switch (GV->getLinkage()) { default: @@ -396,8 +403,7 @@ Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input, if (Sym.getFlags() & object::BasicSymbolRef::SF_Common) { // FIXME: We should figure out what to do about commons defined by asm. // For now they aren't reported correctly by ModuleSymbolTable. - assert(GV); - auto &CommonRes = RegularLTO.Commons[GV->getName()]; + auto &CommonRes = RegularLTO.Commons[Sym.getGV()->getName()]; CommonRes.Size = std::max(CommonRes.Size, Sym.getCommonSize()); CommonRes.Align = std::max(CommonRes.Align, Sym.getCommonAlignment()); CommonRes.Prevailing |= Res.Prevailing; @@ -407,7 +413,7 @@ Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input, } assert(ResI == Res.end()); - return RegularLTO.Mover->move(Obj->takeModule(), Keep, + return RegularLTO.Mover->move(std::move(*MOrErr), Keep, [](GlobalValue &, IRMover::ValueAdder) {}, /* LinkModuleInlineAsm */ true, /* IsPerformingImport */ false); @@ -416,11 +422,11 @@ Error LTO::addRegularLTO(std::unique_ptr<InputFile> Input, // Add a ThinLTO object to the link. Error LTO::addThinLTO(std::unique_ptr<InputFile> Input, ArrayRef<SymbolResolution> Res) { - Module &M = Input->Obj->getModule(); + Module &M = *Input->Mod; SmallPtrSet<GlobalValue *, 8> Used; collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); - MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef(); + MemoryBufferRef MBRef = Input->MBRef; Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> SummaryObjOrErr = object::ModuleSummaryIndexObjectFile::create(MBRef); if (!SummaryObjOrErr) @@ -432,12 +438,10 @@ Error LTO::addThinLTO(std::unique_ptr<InputFile> Input, for (const InputFile::Symbol &Sym : Input->symbols()) { assert(ResI != Res.end()); SymbolResolution Res = *ResI++; - addSymbolToGlobalRes(Input->Obj.get(), Used, Sym, Res, - ThinLTO.ModuleMap.size() + 1); + addSymbolToGlobalRes(Used, Sym, Res, ThinLTO.ModuleMap.size() + 1); - GlobalValue *GV = Input->Obj->getSymbolGV(Sym.I->getRawDataRefImpl()); - if (Res.Prevailing && GV) - ThinLTO.PrevailingModuleForGUID[GV->getGUID()] = + if (Res.Prevailing && Sym.isGV()) + ThinLTO.PrevailingModuleForGUID[Sym.getGV()->getGUID()] = MBRef.getBufferIdentifier(); } assert(ResI == Res.end()); |