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