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