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