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 {
195 backend = lto::createInProcessThinBackend(
196 Parallelism: llvm::heavyweight_hardware_concurrency(Num: ctx.arg.thinLTOJobs),
197 OnWrite: onIndexWrite, ShouldEmitIndexFiles: ctx.arg.thinLTOEmitIndexFiles,
198 ShouldEmitImportsFiles: ctx.arg.thinLTOEmitImportsFiles);
199 }
200
201 constexpr llvm::lto::LTO::LTOKind ltoModes[3] = {
202 llvm::lto::LTO::LTOKind::LTOK_UnifiedThin,
203 llvm::lto::LTO::LTOKind::LTOK_UnifiedRegular,
204 llvm::lto::LTO::LTOKind::LTOK_Default};
205
206 if (ctx.arg.dtltoDistributor.empty())
207 ltoObj = std::make_unique<lto::LTO>(args: createConfig(ctx), args&: backend,
208 args&: ctx.arg.ltoPartitions,
209 args: ltoModes[ctx.arg.ltoKind]);
210 else
211 ltoObj = std::make_unique<lto::DTLTO>(
212 args: createConfig(ctx), args&: ctx.arg.ltoPartitions, args: ltoModes[ctx.arg.ltoKind],
213 args&: onIndexWrite, args&: ctx.arg.thinLTOEmitIndexFiles,
214 args&: ctx.arg.thinLTOEmitImportsFiles, args&: ctx.arg.outputFile,
215 args&: ctx.arg.dtltoDistributor, args&: ctx.arg.dtltoDistributorArgs,
216 args&: ctx.arg.dtltoCompiler, args&: ctx.arg.dtltoCompilerPrependArgs,
217 args&: ctx.arg.dtltoCompilerArgs, args: createAddBufferFn(files, filenames),
218 args: !ctx.arg.saveTempsArgs.empty());
219 // Initialize usedStartStop.
220 if (ctx.bitcodeFiles.empty())
221 return;
222 for (Symbol *sym : ctx.symtab->getSymbols()) {
223 if (sym->isPlaceholder())
224 continue;
225 StringRef s = sym->getName();
226 for (StringRef prefix : {"__start_", "__stop_"})
227 if (s.starts_with(Prefix: prefix))
228 usedStartStop.insert(V: s.substr(Start: prefix.size()));
229 }
230}
231
232BitcodeCompiler::~BitcodeCompiler() = default;
233
234void BitcodeCompiler::waitForLTOCleanup() {
235 if (ltoObj)
236 ltoObj->waitForCleanup();
237}
238
239void BitcodeCompiler::add(BitcodeFile &f) {
240 lto::InputFile &obj = *f.obj;
241 bool isExec = !ctx.arg.shared && !ctx.arg.relocatable;
242
243 if (ctx.arg.thinLTOEmitIndexFiles)
244 thinIndices.insert(V: obj.getName());
245
246 ArrayRef<Symbol *> syms = f.getSymbols();
247 ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
248 std::vector<lto::SymbolResolution> resols(syms.size());
249
250 // Provide a resolution to the LTO API for each symbol.
251 for (size_t i = 0, e = syms.size(); i != e; ++i) {
252 Symbol *sym = syms[i];
253 const lto::InputFile::Symbol &objSym = objSyms[i];
254 lto::SymbolResolution &r = resols[i];
255
256 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
257 // reports two symbols for module ASM defined. Without this check, lld
258 // flags an undefined in IR with a definition in ASM as prevailing.
259 // Once IRObjectFile is fixed to report only one symbol this hack can
260 // be removed.
261 r.Prevailing = !objSym.isUndefined() && sym->file == &f;
262
263 // We ask LTO to preserve following global symbols:
264 // 1) All symbols when doing relocatable link, so that them can be used
265 // for doing final link.
266 // 2) Symbols that are used in regular objects.
267 // 3) C named sections if we have corresponding __start_/__stop_ symbol.
268 // 4) Symbols that are defined in bitcode files and used for dynamic
269 // linking.
270 // 5) Symbols that will be referenced after linker wrapping is performed.
271 r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
272 sym->referencedAfterWrap ||
273 (r.Prevailing && sym->isExported) ||
274 usedStartStop.contains(V: objSym.getSectionName());
275 // Identify symbols exported dynamically, and that therefore could be
276 // referenced by a shared library not visible to the linker.
277 r.ExportDynamic = sym->computeBinding(ctx) != STB_LOCAL &&
278 (ctx.arg.exportDynamic || sym->isExported);
279 const auto *dr = dyn_cast<Defined>(Val: sym);
280 r.FinalDefinitionInLinkageUnit =
281 (isExec || sym->visibility() != STV_DEFAULT) && dr &&
282 // Skip absolute symbols from ELF objects, otherwise PC-rel relocations
283 // will be generated by for them, triggering linker errors.
284 // Symbol section is always null for bitcode symbols, hence the check
285 // for isElf(). Skip linker script defined symbols as well: they have
286 // no File defined.
287 !(dr->section == nullptr &&
288 (sym->file->isInternal() || sym->file->isElf()));
289
290 if (r.Prevailing)
291 Undefined(ctx.internalFile, StringRef(), STB_GLOBAL, STV_DEFAULT,
292 sym->type)
293 .overwrite(sym&: *sym);
294
295 // We tell LTO to not apply interprocedural optimization for wrapped
296 // (with --wrap) symbols because otherwise LTO would inline them while
297 // their values are still not final.
298 r.LinkerRedefined = sym->scriptDefined;
299 }
300 checkError(eh&: ctx.e, e: ltoObj->add(Obj: std::move(f.obj), Res: resols));
301}
302
303// If LazyObjFile has not been added to link, emit empty index files.
304// This is needed because this is what GNU gold plugin does and we have a
305// distributed build system that depends on that behavior.
306static void thinLTOCreateEmptyIndexFiles(Ctx &ctx) {
307 DenseSet<StringRef> linkedBitCodeFiles;
308 for (BitcodeFile *f : ctx.bitcodeFiles)
309 linkedBitCodeFiles.insert(V: f->getName());
310
311 for (BitcodeFile *f : ctx.lazyBitcodeFiles) {
312 if (!f->lazy)
313 continue;
314 if (linkedBitCodeFiles.contains(V: f->getName()))
315 continue;
316 std::string path =
317 replaceThinLTOSuffix(ctx, path: getThinLTOOutputFile(ctx, modulePath: f->obj->getName()));
318 std::unique_ptr<raw_fd_ostream> os = openFile(file: path + ".thinlto.bc");
319 if (!os)
320 continue;
321
322 ModuleSummaryIndex m(/*HaveGVs*/ false);
323 m.setSkipModuleByDistributedBackend();
324 writeIndexToFile(Index: m, Out&: *os);
325 if (ctx.arg.thinLTOEmitImportsFiles)
326 openFile(file: path + ".imports");
327 }
328}
329
330// Merge all the bitcode files we have seen, codegen the result
331// and return the resulting ObjectFile(s).
332SmallVector<std::unique_ptr<InputFile>, 0> BitcodeCompiler::compile() {
333 unsigned maxTasks = ltoObj->getMaxTasks();
334 buf.resize(N: maxTasks);
335 files.resize(new_size: maxTasks);
336 filenames.resize(N: maxTasks);
337
338 // The --thinlto-cache-dir option specifies the path to a directory in which
339 // to cache native object files for ThinLTO incremental builds. If a path was
340 // specified, configure LTO to use it as the cache directory.
341 FileCache cache;
342 if (!ctx.arg.thinLTOCacheDir.empty())
343 cache = check(e: localCache(CacheNameRef: "ThinLTO", TempFilePrefixRef: "Thin", CacheDirectoryPathRef: ctx.arg.thinLTOCacheDir,
344 AddBuffer: createAddBufferFn(files, filenames)));
345
346 if (!ctx.bitcodeFiles.empty())
347 checkError(eh&: ctx.e, e: ltoObj->run(
348 AddStream: [&](size_t task, const Twine &moduleName) {
349 buf[task].first = moduleName.str();
350 return std::make_unique<CachedFileStream>(
351 args: std::make_unique<raw_svector_ostream>(
352 args&: buf[task].second));
353 },
354 Cache: cache));
355
356 // Emit empty index files for non-indexed files but not in single-module mode.
357 if (ctx.arg.thinLTOModulesToCompile.empty()) {
358 for (StringRef s : thinIndices) {
359 std::string path = getThinLTOOutputFile(ctx, modulePath: s);
360 openFile(file: path + ".thinlto.bc");
361 if (ctx.arg.thinLTOEmitImportsFiles)
362 openFile(file: path + ".imports");
363 }
364 }
365
366 if (ctx.arg.thinLTOEmitIndexFiles)
367 thinLTOCreateEmptyIndexFiles(ctx);
368
369 if (ctx.arg.thinLTOIndexOnly) {
370 if (!ctx.arg.ltoObjPath.empty())
371 saveBuffer(buffer: buf[0].second, path: ctx.arg.ltoObjPath);
372
373 // ThinLTO with index only option is required to generate only the index
374 // files. After that, we exit from linker and ThinLTO backend runs in a
375 // distributed environment.
376 if (indexFile)
377 indexFile->close();
378 return {};
379 }
380
381 if (!ctx.arg.thinLTOCacheDir.empty())
382 check(
383 e: pruneCache(Path: ctx.arg.thinLTOCacheDir, Policy: ctx.arg.thinLTOCachePolicy, Files: files));
384
385 if (!ctx.arg.ltoObjPath.empty()) {
386 saveBuffer(buffer: buf[0].second, path: ctx.arg.ltoObjPath);
387 for (unsigned i = 1; i != maxTasks; ++i)
388 saveBuffer(buffer: buf[i].second, path: ctx.arg.ltoObjPath + Twine(i));
389 }
390
391 bool savePrelink = ctx.arg.saveTempsArgs.contains(V: "prelink");
392 SmallVector<std::unique_ptr<InputFile>, 0> ret;
393 const char *ext = ctx.arg.ltoEmitAsm ? ".s" : ".o";
394 for (unsigned i = 0; i != maxTasks; ++i) {
395 StringRef bitcodeFilePath;
396 StringRef objBuf;
397 if (files[i]) {
398 // When files[i] is not null, it holds a native relocatable file provided
399 // as a MemoryBuffer, for example from the cache or from an external DTLTO
400 // backend compilation. filenames[i] contains the original BitcodeFile's
401 // identifier.
402 objBuf = files[i]->getBuffer();
403 bitcodeFilePath = filenames[i];
404 } else {
405 // Get the native relocatable file after in-process LTO compilation.
406 objBuf = buf[i].second;
407 bitcodeFilePath = buf[i].first;
408 }
409 if (objBuf.empty())
410 continue;
411
412 // If the input bitcode file is path/to/x.o and -o specifies a.out, the
413 // corresponding native relocatable file path will look like:
414 // path/to/a.out.lto.x.o.
415 StringRef ltoObjName;
416 if (bitcodeFilePath == "ld-temp.o") {
417 ltoObjName =
418 ctx.saver.save(S: Twine(ctx.arg.outputFile) + ".lto" +
419 (i == 0 ? Twine("") : Twine('.') + Twine(i)) + ext);
420 } else {
421 StringRef directory = sys::path::parent_path(path: bitcodeFilePath);
422 // For an archive member, which has an identifier like "d/a.a(coll.o at
423 // 8)" (see BitcodeFile::BitcodeFile), use the filename; otherwise, use
424 // the stem (d/a.o => a).
425 StringRef baseName = bitcodeFilePath.ends_with(Suffix: ")")
426 ? sys::path::filename(path: bitcodeFilePath)
427 : sys::path::stem(path: bitcodeFilePath);
428 StringRef outputFileBaseName = sys::path::filename(path: ctx.arg.outputFile);
429 SmallString<256> path;
430 sys::path::append(path, a: directory,
431 b: outputFileBaseName + ".lto." + baseName + ext);
432 sys::path::remove_dots(path, remove_dot_dot: true);
433 ltoObjName = ctx.saver.save(S: path.str());
434 }
435 if (savePrelink || ctx.arg.ltoEmitAsm)
436 saveBuffer(buffer: buf[i].second, path: ltoObjName);
437 if (!ctx.arg.ltoEmitAsm)
438 ret.push_back(Elt: createObjFile(ctx, mb: MemoryBufferRef(objBuf, ltoObjName)));
439 }
440 return ret;
441}
442
443void BitcodeCompiler::setBitcodeLibFuncs(ArrayRef<StringRef> bitcodeLibFuncs) {
444 ltoObj->setBitcodeLibFuncs(bitcodeLibFuncs);
445}
446