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 using namespace lld; 45 using namespace lld::elf; 46 47 // Creates an empty file to store a list of object files for final 48 // linking of distributed ThinLTO. 49 static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) { 50 std::error_code ec; 51 auto ret = 52 std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None); 53 if (ec) { 54 error("cannot open " + file + ": " + ec.message()); 55 return nullptr; 56 } 57 return ret; 58 } 59 60 // The merged bitcode after LTO is large. Try opening a file stream that 61 // supports reading, seeking and writing. Such a file allows BitcodeWriter to 62 // flush buffered data to reduce memory consumption. If this fails, open a file 63 // stream that supports only write. 64 static std::unique_ptr<raw_fd_ostream> openLTOOutputFile(StringRef file) { 65 std::error_code ec; 66 std::unique_ptr<raw_fd_ostream> fs = 67 std::make_unique<raw_fd_stream>(file, ec); 68 if (!ec) 69 return fs; 70 return openFile(file); 71 } 72 73 static std::string getThinLTOOutputFile(StringRef modulePath) { 74 return lto::getThinLTOOutputFile( 75 std::string(modulePath), std::string(config->thinLTOPrefixReplace.first), 76 std::string(config->thinLTOPrefixReplace.second)); 77 } 78 79 static lto::Config createConfig() { 80 lto::Config c; 81 82 // LLD supports the new relocations and address-significance tables. 83 c.Options = initTargetOptionsFromCodeGenFlags(); 84 c.Options.RelaxELFRelocations = true; 85 c.Options.EmitAddrsig = true; 86 87 // Always emit a section per function/datum with LTO. 88 c.Options.FunctionSections = true; 89 c.Options.DataSections = true; 90 91 // Check if basic block sections must be used. 92 // Allowed values for --lto-basic-block-sections are "all", "labels", 93 // "<file name specifying basic block ids>", or none. This is the equivalent 94 // of -fbasic-block-sections= flag in clang. 95 if (!config->ltoBasicBlockSections.empty()) { 96 if (config->ltoBasicBlockSections == "all") { 97 c.Options.BBSections = BasicBlockSection::All; 98 } else if (config->ltoBasicBlockSections == "labels") { 99 c.Options.BBSections = BasicBlockSection::Labels; 100 } else if (config->ltoBasicBlockSections == "none") { 101 c.Options.BBSections = BasicBlockSection::None; 102 } else { 103 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr = 104 MemoryBuffer::getFile(config->ltoBasicBlockSections.str()); 105 if (!MBOrErr) { 106 error("cannot open " + config->ltoBasicBlockSections + ":" + 107 MBOrErr.getError().message()); 108 } else { 109 c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr); 110 } 111 c.Options.BBSections = BasicBlockSection::List; 112 } 113 } 114 115 c.Options.PseudoProbeForProfiling = config->ltoPseudoProbeForProfiling; 116 c.Options.UniqueBasicBlockSectionNames = 117 config->ltoUniqueBasicBlockSectionNames; 118 119 if (auto relocModel = getRelocModelFromCMModel()) 120 c.RelocModel = *relocModel; 121 else if (config->relocatable) 122 c.RelocModel = None; 123 else if (config->isPic) 124 c.RelocModel = Reloc::PIC_; 125 else 126 c.RelocModel = Reloc::Static; 127 128 c.CodeModel = getCodeModelFromCMModel(); 129 c.DisableVerify = config->disableVerify; 130 c.DiagHandler = diagnosticHandler; 131 c.OptLevel = config->ltoo; 132 c.CPU = getCPUStr(); 133 c.MAttrs = getMAttrs(); 134 c.CGOptLevel = args::getCGOptLevel(config->ltoo); 135 136 c.PTO.LoopVectorization = c.OptLevel > 1; 137 c.PTO.SLPVectorization = c.OptLevel > 1; 138 139 // Set up a custom pipeline if we've been asked to. 140 c.OptPipeline = std::string(config->ltoNewPmPasses); 141 c.AAPipeline = std::string(config->ltoAAPipeline); 142 143 // Set up optimization remarks if we've been asked to. 144 c.RemarksFilename = std::string(config->optRemarksFilename); 145 c.RemarksPasses = std::string(config->optRemarksPasses); 146 c.RemarksWithHotness = config->optRemarksWithHotness; 147 c.RemarksHotnessThreshold = config->optRemarksHotnessThreshold; 148 c.RemarksFormat = std::string(config->optRemarksFormat); 149 150 c.SampleProfile = std::string(config->ltoSampleProfile); 151 c.UseNewPM = config->ltoNewPassManager; 152 c.DebugPassManager = config->ltoDebugPassManager; 153 c.DwoDir = std::string(config->dwoDir); 154 155 c.HasWholeProgramVisibility = config->ltoWholeProgramVisibility; 156 c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty(); 157 158 for (const llvm::StringRef &name : config->thinLTOModulesToCompile) 159 c.ThinLTOModulesToCompile.emplace_back(name); 160 161 c.TimeTraceEnabled = config->timeTraceEnabled; 162 c.TimeTraceGranularity = config->timeTraceGranularity; 163 164 c.CSIRProfile = std::string(config->ltoCSProfileFile); 165 c.RunCSIRInstr = config->ltoCSProfileGenerate; 166 c.PGOWarnMismatch = config->ltoPGOWarnMismatch; 167 168 if (config->emitLLVM) { 169 c.PostInternalizeModuleHook = [](size_t task, const Module &m) { 170 if (std::unique_ptr<raw_fd_ostream> os = 171 openLTOOutputFile(config->outputFile)) 172 WriteBitcodeToFile(m, *os, false); 173 return false; 174 }; 175 } 176 177 if (config->ltoEmitAsm) 178 c.CGFileType = CGFT_AssemblyFile; 179 180 if (config->saveTemps) 181 checkError(c.addSaveTemps(config->outputFile.str() + ".", 182 /*UseInputModulePath*/ true)); 183 return c; 184 } 185 186 BitcodeCompiler::BitcodeCompiler() { 187 // Initialize indexFile. 188 if (!config->thinLTOIndexOnlyArg.empty()) 189 indexFile = openFile(config->thinLTOIndexOnlyArg); 190 191 // Initialize ltoObj. 192 lto::ThinBackend backend; 193 if (config->thinLTOIndexOnly) { 194 auto onIndexWrite = [&](StringRef s) { thinIndices.erase(s); }; 195 backend = lto::createWriteIndexesThinBackend( 196 std::string(config->thinLTOPrefixReplace.first), 197 std::string(config->thinLTOPrefixReplace.second), 198 config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite); 199 } else { 200 backend = lto::createInProcessThinBackend( 201 llvm::heavyweight_hardware_concurrency(config->thinLTOJobs)); 202 } 203 204 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend, 205 config->ltoPartitions); 206 207 // Initialize usedStartStop. 208 for (Symbol *sym : symtab->symbols()) { 209 StringRef s = sym->getName(); 210 for (StringRef prefix : {"__start_", "__stop_"}) 211 if (s.startswith(prefix)) 212 usedStartStop.insert(s.substr(prefix.size())); 213 } 214 } 215 216 BitcodeCompiler::~BitcodeCompiler() = default; 217 218 void BitcodeCompiler::add(BitcodeFile &f) { 219 lto::InputFile &obj = *f.obj; 220 bool isExec = !config->shared && !config->relocatable; 221 222 if (config->thinLTOIndexOnly) 223 thinIndices.insert(obj.getName()); 224 225 ArrayRef<Symbol *> syms = f.getSymbols(); 226 ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols(); 227 std::vector<lto::SymbolResolution> resols(syms.size()); 228 229 // Provide a resolution to the LTO API for each symbol. 230 for (size_t i = 0, e = syms.size(); i != e; ++i) { 231 Symbol *sym = syms[i]; 232 const lto::InputFile::Symbol &objSym = objSyms[i]; 233 lto::SymbolResolution &r = resols[i]; 234 235 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 236 // reports two symbols for module ASM defined. Without this check, lld 237 // flags an undefined in IR with a definition in ASM as prevailing. 238 // Once IRObjectFile is fixed to report only one symbol this hack can 239 // be removed. 240 r.Prevailing = !objSym.isUndefined() && sym->file == &f; 241 242 // We ask LTO to preserve following global symbols: 243 // 1) All symbols when doing relocatable link, so that them can be used 244 // for doing final link. 245 // 2) Symbols that are used in regular objects. 246 // 3) C named sections if we have corresponding __start_/__stop_ symbol. 247 // 4) Symbols that are defined in bitcode files and used for dynamic linking. 248 r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj || 249 (r.Prevailing && sym->includeInDynsym()) || 250 usedStartStop.count(objSym.getSectionName()); 251 // Identify symbols exported dynamically, and that therefore could be 252 // referenced by a shared library not visible to the linker. 253 r.ExportDynamic = sym->computeBinding() != STB_LOCAL && 254 (sym->isExportDynamic(sym->kind(), sym->visibility) || 255 sym->exportDynamic || sym->inDynamicList); 256 const auto *dr = dyn_cast<Defined>(sym); 257 r.FinalDefinitionInLinkageUnit = 258 (isExec || sym->visibility != STV_DEFAULT) && dr && 259 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations 260 // will be generated by for them, triggering linker errors. 261 // Symbol section is always null for bitcode symbols, hence the check 262 // for isElf(). Skip linker script defined symbols as well: they have 263 // no File defined. 264 !(dr->section == nullptr && (!sym->file || sym->file->isElf())); 265 266 if (r.Prevailing) 267 sym->replace(Undefined{nullptr, sym->getName(), STB_GLOBAL, STV_DEFAULT, 268 sym->type}); 269 270 // We tell LTO to not apply interprocedural optimization for wrapped 271 // (with --wrap) symbols because otherwise LTO would inline them while 272 // their values are still not final. 273 r.LinkerRedefined = !sym->canInline; 274 } 275 checkError(ltoObj->add(std::move(f.obj), resols)); 276 } 277 278 // If LazyObjFile has not been added to link, emit empty index files. 279 // This is needed because this is what GNU gold plugin does and we have a 280 // distributed build system that depends on that behavior. 281 static void thinLTOCreateEmptyIndexFiles() { 282 for (LazyObjFile *f : lazyObjFiles) { 283 if (f->fetched || !isBitcode(f->mb)) 284 continue; 285 std::string path = replaceThinLTOSuffix(getThinLTOOutputFile(f->getName())); 286 std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc"); 287 if (!os) 288 continue; 289 290 ModuleSummaryIndex m(/*HaveGVs*/ false); 291 m.setSkipModuleByDistributedBackend(); 292 WriteIndexToFile(m, *os); 293 if (config->thinLTOEmitImportsFiles) 294 openFile(path + ".imports"); 295 } 296 } 297 298 // Merge all the bitcode files we have seen, codegen the result 299 // and return the resulting ObjectFile(s). 300 std::vector<InputFile *> BitcodeCompiler::compile() { 301 unsigned maxTasks = ltoObj->getMaxTasks(); 302 buf.resize(maxTasks); 303 files.resize(maxTasks); 304 305 // The --thinlto-cache-dir option specifies the path to a directory in which 306 // to cache native object files for ThinLTO incremental builds. If a path was 307 // specified, configure LTO to use it as the cache directory. 308 lto::NativeObjectCache cache; 309 if (!config->thinLTOCacheDir.empty()) 310 cache = check( 311 lto::localCache(config->thinLTOCacheDir, 312 [&](size_t task, std::unique_ptr<MemoryBuffer> mb) { 313 files[task] = std::move(mb); 314 })); 315 316 if (!bitcodeFiles.empty()) 317 checkError(ltoObj->run( 318 [&](size_t task) { 319 return std::make_unique<lto::NativeObjectStream>( 320 std::make_unique<raw_svector_ostream>(buf[task])); 321 }, 322 cache)); 323 324 // Emit empty index files for non-indexed files but not in single-module mode. 325 if (config->thinLTOModulesToCompile.empty()) { 326 for (StringRef s : thinIndices) { 327 std::string path = getThinLTOOutputFile(s); 328 openFile(path + ".thinlto.bc"); 329 if (config->thinLTOEmitImportsFiles) 330 openFile(path + ".imports"); 331 } 332 } 333 334 if (config->thinLTOIndexOnly) { 335 thinLTOCreateEmptyIndexFiles(); 336 337 if (!config->ltoObjPath.empty()) 338 saveBuffer(buf[0], config->ltoObjPath); 339 340 // ThinLTO with index only option is required to generate only the index 341 // files. After that, we exit from linker and ThinLTO backend runs in a 342 // distributed environment. 343 if (indexFile) 344 indexFile->close(); 345 return {}; 346 } 347 348 if (!config->thinLTOCacheDir.empty()) 349 pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy); 350 351 if (!config->ltoObjPath.empty()) { 352 saveBuffer(buf[0], config->ltoObjPath); 353 for (unsigned i = 1; i != maxTasks; ++i) 354 saveBuffer(buf[i], config->ltoObjPath + Twine(i)); 355 } 356 357 if (config->saveTemps) { 358 if (!buf[0].empty()) 359 saveBuffer(buf[0], config->outputFile + ".lto.o"); 360 for (unsigned i = 1; i != maxTasks; ++i) 361 saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o"); 362 } 363 364 if (config->ltoEmitAsm) { 365 saveBuffer(buf[0], config->outputFile); 366 for (unsigned i = 1; i != maxTasks; ++i) 367 saveBuffer(buf[i], config->outputFile + Twine(i)); 368 return {}; 369 } 370 371 std::vector<InputFile *> ret; 372 for (unsigned i = 0; i != maxTasks; ++i) 373 if (!buf[i].empty()) 374 ret.push_back(createObjectFile(MemoryBufferRef(buf[i], "lto.tmp"))); 375 376 for (std::unique_ptr<MemoryBuffer> &file : files) 377 if (file) 378 ret.push_back(createObjectFile(*file)); 379 return ret; 380 } 381