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 "Driver.h" 12 #include "InputFiles.h" 13 #include "Symbols.h" 14 #include "Target.h" 15 16 #include "lld/Common/Args.h" 17 #include "lld/Common/CommonLinkerContext.h" 18 #include "lld/Common/Filesystem.h" 19 #include "lld/Common/Strings.h" 20 #include "lld/Common/TargetOptionsCommandFlags.h" 21 #include "llvm/Bitcode/BitcodeWriter.h" 22 #include "llvm/LTO/Config.h" 23 #include "llvm/LTO/LTO.h" 24 #include "llvm/Support/Caching.h" 25 #include "llvm/Support/FileSystem.h" 26 #include "llvm/Support/Path.h" 27 #include "llvm/Support/raw_ostream.h" 28 #include "llvm/Transforms/ObjCARC.h" 29 30 using namespace lld; 31 using namespace lld::macho; 32 using namespace llvm; 33 using namespace llvm::MachO; 34 using namespace llvm::sys; 35 36 static std::string getThinLTOOutputFile(StringRef modulePath) { 37 return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld, 38 config->thinLTOPrefixReplaceNew); 39 } 40 41 static lto::Config createConfig() { 42 lto::Config c; 43 c.Options = initTargetOptionsFromCodeGenFlags(); 44 c.Options.EmitAddrsig = config->icfLevel == ICFLevel::safe; 45 for (StringRef C : config->mllvmOpts) 46 c.MllvmArgs.emplace_back(C.str()); 47 for (StringRef pluginFn : config->passPlugins) 48 c.PassPlugins.push_back(std::string(pluginFn)); 49 c.OptPipeline = std::string(config->ltoNewPmPasses); 50 c.CodeModel = getCodeModelFromCMModel(); 51 c.CPU = getCPUStr(); 52 c.MAttrs = getMAttrs(); 53 c.DiagHandler = diagnosticHandler; 54 55 c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty(); 56 57 c.TimeTraceEnabled = config->timeTraceEnabled; 58 c.TimeTraceGranularity = config->timeTraceGranularity; 59 c.DebugPassManager = config->ltoDebugPassManager; 60 c.CSIRProfile = std::string(config->csProfilePath); 61 c.RunCSIRInstr = config->csProfileGenerate; 62 c.PGOWarnMismatch = config->pgoWarnMismatch; 63 c.OptLevel = config->ltoo; 64 c.CGOptLevel = config->ltoCgo; 65 if (config->saveTemps) 66 checkError(c.addSaveTemps(config->outputFile.str() + ".", 67 /*UseInputModulePath=*/true)); 68 return c; 69 } 70 71 // If `originalPath` exists, hardlinks `path` to `originalPath`. If that fails, 72 // or `originalPath` is not set, saves `buffer` to `path`. 73 static void saveOrHardlinkBuffer(StringRef buffer, const Twine &path, 74 std::optional<StringRef> originalPath) { 75 if (originalPath) { 76 auto err = fs::create_hard_link(*originalPath, path); 77 if (!err) 78 return; 79 } 80 saveBuffer(buffer, path); 81 } 82 83 BitcodeCompiler::BitcodeCompiler() { 84 // Initialize indexFile. 85 if (!config->thinLTOIndexOnlyArg.empty()) 86 indexFile = openFile(config->thinLTOIndexOnlyArg); 87 88 // Initialize ltoObj. 89 lto::ThinBackend backend; 90 auto onIndexWrite = [&](StringRef S) { thinIndices.erase(S); }; 91 if (config->thinLTOIndexOnly) { 92 backend = lto::createWriteIndexesThinBackend( 93 llvm::hardware_concurrency(config->thinLTOJobs), 94 std::string(config->thinLTOPrefixReplaceOld), 95 std::string(config->thinLTOPrefixReplaceNew), 96 std::string(config->thinLTOPrefixReplaceNativeObject), 97 config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite); 98 } else { 99 backend = lto::createInProcessThinBackend( 100 llvm::heavyweight_hardware_concurrency(config->thinLTOJobs), 101 onIndexWrite, config->thinLTOEmitIndexFiles, 102 config->thinLTOEmitImportsFiles); 103 } 104 105 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend); 106 } 107 108 void BitcodeCompiler::add(BitcodeFile &f) { 109 lto::InputFile &obj = *f.obj; 110 111 if (config->thinLTOEmitIndexFiles) 112 thinIndices.insert(obj.getName()); 113 114 ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols(); 115 std::vector<lto::SymbolResolution> resols; 116 resols.reserve(objSyms.size()); 117 118 // Provide a resolution to the LTO API for each symbol. 119 bool exportDynamic = 120 config->outputType != MH_EXECUTE || config->exportDynamic; 121 auto symIt = f.symbols.begin(); 122 for (const lto::InputFile::Symbol &objSym : objSyms) { 123 resols.emplace_back(); 124 lto::SymbolResolution &r = resols.back(); 125 Symbol *sym = *symIt++; 126 127 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 128 // reports two symbols for module ASM defined. Without this check, lld 129 // flags an undefined in IR with a definition in ASM as prevailing. 130 // Once IRObjectFile is fixed to report only one symbol this hack can 131 // be removed. 132 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f; 133 134 if (const auto *defined = dyn_cast<Defined>(sym)) { 135 r.ExportDynamic = 136 defined->isExternal() && !defined->privateExtern && exportDynamic; 137 r.FinalDefinitionInLinkageUnit = 138 !defined->isExternalWeakDef() && !defined->interposable; 139 } else if (const auto *common = dyn_cast<CommonSymbol>(sym)) { 140 r.ExportDynamic = !common->privateExtern && exportDynamic; 141 r.FinalDefinitionInLinkageUnit = true; 142 } 143 144 r.VisibleToRegularObj = 145 sym->isUsedInRegularObj || (r.Prevailing && r.ExportDynamic); 146 147 // Un-define the symbol so that we don't get duplicate symbol errors when we 148 // load the ObjFile emitted by LTO compilation. 149 if (r.Prevailing) 150 replaceSymbol<Undefined>(sym, sym->getName(), sym->getFile(), 151 RefState::Strong, /*wasBitcodeSymbol=*/true); 152 153 // TODO: set the other resolution configs properly 154 } 155 checkError(ltoObj->add(std::move(f.obj), resols)); 156 hasFiles = true; 157 } 158 159 // If LazyObjFile has not been added to link, emit empty index files. 160 // This is needed because this is what GNU gold plugin does and we have a 161 // distributed build system that depends on that behavior. 162 static void thinLTOCreateEmptyIndexFiles() { 163 DenseSet<StringRef> linkedBitCodeFiles; 164 for (InputFile *file : inputFiles) 165 if (auto *f = dyn_cast<BitcodeFile>(file)) 166 if (!f->lazy) 167 linkedBitCodeFiles.insert(f->getName()); 168 169 for (InputFile *file : inputFiles) { 170 if (auto *f = dyn_cast<BitcodeFile>(file)) { 171 if (!f->lazy) 172 continue; 173 if (linkedBitCodeFiles.contains(f->getName())) 174 continue; 175 std::string path = 176 replaceThinLTOSuffix(getThinLTOOutputFile(f->obj->getName())); 177 std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc"); 178 if (!os) 179 continue; 180 181 ModuleSummaryIndex m(/*HaveGVs=*/false); 182 m.setSkipModuleByDistributedBackend(); 183 writeIndexToFile(m, *os); 184 if (config->thinLTOEmitImportsFiles) 185 openFile(path + ".imports"); 186 } 187 } 188 } 189 190 // Merge all the bitcode files we have seen, codegen the result 191 // and return the resulting ObjectFile(s). 192 std::vector<ObjFile *> BitcodeCompiler::compile() { 193 unsigned maxTasks = ltoObj->getMaxTasks(); 194 buf.resize(maxTasks); 195 files.resize(maxTasks); 196 197 // The -cache_path_lto option specifies the path to a directory in which 198 // to cache native object files for ThinLTO incremental builds. If a path was 199 // specified, configure LTO to use it as the cache directory. 200 FileCache cache; 201 if (!config->thinLTOCacheDir.empty()) 202 cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir, 203 [&](size_t task, const Twine &moduleName, 204 std::unique_ptr<MemoryBuffer> mb) { 205 files[task] = std::move(mb); 206 })); 207 208 if (hasFiles) 209 checkError(ltoObj->run( 210 [&](size_t task, const Twine &moduleName) { 211 return std::make_unique<CachedFileStream>( 212 std::make_unique<raw_svector_ostream>(buf[task])); 213 }, 214 cache)); 215 216 // Emit empty index files for non-indexed files 217 for (StringRef s : thinIndices) { 218 std::string path = getThinLTOOutputFile(s); 219 openFile(path + ".thinlto.bc"); 220 if (config->thinLTOEmitImportsFiles) 221 openFile(path + ".imports"); 222 } 223 224 if (config->thinLTOEmitIndexFiles) 225 thinLTOCreateEmptyIndexFiles(); 226 227 // In ThinLTO mode, Clang passes a temporary directory in -object_path_lto, 228 // while the argument is a single file in FullLTO mode. 229 bool objPathIsDir = true; 230 if (!config->ltoObjPath.empty()) { 231 if (std::error_code ec = fs::create_directories(config->ltoObjPath)) 232 fatal("cannot create LTO object path " + config->ltoObjPath + ": " + 233 ec.message()); 234 235 if (!fs::is_directory(config->ltoObjPath)) { 236 objPathIsDir = false; 237 unsigned objCount = 238 count_if(buf, [](const SmallString<0> &b) { return !b.empty(); }); 239 if (objCount > 1) 240 fatal("-object_path_lto must specify a directory when using ThinLTO"); 241 } 242 } 243 244 auto outputFilePath = [objPathIsDir](int i) { 245 SmallString<261> filePath("/tmp/lto.tmp"); 246 if (!config->ltoObjPath.empty()) { 247 filePath = config->ltoObjPath; 248 if (objPathIsDir) 249 path::append(filePath, Twine(i) + "." + 250 getArchitectureName(config->arch()) + 251 ".lto.o"); 252 } 253 return filePath; 254 }; 255 256 // ThinLTO with index only option is required to generate only the index 257 // files. After that, we exit from linker and ThinLTO backend runs in a 258 // distributed environment. 259 if (config->thinLTOIndexOnly) { 260 if (!config->ltoObjPath.empty()) 261 saveBuffer(buf[0], outputFilePath(0)); 262 if (indexFile) 263 indexFile->close(); 264 return {}; 265 } 266 267 if (!config->thinLTOCacheDir.empty()) 268 pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files); 269 270 std::vector<ObjFile *> ret; 271 for (unsigned i = 0; i < maxTasks; ++i) { 272 // Get the native object contents either from the cache or from memory. Do 273 // not use the cached MemoryBuffer directly to ensure dsymutil does not 274 // race with the cache pruner. 275 StringRef objBuf; 276 std::optional<StringRef> cachePath; 277 if (files[i]) { 278 objBuf = files[i]->getBuffer(); 279 cachePath = files[i]->getBufferIdentifier(); 280 } else { 281 objBuf = buf[i]; 282 } 283 if (objBuf.empty()) 284 continue; 285 286 // FIXME: should `saveTemps` and `ltoObjPath` use the same file name? 287 if (config->saveTemps) 288 saveBuffer(objBuf, 289 config->outputFile + ((i == 0) ? "" : Twine(i)) + ".lto.o"); 290 291 auto filePath = outputFilePath(i); 292 uint32_t modTime = 0; 293 if (!config->ltoObjPath.empty()) { 294 saveOrHardlinkBuffer(objBuf, filePath, cachePath); 295 modTime = getModTime(filePath); 296 } 297 ret.push_back(make<ObjFile>( 298 MemoryBufferRef(objBuf, saver().save(filePath.str())), modTime, 299 /*archiveName=*/"", /*lazy=*/false, 300 /*forceHidden=*/false, /*compatArch=*/true, /*builtFromBitcode=*/true)); 301 } 302 303 return ret; 304 } 305