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 "SymbolTable.h" 13 #include "Symbols.h" 14 #include "lld/Common/Args.h" 15 #include "lld/Common/CommonLinkerContext.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/Filesystem.h" 18 #include "lld/Common/Strings.h" 19 #include "lld/Common/TargetOptionsCommandFlags.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/ADT/Twine.h" 23 #include "llvm/BinaryFormat/ELF.h" 24 #include "llvm/Bitcode/BitcodeWriter.h" 25 #include "llvm/LTO/Config.h" 26 #include "llvm/LTO/LTO.h" 27 #include "llvm/Support/Caching.h" 28 #include "llvm/Support/CodeGen.h" 29 #include "llvm/Support/Error.h" 30 #include "llvm/Support/FileSystem.h" 31 #include "llvm/Support/MemoryBuffer.h" 32 #include "llvm/Support/Path.h" 33 #include <algorithm> 34 #include <cstddef> 35 #include <memory> 36 #include <string> 37 #include <system_error> 38 #include <vector> 39 40 using namespace llvm; 41 using namespace llvm::object; 42 using namespace llvm::ELF; 43 using namespace lld; 44 using namespace lld::elf; 45 46 static std::string getThinLTOOutputFile(Ctx &ctx, StringRef modulePath) { 47 return lto::getThinLTOOutputFile(modulePath, ctx.arg.thinLTOPrefixReplaceOld, 48 ctx.arg.thinLTOPrefixReplaceNew); 49 } 50 51 static lto::Config createConfig(Ctx &ctx) { 52 lto::Config c; 53 54 // LLD supports the new relocations and address-significance tables. 55 c.Options = initTargetOptionsFromCodeGenFlags(); 56 c.Options.EmitAddrsig = true; 57 for (StringRef C : ctx.arg.mllvmOpts) 58 c.MllvmArgs.emplace_back(C.str()); 59 60 // Always emit a section per function/datum with LTO. 61 c.Options.FunctionSections = true; 62 c.Options.DataSections = true; 63 64 // Check if basic block sections must be used. 65 // Allowed values for --lto-basic-block-sections are "all", 66 // "<file name specifying basic block ids>", or none. This is the equivalent 67 // of -fbasic-block-sections= flag in clang. 68 if (!ctx.arg.ltoBasicBlockSections.empty()) { 69 if (ctx.arg.ltoBasicBlockSections == "all") { 70 c.Options.BBSections = BasicBlockSection::All; 71 } else if (ctx.arg.ltoBasicBlockSections == "labels") { 72 c.Options.BBAddrMap = true; 73 Warn(ctx) 74 << "'--lto-basic-block-sections=labels' is deprecated; Please use " 75 "'--lto-basic-block-address-map' instead"; 76 } else if (ctx.arg.ltoBasicBlockSections == "none") { 77 c.Options.BBSections = BasicBlockSection::None; 78 } else { 79 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 80 MemoryBuffer::getFile(ctx.arg.ltoBasicBlockSections.str()); 81 if (!MBOrErr) { 82 ErrAlways(ctx) << "cannot open " << ctx.arg.ltoBasicBlockSections << ":" 83 << MBOrErr.getError().message(); 84 } else { 85 c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr); 86 } 87 c.Options.BBSections = BasicBlockSection::List; 88 } 89 } 90 91 c.Options.BBAddrMap = ctx.arg.ltoBBAddrMap; 92 93 c.Options.UniqueBasicBlockSectionNames = 94 ctx.arg.ltoUniqueBasicBlockSectionNames; 95 96 if (auto relocModel = getRelocModelFromCMModel()) 97 c.RelocModel = *relocModel; 98 else if (ctx.arg.relocatable) 99 c.RelocModel = std::nullopt; 100 else if (ctx.arg.isPic) 101 c.RelocModel = Reloc::PIC_; 102 else 103 c.RelocModel = Reloc::Static; 104 105 c.CodeModel = getCodeModelFromCMModel(); 106 c.DisableVerify = ctx.arg.disableVerify; 107 c.DiagHandler = diagnosticHandler; 108 c.OptLevel = ctx.arg.ltoo; 109 c.CPU = getCPUStr(); 110 c.MAttrs = getMAttrs(); 111 c.CGOptLevel = ctx.arg.ltoCgo; 112 113 c.PTO.LoopVectorization = c.OptLevel > 1; 114 c.PTO.SLPVectorization = c.OptLevel > 1; 115 116 // Set up a custom pipeline if we've been asked to. 117 c.OptPipeline = std::string(ctx.arg.ltoNewPmPasses); 118 c.AAPipeline = std::string(ctx.arg.ltoAAPipeline); 119 120 // Set up optimization remarks if we've been asked to. 121 c.RemarksFilename = std::string(ctx.arg.optRemarksFilename); 122 c.RemarksPasses = std::string(ctx.arg.optRemarksPasses); 123 c.RemarksWithHotness = ctx.arg.optRemarksWithHotness; 124 c.RemarksHotnessThreshold = ctx.arg.optRemarksHotnessThreshold; 125 c.RemarksFormat = std::string(ctx.arg.optRemarksFormat); 126 127 // Set up output file to emit statistics. 128 c.StatsFile = std::string(ctx.arg.optStatsFilename); 129 130 c.SampleProfile = std::string(ctx.arg.ltoSampleProfile); 131 for (StringRef pluginFn : ctx.arg.passPlugins) 132 c.PassPlugins.push_back(std::string(pluginFn)); 133 c.DebugPassManager = ctx.arg.ltoDebugPassManager; 134 c.DwoDir = std::string(ctx.arg.dwoDir); 135 136 c.HasWholeProgramVisibility = ctx.arg.ltoWholeProgramVisibility; 137 c.ValidateAllVtablesHaveTypeInfos = 138 ctx.arg.ltoValidateAllVtablesHaveTypeInfos; 139 c.AllVtablesHaveTypeInfos = ctx.ltoAllVtablesHaveTypeInfos; 140 c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty(); 141 c.KeepSymbolNameCopies = false; 142 143 for (const llvm::StringRef &name : ctx.arg.thinLTOModulesToCompile) 144 c.ThinLTOModulesToCompile.emplace_back(name); 145 146 c.TimeTraceEnabled = ctx.arg.timeTraceEnabled; 147 c.TimeTraceGranularity = ctx.arg.timeTraceGranularity; 148 149 c.CSIRProfile = std::string(ctx.arg.ltoCSProfileFile); 150 c.RunCSIRInstr = ctx.arg.ltoCSProfileGenerate; 151 c.PGOWarnMismatch = ctx.arg.ltoPGOWarnMismatch; 152 153 if (ctx.arg.emitLLVM) { 154 c.PreCodeGenModuleHook = [&ctx](size_t task, const Module &m) { 155 if (std::unique_ptr<raw_fd_ostream> os = 156 openLTOOutputFile(ctx.arg.outputFile)) 157 WriteBitcodeToFile(m, *os, false); 158 return false; 159 }; 160 } 161 162 if (ctx.arg.ltoEmitAsm) { 163 c.CGFileType = CodeGenFileType::AssemblyFile; 164 c.Options.MCOptions.AsmVerbose = true; 165 } 166 167 if (!ctx.arg.saveTempsArgs.empty()) 168 checkError(ctx.e, c.addSaveTemps(ctx.arg.outputFile.str() + ".", 169 /*UseInputModulePath*/ true, 170 ctx.arg.saveTempsArgs)); 171 return c; 172 } 173 174 BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) { 175 // Initialize indexFile. 176 if (!ctx.arg.thinLTOIndexOnlyArg.empty()) 177 indexFile = openFile(ctx.arg.thinLTOIndexOnlyArg); 178 179 // Initialize ltoObj. 180 lto::ThinBackend backend; 181 auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); }; 182 if (ctx.arg.thinLTOIndexOnly) { 183 backend = lto::createWriteIndexesThinBackend( 184 llvm::hardware_concurrency(ctx.arg.thinLTOJobs), 185 std::string(ctx.arg.thinLTOPrefixReplaceOld), 186 std::string(ctx.arg.thinLTOPrefixReplaceNew), 187 std::string(ctx.arg.thinLTOPrefixReplaceNativeObject), 188 ctx.arg.thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite); 189 } else { 190 backend = lto::createInProcessThinBackend( 191 llvm::heavyweight_hardware_concurrency(ctx.arg.thinLTOJobs), 192 onIndexWrite, ctx.arg.thinLTOEmitIndexFiles, 193 ctx.arg.thinLTOEmitImportsFiles); 194 } 195 196 constexpr llvm::lto::LTO::LTOKind ltoModes[3] = 197 {llvm::lto::LTO::LTOKind::LTOK_UnifiedThin, 198 llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular, 199 llvm::lto::LTO::LTOKind::LTOK_Default}; 200 ltoObj = std::make_unique<lto::LTO>(createConfig(ctx), backend, 201 ctx.arg.ltoPartitions, 202 ltoModes[ctx.arg.ltoKind]); 203 204 // Initialize usedStartStop. 205 if (ctx.bitcodeFiles.empty()) 206 return; 207 for (Symbol *sym : ctx.symtab->getSymbols()) { 208 if (sym->isPlaceholder()) 209 continue; 210 StringRef s = sym->getName(); 211 for (StringRef prefix : {"__start_", "__stop_"}) 212 if (s.starts_with(prefix)) 213 usedStartStop.insert(s.substr(prefix.size())); 214 } 215 } 216 217 BitcodeCompiler::~BitcodeCompiler() = default; 218 219 void BitcodeCompiler::add(BitcodeFile &f) { 220 lto::InputFile &obj = *f.obj; 221 bool isExec = !ctx.arg.shared && !ctx.arg.relocatable; 222 223 if (ctx.arg.thinLTOEmitIndexFiles) 224 thinIndices.insert(obj.getName()); 225 226 ArrayRef<Symbol *> syms = f.getSymbols(); 227 ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols(); 228 std::vector<lto::SymbolResolution> resols(syms.size()); 229 230 // Provide a resolution to the LTO API for each symbol. 231 for (size_t i = 0, e = syms.size(); i != e; ++i) { 232 Symbol *sym = syms[i]; 233 const lto::InputFile::Symbol &objSym = objSyms[i]; 234 lto::SymbolResolution &r = resols[i]; 235 236 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 237 // reports two symbols for module ASM defined. Without this check, lld 238 // flags an undefined in IR with a definition in ASM as prevailing. 239 // Once IRObjectFile is fixed to report only one symbol this hack can 240 // be removed. 241 r.Prevailing = !objSym.isUndefined() && sym->file == &f; 242 243 // We ask LTO to preserve following global symbols: 244 // 1) All symbols when doing relocatable link, so that them can be used 245 // for doing final link. 246 // 2) Symbols that are used in regular objects. 247 // 3) C named sections if we have corresponding __start_/__stop_ symbol. 248 // 4) Symbols that are defined in bitcode files and used for dynamic 249 // linking. 250 // 5) Symbols that will be referenced after linker wrapping is performed. 251 r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj || 252 sym->referencedAfterWrap || 253 (r.Prevailing && sym->isExported) || 254 usedStartStop.count(objSym.getSectionName()); 255 // Identify symbols exported dynamically, and that therefore could be 256 // referenced by a shared library not visible to the linker. 257 r.ExportDynamic = sym->computeBinding(ctx) != STB_LOCAL && 258 (ctx.arg.exportDynamic || sym->isExported); 259 const auto *dr = dyn_cast<Defined>(sym); 260 r.FinalDefinitionInLinkageUnit = 261 (isExec || sym->visibility() != STV_DEFAULT) && dr && 262 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations 263 // will be generated by for them, triggering linker errors. 264 // Symbol section is always null for bitcode symbols, hence the check 265 // for isElf(). Skip linker script defined symbols as well: they have 266 // no File defined. 267 !(dr->section == nullptr && 268 (sym->file->isInternal() || sym->file->isElf())); 269 270 if (r.Prevailing) 271 Undefined(ctx.internalFile, StringRef(), STB_GLOBAL, STV_DEFAULT, 272 sym->type) 273 .overwrite(*sym); 274 275 // We tell LTO to not apply interprocedural optimization for wrapped 276 // (with --wrap) symbols because otherwise LTO would inline them while 277 // their values are still not final. 278 r.LinkerRedefined = sym->scriptDefined; 279 } 280 checkError(ctx.e, ltoObj->add(std::move(f.obj), resols)); 281 } 282 283 // If LazyObjFile has not been added to link, emit empty index files. 284 // This is needed because this is what GNU gold plugin does and we have a 285 // distributed build system that depends on that behavior. 286 static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) { 287 DenseSet<StringRef> linkedBitCodeFiles; 288 for (BitcodeFile *f : ctx.bitcodeFiles) 289 linkedBitCodeFiles.insert(f->getName()); 290 291 for (BitcodeFile *f : ctx.lazyBitcodeFiles) { 292 if (!f->lazy) 293 continue; 294 if (linkedBitCodeFiles.contains(f->getName())) 295 continue; 296 std::string path = 297 replaceThinLTOSuffix(ctx, getThinLTOOutputFile(ctx, f->obj->getName())); 298 std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc"); 299 if (!os) 300 continue; 301 302 ModuleSummaryIndex m(/*HaveGVs*/ false); 303 m.setSkipModuleByDistributedBackend(); 304 writeIndexToFile(m, *os); 305 if (ctx.arg.thinLTOEmitImportsFiles) 306 openFile(path + ".imports"); 307 } 308 } 309 310 // Merge all the bitcode files we have seen, codegen the result 311 // and return the resulting ObjectFile(s). 312 SmallVector<std::unique_ptr<InputFile>, 0> BitcodeCompiler::compile() { 313 unsigned maxTasks = ltoObj->getMaxTasks(); 314 buf.resize(maxTasks); 315 files.resize(maxTasks); 316 filenames.resize(maxTasks); 317 318 // The --thinlto-cache-dir option specifies the path to a directory in which 319 // to cache native object files for ThinLTO incremental builds. If a path was 320 // specified, configure LTO to use it as the cache directory. 321 FileCache cache; 322 if (!ctx.arg.thinLTOCacheDir.empty()) 323 cache = check(localCache("ThinLTO", "Thin", ctx.arg.thinLTOCacheDir, 324 [&](size_t task, const Twine &moduleName, 325 std::unique_ptr<MemoryBuffer> mb) { 326 files[task] = std::move(mb); 327 filenames[task] = moduleName.str(); 328 })); 329 330 if (!ctx.bitcodeFiles.empty()) 331 checkError(ctx.e, ltoObj->run( 332 [&](size_t task, const Twine &moduleName) { 333 buf[task].first = moduleName.str(); 334 return std::make_unique<CachedFileStream>( 335 std::make_unique<raw_svector_ostream>( 336 buf[task].second)); 337 }, 338 cache)); 339 340 // Emit empty index files for non-indexed files but not in single-module mode. 341 if (ctx.arg.thinLTOModulesToCompile.empty()) { 342 for (StringRef s : thinIndices) { 343 std::string path = getThinLTOOutputFile(ctx, s); 344 openFile(path + ".thinlto.bc"); 345 if (ctx.arg.thinLTOEmitImportsFiles) 346 openFile(path + ".imports"); 347 } 348 } 349 350 if (ctx.arg.thinLTOEmitIndexFiles) 351 thinLTOCreateEmptyIndexFiles(ctx); 352 353 if (ctx.arg.thinLTOIndexOnly) { 354 if (!ctx.arg.ltoObjPath.empty()) 355 saveBuffer(buf[0].second, ctx.arg.ltoObjPath); 356 357 // ThinLTO with index only option is required to generate only the index 358 // files. After that, we exit from linker and ThinLTO backend runs in a 359 // distributed environment. 360 if (indexFile) 361 indexFile->close(); 362 return {}; 363 } 364 365 if (!ctx.arg.thinLTOCacheDir.empty()) 366 pruneCache(ctx.arg.thinLTOCacheDir, ctx.arg.thinLTOCachePolicy, files); 367 368 if (!ctx.arg.ltoObjPath.empty()) { 369 saveBuffer(buf[0].second, ctx.arg.ltoObjPath); 370 for (unsigned i = 1; i != maxTasks; ++i) 371 saveBuffer(buf[i].second, ctx.arg.ltoObjPath + Twine(i)); 372 } 373 374 bool savePrelink = ctx.arg.saveTempsArgs.contains("prelink"); 375 SmallVector<std::unique_ptr<InputFile>, 0> ret; 376 const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o"; 377 for (unsigned i = 0; i != maxTasks; ++i) { 378 StringRef bitcodeFilePath; 379 StringRef objBuf; 380 if (files[i]) { 381 // When files[i] is not null, we get the native relocatable file from the 382 // cache. filenames[i] contains the original BitcodeFile's identifier. 383 objBuf = files[i]->getBuffer(); 384 bitcodeFilePath = filenames[i]; 385 } else { 386 // Get the native relocatable file after in-process LTO compilation. 387 objBuf = buf[i].second; 388 bitcodeFilePath = buf[i].first; 389 } 390 if (objBuf.empty()) 391 continue; 392 393 // If the input bitcode file is path/to/x.o and -o specifies a.out, the 394 // corresponding native relocatable file path will look like: 395 // path/to/a.out.lto.x.o. 396 StringRef ltoObjName; 397 if (bitcodeFilePath == "ld-temp.o") { 398 ltoObjName = 399 ctx.saver.save(Twine(ctx.arg.outputFile) + ".lto" + 400 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext); 401 } else { 402 StringRef directory = sys::path::parent_path(bitcodeFilePath); 403 // For an archive member, which has an identifier like "d/a.a(coll.o at 404 // 8)" (see BitcodeFile::BitcodeFile), use the filename; otherwise, use 405 // the stem (d/a.o => a). 406 StringRef baseName = bitcodeFilePath.ends_with(")") 407 ? sys::path::filename(bitcodeFilePath) 408 : sys::path::stem(bitcodeFilePath); 409 StringRef outputFileBaseName = sys::path::filename(ctx.arg.outputFile); 410 SmallString<256> path; 411 sys::path::append(path, directory, 412 outputFileBaseName + ".lto." + baseName + ext); 413 sys::path::remove_dots(path, true); 414 ltoObjName = ctx.saver.save(path.str()); 415 } 416 if (savePrelink || ctx.arg.ltoEmitAsm) 417 saveBuffer(buf[i].second, ltoObjName); 418 if (!ctx.arg.ltoEmitAsm) 419 ret.push_back(createObjFile(ctx, MemoryBufferRef(objBuf, ltoObjName))); 420 } 421 return ret; 422 } 423