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 "COFFLinkerContext.h" 11 #include "Config.h" 12 #include "InputFiles.h" 13 #include "Symbols.h" 14 #include "lld/Common/Args.h" 15 #include "lld/Common/CommonLinkerContext.h" 16 #include "lld/Common/Filesystem.h" 17 #include "lld/Common/Strings.h" 18 #include "lld/Common/TargetOptionsCommandFlags.h" 19 #include "llvm/ADT/STLExtras.h" 20 #include "llvm/ADT/SmallString.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/ADT/Twine.h" 23 #include "llvm/Bitcode/BitcodeWriter.h" 24 #include "llvm/IR/DiagnosticPrinter.h" 25 #include "llvm/LTO/Config.h" 26 #include "llvm/LTO/LTO.h" 27 #include "llvm/Object/SymbolicFile.h" 28 #include "llvm/Support/Caching.h" 29 #include "llvm/Support/CodeGen.h" 30 #include "llvm/Support/Error.h" 31 #include "llvm/Support/FileSystem.h" 32 #include "llvm/Support/MemoryBuffer.h" 33 #include "llvm/Support/raw_ostream.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 lld; 44 using namespace lld::coff; 45 46 std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) { 47 return lto::getThinLTOOutputFile(path, ctx.config.thinLTOPrefixReplaceOld, 48 ctx.config.thinLTOPrefixReplaceNew); 49 } 50 51 lto::Config BitcodeCompiler::createConfig() { 52 lto::Config c; 53 c.Options = initTargetOptionsFromCodeGenFlags(); 54 c.Options.EmitAddrsig = true; 55 for (StringRef C : ctx.config.mllvmOpts) 56 c.MllvmArgs.emplace_back(C.str()); 57 58 // Always emit a section per function/datum with LTO. LLVM LTO should get most 59 // of the benefit of linker GC, but there are still opportunities for ICF. 60 c.Options.FunctionSections = true; 61 c.Options.DataSections = true; 62 63 // Use static reloc model on 32-bit x86 because it usually results in more 64 // compact code, and because there are also known code generation bugs when 65 // using the PIC model (see PR34306). 66 if (ctx.config.machine == COFF::IMAGE_FILE_MACHINE_I386) 67 c.RelocModel = Reloc::Static; 68 else 69 c.RelocModel = Reloc::PIC_; 70 #ifndef NDEBUG 71 c.DisableVerify = false; 72 #else 73 c.DisableVerify = true; 74 #endif 75 c.DiagHandler = diagnosticHandler; 76 c.DwoDir = ctx.config.dwoDir.str(); 77 c.OptLevel = ctx.config.ltoo; 78 c.CPU = getCPUStr(); 79 c.MAttrs = getMAttrs(); 80 std::optional<CodeGenOptLevel> optLevelOrNone = CodeGenOpt::getLevel( 81 ctx.config.ltoCgo.value_or(args::getCGOptLevel(ctx.config.ltoo))); 82 assert(optLevelOrNone && "Invalid optimization level!"); 83 c.CGOptLevel = *optLevelOrNone; 84 c.AlwaysEmitRegularLTOObj = !ctx.config.ltoObjPath.empty(); 85 c.DebugPassManager = ctx.config.ltoDebugPassManager; 86 c.CSIRProfile = std::string(ctx.config.ltoCSProfileFile); 87 c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate; 88 c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch; 89 c.SampleProfile = ctx.config.ltoSampleProfileName; 90 c.TimeTraceEnabled = ctx.config.timeTraceEnabled; 91 c.TimeTraceGranularity = ctx.config.timeTraceGranularity; 92 93 if (ctx.config.emit == EmitKind::LLVM) { 94 c.PreCodeGenModuleHook = [this](size_t task, const Module &m) { 95 if (std::unique_ptr<raw_fd_ostream> os = 96 openLTOOutputFile(ctx.config.outputFile)) 97 WriteBitcodeToFile(m, *os, false); 98 return false; 99 }; 100 } else if (ctx.config.emit == EmitKind::ASM) { 101 c.CGFileType = CodeGenFileType::AssemblyFile; 102 c.Options.MCOptions.AsmVerbose = true; 103 } 104 105 if (!ctx.config.saveTempsArgs.empty()) 106 checkError(c.addSaveTemps(std::string(ctx.config.outputFile) + ".", 107 /*UseInputModulePath*/ true, 108 ctx.config.saveTempsArgs)); 109 return c; 110 } 111 112 BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) { 113 // Initialize indexFile. 114 if (!ctx.config.thinLTOIndexOnlyArg.empty()) 115 indexFile = openFile(ctx.config.thinLTOIndexOnlyArg); 116 117 // Initialize ltoObj. 118 lto::ThinBackend backend; 119 if (ctx.config.thinLTOIndexOnly) { 120 auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); }; 121 backend = lto::createWriteIndexesThinBackend( 122 llvm::hardware_concurrency(ctx.config.thinLTOJobs), 123 std::string(ctx.config.thinLTOPrefixReplaceOld), 124 std::string(ctx.config.thinLTOPrefixReplaceNew), 125 std::string(ctx.config.thinLTOPrefixReplaceNativeObject), 126 ctx.config.thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite); 127 } else { 128 backend = lto::createInProcessThinBackend( 129 llvm::heavyweight_hardware_concurrency(ctx.config.thinLTOJobs)); 130 } 131 132 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend, 133 ctx.config.ltoPartitions); 134 } 135 136 BitcodeCompiler::~BitcodeCompiler() = default; 137 138 static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, s->getName()); } 139 140 void BitcodeCompiler::add(BitcodeFile &f) { 141 lto::InputFile &obj = *f.obj; 142 unsigned symNum = 0; 143 std::vector<Symbol *> symBodies = f.getSymbols(); 144 std::vector<lto::SymbolResolution> resols(symBodies.size()); 145 146 if (ctx.config.thinLTOIndexOnly) 147 thinIndices.insert(obj.getName()); 148 149 // Provide a resolution to the LTO API for each symbol. 150 for (const lto::InputFile::Symbol &objSym : obj.symbols()) { 151 Symbol *sym = symBodies[symNum]; 152 lto::SymbolResolution &r = resols[symNum]; 153 ++symNum; 154 155 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 156 // reports two symbols for module ASM defined. Without this check, lld 157 // flags an undefined in IR with a definition in ASM as prevailing. 158 // Once IRObjectFile is fixed to report only one symbol this hack can 159 // be removed. 160 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f; 161 r.VisibleToRegularObj = sym->isUsedInRegularObj; 162 if (r.Prevailing) 163 undefine(sym); 164 165 // We tell LTO to not apply interprocedural optimization for wrapped 166 // (with -wrap) symbols because otherwise LTO would inline them while 167 // their values are still not final. 168 r.LinkerRedefined = !sym->canInline; 169 } 170 checkError(ltoObj->add(std::move(f.obj), resols)); 171 } 172 173 // Merge all the bitcode files we have seen, codegen the result 174 // and return the resulting objects. 175 std::vector<InputFile *> BitcodeCompiler::compile() { 176 unsigned maxTasks = ltoObj->getMaxTasks(); 177 buf.resize(maxTasks); 178 files.resize(maxTasks); 179 file_names.resize(maxTasks); 180 181 // The /lldltocache option specifies the path to a directory in which to cache 182 // native object files for ThinLTO incremental builds. If a path was 183 // specified, configure LTO to use it as the cache directory. 184 FileCache cache; 185 if (!ctx.config.ltoCache.empty()) 186 cache = check(localCache("ThinLTO", "Thin", ctx.config.ltoCache, 187 [&](size_t task, const Twine &moduleName, 188 std::unique_ptr<MemoryBuffer> mb) { 189 files[task] = std::move(mb); 190 file_names[task] = moduleName.str(); 191 })); 192 193 checkError(ltoObj->run( 194 [&](size_t task, const Twine &moduleName) { 195 buf[task].first = moduleName.str(); 196 return std::make_unique<CachedFileStream>( 197 std::make_unique<raw_svector_ostream>(buf[task].second)); 198 }, 199 cache)); 200 201 // Emit empty index files for non-indexed files 202 for (StringRef s : thinIndices) { 203 std::string path = getThinLTOOutputFile(s); 204 openFile(path + ".thinlto.bc"); 205 if (ctx.config.thinLTOEmitImportsFiles) 206 openFile(path + ".imports"); 207 } 208 209 // ThinLTO with index only option is required to generate only the index 210 // files. After that, we exit from linker and ThinLTO backend runs in a 211 // distributed environment. 212 if (ctx.config.thinLTOIndexOnly) { 213 if (!ctx.config.ltoObjPath.empty()) 214 saveBuffer(buf[0].second, ctx.config.ltoObjPath); 215 if (indexFile) 216 indexFile->close(); 217 return {}; 218 } 219 220 if (!ctx.config.ltoCache.empty()) 221 pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, files); 222 223 std::vector<InputFile *> ret; 224 bool emitASM = ctx.config.emit == EmitKind::ASM; 225 const char *Ext = emitASM ? ".s" : ".obj"; 226 for (unsigned i = 0; i != maxTasks; ++i) { 227 StringRef bitcodeFilePath; 228 // Get the native object contents either from the cache or from memory. Do 229 // not use the cached MemoryBuffer directly, or the PDB will not be 230 // deterministic. 231 StringRef objBuf; 232 if (files[i]) { 233 objBuf = files[i]->getBuffer(); 234 bitcodeFilePath = file_names[i]; 235 } else { 236 objBuf = buf[i].second; 237 bitcodeFilePath = buf[i].first; 238 } 239 if (objBuf.empty()) 240 continue; 241 242 // If the input bitcode file is path/to/a.obj, then the corresponding lto 243 // object file name will look something like: path/to/main.exe.lto.a.obj. 244 StringRef ltoObjName; 245 if (bitcodeFilePath == "ld-temp.o") { 246 ltoObjName = 247 saver().save(Twine(ctx.config.outputFile) + ".lto" + 248 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + Ext); 249 } else { 250 StringRef directory = sys::path::parent_path(bitcodeFilePath); 251 StringRef baseName = sys::path::stem(bitcodeFilePath); 252 StringRef outputFileBaseName = sys::path::filename(ctx.config.outputFile); 253 SmallString<64> path; 254 sys::path::append(path, directory, 255 outputFileBaseName + ".lto." + baseName + Ext); 256 sys::path::remove_dots(path, true); 257 ltoObjName = saver().save(path.str()); 258 } 259 if (llvm::is_contained(ctx.config.saveTempsArgs, "prelink") || emitASM) 260 saveBuffer(buf[i].second, ltoObjName); 261 if (!emitASM) 262 ret.push_back(ObjFile::create(ctx, MemoryBufferRef(objBuf, ltoObjName))); 263 } 264 265 return ret; 266 } 267