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 "SymbolTable.h"
13#include "Symbols.h"
14#include "lld/Common/ErrorHandler.h"
15#include "lld/Common/Filesystem.h"
16#include "lld/Common/Strings.h"
17#include "lld/Common/TargetOptionsCommandFlags.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/BinaryFormat/ELF.h"
21#include "llvm/Bitcode/BitcodeWriter.h"
22#include "llvm/DTLTO/DTLTO.h"
23#include "llvm/LTO/Config.h"
24#include "llvm/LTO/LTO.h"
25#include "llvm/Support/Caching.h"
26#include "llvm/Support/CodeGen.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/Path.h"
29#include <cstddef>
30#include <memory>
31#include <string>
32#include <system_error>
33#include <vector>
34
35using namespace llvm;
36using namespace llvm::object;
37using namespace llvm::ELF;
38using namespace lld;
39using namespace lld::elf;
40
41static AddBufferFn
42createAddBufferFn(std::vector<std::unique_ptr<MemoryBuffer>> &files,
43 SmallVectorImpl<std::string> &filenames) {
44 return [&files, &filenames](unsigned task, const Twine &moduleName,
45 std::unique_ptr<MemoryBuffer> mb) {
46 files[task] = std::move(mb);
47 filenames[task] = moduleName.str();
48 };
49}
50
51static std::string getThinLTOOutputFile(Ctx &ctx, StringRef modulePath) {
52 return lto::getThinLTOOutputFile(Path: modulePath, OldPrefix: ctx.arg.thinLTOPrefixReplaceOld,
53 NewPrefix: ctx.arg.thinLTOPrefixReplaceNew);
54}
55
56static lto::Config createConfig(Ctx &ctx) {
57 lto::Config c;
58
59 // LLD supports the new relocations and address-significance tables.
60 c.Options = initTargetOptionsFromCodeGenFlags();
61 c.Options.EmitAddrsig = true;
62 for (StringRef C : ctx.arg.mllvmOpts)
63 c.MllvmArgs.emplace_back(args: C.str());
64
65 // Always emit a section per function/datum with LTO.
66 c.Options.FunctionSections = true;
67 c.Options.DataSections = true;
68
69 // Check if basic block sections must be used.
70 // Allowed values for --lto-basic-block-sections are "all",
71 // "<file name specifying basic block ids>", or none. This is the equivalent
72 // of -fbasic-block-sections= flag in clang.
73 if (!ctx.arg.ltoBasicBlockSections.empty()) {
74 if (ctx.arg.ltoBasicBlockSections == "all") {
75 c.Options.BBSections = BasicBlockSection::All;
76 } else if (ctx.arg.ltoBasicBlockSections == "labels") {
77 c.Options.BBAddrMap = true;
78 Warn(ctx)
79 << "'--lto-basic-block-sections=labels' is deprecated; Please use "
80 "'--lto-basic-block-address-map' instead";
81 } else if (ctx.arg.ltoBasicBlockSections == "none") {
82 c.Options.BBSections = BasicBlockSection::None;
83 } else {
84 ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
85 MemoryBuffer::getFile(Filename: ctx.arg.ltoBasicBlockSections.str());
86 if (!MBOrErr) {
87 ErrAlways(ctx) << "cannot open " << ctx.arg.ltoBasicBlockSections << ":"
88 << MBOrErr.getError().message();
89 } else {
90 c.Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
91 }
92 c.Options.BBSections = BasicBlockSection::List;
93 }
94 }
95
96 c.Options.BBAddrMap = ctx.arg.ltoBBAddrMap;
97
98 c.Options.UniqueBasicBlockSectionNames =
99 ctx.arg.ltoUniqueBasicBlockSectionNames;
100
101 if (auto relocModel = getRelocModelFromCMModel())
102 c.RelocModel = *relocModel;
103 else if (ctx.arg.relocatable)
104 c.RelocModel = std::nullopt;
105 else if (ctx.arg.isPic)
106 c.RelocModel = Reloc::PIC_;
107 else
108 c.RelocModel = Reloc::Static;
109
110 c.CodeModel = getCodeModelFromCMModel();
111 c.DisableVerify = ctx.arg.disableVerify;
112 c.DiagHandler = diagnosticHandler;
113 c.OptLevel = ctx.arg.ltoo;
114 c.CPU = getCPUStr();
115 c.MAttrs = getMAttrs();
116 c.CGOptLevel = ctx.arg.ltoCgo;
117
118 c.PTO.LoopVectorization = c.OptLevel > 1;
119 c.PTO.SLPVectorization = c.OptLevel > 1;
120
121 // Set up a custom pipeline if we've been asked to.
122 c.OptPipeline = std::string(ctx.arg.ltoNewPmPasses);
123 c.AAPipeline = std::string(ctx.arg.ltoAAPipeline);
124
125 // Set up optimization remarks if we've been asked to.
126 c.RemarksFilename = std::string(ctx.arg.optRemarksFilename);
127 c.RemarksPasses = std::string(ctx.arg.optRemarksPasses);
128 c.RemarksWithHotness = ctx.arg.optRemarksWithHotness;
129 c.RemarksHotnessThreshold = ctx.arg.optRemarksHotnessThreshold;
130 c.RemarksFormat = std::string(ctx.arg.optRemarksFormat);
131
132 // Set up output file to emit statistics.
133 c.StatsFile = std::string(ctx.arg.optStatsFilename);
134
135 c.SampleProfile = std::string(ctx.arg.ltoSampleProfile);
136 for (StringRef pluginFn : ctx.arg.passPlugins)
137 c.PassPluginFilenames.push_back(x: std::string(pluginFn));
138 c.DebugPassManager = ctx.arg.ltoDebugPassManager;
139 c.DwoDir = std::string(ctx.arg.dwoDir);
140
141 c.HasWholeProgramVisibility = ctx.arg.ltoWholeProgramVisibility;
142 c.ValidateAllVtablesHaveTypeInfos =
143 ctx.arg.ltoValidateAllVtablesHaveTypeInfos;
144 c.AllVtablesHaveTypeInfos = ctx.ltoAllVtablesHaveTypeInfos;
145 c.AlwaysEmitRegularLTOObj = !ctx.arg.ltoObjPath.empty();
146 c.KeepSymbolNameCopies = false;
147
148 for (const llvm::StringRef &name : ctx.arg.thinLTOModulesToCompile)
149 c.ThinLTOModulesToCompile.emplace_back(args: name);
150
151 c.TimeTraceEnabled = ctx.arg.timeTraceEnabled;
152 c.TimeTraceGranularity = ctx.arg.timeTraceGranularity;
153
154 c.CSIRProfile = std::string(ctx.arg.ltoCSProfileFile);
155 c.RunCSIRInstr = ctx.arg.ltoCSProfileGenerate;
156 c.PGOWarnMismatch = ctx.arg.ltoPGOWarnMismatch;
157
158 if (ctx.arg.emitLLVM) {
159 c.PreCodeGenModuleHook = [&ctx](size_t task, const Module &m) {
160 if (std::unique_ptr<raw_fd_ostream> os =
161 openLTOOutputFile(file: ctx.arg.outputFile))
162 WriteBitcodeToFile(M: m, Out&: *os, ShouldPreserveUseListOrder: false);
163 return false;
164 };
165 }
166
167 if (ctx.arg.ltoEmitAsm) {
168 c.CGFileType = CodeGenFileType::AssemblyFile;
169 c.Options.MCOptions.AsmVerbose = true;
170 }
171
172 if (!ctx.arg.saveTempsArgs.empty())
173 checkError(eh&: ctx.e, e: c.addSaveTemps(OutputFileName: ctx.arg.outputFile.str() + ".",
174 /*UseInputModulePath*/ true,
175 SaveTempsArgs: ctx.arg.saveTempsArgs));
176 return c;
177}
178
179BitcodeCompiler::BitcodeCompiler(Ctx &ctx) : ctx(ctx) {
180 // Initialize indexFile.
181 if (!ctx.arg.thinLTOIndexOnlyArg.empty())
182 indexFile = openFile(file: ctx.arg.thinLTOIndexOnlyArg);
183
184 // Initialize ltoObj.
185 lto::ThinBackend backend;
186 auto onIndexWrite = [&](StringRef s) { thinIndices.erase(V: s); };
187 if (ctx.arg.thinLTOIndexOnly) {
188 backend = lto::createWriteIndexesThinBackend(
189 Parallelism: llvm::hardware_concurrency(Num: ctx.arg.thinLTOJobs),
190 OldPrefix: std::string(ctx.arg.thinLTOPrefixReplaceOld),
191 NewPrefix: std::string(ctx.arg.thinLTOPrefixReplaceNew),
192 NativeObjectPrefix: std::string(ctx.arg.thinLTOPrefixReplaceNativeObject),
193 ShouldEmitImportsFiles: ctx.arg.thinLTOEmitImportsFiles, LinkedObjectsFile: indexFile.get(), OnWrite: onIndexWrite);
194 } else if (!ctx.arg.dtltoDistributor.empty()) {
195 backend = lto::createOutOfProcessThinBackend(
196 Parallelism: llvm::hardware_concurrency(Num: ctx.arg.thinLTOJobs), OnWrite: onIndexWrite,
197 ShouldEmitIndexFiles: ctx.arg.thinLTOEmitIndexFiles, ShouldEmitImportsFiles: ctx.arg.thinLTOEmitImportsFiles,
198 LinkerOutputFile: ctx.arg.outputFile, Distributor: ctx.arg.dtltoDistributor,
199 DistributorArgs: ctx.arg.dtltoDistributorArgs, RemoteCompiler: ctx.arg.dtltoCompiler,
200 RemoteCompilerPrependArgs: ctx.arg.dtltoCompilerPrependArgs, RemoteCompilerArgs: ctx.arg.dtltoCompilerArgs,
201 SaveTemps: !ctx.arg.saveTempsArgs.empty(), AddBuffer: createAddBufferFn(files, filenames));
202 } else {
203 backend = lto::createInProcessThinBackend(
204 Parallelism: llvm::heavyweight_hardware_concurrency(Num: ctx.arg.thinLTOJobs),
205 OnWrite: onIndexWrite, ShouldEmitIndexFiles: ctx.arg.thinLTOEmitIndexFiles,
206 ShouldEmitImportsFiles: ctx.arg.thinLTOEmitImportsFiles);
207 }
208
209 constexpr llvm::lto::LTO::LTOKind ltoModes[3] = {
210 llvm::lto::LTO::LTOKind::LTOK_UnifiedThin,
211 llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular,
212 llvm::lto::LTO::LTOKind::LTOK_Default};
213 if (ctx.arg.dtltoDistributor.empty())
214 ltoObj = std::make_unique<lto::LTO>(args: createConfig(ctx), args&: backend,
215 args&: ctx.arg.ltoPartitions,
216 args: ltoModes[ctx.arg.ltoKind]);
217 else
218 ltoObj = std::make_unique<lto::DTLTO>(
219 args: createConfig(ctx), args&: backend, args&: ctx.arg.ltoPartitions,
220 args: ltoModes[ctx.arg.ltoKind], args&: ctx.arg.outputFile,
221 args: !ctx.arg.saveTempsArgs.empty());
222 // Initialize usedStartStop.
223 if (ctx.bitcodeFiles.empty())
224 return;
225 for (Symbol *sym : ctx.symtab->getSymbols()) {
226 if (sym->isPlaceholder())
227 continue;
228 StringRef s = sym->getName();
229 for (StringRef prefix : {"__start_", "__stop_"})
230 if (s.starts_with(Prefix: prefix))
231 usedStartStop.insert(V: s.substr(Start: prefix.size()));
232 }
233}
234
235BitcodeCompiler::~BitcodeCompiler() = default;
236
237void BitcodeCompiler::add(BitcodeFile &f) {
238 lto::InputFile &obj = *f.obj;
239 bool isExec = !ctx.arg.shared && !ctx.arg.relocatable;
240
241 if (ctx.arg.thinLTOEmitIndexFiles)
242 thinIndices.insert(V: obj.getName());
243
244 ArrayRef<Symbol *> syms = f.getSymbols();
245 ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
246 std::vector<lto::SymbolResolution> resols(syms.size());
247
248 // Provide a resolution to the LTO API for each symbol.
249 for (size_t i = 0, e = syms.size(); i != e; ++i) {
250 Symbol *sym = syms[i];
251 const lto::InputFile::Symbol &objSym = objSyms[i];
252 lto::SymbolResolution &r = resols[i];
253
254 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
255 // reports two symbols for module ASM defined. Without this check, lld
256 // flags an undefined in IR with a definition in ASM as prevailing.
257 // Once IRObjectFile is fixed to report only one symbol this hack can
258 // be removed.
259 r.Prevailing = !objSym.isUndefined() && sym->file == &f;
260
261 // We ask LTO to preserve following global symbols:
262 // 1) All symbols when doing relocatable link, so that them can be used
263 // for doing final link.
264 // 2) Symbols that are used in regular objects.
265 // 3) C named sections if we have corresponding __start_/__stop_ symbol.
266 // 4) Symbols that are defined in bitcode files and used for dynamic
267 // linking.
268 // 5) Symbols that will be referenced after linker wrapping is performed.
269 r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
270 sym->referencedAfterWrap ||
271 (r.Prevailing && sym->isExported) ||
272 usedStartStop.contains(V: objSym.getSectionName());
273 // Identify symbols exported dynamically, and that therefore could be
274 // referenced by a shared library not visible to the linker.
275 r.ExportDynamic = sym->computeBinding(ctx) != STB_LOCAL &&
276 (ctx.arg.exportDynamic || sym->isExported);
277 const auto *dr = dyn_cast<Defined>(Val: sym);
278 r.FinalDefinitionInLinkageUnit =
279 (isExec || sym->visibility() != STV_DEFAULT) && dr &&
280 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
281 // will be generated by for them, triggering linker errors.
282 // Symbol section is always null for bitcode symbols, hence the check
283 // for isElf(). Skip linker script defined symbols as well: they have
284 // no File defined.
285 !(dr->section == nullptr &&
286 (sym->file->isInternal() || sym->file->isElf()));
287
288 if (r.Prevailing)
289 Undefined(ctx.internalFile, StringRef(), STB_GLOBAL, STV_DEFAULT,
290 sym->type)
291 .overwrite(sym&: *sym);
292
293 // We tell LTO to not apply interprocedural optimization for wrapped
294 // (with --wrap) symbols because otherwise LTO would inline them while
295 // their values are still not final.
296 r.LinkerRedefined = sym->scriptDefined;
297 }
298 checkError(eh&: ctx.e, e: ltoObj->add(Obj: std::move(f.obj), Res: resols));
299}
300
301// If LazyObjFile has not been added to link, emit empty index files.
302// This is needed because this is what GNU gold plugin does and we have a
303// distributed build system that depends on that behavior.
304static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) {
305 DenseSet<StringRef> linkedBitCodeFiles;
306 for (BitcodeFile *f : ctx.bitcodeFiles)
307 linkedBitCodeFiles.insert(V: f->getName());
308
309 for (BitcodeFile *f : ctx.lazyBitcodeFiles) {
310 if (!f->lazy)
311 continue;
312 if (linkedBitCodeFiles.contains(V: f->getName()))
313 continue;
314 std::string path =
315 replaceThinLTOSuffix(ctx, path: getThinLTOOutputFile(ctx, modulePath: f->obj->getName()));
316 std::unique_ptr<raw_fd_ostream> os = openFile(file: path + ".thinlto.bc");
317 if (!os)
318 continue;
319
320 ModuleSummaryIndex m(/*HaveGVs*/ false);
321 m.setSkipModuleByDistributedBackend();
322 writeIndexToFile(Index: m, Out&: *os);
323 if (ctx.arg.thinLTOEmitImportsFiles)
324 openFile(file: path + ".imports");
325 }
326}
327
328// Merge all the bitcode files we have seen, codegen the result
329// and return the resulting ObjectFile(s).
330SmallVector<std::unique_ptr<InputFile>, 0> BitcodeCompiler::compile() {
331 unsigned maxTasks = ltoObj->getMaxTasks();
332 buf.resize(N: maxTasks);
333 files.resize(new_size: maxTasks);
334 filenames.resize(N: maxTasks);
335
336 // The --thinlto-cache-dir option specifies the path to a directory in which
337 // to cache native object files for ThinLTO incremental builds. If a path was
338 // specified, configure LTO to use it as the cache directory.
339 FileCache cache;
340 if (!ctx.arg.thinLTOCacheDir.empty())
341 cache = check(e: localCache(CacheNameRef: "ThinLTO", TempFilePrefixRef: "Thin", CacheDirectoryPathRef: ctx.arg.thinLTOCacheDir,
342 AddBuffer: createAddBufferFn(files, filenames)));
343
344 if (!ctx.bitcodeFiles.empty())
345 checkError(eh&: ctx.e, e: ltoObj->run(
346 AddStream: [&](size_t task, const Twine &moduleName) {
347 buf[task].first = moduleName.str();
348 return std::make_unique<CachedFileStream>(
349 args: std::make_unique<raw_svector_ostream>(
350 args&: buf[task].second));
351 },
352 Cache: cache));
353
354 // Emit empty index files for non-indexed files but not in single-module mode.
355 if (ctx.arg.thinLTOModulesToCompile.empty()) {
356 for (StringRef s : thinIndices) {
357 std::string path = getThinLTOOutputFile(ctx, modulePath: s);
358 openFile(file: path + ".thinlto.bc");
359 if (ctx.arg.thinLTOEmitImportsFiles)
360 openFile(file: path + ".imports");
361 }
362 }
363
364 if (ctx.arg.thinLTOEmitIndexFiles)
365 thinLTOCreateEmptyIndexFiles(ctx);
366
367 if (ctx.arg.thinLTOIndexOnly) {
368 if (!ctx.arg.ltoObjPath.empty())
369 saveBuffer(buffer: buf[0].second, path: ctx.arg.ltoObjPath);
370
371 // ThinLTO with index only option is required to generate only the index
372 // files. After that, we exit from linker and ThinLTO backend runs in a
373 // distributed environment.
374 if (indexFile)
375 indexFile->close();
376 return {};
377 }
378
379 if (!ctx.arg.thinLTOCacheDir.empty())
380 pruneCache(Path: ctx.arg.thinLTOCacheDir, Policy: ctx.arg.thinLTOCachePolicy, Files: files);
381
382 if (!ctx.arg.ltoObjPath.empty()) {
383 saveBuffer(buffer: buf[0].second, path: ctx.arg.ltoObjPath);
384 for (unsigned i = 1; i != maxTasks; ++i)
385 saveBuffer(buffer: buf[i].second, path: ctx.arg.ltoObjPath + Twine(i));
386 }
387
388 bool savePrelink = ctx.arg.saveTempsArgs.contains(V: "prelink");
389 SmallVector<std::unique_ptr<InputFile>, 0> ret;
390 const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o";
391 for (unsigned i = 0; i != maxTasks; ++i) {
392 StringRef bitcodeFilePath;
393 StringRef objBuf;
394 if (files[i]) {
395 // When files[i] is not null, it holds a native relocatable file provided
396 // as a MemoryBuffer, for example from the cache or from an external DTLTO
397 // backend compilation. filenames[i] contains the original BitcodeFile's
398 // identifier.
399 objBuf = files[i]->getBuffer();
400 bitcodeFilePath = filenames[i];
401 } else {
402 // Get the native relocatable file after in-process LTO compilation.
403 objBuf = buf[i].second;
404 bitcodeFilePath = buf[i].first;
405 }
406 if (objBuf.empty())
407 continue;
408
409 // If the input bitcode file is path/to/x.o and -o specifies a.out, the
410 // corresponding native relocatable file path will look like:
411 // path/to/a.out.lto.x.o.
412 StringRef ltoObjName;
413 if (bitcodeFilePath == "ld-temp.o") {
414 ltoObjName =
415 ctx.saver.save(S: Twine(ctx.arg.outputFile) + ".lto" +
416 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
417 } else {
418 StringRef directory = sys::path::parent_path(path: bitcodeFilePath);
419 // For an archive member, which has an identifier like "d/a.a(coll.o at
420 // 8)" (see BitcodeFile::BitcodeFile), use the filename; otherwise, use
421 // the stem (d/a.o => a).
422 StringRef baseName = bitcodeFilePath.ends_with(Suffix: ")")
423 ? sys::path::filename(path: bitcodeFilePath)
424 : sys::path::stem(path: bitcodeFilePath);
425 StringRef outputFileBaseName = sys::path::filename(path: ctx.arg.outputFile);
426 SmallString<256> path;
427 sys::path::append(path, a: directory,
428 b: outputFileBaseName + ".lto." + baseName + ext);
429 sys::path::remove_dots(path, remove_dot_dot: true);
430 ltoObjName = ctx.saver.save(S: path.str());
431 }
432 if (savePrelink || ctx.arg.ltoEmitAsm)
433 saveBuffer(buffer: buf[i].second, path: ltoObjName);
434 if (!ctx.arg.ltoEmitAsm)
435 ret.push_back(Elt: createObjFile(ctx, mb: MemoryBufferRef(objBuf, ltoObjName)));
436 }
437 return ret;
438}
439