Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/llvm/tools/llvm-lto2/llvm-lto2.cpp
35260 views
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
33
using namespace llvm;
34
using namespace lto;
35
36
static codegen::RegisterCodeGenFlags CGF;
37
38
static cl::opt<char>
39
OptLevel("O",
40
cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
41
"(default = '-O2')"),
42
cl::Prefix, cl::init('2'));
43
44
static cl::opt<char> CGOptLevel(
45
"cg-opt-level",
46
cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"),
47
cl::init('2'));
48
49
static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
50
cl::desc("<input bitcode files>"));
51
52
static cl::opt<std::string> OutputFilename("o", cl::Required,
53
cl::desc("Output filename"),
54
cl::value_desc("filename"));
55
56
static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
57
cl::value_desc("directory"));
58
59
static cl::opt<std::string> OptPipeline("opt-pipeline",
60
cl::desc("Optimizer Pipeline"),
61
cl::value_desc("pipeline"));
62
63
static cl::opt<std::string> AAPipeline("aa-pipeline",
64
cl::desc("Alias Analysis Pipeline"),
65
cl::value_desc("aapipeline"));
66
67
static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
68
69
static 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
78
constexpr const char *SaveTempsValues[] = {
79
"resolution", "preopt", "promote", "internalize",
80
"import", "opt", "precodegen", "combinedindex"};
81
82
static 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
88
static cl::opt<bool>
89
ThinLTOEmitIndexes("thinlto-emit-indexes",
90
cl::desc("Write out individual index files via "
91
"InProcessThinLTO"));
92
93
static 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.
104
static cl::opt<std::string> Threads("thinlto-threads");
105
106
static 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
119
static cl::opt<std::string> OverrideTriple(
120
"override-triple",
121
cl::desc("Replace target triples in input files with this triple"));
122
123
static cl::opt<std::string> DefaultTriple(
124
"default-triple",
125
cl::desc(
126
"Replace unspecified target triples in input files with this triple"));
127
128
static cl::opt<bool> RemarksWithHotness(
129
"pass-remarks-with-hotness",
130
cl::desc("With PGO, include profile count in optimization remarks"),
131
cl::Hidden);
132
133
cl::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(0), cl::Hidden);
140
141
static cl::opt<std::string>
142
RemarksFilename("pass-remarks-output",
143
cl::desc("Output filename for pass remarks"),
144
cl::value_desc("filename"));
145
146
static 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
152
static 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("yaml"));
156
157
static cl::opt<std::string>
158
SamplePGOFile("lto-sample-profile-file",
159
cl::desc("Specify a SamplePGO profile file"));
160
161
static cl::opt<std::string>
162
CSPGOFile("lto-cspgo-profile-file",
163
cl::desc("Specify a context sensitive PGO profile file"));
164
165
static cl::opt<bool>
166
RunCSIRInstr("lto-cspgo-gen",
167
cl::desc("Run PGO context sensitive IR instrumentation"),
168
cl::Hidden);
169
170
static cl::opt<bool>
171
DebugPassManager("debug-pass-manager", cl::Hidden,
172
cl::desc("Print pass management debugging information"));
173
174
static cl::opt<std::string>
175
StatsFile("stats-file", cl::desc("Filename to write statistics to"));
176
177
static cl::list<std::string>
178
PassPlugins("load-pass-plugin",
179
cl::desc("Load passes from plugin library"));
180
181
static cl::opt<std::string> UnifiedLTOMode("unified-lto", cl::Optional,
182
cl::desc("Set LTO mode"),
183
cl::value_desc("mode"));
184
185
static cl::opt<bool> EnableFreestanding(
186
"lto-freestanding",
187
cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"),
188
cl::Hidden);
189
190
static cl::opt<bool> TryUseNewDbgInfoFormat(
191
"try-experimental-debuginfo-iterators",
192
cl::desc("Enable debuginfo iterator positions, if they're built in"),
193
cl::init(false), cl::Hidden);
194
195
extern cl::opt<bool> UseNewDbgInfoFormat;
196
extern cl::opt<cl::boolOrDefault> LoadBitcodeIntoNewDbgInfoFormat;
197
extern cl::opt<cl::boolOrDefault> PreserveInputDbgFormat;
198
199
static void check(Error E, std::string Msg) {
200
if (!E)
201
return;
202
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
203
errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';
204
});
205
exit(1);
206
}
207
208
template <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
215
static void check(std::error_code EC, std::string Msg) {
216
check(errorCodeToError(EC), Msg);
217
}
218
219
template <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
226
static int usage() {
227
errs() << "Available subcommands: dump-symtab run\n";
228
return 1;
229
}
230
231
static int run(int argc, char **argv) {
232
cl::ParseCommandLineOptions(argc, argv, "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 (StringRef R : SymbolResolutions) {
255
StringRef Rest, FileName, SymbolName;
256
std::tie(FileName, Rest) = R.split(',');
257
if (Rest.empty()) {
258
llvm::errs() << "invalid resolution: " << R << '\n';
259
return 1;
260
}
261
std::tie(SymbolName, Rest) = Rest.split(',');
262
SymbolResolution Res;
263
for (char C : Rest) {
264
if (C == 'p')
265
Res.Prevailing = true;
266
else if (C == 'l')
267
Res.FinalDefinitionInLinkageUnit = true;
268
else if (C == 'x')
269
Res.VisibleToRegularObj = true;
270
else if (C == 'r')
271
Res.LinkerRedefined = true;
272
else {
273
llvm::errs() << "invalid character " << C << " in resolution: " << R
274
<< '\n';
275
return 1;
276
}
277
}
278
CommandLineResolutions[{std::string(FileName), std::string(SymbolName)}]
279
.push_back(Res);
280
}
281
282
std::vector<std::unique_ptr<MemoryBuffer>> MBs;
283
284
Config Conf;
285
286
Conf.CPU = codegen::getMCPU();
287
Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(Triple());
288
Conf.MAttrs = codegen::getMAttrs();
289
if (auto RM = codegen::getExplicitRelocModel())
290
Conf.RelocModel = *RM;
291
Conf.CodeModel = codegen::getExplicitCodeModel();
292
293
Conf.DebugPassManager = DebugPassManager;
294
295
if (SaveTemps && !SelectSaveTemps.empty()) {
296
llvm::errs() << "-save-temps cannot be specified with -select-save-temps\n";
297
return 1;
298
}
299
if (SaveTemps || !SelectSaveTemps.empty()) {
300
DenseSet<StringRef> SaveTempsArgs;
301
for (auto &S : SelectSaveTemps)
302
if (is_contained(SaveTempsValues, S))
303
SaveTempsArgs.insert(S);
304
else {
305
llvm::errs() << ("invalid -select-save-temps argument: " + S) << '\n';
306
return 1;
307
}
308
check(Conf.addSaveTemps(OutputFilename + ".", false, SaveTempsArgs),
309
"Config::addSaveTemps failed");
310
}
311
312
// Optimization remarks.
313
Conf.RemarksFilename = RemarksFilename;
314
Conf.RemarksPasses = RemarksPasses;
315
Conf.RemarksWithHotness = RemarksWithHotness;
316
Conf.RemarksHotnessThreshold = RemarksHotnessThreshold;
317
Conf.RemarksFormat = RemarksFormat;
318
319
Conf.SampleProfile = SamplePGOFile;
320
Conf.CSIRProfile = CSPGOFile;
321
Conf.RunCSIRInstr = RunCSIRInstr;
322
323
// Run a custom pipeline, if asked for.
324
Conf.OptPipeline = OptPipeline;
325
Conf.AAPipeline = AAPipeline;
326
327
Conf.OptLevel = OptLevel - '0';
328
Conf.Freestanding = EnableFreestanding;
329
for (auto &PluginFN : PassPlugins)
330
Conf.PassPlugins.push_back(PluginFN);
331
if (auto Level = CodeGenOpt::parseLevel(CGOptLevel)) {
332
Conf.CGOptLevel = *Level;
333
} else {
334
llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
335
return 1;
336
}
337
338
if (auto FT = codegen::getExplicitFileType())
339
Conf.CGFileType = *FT;
340
341
Conf.OverrideTriple = OverrideTriple;
342
Conf.DefaultTriple = DefaultTriple;
343
Conf.StatsFile = StatsFile;
344
Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
345
Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
346
347
ThinBackend Backend;
348
if (ThinLTODistributedIndexes)
349
Backend = createWriteIndexesThinBackend(/*OldPrefix=*/"",
350
/*NewPrefix=*/"",
351
/*NativeObjectPrefix=*/"",
352
ThinLTOEmitImports,
353
/*LinkedObjectsFile=*/nullptr,
354
/*OnWrite=*/{});
355
else
356
Backend = createInProcessThinBackend(
357
llvm::heavyweight_hardware_concurrency(Threads),
358
/* OnWrite */ {}, ThinLTOEmitIndexes, ThinLTOEmitImports);
359
360
// Track whether we hit an error; in particular, in the multi-threaded case,
361
// we can't exit() early because the rest of the threads wouldn't have had a
362
// change to be join-ed, and that would result in a "terminate called without
363
// an active exception". Altogether, this results in nondeterministic
364
// behavior. Instead, we don't exit in the multi-threaded case, but we make
365
// sure to report the error and then at the end (after joining cleanly)
366
// exit(1).
367
std::atomic<bool> HasErrors;
368
std::atomic_init(&HasErrors, false);
369
Conf.DiagHandler = [&](const DiagnosticInfo &DI) {
370
DiagnosticPrinterRawOStream DP(errs());
371
DI.print(DP);
372
errs() << '\n';
373
if (DI.getSeverity() == DS_Error)
374
HasErrors = true;
375
};
376
377
LTO::LTOKind LTOMode = LTO::LTOK_Default;
378
379
if (UnifiedLTOMode == "full") {
380
LTOMode = LTO::LTOK_UnifiedRegular;
381
} else if (UnifiedLTOMode == "thin") {
382
LTOMode = LTO::LTOK_UnifiedThin;
383
} else if (UnifiedLTOMode == "default") {
384
LTOMode = LTO::LTOK_Default;
385
} else if (!UnifiedLTOMode.empty()) {
386
llvm::errs() << "invalid LTO mode\n";
387
return 1;
388
}
389
390
LTO Lto(std::move(Conf), std::move(Backend), 1, LTOMode);
391
392
for (std::string F : InputFilenames) {
393
std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
394
std::unique_ptr<InputFile> Input =
395
check(InputFile::create(MB->getMemBufferRef()), F);
396
397
std::vector<SymbolResolution> Res;
398
for (const InputFile::Symbol &Sym : Input->symbols()) {
399
auto I = CommandLineResolutions.find({F, std::string(Sym.getName())});
400
// If it isn't found, look for ".", which would have been added
401
// (followed by a hash) when the symbol was promoted during module
402
// splitting if it was defined in one part and used in the other.
403
// Try looking up the symbol name before the suffix.
404
if (I == CommandLineResolutions.end()) {
405
auto SplitName = Sym.getName().rsplit(".");
406
I = CommandLineResolutions.find({F, std::string(SplitName.first)});
407
}
408
if (I == CommandLineResolutions.end()) {
409
llvm::errs() << argv[0] << ": missing symbol resolution for " << F
410
<< ',' << Sym.getName() << '\n';
411
HasErrors = true;
412
} else {
413
Res.push_back(I->second.front());
414
I->second.pop_front();
415
if (I->second.empty())
416
CommandLineResolutions.erase(I);
417
}
418
}
419
420
if (HasErrors)
421
continue;
422
423
MBs.push_back(std::move(MB));
424
check(Lto.add(std::move(Input), Res), F);
425
}
426
427
if (!CommandLineResolutions.empty()) {
428
HasErrors = true;
429
for (auto UnusedRes : CommandLineResolutions)
430
llvm::errs() << argv[0] << ": unused symbol resolution for "
431
<< UnusedRes.first.first << ',' << UnusedRes.first.second
432
<< '\n';
433
}
434
if (HasErrors)
435
return 1;
436
437
auto AddStream =
438
[&](size_t Task,
439
const Twine &ModuleName) -> std::unique_ptr<CachedFileStream> {
440
std::string Path = OutputFilename + "." + utostr(Task);
441
442
std::error_code EC;
443
auto S = std::make_unique<raw_fd_ostream>(Path, EC, sys::fs::OF_None);
444
check(EC, Path);
445
return std::make_unique<CachedFileStream>(std::move(S), Path);
446
};
447
448
auto AddBuffer = [&](size_t Task, const Twine &ModuleName,
449
std::unique_ptr<MemoryBuffer> MB) {
450
*AddStream(Task, ModuleName)->OS << MB->getBuffer();
451
};
452
453
FileCache Cache;
454
if (!CacheDir.empty())
455
Cache = check(localCache("ThinLTO", "Thin", CacheDir, AddBuffer),
456
"failed to create cache");
457
458
check(Lto.run(AddStream, Cache), "LTO::run failed");
459
return static_cast<int>(HasErrors);
460
}
461
462
static int dumpSymtab(int argc, char **argv) {
463
for (StringRef F : make_range(argv + 1, argv + argc)) {
464
std::unique_ptr<MemoryBuffer> MB =
465
check(MemoryBuffer::getFile(F), std::string(F));
466
BitcodeFileContents BFC =
467
check(getBitcodeFileContents(*MB), std::string(F));
468
469
if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
470
auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(
471
BFC.Symtab.data());
472
outs() << "version: " << Hdr->Version << '\n';
473
if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
474
outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)
475
<< '\n';
476
}
477
478
std::unique_ptr<InputFile> Input =
479
check(InputFile::create(MB->getMemBufferRef()), std::string(F));
480
481
outs() << "target triple: " << Input->getTargetTriple() << '\n';
482
Triple TT(Input->getTargetTriple());
483
484
outs() << "source filename: " << Input->getSourceFileName() << '\n';
485
486
if (TT.isOSBinFormatCOFF())
487
outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';
488
489
if (TT.isOSBinFormatELF()) {
490
outs() << "dependent libraries:";
491
for (auto L : Input->getDependentLibraries())
492
outs() << " \"" << L << "\"";
493
outs() << '\n';
494
}
495
496
ArrayRef<std::pair<StringRef, Comdat::SelectionKind>> ComdatTable =
497
Input->getComdatTable();
498
for (const InputFile::Symbol &Sym : Input->symbols()) {
499
switch (Sym.getVisibility()) {
500
case GlobalValue::HiddenVisibility:
501
outs() << 'H';
502
break;
503
case GlobalValue::ProtectedVisibility:
504
outs() << 'P';
505
break;
506
case GlobalValue::DefaultVisibility:
507
outs() << 'D';
508
break;
509
}
510
511
auto PrintBool = [&](char C, bool B) { outs() << (B ? C : '-'); };
512
PrintBool('U', Sym.isUndefined());
513
PrintBool('C', Sym.isCommon());
514
PrintBool('W', Sym.isWeak());
515
PrintBool('I', Sym.isIndirect());
516
PrintBool('O', Sym.canBeOmittedFromSymbolTable());
517
PrintBool('T', Sym.isTLS());
518
PrintBool('X', Sym.isExecutable());
519
outs() << ' ' << Sym.getName() << '\n';
520
521
if (Sym.isCommon())
522
outs() << " size " << Sym.getCommonSize() << " align "
523
<< Sym.getCommonAlignment() << '\n';
524
525
int Comdat = Sym.getComdatIndex();
526
if (Comdat != -1) {
527
outs() << " comdat ";
528
switch (ComdatTable[Comdat].second) {
529
case Comdat::Any:
530
outs() << "any";
531
break;
532
case Comdat::ExactMatch:
533
outs() << "exactmatch";
534
break;
535
case Comdat::Largest:
536
outs() << "largest";
537
break;
538
case Comdat::NoDeduplicate:
539
outs() << "nodeduplicate";
540
break;
541
case Comdat::SameSize:
542
outs() << "samesize";
543
break;
544
}
545
outs() << ' ' << ComdatTable[Comdat].first << '\n';
546
}
547
548
if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())
549
outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';
550
551
if (!Sym.getSectionName().empty())
552
outs() << " section " << Sym.getSectionName() << "\n";
553
}
554
555
outs() << '\n';
556
}
557
558
return 0;
559
}
560
561
int main(int argc, char **argv) {
562
InitLLVM X(argc, argv);
563
InitializeAllTargets();
564
InitializeAllTargetMCs();
565
InitializeAllAsmPrinters();
566
InitializeAllAsmParsers();
567
568
// FIXME: This should use llvm::cl subcommands, but it isn't currently
569
// possible to pass an argument not associated with a subcommand to a
570
// subcommand (e.g. -use-new-pm).
571
if (argc < 2)
572
return usage();
573
574
StringRef Subcommand = argv[1];
575
// Ensure that argv[0] is correct after adjusting argv/argc.
576
argv[1] = argv[0];
577
if (Subcommand == "dump-symtab")
578
return dumpSymtab(argc - 1, argv + 1);
579
if (Subcommand == "run")
580
return run(argc - 1, argv + 1);
581
return usage();
582
}
583
584