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 "Symbols.h" 13 #include "lld/Common/Args.h" 14 #include "lld/Common/ErrorHandler.h" 15 #include "lld/Common/Strings.h" 16 #include "lld/Common/TargetOptionsCommandFlags.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/Twine.h" 21 #include "llvm/Bitcode/BitcodeWriter.h" 22 #include "llvm/IR/DiagnosticPrinter.h" 23 #include "llvm/LTO/Caching.h" 24 #include "llvm/LTO/Config.h" 25 #include "llvm/LTO/LTO.h" 26 #include "llvm/Object/SymbolicFile.h" 27 #include "llvm/Support/CodeGen.h" 28 #include "llvm/Support/Error.h" 29 #include "llvm/Support/FileSystem.h" 30 #include "llvm/Support/MemoryBuffer.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include <algorithm> 33 #include <cstddef> 34 #include <memory> 35 #include <string> 36 #include <system_error> 37 #include <vector> 38 39 using namespace llvm; 40 using namespace llvm::object; 41 using namespace lld; 42 using namespace lld::coff; 43 44 // Creates an empty file to and returns a raw_fd_ostream to write to it. 45 static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) { 46 std::error_code ec; 47 auto ret = 48 std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None); 49 if (ec) { 50 error("cannot open " + file + ": " + ec.message()); 51 return nullptr; 52 } 53 return ret; 54 } 55 56 static std::string getThinLTOOutputFile(StringRef path) { 57 return lto::getThinLTOOutputFile( 58 std::string(path), std::string(config->thinLTOPrefixReplace.first), 59 std::string(config->thinLTOPrefixReplace.second)); 60 } 61 62 static lto::Config createConfig() { 63 lto::Config c; 64 c.Options = initTargetOptionsFromCodeGenFlags(); 65 66 // Always emit a section per function/datum with LTO. LLVM LTO should get most 67 // of the benefit of linker GC, but there are still opportunities for ICF. 68 c.Options.FunctionSections = true; 69 c.Options.DataSections = true; 70 71 // Use static reloc model on 32-bit x86 because it usually results in more 72 // compact code, and because there are also known code generation bugs when 73 // using the PIC model (see PR34306). 74 if (config->machine == COFF::IMAGE_FILE_MACHINE_I386) 75 c.RelocModel = Reloc::Static; 76 else 77 c.RelocModel = Reloc::PIC_; 78 c.DisableVerify = true; 79 c.DiagHandler = diagnosticHandler; 80 c.OptLevel = config->ltoo; 81 c.CPU = getCPUStr(); 82 c.MAttrs = getMAttrs(); 83 c.CGOptLevel = args::getCGOptLevel(config->ltoo); 84 c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty(); 85 86 if (config->saveTemps) 87 checkError(c.addSaveTemps(std::string(config->outputFile) + ".", 88 /*UseInputModulePath*/ true)); 89 return c; 90 } 91 92 BitcodeCompiler::BitcodeCompiler() { 93 // Initialize indexFile. 94 if (!config->thinLTOIndexOnlyArg.empty()) 95 indexFile = openFile(config->thinLTOIndexOnlyArg); 96 97 // Initialize ltoObj. 98 lto::ThinBackend backend; 99 if (config->thinLTOIndexOnly) { 100 auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); }; 101 backend = lto::createWriteIndexesThinBackend( 102 std::string(config->thinLTOPrefixReplace.first), 103 std::string(config->thinLTOPrefixReplace.second), 104 config->thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite); 105 } else { 106 backend = lto::createInProcessThinBackend( 107 llvm::heavyweight_hardware_concurrency(config->thinLTOJobs)); 108 } 109 110 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend, 111 config->ltoPartitions); 112 } 113 114 BitcodeCompiler::~BitcodeCompiler() = default; 115 116 static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, s->getName()); } 117 118 void BitcodeCompiler::add(BitcodeFile &f) { 119 lto::InputFile &obj = *f.obj; 120 unsigned symNum = 0; 121 std::vector<Symbol *> symBodies = f.getSymbols(); 122 std::vector<lto::SymbolResolution> resols(symBodies.size()); 123 124 if (config->thinLTOIndexOnly) 125 thinIndices.insert(obj.getName()); 126 127 // Provide a resolution to the LTO API for each symbol. 128 for (const lto::InputFile::Symbol &objSym : obj.symbols()) { 129 Symbol *sym = symBodies[symNum]; 130 lto::SymbolResolution &r = resols[symNum]; 131 ++symNum; 132 133 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 134 // reports two symbols for module ASM defined. Without this check, lld 135 // flags an undefined in IR with a definition in ASM as prevailing. 136 // Once IRObjectFile is fixed to report only one symbol this hack can 137 // be removed. 138 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f; 139 r.VisibleToRegularObj = sym->isUsedInRegularObj; 140 if (r.Prevailing) 141 undefine(sym); 142 } 143 checkError(ltoObj->add(std::move(f.obj), resols)); 144 } 145 146 // Merge all the bitcode files we have seen, codegen the result 147 // and return the resulting objects. 148 std::vector<InputFile *> BitcodeCompiler::compile() { 149 unsigned maxTasks = ltoObj->getMaxTasks(); 150 buf.resize(maxTasks); 151 files.resize(maxTasks); 152 153 // The /lldltocache option specifies the path to a directory in which to cache 154 // native object files for ThinLTO incremental builds. If a path was 155 // specified, configure LTO to use it as the cache directory. 156 lto::NativeObjectCache cache; 157 if (!config->ltoCache.empty()) 158 cache = check(lto::localCache( 159 config->ltoCache, [&](size_t task, std::unique_ptr<MemoryBuffer> mb) { 160 files[task] = std::move(mb); 161 })); 162 163 checkError(ltoObj->run( 164 [&](size_t task) { 165 return std::make_unique<lto::NativeObjectStream>( 166 std::make_unique<raw_svector_ostream>(buf[task])); 167 }, 168 cache)); 169 170 // Emit empty index files for non-indexed files 171 for (StringRef s : thinIndices) { 172 std::string path = getThinLTOOutputFile(s); 173 openFile(path + ".thinlto.bc"); 174 if (config->thinLTOEmitImportsFiles) 175 openFile(path + ".imports"); 176 } 177 178 // ThinLTO with index only option is required to generate only the index 179 // files. After that, we exit from linker and ThinLTO backend runs in a 180 // distributed environment. 181 if (config->thinLTOIndexOnly) { 182 if (!config->ltoObjPath.empty()) 183 saveBuffer(buf[0], config->ltoObjPath); 184 if (indexFile) 185 indexFile->close(); 186 return {}; 187 } 188 189 if (!config->ltoCache.empty()) 190 pruneCache(config->ltoCache, config->ltoCachePolicy); 191 192 std::vector<InputFile *> ret; 193 for (unsigned i = 0; i != maxTasks; ++i) { 194 // Assign unique names to LTO objects. This ensures they have unique names 195 // in the PDB if one is produced. The names should look like: 196 // - foo.exe.lto.obj 197 // - foo.exe.lto.1.obj 198 // - ... 199 StringRef ltoObjName = 200 saver.save(Twine(config->outputFile) + ".lto" + 201 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ".obj"); 202 203 // Get the native object contents either from the cache or from memory. Do 204 // not use the cached MemoryBuffer directly, or the PDB will not be 205 // deterministic. 206 StringRef objBuf; 207 if (files[i]) 208 objBuf = files[i]->getBuffer(); 209 else 210 objBuf = buf[i]; 211 if (objBuf.empty()) 212 continue; 213 214 if (config->saveTemps) 215 saveBuffer(buf[i], ltoObjName); 216 ret.push_back(make<ObjFile>(MemoryBufferRef(objBuf, ltoObjName))); 217 } 218 219 return ret; 220 } 221