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/Strings.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/Bitcode/BitcodeWriter.h" 23 #include "llvm/IR/DiagnosticPrinter.h" 24 #include "llvm/LTO/Config.h" 25 #include "llvm/LTO/LTO.h" 26 #include "llvm/Object/SymbolicFile.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/raw_ostream.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 lld; 43 using namespace lld::coff; 44 45 // Creates an empty file to and returns a raw_fd_ostream to write to it. 46 static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) { 47 std::error_code ec; 48 auto ret = 49 std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None); 50 if (ec) { 51 error("cannot open " + file + ": " + ec.message()); 52 return nullptr; 53 } 54 return ret; 55 } 56 57 std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) { 58 return lto::getThinLTOOutputFile(path, ctx.config.thinLTOPrefixReplaceOld, 59 ctx.config.thinLTOPrefixReplaceNew); 60 } 61 62 lto::Config BitcodeCompiler::createConfig() { 63 lto::Config c; 64 c.Options = initTargetOptionsFromCodeGenFlags(); 65 c.Options.EmitAddrsig = true; 66 for (StringRef C : ctx.config.mllvmOpts) 67 c.MllvmArgs.emplace_back(C.str()); 68 69 // Always emit a section per function/datum with LTO. LLVM LTO should get most 70 // of the benefit of linker GC, but there are still opportunities for ICF. 71 c.Options.FunctionSections = true; 72 c.Options.DataSections = true; 73 74 // Use static reloc model on 32-bit x86 because it usually results in more 75 // compact code, and because there are also known code generation bugs when 76 // using the PIC model (see PR34306). 77 if (ctx.config.machine == COFF::IMAGE_FILE_MACHINE_I386) 78 c.RelocModel = Reloc::Static; 79 else 80 c.RelocModel = Reloc::PIC_; 81 #ifndef NDEBUG 82 c.DisableVerify = false; 83 #else 84 c.DisableVerify = true; 85 #endif 86 c.DiagHandler = diagnosticHandler; 87 c.DwoDir = ctx.config.dwoDir.str(); 88 c.OptLevel = ctx.config.ltoo; 89 c.CPU = getCPUStr(); 90 c.MAttrs = getMAttrs(); 91 std::optional<CodeGenOpt::Level> optLevelOrNone = CodeGenOpt::getLevel( 92 ctx.config.ltoCgo.value_or(args::getCGOptLevel(ctx.config.ltoo))); 93 assert(optLevelOrNone && "Invalid optimization level!"); 94 c.CGOptLevel = *optLevelOrNone; 95 c.AlwaysEmitRegularLTOObj = !ctx.config.ltoObjPath.empty(); 96 c.DebugPassManager = ctx.config.ltoDebugPassManager; 97 c.CSIRProfile = std::string(ctx.config.ltoCSProfileFile); 98 c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate; 99 c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch; 100 101 if (ctx.config.saveTemps) 102 checkError(c.addSaveTemps(std::string(ctx.config.outputFile) + ".", 103 /*UseInputModulePath*/ true)); 104 return c; 105 } 106 107 BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) { 108 // Initialize indexFile. 109 if (!ctx.config.thinLTOIndexOnlyArg.empty()) 110 indexFile = openFile(ctx.config.thinLTOIndexOnlyArg); 111 112 // Initialize ltoObj. 113 lto::ThinBackend backend; 114 if (ctx.config.thinLTOIndexOnly) { 115 auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); }; 116 backend = lto::createWriteIndexesThinBackend( 117 std::string(ctx.config.thinLTOPrefixReplaceOld), 118 std::string(ctx.config.thinLTOPrefixReplaceNew), 119 std::string(ctx.config.thinLTOPrefixReplaceNativeObject), 120 ctx.config.thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite); 121 } else { 122 backend = lto::createInProcessThinBackend( 123 llvm::heavyweight_hardware_concurrency(ctx.config.thinLTOJobs)); 124 } 125 126 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend, 127 ctx.config.ltoPartitions); 128 } 129 130 BitcodeCompiler::~BitcodeCompiler() = default; 131 132 static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, 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(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(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(ltoObj->add(std::move(f.obj), 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(maxTasks); 172 files.resize(maxTasks); 173 file_names.resize(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(localCache("ThinLTO", "Thin", ctx.config.ltoCache, 181 [&](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(ltoObj->run( 188 [&](size_t task, const Twine &moduleName) { 189 buf[task].first = moduleName.str(); 190 return std::make_unique<CachedFileStream>( 191 std::make_unique<raw_svector_ostream>(buf[task].second)); 192 }, 193 cache)); 194 195 // Emit empty index files for non-indexed files 196 for (StringRef s : thinIndices) { 197 std::string path = getThinLTOOutputFile(s); 198 openFile(path + ".thinlto.bc"); 199 if (ctx.config.thinLTOEmitImportsFiles) 200 openFile(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(buf[0].second, ctx.config.ltoObjPath); 209 if (indexFile) 210 indexFile->close(); 211 return {}; 212 } 213 214 if (!ctx.config.ltoCache.empty()) 215 pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, files); 216 217 std::vector<InputFile *> ret; 218 for (unsigned i = 0; i != maxTasks; ++i) { 219 StringRef bitcodeFilePath; 220 // Get the native object contents either from the cache or from memory. Do 221 // not use the cached MemoryBuffer directly, or the PDB will not be 222 // deterministic. 223 StringRef objBuf; 224 if (files[i]) { 225 objBuf = files[i]->getBuffer(); 226 bitcodeFilePath = file_names[i]; 227 } else { 228 objBuf = buf[i].second; 229 bitcodeFilePath = buf[i].first; 230 } 231 if (objBuf.empty()) 232 continue; 233 234 // If the input bitcode file is path/to/a.obj, then the corresponding lto 235 // object file name will look something like: path/to/main.exe.lto.a.obj. 236 StringRef ltoObjName; 237 if (bitcodeFilePath == "ld-temp.o") { 238 ltoObjName = 239 saver().save(Twine(ctx.config.outputFile) + ".lto" + 240 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ".obj"); 241 } else { 242 StringRef directory = sys::path::parent_path(bitcodeFilePath); 243 StringRef baseName = sys::path::filename(bitcodeFilePath); 244 StringRef outputFileBaseName = sys::path::filename(ctx.config.outputFile); 245 SmallString<64> path; 246 sys::path::append(path, directory, 247 outputFileBaseName + ".lto." + baseName); 248 sys::path::remove_dots(path, true); 249 ltoObjName = saver().save(path.str()); 250 } 251 if (ctx.config.saveTemps) 252 saveBuffer(buf[i].second, ltoObjName); 253 ret.push_back(make<ObjFile>(ctx, MemoryBufferRef(objBuf, ltoObjName))); 254 } 255 256 return ret; 257 } 258