1//===-- llvm-lto2: test harness for the resolution-based LTO interface ----===//
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// This program takes in a list of bitcode files, links them and performs
10// link-time optimization according to the provided symbol resolutions using the
11// resolution-based LTO interface, and outputs one or more object files.
12//
13// This program is intended to eventually replace llvm-lto which uses the legacy
14// LTO interface.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/Bitcode/BitcodeReader.h"
19#include "llvm/CodeGen/CommandFlags.h"
20#include "llvm/IR/DiagnosticPrinter.h"
21#include "llvm/LTO/LTO.h"
22#include "llvm/Passes/PassPlugin.h"
23#include "llvm/Remarks/HotnessThresholdParser.h"
24#include "llvm/Support/Caching.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/InitLLVM.h"
28#include "llvm/Support/PluginLoader.h"
29#include "llvm/Support/TargetSelect.h"
30#include "llvm/Support/Threading.h"
31#include <atomic>
32
33using namespace llvm;
34using namespace lto;
35
36static codegen::RegisterCodeGenFlags CGF;
37
38static cl::opt<char>
39 OptLevel("O",
40 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
41 "(default = '-O2')"),
42 cl::Prefix, cl::init(Val: '2'));
43
44static cl::opt<char> CGOptLevel(
45 "cg-opt-level",
46 cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"),
47 cl::init(Val: '2'));
48
49static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
50 cl::desc("<input bitcode files>"));
51
52static cl::opt<std::string> OutputFilename("o", cl::Required,
53 cl::desc("Output filename"),
54 cl::value_desc("filename"));
55
56static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
57 cl::value_desc("directory"));
58
59static cl::opt<std::string> OptPipeline("opt-pipeline",
60 cl::desc("Optimizer Pipeline"),
61 cl::value_desc("pipeline"));
62
63static cl::opt<std::string> AAPipeline("aa-pipeline",
64 cl::desc("Alias Analysis Pipeline"),
65 cl::value_desc("aapipeline"));
66
67static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
68
69static cl::list<std::string> SelectSaveTemps(
70 "select-save-temps",
71 cl::value_desc("One, or multiple of: "
72 "resolution,preopt,promote,internalize,import,opt,precodegen"
73 ",combinedindex"),
74 cl::desc("Save selected temporary files. Cannot be specified together with "
75 "-save-temps"),
76 cl::CommaSeparated);
77
78constexpr const char *SaveTempsValues[] = {
79 "resolution", "preopt", "promote", "internalize",
80 "import", "opt", "precodegen", "combinedindex"};
81
82static cl::opt<bool>
83 ThinLTODistributedIndexes("thinlto-distributed-indexes",
84 cl::desc("Write out individual index and "
85 "import files for the "
86 "distributed backend case"));
87
88static cl::opt<bool>
89 ThinLTOEmitIndexes("thinlto-emit-indexes",
90 cl::desc("Write out individual index files via "
91 "InProcessThinLTO"));
92
93static cl::opt<bool>
94 ThinLTOEmitImports("thinlto-emit-imports",
95 cl::desc("Write out individual imports files via "
96 "InProcessThinLTO. Has no effect unless "
97 "specified with -thinlto-emit-indexes or "
98 "-thinlto-distributed-indexes"));
99
100// Default to using all available threads in the system, but using only one
101// thread per core (no SMT).
102// Use -thinlto-threads=all to use hardware_concurrency() instead, which means
103// to use all hardware threads or cores in the system.
104static cl::opt<std::string> Threads("thinlto-threads");
105
106static cl::list<std::string> SymbolResolutions(
107 "r",
108 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
109 "where \"resolution\" is a sequence (which may be empty) of the\n"
110 "following characters:\n"
111 " p - prevailing: the linker has chosen this definition of the\n"
112 " symbol\n"
113 " l - local: the definition of this symbol is unpreemptable at\n"
114 " runtime and is known to be in this linkage unit\n"
115 " x - externally visible: the definition of this symbol is\n"
116 " visible outside of the LTO unit\n"
117 "A resolution for each symbol must be specified"));
118
119static cl::opt<std::string> OverrideTriple(
120 "override-triple",
121 cl::desc("Replace target triples in input files with this triple"));
122
123static cl::opt<std::string> DefaultTriple(
124 "default-triple",
125 cl::desc(
126 "Replace unspecified target triples in input files with this triple"));
127
128static cl::opt<bool> RemarksWithHotness(
129 "pass-remarks-with-hotness",
130 cl::desc("With PGO, include profile count in optimization remarks"),
131 cl::Hidden);
132
133cl::opt<std::optional<uint64_t>, false, remarks::HotnessThresholdParser>
134 RemarksHotnessThreshold(
135 "pass-remarks-hotness-threshold",
136 cl::desc("Minimum profile count required for an "
137 "optimization remark to be output."
138 " Use 'auto' to apply the threshold from profile summary."),
139 cl::value_desc("uint or 'auto'"), cl::init(Val: 0), cl::Hidden);
140
141static cl::opt<std::string>
142 RemarksFilename("pass-remarks-output",
143 cl::desc("Output filename for pass remarks"),
144 cl::value_desc("filename"));
145
146static cl::opt<std::string>
147 RemarksPasses("pass-remarks-filter",
148 cl::desc("Only record optimization remarks from passes whose "
149 "names match the given regular expression"),
150 cl::value_desc("regex"));
151
152static cl::opt<std::string> RemarksFormat(
153 "pass-remarks-format",
154 cl::desc("The format used for serializing remarks (default: YAML)"),
155 cl::value_desc("format"), cl::init(Val: "yaml"));
156
157static cl::opt<std::string>
158 SamplePGOFile("lto-sample-profile-file",
159 cl::desc("Specify a SamplePGO profile file"));
160
161static cl::opt<std::string>
162 CSPGOFile("lto-cspgo-profile-file",
163 cl::desc("Specify a context sensitive PGO profile file"));
164
165static cl::opt<bool>
166 RunCSIRInstr("lto-cspgo-gen",
167 cl::desc("Run PGO context sensitive IR instrumentation"),
168 cl::Hidden);
169
170static cl::opt<bool>
171 DebugPassManager("debug-pass-manager", cl::Hidden,
172 cl::desc("Print pass management debugging information"));
173
174static cl::opt<std::string>
175 StatsFile("stats-file", cl::desc("Filename to write statistics to"));
176
177static cl::list<std::string>
178 PassPlugins("load-pass-plugin",
179 cl::desc("Load passes from plugin library"));
180
181static cl::opt<std::string> UnifiedLTOMode("unified-lto", cl::Optional,
182 cl::desc("Set LTO mode"),
183 cl::value_desc("mode"));
184
185static cl::opt<bool> EnableFreestanding(
186 "lto-freestanding",
187 cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"),
188 cl::Hidden);
189
190static cl::opt<bool> TryUseNewDbgInfoFormat(
191 "try-experimental-debuginfo-iterators",
192 cl::desc("Enable debuginfo iterator positions, if they're built in"),
193 cl::init(Val: false), cl::Hidden);
194
195extern cl::opt<bool> UseNewDbgInfoFormat;
196extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
197extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
198
199static void check(Error E, std::string Msg) {
200 if (!E)
201 return;
202 handleAllErrors(E: std::move(E), Handlers: [&](ErrorInfoBase &EIB) {
203 errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';
204 });
205 exit(status: 1);
206}
207
208template <typename T> static T check(Expected<T> E, std::string Msg) {
209 if (E)
210 return std::move(*E);
211 check(E.takeError(), Msg);
212 return T();
213}
214
215static void check(std::error_code EC, std::string Msg) {
216 check(E: errorCodeToError(EC), Msg);
217}
218
219template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
220 if (E)
221 return std::move(*E);
222 check(E.getError(), Msg);
223 return T();
224}
225
226static int usage() {
227 errs() << "Available subcommands: dump-symtab run\n";
228 return 1;
229}
230
231static int run(int argc, char **argv) {
232 cl::ParseCommandLineOptions(argc, argv, Overview: "Resolution-based LTO test harness");
233 // Load bitcode into the new debug info format by default.
234 if (LoadBitcodeIntoNewDbgInfoFormat == cl::boolOrDefault::BOU_UNSET)
235 LoadBitcodeIntoNewDbgInfoFormat = cl::boolOrDefault::BOU_TRUE;
236
237 // RemoveDIs debug-info transition: tests may request that we /try/ to use the
238 // new debug-info format.
239 if (TryUseNewDbgInfoFormat) {
240 // Turn the new debug-info format on.
241 UseNewDbgInfoFormat = true;
242 }
243 // Since llvm-lto2 collects multiple IR modules together, for simplicity's
244 // sake we disable the "PreserveInputDbgFormat" flag to enforce a single debug
245 // info format.
246 PreserveInputDbgFormat = cl::boolOrDefault::BOU_FALSE;
247
248 // FIXME: Workaround PR30396 which means that a symbol can appear
249 // more than once if it is defined in module-level assembly and
250 // has a GV declaration. We allow (file, symbol) pairs to have multiple
251 // resolutions and apply them in the order observed.
252 std::map<std::pair<std::string, std::string>, std::list<SymbolResolution>>
253 CommandLineResolutions;
254 for (std::string R : SymbolResolutions) {
255 StringRef Rest = R;
256 StringRef FileName, SymbolName;
257 std::tie(args&: FileName, args&: Rest) = Rest.split(Separator: ',');
258 if (Rest.empty()) {
259 llvm::errs() << "invalid resolution: " << R << '\n';
260 return 1;
261 }
262 std::tie(args&: SymbolName, args&: Rest) = Rest.split(Separator: ',');
263 SymbolResolution Res;
264 for (char C : Rest) {
265 if (C == 'p')
266 Res.Prevailing = true;
267 else if (C == 'l')
268 Res.FinalDefinitionInLinkageUnit = true;
269 else if (C == 'x')
270 Res.VisibleToRegularObj = true;
271 else if (C == 'r')
272 Res.LinkerRedefined = true;
273 else {
274 llvm::errs() << "invalid character " << C << " in resolution: " << R
275 << '\n';
276 return 1;
277 }
278 }
279 CommandLineResolutions[{std::string(FileName), std::string(SymbolName)}]
280 .push_back(x: Res);
281 }
282
283 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
284
285 Config Conf;
286
287 Conf.CPU = codegen::getMCPU();
288 Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple: Triple());
289 Conf.MAttrs = codegen::getMAttrs();
290 if (auto RM = codegen::getExplicitRelocModel())
291 Conf.RelocModel = *RM;
292 Conf.CodeModel = codegen::getExplicitCodeModel();
293
294 Conf.DebugPassManager = DebugPassManager;
295
296 if (SaveTemps && !SelectSaveTemps.empty()) {
297 llvm::errs() << "-save-temps cannot be specified with -select-save-temps\n";
298 return 1;
299 }
300 if (SaveTemps || !SelectSaveTemps.empty()) {
301 DenseSet<StringRef> SaveTempsArgs;
302 for (auto &S : SelectSaveTemps)
303 if (is_contained(Range: SaveTempsValues, Element: S))
304 SaveTempsArgs.insert(V: S);
305 else {
306 llvm::errs() << ("invalid -select-save-temps argument: " + S) << '\n';
307 return 1;
308 }
309 check(E: Conf.addSaveTemps(OutputFileName: OutputFilename + ".", UseInputModulePath: false, SaveTempsArgs),
310 Msg: "Config::addSaveTemps failed");
311 }
312
313 // Optimization remarks.
314 Conf.RemarksFilename = RemarksFilename;
315 Conf.RemarksPasses = RemarksPasses;
316 Conf.RemarksWithHotness = RemarksWithHotness;
317 Conf.RemarksHotnessThreshold = RemarksHotnessThreshold;
318 Conf.RemarksFormat = RemarksFormat;
319
320 Conf.SampleProfile = SamplePGOFile;
321 Conf.CSIRProfile = CSPGOFile;
322 Conf.RunCSIRInstr = RunCSIRInstr;
323
324 // Run a custom pipeline, if asked for.
325 Conf.OptPipeline = OptPipeline;
326 Conf.AAPipeline = AAPipeline;
327
328 Conf.OptLevel = OptLevel - '0';
329 Conf.Freestanding = EnableFreestanding;
330 for (auto &PluginFN : PassPlugins)
331 Conf.PassPlugins.push_back(x: PluginFN);
332 if (auto Level = CodeGenOpt::parseLevel(C: CGOptLevel)) {
333 Conf.CGOptLevel = *Level;
334 } else {
335 llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
336 return 1;
337 }
338
339 if (auto FT = codegen::getExplicitFileType())
340 Conf.CGFileType = *FT;
341
342 Conf.OverrideTriple = OverrideTriple;
343 Conf.DefaultTriple = DefaultTriple;
344 Conf.StatsFile = StatsFile;
345 Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
346 Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
347
348 ThinBackend Backend;
349 if (ThinLTODistributedIndexes)
350 Backend = createWriteIndexesThinBackend(/*OldPrefix=*/"",
351 /*NewPrefix=*/"",
352 /*NativeObjectPrefix=*/"",
353 ShouldEmitImportsFiles: ThinLTOEmitImports,
354 /*LinkedObjectsFile=*/nullptr,
355 /*OnWrite=*/{});
356 else
357 Backend = createInProcessThinBackend(
358 Parallelism: llvm::heavyweight_hardware_concurrency(Num: Threads),
359 /* OnWrite */ {}, ShouldEmitIndexFiles: ThinLTOEmitIndexes, ShouldEmitImportsFiles: ThinLTOEmitImports);
360
361 // Track whether we hit an error; in particular, in the multi-threaded case,
362 // we can't exit() early because the rest of the threads wouldn't have had a
363 // change to be join-ed, and that would result in a "terminate called without
364 // an active exception". Altogether, this results in nondeterministic
365 // behavior. Instead, we don't exit in the multi-threaded case, but we make
366 // sure to report the error and then at the end (after joining cleanly)
367 // exit(1).
368 std::atomic<bool> HasErrors;
369 std::atomic_init(a: &HasErrors, i: false);
370 Conf.DiagHandler = [&](const DiagnosticInfo &DI) {
371 DiagnosticPrinterRawOStream DP(errs());
372 DI.print(DP);
373 errs() << '\n';
374 if (DI.getSeverity() == DS_Error)
375 HasErrors = true;
376 };
377
378 LTO::LTOKind LTOMode = LTO::LTOK_Default;
379
380 if (UnifiedLTOMode == "full") {
381 LTOMode = LTO::LTOK_UnifiedRegular;
382 } else if (UnifiedLTOMode == "thin") {
383 LTOMode = LTO::LTOK_UnifiedThin;
384 } else if (UnifiedLTOMode == "default") {
385 LTOMode = LTO::LTOK_Default;
386 } else if (!UnifiedLTOMode.empty()) {
387 llvm::errs() << "invalid LTO mode\n";
388 return 1;
389 }
390
391 LTO Lto(std::move(Conf), std::move(Backend), 1, LTOMode);
392
393 for (std::string F : InputFilenames) {
394 std::unique_ptr<MemoryBuffer> MB = check(E: MemoryBuffer::getFile(Filename: F), Msg: F);
395 std::unique_ptr<InputFile> Input =
396 check(E: InputFile::create(Object: MB->getMemBufferRef()), Msg: F);
397
398 std::vector<SymbolResolution> Res;
399 for (const InputFile::Symbol &Sym : Input->symbols()) {
400 auto I = CommandLineResolutions.find(x: {F, std::string(Sym.getName())});
401 // If it isn't found, look for ".", which would have been added
402 // (followed by a hash) when the symbol was promoted during module
403 // splitting if it was defined in one part and used in the other.
404 // Try looking up the symbol name before the suffix.
405 if (I == CommandLineResolutions.end()) {
406 auto SplitName = Sym.getName().rsplit(Separator: ".");
407 I = CommandLineResolutions.find(x: {F, std::string(SplitName.first)});
408 }
409 if (I == CommandLineResolutions.end()) {
410 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
411 << ',' << Sym.getName() << '\n';
412 HasErrors = true;
413 } else {
414 Res.push_back(x: I->second.front());
415 I->second.pop_front();
416 if (I->second.empty())
417 CommandLineResolutions.erase(position: I);
418 }
419 }
420
421 if (HasErrors)
422 continue;
423
424 MBs.push_back(x: std::move(MB));
425 check(E: Lto.add(Obj: std::move(Input), Res), Msg: F);
426 }
427
428 if (!CommandLineResolutions.empty()) {
429 HasErrors = true;
430 for (auto UnusedRes : CommandLineResolutions)
431 llvm::errs() << argv[0] << ": unused symbol resolution for "
432 << UnusedRes.first.first << ',' << UnusedRes.first.second
433 << '\n';
434 }
435 if (HasErrors)
436 return 1;
437
438 auto AddStream =
439 [&](size_t Task,
440 const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
441 std::string Path = OutputFilename + "." + utostr(X: Task);
442
443 std::error_code EC;
444 auto S = std::make_unique<raw_fd_ostream>(args&: Path, args&: EC, args: sys::fs::OF_None);
445 check(EC, Msg: Path);
446 return std::make_unique<CachedFileStream>(args: std::move(S), args&: Path);
447 };
448
449 auto AddBuffer = [&](size_t Task, const Twine &ModuleName,
450 std::unique_ptr<MemoryBuffer> MB) {
451 *AddStream(Task, ModuleName)->OS << MB->getBuffer();
452 };
453
454 FileCache Cache;
455 if (!CacheDir.empty())
456 Cache = check(E: localCache(CacheNameRef: "ThinLTO", TempFilePrefixRef: "Thin", CacheDirectoryPathRef: CacheDir, AddBuffer),
457 Msg: "failed to create cache");
458
459 check(E: Lto.run(AddStream, Cache), Msg: "LTO::run failed");
460 return static_cast<int>(HasErrors);
461}
462
463static int dumpSymtab(int argc, char **argv) {
464 for (StringRef F : make_range(x: argv + 1, y: argv + argc)) {
465 std::unique_ptr<MemoryBuffer> MB =
466 check(E: MemoryBuffer::getFile(Filename: F), Msg: std::string(F));
467 BitcodeFileContents BFC =
468 check(E: getBitcodeFileContents(Buffer: *MB), Msg: std::string(F));
469
470 if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
471 auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(
472 BFC.Symtab.data());
473 outs() << "version: " << Hdr->Version << '\n';
474 if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
475 outs() << "producer: " << Hdr->Producer.get(Strtab: BFC.StrtabForSymtab)
476 << '\n';
477 }
478
479 std::unique_ptr<InputFile> Input =
480 check(E: InputFile::create(Object: MB->getMemBufferRef()), Msg: std::string(F));
481
482 outs() << "target triple: " << Input->getTargetTriple() << '\n';
483 Triple TT(Input->getTargetTriple());
484
485 outs() << "source filename: " << Input->getSourceFileName() << '\n';
486
487 if (TT.isOSBinFormatCOFF())
488 outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';
489
490 if (TT.isOSBinFormatELF()) {
491 outs() << "dependent libraries:";
492 for (auto L : Input->getDependentLibraries())
493 outs() << " \"" << L << "\"";
494 outs() << '\n';
495 }
496
497 ArrayRef<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable =
498 Input->getComdatTable();
499 for (const InputFile::Symbol &Sym : Input->symbols()) {
500 switch (Sym.getVisibility()) {
501 case GlobalValue::HiddenVisibility:
502 outs() << 'H';
503 break;
504 case GlobalValue::ProtectedVisibility:
505 outs() << 'P';
506 break;
507 case GlobalValue::DefaultVisibility:
508 outs() << 'D';
509 break;
510 }
511
512 auto PrintBool = [&](char C, bool B) { outs() << (B ? C : '-'); };
513 PrintBool('U', Sym.isUndefined());
514 PrintBool('C', Sym.isCommon());
515 PrintBool('W', Sym.isWeak());
516 PrintBool('I', Sym.isIndirect());
517 PrintBool('O', Sym.canBeOmittedFromSymbolTable());
518 PrintBool('T', Sym.isTLS());
519 PrintBool('X', Sym.isExecutable());
520 outs() << ' ' << Sym.getName() << '\n';
521
522 if (Sym.isCommon())
523 outs() << " size " << Sym.getCommonSize() << " align "
524 << Sym.getCommonAlignment() << '\n';
525
526 int Comdat = Sym.getComdatIndex();
527 if (Comdat != -1) {
528 outs() << " comdat ";
529 switch (ComdatTable[Comdat].second) {
530 case Comdat::Any:
531 outs() << "any";
532 break;
533 case Comdat::ExactMatch:
534 outs() << "exactmatch";
535 break;
536 case Comdat::Largest:
537 outs() << "largest";
538 break;
539 case Comdat::NoDeduplicate:
540 outs() << "nodeduplicate";
541 break;
542 case Comdat::SameSize:
543 outs() << "samesize";
544 break;
545 }
546 outs() << ' ' << ComdatTable[Comdat].first << '\n';
547 }
548
549 if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())
550 outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';
551
552 if (!Sym.getSectionName().empty())
553 outs() << " section " << Sym.getSectionName() << "\n";
554 }
555
556 outs() << '\n';
557 }
558
559 return 0;
560}
561
562int main(int argc, char **argv) {
563 InitLLVM X(argc, argv);
564 InitializeAllTargets();
565 InitializeAllTargetMCs();
566 InitializeAllAsmPrinters();
567 InitializeAllAsmParsers();
568
569 // FIXME: This should use llvm::cl subcommands, but it isn't currently
570 // possible to pass an argument not associated with a subcommand to a
571 // subcommand (e.g. -use-new-pm).
572 if (argc < 2)
573 return usage();
574
575 StringRef Subcommand = argv[1];
576 // Ensure that argv[0] is correct after adjusting argv/argc.
577 argv[1] = argv[0];
578 if (Subcommand == "dump-symtab")
579 return dumpSymtab(argc: argc - 1, argv: argv + 1);
580 if (Subcommand == "run")
581 return run(argc: argc - 1, argv: argv + 1);
582 return usage();
583}
584

source code of llvm/tools/llvm-lto2/llvm-lto2.cpp