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