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