1 //===- LTO.cpp ------------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "LTO.h" 11 #include "Config.h" 12 #include "Error.h" 13 #include "InputFiles.h" 14 #include "Symbols.h" 15 #include "lld/Core/TargetOptionsCommandFlags.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/ADT/Twine.h" 20 #include "llvm/IR/DiagnosticPrinter.h" 21 #include "llvm/LTO/Config.h" 22 #include "llvm/LTO/LTO.h" 23 #include "llvm/Object/SymbolicFile.h" 24 #include "llvm/Support/CodeGen.h" 25 #include "llvm/Support/Error.h" 26 #include "llvm/Support/FileSystem.h" 27 #include "llvm/Support/MemoryBuffer.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <algorithm> 30 #include <cstddef> 31 #include <memory> 32 #include <string> 33 #include <system_error> 34 #include <vector> 35 36 using namespace llvm; 37 using namespace llvm::object; 38 39 using namespace lld; 40 using namespace lld::coff; 41 42 static void diagnosticHandler(const DiagnosticInfo &DI) { 43 SmallString<128> ErrStorage; 44 raw_svector_ostream OS(ErrStorage); 45 DiagnosticPrinterRawOStream DP(OS); 46 DI.print(DP); 47 warn(ErrStorage); 48 } 49 50 static void checkError(Error E) { 51 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error { 52 error(EIB.message()); 53 return Error::success(); 54 }); 55 } 56 57 static void saveBuffer(StringRef Buffer, const Twine &Path) { 58 std::error_code EC; 59 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None); 60 if (EC) 61 error("cannot create " + Path + ": " + EC.message()); 62 OS << Buffer; 63 } 64 65 static std::unique_ptr<lto::LTO> createLTO() { 66 lto::Config Conf; 67 Conf.Options = InitTargetOptionsFromCodeGenFlags(); 68 Conf.RelocModel = Reloc::PIC_; 69 Conf.DisableVerify = true; 70 Conf.DiagHandler = diagnosticHandler; 71 Conf.OptLevel = Config->LTOOptLevel; 72 if (Config->SaveTemps) 73 checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".", 74 /*UseInputModulePath*/ true)); 75 lto::ThinBackend Backend; 76 if (Config->LTOJobs != 0) 77 Backend = lto::createInProcessThinBackend(Config->LTOJobs); 78 return llvm::make_unique<lto::LTO>(std::move(Conf), Backend, 79 Config->LTOPartitions); 80 } 81 82 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {} 83 84 BitcodeCompiler::~BitcodeCompiler() = default; 85 86 static void undefine(Symbol *S) { 87 replaceBody<Undefined>(S, S->body()->getName()); 88 } 89 90 void BitcodeCompiler::add(BitcodeFile &F) { 91 lto::InputFile &Obj = *F.Obj; 92 unsigned SymNum = 0; 93 std::vector<SymbolBody *> SymBodies = F.getSymbols(); 94 std::vector<lto::SymbolResolution> Resols(SymBodies.size()); 95 96 // Provide a resolution to the LTO API for each symbol. 97 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) { 98 SymbolBody *B = SymBodies[SymNum]; 99 Symbol *Sym = B->symbol(); 100 lto::SymbolResolution &R = Resols[SymNum]; 101 ++SymNum; 102 103 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 104 // reports two symbols for module ASM defined. Without this check, lld 105 // flags an undefined in IR with a definition in ASM as prevailing. 106 // Once IRObjectFile is fixed to report only one symbol this hack can 107 // be removed. 108 R.Prevailing = !ObjSym.isUndefined() && B->getFile() == &F; 109 R.VisibleToRegularObj = Sym->IsUsedInRegularObj; 110 if (R.Prevailing) 111 undefine(Sym); 112 } 113 checkError(LTOObj->add(std::move(F.Obj), Resols)); 114 } 115 116 // Merge all the bitcode files we have seen, codegen the result 117 // and return the resulting objects. 118 std::vector<StringRef> BitcodeCompiler::compile() { 119 unsigned MaxTasks = LTOObj->getMaxTasks(); 120 Buff.resize(MaxTasks); 121 122 checkError(LTOObj->run([&](size_t Task) { 123 return llvm::make_unique<lto::NativeObjectStream>( 124 llvm::make_unique<raw_svector_ostream>(Buff[Task])); 125 })); 126 127 std::vector<StringRef> Ret; 128 for (unsigned I = 0; I != MaxTasks; ++I) { 129 if (Buff[I].empty()) 130 continue; 131 if (Config->SaveTemps) { 132 if (I == 0) 133 saveBuffer(Buff[I], Config->OutputFile + ".lto.obj"); 134 else 135 saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.obj"); 136 } 137 Ret.emplace_back(Buff[I].data(), Buff[I].size()); 138 } 139 return Ret; 140 } 141