[MIParser] Set RegClassOrRegBank during instruction parsing
[llvm-complete.git] / tools / llvm-lto2 / llvm-lto2.cpp
blob5e3b3dcb6c31065e443527ee5ccbd9f4acb7671c
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.
13 // This program is intended to eventually replace llvm-lto which uses the legacy
14 // LTO interface.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Bitcode/BitcodeReader.h"
19 #include "llvm/CodeGen/CommandFlags.inc"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/LTO/Caching.h"
22 #include "llvm/LTO/LTO.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/InitLLVM.h"
26 #include "llvm/Support/TargetSelect.h"
27 #include "llvm/Support/Threading.h"
29 using namespace llvm;
30 using namespace lto;
32 static cl::opt<char>
33 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
34 "(default = '-O2')"),
35 cl::Prefix, cl::ZeroOrMore, cl::init('2'));
37 static cl::opt<char> CGOptLevel(
38 "cg-opt-level",
39 cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"),
40 cl::init('2'));
42 static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
43 cl::desc("<input bitcode files>"));
45 static cl::opt<std::string> OutputFilename("o", cl::Required,
46 cl::desc("Output filename"),
47 cl::value_desc("filename"));
49 static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
50 cl::value_desc("directory"));
52 static cl::opt<std::string> OptPipeline("opt-pipeline",
53 cl::desc("Optimizer Pipeline"),
54 cl::value_desc("pipeline"));
56 static cl::opt<std::string> AAPipeline("aa-pipeline",
57 cl::desc("Alias Analysis Pipeline"),
58 cl::value_desc("aapipeline"));
60 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
62 static cl::opt<bool>
63 ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false),
64 cl::desc("Write out individual index and "
65 "import files for the "
66 "distributed backend case"));
68 static cl::opt<int> Threads("thinlto-threads",
69 cl::init(llvm::heavyweight_hardware_concurrency()));
71 static cl::list<std::string> SymbolResolutions(
72 "r",
73 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
74 "where \"resolution\" is a sequence (which may be empty) of the\n"
75 "following characters:\n"
76 " p - prevailing: the linker has chosen this definition of the\n"
77 " symbol\n"
78 " l - local: the definition of this symbol is unpreemptable at\n"
79 " runtime and is known to be in this linkage unit\n"
80 " x - externally visible: the definition of this symbol is\n"
81 " visible outside of the LTO unit\n"
82 "A resolution for each symbol must be specified."),
83 cl::ZeroOrMore);
85 static cl::opt<std::string> OverrideTriple(
86 "override-triple",
87 cl::desc("Replace target triples in input files with this triple"));
89 static cl::opt<std::string> DefaultTriple(
90 "default-triple",
91 cl::desc(
92 "Replace unspecified target triples in input files with this triple"));
94 static cl::opt<bool> RemarksWithHotness(
95 "pass-remarks-with-hotness",
96 cl::desc("With PGO, include profile count in optimization remarks"),
97 cl::Hidden);
99 static cl::opt<std::string>
100 RemarksFilename("pass-remarks-output",
101 cl::desc("Output filename for pass remarks"),
102 cl::value_desc("filename"));
104 static cl::opt<std::string>
105 RemarksPasses("pass-remarks-filter",
106 cl::desc("Only record optimization remarks from passes whose "
107 "names match the given regular expression"),
108 cl::value_desc("regex"));
110 static cl::opt<std::string> RemarksFormat(
111 "pass-remarks-format",
112 cl::desc("The format used for serializing remarks (default: YAML)"),
113 cl::value_desc("format"), cl::init("yaml"));
115 static cl::opt<std::string>
116 SamplePGOFile("lto-sample-profile-file",
117 cl::desc("Specify a SamplePGO profile file"));
119 static cl::opt<std::string>
120 CSPGOFile("lto-cspgo-profile-file",
121 cl::desc("Specify a context sensitive PGO profile file"));
123 static cl::opt<bool>
124 RunCSIRInstr("lto-cspgo-gen",
125 cl::desc("Run PGO context sensitive IR instrumentation"),
126 cl::init(false), cl::Hidden);
128 static cl::opt<bool>
129 UseNewPM("use-new-pm",
130 cl::desc("Run LTO passes using the new pass manager"),
131 cl::init(false), cl::Hidden);
133 static cl::opt<bool>
134 DebugPassManager("debug-pass-manager", cl::init(false), cl::Hidden,
135 cl::desc("Print pass management debugging information"));
137 static cl::opt<std::string>
138 StatsFile("stats-file", cl::desc("Filename to write statistics to"));
140 static void check(Error E, std::string Msg) {
141 if (!E)
142 return;
143 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
144 errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';
146 exit(1);
149 template <typename T> static T check(Expected<T> E, std::string Msg) {
150 if (E)
151 return std::move(*E);
152 check(E.takeError(), Msg);
153 return T();
156 static void check(std::error_code EC, std::string Msg) {
157 check(errorCodeToError(EC), Msg);
160 template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
161 if (E)
162 return std::move(*E);
163 check(E.getError(), Msg);
164 return T();
167 static int usage() {
168 errs() << "Available subcommands: dump-symtab run\n";
169 return 1;
172 static int run(int argc, char **argv) {
173 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
175 // FIXME: Workaround PR30396 which means that a symbol can appear
176 // more than once if it is defined in module-level assembly and
177 // has a GV declaration. We allow (file, symbol) pairs to have multiple
178 // resolutions and apply them in the order observed.
179 std::map<std::pair<std::string, std::string>, std::list<SymbolResolution>>
180 CommandLineResolutions;
181 for (std::string R : SymbolResolutions) {
182 StringRef Rest = R;
183 StringRef FileName, SymbolName;
184 std::tie(FileName, Rest) = Rest.split(',');
185 if (Rest.empty()) {
186 llvm::errs() << "invalid resolution: " << R << '\n';
187 return 1;
189 std::tie(SymbolName, Rest) = Rest.split(',');
190 SymbolResolution Res;
191 for (char C : Rest) {
192 if (C == 'p')
193 Res.Prevailing = true;
194 else if (C == 'l')
195 Res.FinalDefinitionInLinkageUnit = true;
196 else if (C == 'x')
197 Res.VisibleToRegularObj = true;
198 else if (C == 'r')
199 Res.LinkerRedefined = true;
200 else {
201 llvm::errs() << "invalid character " << C << " in resolution: " << R
202 << '\n';
203 return 1;
206 CommandLineResolutions[{FileName, SymbolName}].push_back(Res);
209 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
211 Config Conf;
212 Conf.DiagHandler = [](const DiagnosticInfo &DI) {
213 DiagnosticPrinterRawOStream DP(errs());
214 DI.print(DP);
215 errs() << '\n';
216 if (DI.getSeverity() == DS_Error)
217 exit(1);
220 Conf.CPU = MCPU;
221 Conf.Options = InitTargetOptionsFromCodeGenFlags();
222 Conf.MAttrs = MAttrs;
223 if (auto RM = getRelocModel())
224 Conf.RelocModel = *RM;
225 Conf.CodeModel = getCodeModel();
227 Conf.DebugPassManager = DebugPassManager;
229 if (SaveTemps)
230 check(Conf.addSaveTemps(OutputFilename + "."),
231 "Config::addSaveTemps failed");
233 // Optimization remarks.
234 Conf.RemarksFilename = RemarksFilename;
235 Conf.RemarksPasses = RemarksPasses;
236 Conf.RemarksWithHotness = RemarksWithHotness;
237 Conf.RemarksFormat = RemarksFormat;
239 Conf.SampleProfile = SamplePGOFile;
240 Conf.CSIRProfile = CSPGOFile;
241 Conf.RunCSIRInstr = RunCSIRInstr;
243 // Run a custom pipeline, if asked for.
244 Conf.OptPipeline = OptPipeline;
245 Conf.AAPipeline = AAPipeline;
247 Conf.OptLevel = OptLevel - '0';
248 Conf.UseNewPM = UseNewPM;
249 switch (CGOptLevel) {
250 case '0':
251 Conf.CGOptLevel = CodeGenOpt::None;
252 break;
253 case '1':
254 Conf.CGOptLevel = CodeGenOpt::Less;
255 break;
256 case '2':
257 Conf.CGOptLevel = CodeGenOpt::Default;
258 break;
259 case '3':
260 Conf.CGOptLevel = CodeGenOpt::Aggressive;
261 break;
262 default:
263 llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
264 return 1;
267 if (FileType.getNumOccurrences())
268 Conf.CGFileType = FileType;
270 Conf.OverrideTriple = OverrideTriple;
271 Conf.DefaultTriple = DefaultTriple;
272 Conf.StatsFile = StatsFile;
274 ThinBackend Backend;
275 if (ThinLTODistributedIndexes)
276 Backend = createWriteIndexesThinBackend(/* OldPrefix */ "",
277 /* NewPrefix */ "",
278 /* ShouldEmitImportsFiles */ true,
279 /* LinkedObjectsFile */ nullptr,
280 /* OnWrite */ {});
281 else
282 Backend = createInProcessThinBackend(Threads);
283 LTO Lto(std::move(Conf), std::move(Backend));
285 bool HasErrors = false;
286 for (std::string F : InputFilenames) {
287 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
288 std::unique_ptr<InputFile> Input =
289 check(InputFile::create(MB->getMemBufferRef()), F);
291 std::vector<SymbolResolution> Res;
292 for (const InputFile::Symbol &Sym : Input->symbols()) {
293 auto I = CommandLineResolutions.find({F, Sym.getName()});
294 // If it isn't found, look for "$", which would have been added
295 // (followed by a hash) when the symbol was promoted during module
296 // splitting if it was defined in one part and used in the other.
297 // Try looking up the symbol name before the "$".
298 if (I == CommandLineResolutions.end()) {
299 auto SplitName = Sym.getName().rsplit("$");
300 I = CommandLineResolutions.find({F, SplitName.first});
302 if (I == CommandLineResolutions.end()) {
303 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
304 << ',' << Sym.getName() << '\n';
305 HasErrors = true;
306 } else {
307 Res.push_back(I->second.front());
308 I->second.pop_front();
309 if (I->second.empty())
310 CommandLineResolutions.erase(I);
314 if (HasErrors)
315 continue;
317 MBs.push_back(std::move(MB));
318 check(Lto.add(std::move(Input), Res), F);
321 if (!CommandLineResolutions.empty()) {
322 HasErrors = true;
323 for (auto UnusedRes : CommandLineResolutions)
324 llvm::errs() << argv[0] << ": unused symbol resolution for "
325 << UnusedRes.first.first << ',' << UnusedRes.first.second
326 << '\n';
328 if (HasErrors)
329 return 1;
331 auto AddStream =
332 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
333 std::string Path = OutputFilename + "." + utostr(Task);
335 std::error_code EC;
336 auto S = std::make_unique<raw_fd_ostream>(Path, EC, sys::fs::OF_None);
337 check(EC, Path);
338 return std::make_unique<lto::NativeObjectStream>(std::move(S));
341 auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
342 *AddStream(Task)->OS << MB->getBuffer();
345 NativeObjectCache Cache;
346 if (!CacheDir.empty())
347 Cache = check(localCache(CacheDir, AddBuffer), "failed to create cache");
349 check(Lto.run(AddStream, Cache), "LTO::run failed");
350 return 0;
353 static int dumpSymtab(int argc, char **argv) {
354 for (StringRef F : make_range(argv + 1, argv + argc)) {
355 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
356 BitcodeFileContents BFC = check(getBitcodeFileContents(*MB), F);
358 if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
359 auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(
360 BFC.Symtab.data());
361 outs() << "version: " << Hdr->Version << '\n';
362 if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
363 outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)
364 << '\n';
367 std::unique_ptr<InputFile> Input =
368 check(InputFile::create(MB->getMemBufferRef()), F);
370 outs() << "target triple: " << Input->getTargetTriple() << '\n';
371 Triple TT(Input->getTargetTriple());
373 outs() << "source filename: " << Input->getSourceFileName() << '\n';
375 if (TT.isOSBinFormatCOFF())
376 outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';
378 if (TT.isOSBinFormatELF()) {
379 outs() << "dependent libraries:";
380 for (auto L : Input->getDependentLibraries())
381 outs() << " \"" << L << "\"";
382 outs() << '\n';
385 std::vector<StringRef> ComdatTable = Input->getComdatTable();
386 for (const InputFile::Symbol &Sym : Input->symbols()) {
387 switch (Sym.getVisibility()) {
388 case GlobalValue::HiddenVisibility:
389 outs() << 'H';
390 break;
391 case GlobalValue::ProtectedVisibility:
392 outs() << 'P';
393 break;
394 case GlobalValue::DefaultVisibility:
395 outs() << 'D';
396 break;
399 auto PrintBool = [&](char C, bool B) { outs() << (B ? C : '-'); };
400 PrintBool('U', Sym.isUndefined());
401 PrintBool('C', Sym.isCommon());
402 PrintBool('W', Sym.isWeak());
403 PrintBool('I', Sym.isIndirect());
404 PrintBool('O', Sym.canBeOmittedFromSymbolTable());
405 PrintBool('T', Sym.isTLS());
406 PrintBool('X', Sym.isExecutable());
407 outs() << ' ' << Sym.getName() << '\n';
409 if (Sym.isCommon())
410 outs() << " size " << Sym.getCommonSize() << " align "
411 << Sym.getCommonAlignment() << '\n';
413 int Comdat = Sym.getComdatIndex();
414 if (Comdat != -1)
415 outs() << " comdat " << ComdatTable[Comdat] << '\n';
417 if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())
418 outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';
420 if (!Sym.getSectionName().empty())
421 outs() << " section " << Sym.getSectionName() << "\n";
424 outs() << '\n';
427 return 0;
430 int main(int argc, char **argv) {
431 InitLLVM X(argc, argv);
432 InitializeAllTargets();
433 InitializeAllTargetMCs();
434 InitializeAllAsmPrinters();
435 InitializeAllAsmParsers();
437 // FIXME: This should use llvm::cl subcommands, but it isn't currently
438 // possible to pass an argument not associated with a subcommand to a
439 // subcommand (e.g. -use-new-pm).
440 if (argc < 2)
441 return usage();
443 StringRef Subcommand = argv[1];
444 // Ensure that argv[0] is correct after adjusting argv/argc.
445 argv[1] = argv[0];
446 if (Subcommand == "dump-symtab")
447 return dumpSymtab(argc - 1, argv + 1);
448 if (Subcommand == "run")
449 return run(argc - 1, argv + 1);
450 return usage();