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/Caching.h" 22 #include "llvm/LTO/Config.h" 23 #include "llvm/LTO/LTO.h" 24 #include "llvm/Object/SymbolicFile.h" 25 #include "llvm/Support/CodeGen.h" 26 #include "llvm/Support/ELF.h" 27 #include "llvm/Support/Error.h" 28 #include "llvm/Support/FileSystem.h" 29 #include "llvm/Support/MemoryBuffer.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include <algorithm> 32 #include <cstddef> 33 #include <memory> 34 #include <string> 35 #include <system_error> 36 #include <vector> 37 38 using namespace llvm; 39 using namespace llvm::object; 40 using namespace llvm::ELF; 41 42 using namespace lld; 43 using namespace lld::elf; 44 45 // This is for use when debugging LTO. 46 static void saveBuffer(StringRef Buffer, const Twine &Path) { 47 std::error_code EC; 48 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None); 49 if (EC) 50 error("cannot create " + Path + ": " + EC.message()); 51 OS << Buffer; 52 } 53 54 static void diagnosticHandler(const DiagnosticInfo &DI) { 55 SmallString<128> ErrStorage; 56 raw_svector_ostream OS(ErrStorage); 57 DiagnosticPrinterRawOStream DP(OS); 58 DI.print(DP); 59 warn(ErrStorage); 60 } 61 62 static void checkError(Error E) { 63 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error { 64 error(EIB.message()); 65 return Error::success(); 66 }); 67 } 68 69 static std::unique_ptr<lto::LTO> createLTO() { 70 lto::Config Conf; 71 72 // LLD supports the new relocations. 73 Conf.Options = InitTargetOptionsFromCodeGenFlags(); 74 Conf.Options.RelaxELFRelocations = true; 75 76 Conf.RelocModel = Config->pic() ? Reloc::PIC_ : Reloc::Static; 77 Conf.CodeModel = GetCodeModelFromCMModel(); 78 Conf.DisableVerify = Config->DisableVerify; 79 Conf.DiagHandler = diagnosticHandler; 80 Conf.OptLevel = Config->LTOO; 81 82 // Set up a custom pipeline if we've been asked to. 83 Conf.OptPipeline = Config->LTONewPmPasses; 84 Conf.AAPipeline = Config->LTOAAPipeline; 85 86 // Set up optimization remarks if we've been asked to. 87 Conf.RemarksFilename = Config->OptRemarksFilename; 88 Conf.RemarksWithHotness = Config->OptRemarksWithHotness; 89 90 if (Config->SaveTemps) 91 checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".", 92 /*UseInputModulePath*/ true)); 93 94 lto::ThinBackend Backend; 95 if (Config->ThinLTOJobs != -1u) 96 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs); 97 return llvm::make_unique<lto::LTO>(std::move(Conf), Backend, 98 Config->LTOPartitions); 99 } 100 101 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {} 102 103 BitcodeCompiler::~BitcodeCompiler() = default; 104 105 static void undefine(Symbol *S) { 106 replaceBody<Undefined>(S, S->body()->getName(), /*IsLocal=*/false, 107 STV_DEFAULT, S->body()->Type, nullptr); 108 } 109 110 void BitcodeCompiler::add(BitcodeFile &F) { 111 lto::InputFile &Obj = *F.Obj; 112 unsigned SymNum = 0; 113 std::vector<Symbol *> Syms = F.getSymbols(); 114 std::vector<lto::SymbolResolution> Resols(Syms.size()); 115 116 // Provide a resolution to the LTO API for each symbol. 117 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) { 118 Symbol *Sym = Syms[SymNum]; 119 lto::SymbolResolution &R = Resols[SymNum]; 120 ++SymNum; 121 SymbolBody *B = Sym->body(); 122 123 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 124 // reports two symbols for module ASM defined. Without this check, lld 125 // flags an undefined in IR with a definition in ASM as prevailing. 126 // Once IRObjectFile is fixed to report only one symbol this hack can 127 // be removed. 128 R.Prevailing = 129 !(ObjSym.getFlags() & object::BasicSymbolRef::SF_Undefined) && 130 B->File == &F; 131 132 R.VisibleToRegularObj = 133 Sym->IsUsedInRegularObj || (R.Prevailing && Sym->includeInDynsym()); 134 if (R.Prevailing) 135 undefine(Sym); 136 } 137 checkError(LTOObj->add(std::move(F.Obj), Resols)); 138 } 139 140 // Merge all the bitcode files we have seen, codegen the result 141 // and return the resulting ObjectFile(s). 142 std::vector<InputFile *> BitcodeCompiler::compile() { 143 std::vector<InputFile *> Ret; 144 unsigned MaxTasks = LTOObj->getMaxTasks(); 145 Buff.resize(MaxTasks); 146 Files.resize(MaxTasks); 147 148 // The --thinlto-cache-dir option specifies the path to a directory in which 149 // to cache native object files for ThinLTO incremental builds. If a path was 150 // specified, configure LTO to use it as the cache directory. 151 lto::NativeObjectCache Cache; 152 if (!Config->ThinLTOCacheDir.empty()) 153 Cache = check(lto::localCache( 154 Config->ThinLTOCacheDir, [&](size_t Task, StringRef Path) { 155 Files[Task] = check(MemoryBuffer::getFile(Path)); 156 })); 157 158 checkError(LTOObj->run( 159 [&](size_t Task) { 160 return llvm::make_unique<lto::NativeObjectStream>( 161 llvm::make_unique<raw_svector_ostream>(Buff[Task])); 162 }, 163 Cache)); 164 165 for (unsigned I = 0; I != MaxTasks; ++I) { 166 if (Buff[I].empty()) 167 continue; 168 if (Config->SaveTemps) { 169 if (I == 0) 170 saveBuffer(Buff[I], Config->OutputFile + ".lto.o"); 171 else 172 saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.o"); 173 } 174 InputFile *Obj = createObjectFile(MemoryBufferRef(Buff[I], "lto.tmp")); 175 Ret.push_back(Obj); 176 } 177 178 for (std::unique_ptr<MemoryBuffer> &File : Files) 179 if (File) 180 Ret.push_back(createObjectFile(*File)); 181 182 return Ret; 183 } 184