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