1 //===- LTO.cpp ------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "LTO.h" 10 #include "Config.h" 11 #include "InputFiles.h" 12 #include "LinkerScript.h" 13 #include "SymbolTable.h" 14 #include "Symbols.h" 15 #include "lld/Common/Args.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/TargetOptionsCommandFlags.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallString.h" 20 #include "llvm/ADT/StringRef.h" 21 #include "llvm/ADT/Twine.h" 22 #include "llvm/BinaryFormat/ELF.h" 23 #include "llvm/Bitcode/BitcodeReader.h" 24 #include "llvm/Bitcode/BitcodeWriter.h" 25 #include "llvm/IR/DiagnosticPrinter.h" 26 #include "llvm/LTO/Caching.h" 27 #include "llvm/LTO/Config.h" 28 #include "llvm/LTO/LTO.h" 29 #include "llvm/Object/SymbolicFile.h" 30 #include "llvm/Support/CodeGen.h" 31 #include "llvm/Support/Error.h" 32 #include "llvm/Support/FileSystem.h" 33 #include "llvm/Support/MemoryBuffer.h" 34 #include <algorithm> 35 #include <cstddef> 36 #include <memory> 37 #include <string> 38 #include <system_error> 39 #include <vector> 40 41 using namespace llvm; 42 using namespace llvm::object; 43 using namespace llvm::ELF; 44 45 using namespace lld; 46 using namespace lld::elf; 47 48 // Creates an empty file to store a list of object files for final 49 // linking of distributed ThinLTO. 50 static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) { 51 std::error_code EC; 52 auto Ret = 53 llvm::make_unique<raw_fd_ostream>(File, EC, sys::fs::OpenFlags::F_None); 54 if (EC) { 55 error("cannot open " + File + ": " + EC.message()); 56 return nullptr; 57 } 58 return Ret; 59 } 60 61 static std::string getThinLTOOutputFile(StringRef ModulePath) { 62 return lto::getThinLTOOutputFile(ModulePath, 63 Config->ThinLTOPrefixReplace.first, 64 Config->ThinLTOPrefixReplace.second); 65 } 66 67 static lto::Config createConfig() { 68 lto::Config C; 69 70 // LLD supports the new relocations and address-significance tables. 71 C.Options = initTargetOptionsFromCodeGenFlags(); 72 C.Options.RelaxELFRelocations = true; 73 C.Options.EmitAddrsig = true; 74 75 // Always emit a section per function/datum with LTO. 76 C.Options.FunctionSections = true; 77 C.Options.DataSections = true; 78 79 if (Config->Relocatable) 80 C.RelocModel = None; 81 else if (Config->Pic) 82 C.RelocModel = Reloc::PIC_; 83 else 84 C.RelocModel = Reloc::Static; 85 86 C.CodeModel = getCodeModelFromCMModel(); 87 C.DisableVerify = Config->DisableVerify; 88 C.DiagHandler = diagnosticHandler; 89 C.OptLevel = Config->LTOO; 90 C.CPU = getCPUStr(); 91 C.MAttrs = getMAttrs(); 92 C.CGOptLevel = args::getCGOptLevel(Config->LTOO); 93 94 // Set up a custom pipeline if we've been asked to. 95 C.OptPipeline = Config->LTONewPmPasses; 96 C.AAPipeline = Config->LTOAAPipeline; 97 98 // Set up optimization remarks if we've been asked to. 99 C.RemarksFilename = Config->OptRemarksFilename; 100 C.RemarksPasses = Config->OptRemarksPasses; 101 C.RemarksWithHotness = Config->OptRemarksWithHotness; 102 103 C.SampleProfile = Config->LTOSampleProfile; 104 C.UseNewPM = Config->LTONewPassManager; 105 C.DebugPassManager = Config->LTODebugPassManager; 106 C.DwoDir = Config->DwoDir; 107 108 C.CSIRProfile = Config->LTOCSProfileFile; 109 C.RunCSIRInstr = Config->LTOCSProfileGenerate; 110 111 if (Config->EmitLLVM) { 112 C.PostInternalizeModuleHook = [](size_t Task, const Module &M) { 113 if (std::unique_ptr<raw_fd_ostream> OS = openFile(Config->OutputFile)) 114 WriteBitcodeToFile(M, *OS, false); 115 return false; 116 }; 117 } 118 119 if (Config->SaveTemps) 120 checkError(C.addSaveTemps(Config->OutputFile.str() + ".", 121 /*UseInputModulePath*/ true)); 122 return C; 123 } 124 125 BitcodeCompiler::BitcodeCompiler() { 126 // Initialize IndexFile. 127 if (!Config->ThinLTOIndexOnlyArg.empty()) 128 IndexFile = openFile(Config->ThinLTOIndexOnlyArg); 129 130 // Initialize LTOObj. 131 lto::ThinBackend Backend; 132 if (Config->ThinLTOIndexOnly) { 133 auto OnIndexWrite = [&](StringRef S) { ThinIndices.erase(S); }; 134 Backend = lto::createWriteIndexesThinBackend( 135 Config->ThinLTOPrefixReplace.first, Config->ThinLTOPrefixReplace.second, 136 Config->ThinLTOEmitImportsFiles, IndexFile.get(), OnIndexWrite); 137 } else if (Config->ThinLTOJobs != -1U) { 138 Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs); 139 } 140 141 LTOObj = llvm::make_unique<lto::LTO>(createConfig(), Backend, 142 Config->LTOPartitions); 143 144 // Initialize UsedStartStop. 145 for (Symbol *Sym : Symtab->getSymbols()) { 146 StringRef S = Sym->getName(); 147 for (StringRef Prefix : {"__start_", "__stop_"}) 148 if (S.startswith(Prefix)) 149 UsedStartStop.insert(S.substr(Prefix.size())); 150 } 151 } 152 153 BitcodeCompiler::~BitcodeCompiler() = default; 154 155 void BitcodeCompiler::add(BitcodeFile &F) { 156 lto::InputFile &Obj = *F.Obj; 157 bool IsExec = !Config->Shared && !Config->Relocatable; 158 159 if (Config->ThinLTOIndexOnly) 160 ThinIndices.insert(Obj.getName()); 161 162 ArrayRef<Symbol *> Syms = F.getSymbols(); 163 ArrayRef<lto::InputFile::Symbol> ObjSyms = Obj.symbols(); 164 std::vector<lto::SymbolResolution> Resols(Syms.size()); 165 166 // Provide a resolution to the LTO API for each symbol. 167 for (size_t I = 0, E = Syms.size(); I != E; ++I) { 168 Symbol *Sym = Syms[I]; 169 const lto::InputFile::Symbol &ObjSym = ObjSyms[I]; 170 lto::SymbolResolution &R = Resols[I]; 171 172 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 173 // reports two symbols for module ASM defined. Without this check, lld 174 // flags an undefined in IR with a definition in ASM as prevailing. 175 // Once IRObjectFile is fixed to report only one symbol this hack can 176 // be removed. 177 R.Prevailing = !ObjSym.isUndefined() && Sym->File == &F; 178 179 // We ask LTO to preserve following global symbols: 180 // 1) All symbols when doing relocatable link, so that them can be used 181 // for doing final link. 182 // 2) Symbols that are used in regular objects. 183 // 3) C named sections if we have corresponding __start_/__stop_ symbol. 184 // 4) Symbols that are defined in bitcode files and used for dynamic linking. 185 R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj || 186 (R.Prevailing && Sym->includeInDynsym()) || 187 UsedStartStop.count(ObjSym.getSectionName()); 188 const auto *DR = dyn_cast<Defined>(Sym); 189 R.FinalDefinitionInLinkageUnit = 190 (IsExec || Sym->Visibility != STV_DEFAULT) && DR && 191 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations 192 // will be generated by for them, triggering linker errors. 193 // Symbol section is always null for bitcode symbols, hence the check 194 // for isElf(). Skip linker script defined symbols as well: they have 195 // no File defined. 196 !(DR->Section == nullptr && (!Sym->File || Sym->File->isElf())); 197 198 if (R.Prevailing) 199 Sym->replace(Undefined{nullptr, Sym->getName(), STB_GLOBAL, STV_DEFAULT, 200 Sym->Type}); 201 202 // We tell LTO to not apply interprocedural optimization for wrapped 203 // (with --wrap) symbols because otherwise LTO would inline them while 204 // their values are still not final. 205 R.LinkerRedefined = !Sym->CanInline; 206 } 207 checkError(LTOObj->add(std::move(F.Obj), Resols)); 208 } 209 210 // If LazyObjFile has not been added to link, emit empty index files. 211 // This is needed because this is what GNU gold plugin does and we have a 212 // distributed build system that depends on that behavior. 213 static void thinLTOCreateEmptyIndexFiles() { 214 for (LazyObjFile *F : LazyObjFiles) { 215 if (!isBitcode(F->MB)) 216 continue; 217 std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(F->getName())); 218 std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc"); 219 if (!OS) 220 continue; 221 222 ModuleSummaryIndex M(/*HaveGVs*/ false); 223 M.setSkipModuleByDistributedBackend(); 224 WriteIndexToFile(M, *OS); 225 if (Config->ThinLTOEmitImportsFiles) 226 openFile(Path + ".imports"); 227 } 228 } 229 230 // Merge all the bitcode files we have seen, codegen the result 231 // and return the resulting ObjectFile(s). 232 std::vector<InputFile *> BitcodeCompiler::compile() { 233 unsigned MaxTasks = LTOObj->getMaxTasks(); 234 Buf.resize(MaxTasks); 235 Files.resize(MaxTasks); 236 237 // The --thinlto-cache-dir option specifies the path to a directory in which 238 // to cache native object files for ThinLTO incremental builds. If a path was 239 // specified, configure LTO to use it as the cache directory. 240 lto::NativeObjectCache Cache; 241 if (!Config->ThinLTOCacheDir.empty()) 242 Cache = check( 243 lto::localCache(Config->ThinLTOCacheDir, 244 [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) { 245 Files[Task] = std::move(MB); 246 })); 247 248 if (!BitcodeFiles.empty()) 249 checkError(LTOObj->run( 250 [&](size_t Task) { 251 return llvm::make_unique<lto::NativeObjectStream>( 252 llvm::make_unique<raw_svector_ostream>(Buf[Task])); 253 }, 254 Cache)); 255 256 // Emit empty index files for non-indexed files 257 for (StringRef S : ThinIndices) { 258 std::string Path = getThinLTOOutputFile(S); 259 openFile(Path + ".thinlto.bc"); 260 if (Config->ThinLTOEmitImportsFiles) 261 openFile(Path + ".imports"); 262 } 263 264 if (Config->ThinLTOIndexOnly) { 265 thinLTOCreateEmptyIndexFiles(); 266 267 if (!Config->LTOObjPath.empty()) 268 saveBuffer(Buf[0], Config->LTOObjPath); 269 270 // ThinLTO with index only option is required to generate only the index 271 // files. After that, we exit from linker and ThinLTO backend runs in a 272 // distributed environment. 273 if (IndexFile) 274 IndexFile->close(); 275 return {}; 276 } 277 278 if (!Config->ThinLTOCacheDir.empty()) 279 pruneCache(Config->ThinLTOCacheDir, Config->ThinLTOCachePolicy); 280 281 if (!Config->LTOObjPath.empty()) { 282 saveBuffer(Buf[0], Config->LTOObjPath); 283 for (unsigned I = 1; I != MaxTasks; ++I) 284 saveBuffer(Buf[I], Config->LTOObjPath + Twine(I)); 285 } 286 287 if (Config->SaveTemps) { 288 saveBuffer(Buf[0], Config->OutputFile + ".lto.o"); 289 for (unsigned I = 1; I != MaxTasks; ++I) 290 saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.o"); 291 } 292 293 std::vector<InputFile *> Ret; 294 for (unsigned I = 0; I != MaxTasks; ++I) 295 if (!Buf[I].empty()) 296 Ret.push_back(createObjectFile(MemoryBufferRef(Buf[I], "lto.tmp"))); 297 298 for (std::unique_ptr<MemoryBuffer> &File : Files) 299 if (File) 300 Ret.push_back(createObjectFile(*File)); 301 return Ret; 302 } 303