1 //===-- llvm-lto2: test harness for the resolution-based LTO interface ----===//
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
7 //===----------------------------------------------------------------------===//
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
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"
33 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
35 cl::Prefix, cl::ZeroOrMore, cl::init('2'));
37 static cl::opt<char> CGOptLevel(
39 cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '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"));
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(
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"
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."),
85 static cl::opt<std::string> OverrideTriple(
87 cl::desc("Replace target triples in input files with this triple"));
89 static cl::opt<std::string> DefaultTriple(
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"),
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"));
124 RunCSIRInstr("lto-cspgo-gen",
125 cl::desc("Run PGO context sensitive IR instrumentation"),
126 cl::init(false), cl::Hidden);
129 UseNewPM("use-new-pm",
130 cl::desc("Run LTO passes using the new pass manager"),
131 cl::init(false), cl::Hidden);
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) {
143 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
144 errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';
149 template <typename T> static T check(Expected<T> E, std::string Msg) {
151 return std::move(*E);
152 check(E.takeError(), Msg);
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) {
162 return std::move(*E);
163 check(E.getError(), Msg);
168 errs() << "Available subcommands: dump-symtab run\n";
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) {
183 StringRef FileName, SymbolName;
184 std::tie(FileName, Rest) = Rest.split(',');
186 llvm::errs() << "invalid resolution: " << R << '\n';
189 std::tie(SymbolName, Rest) = Rest.split(',');
190 SymbolResolution Res;
191 for (char C : Rest) {
193 Res.Prevailing = true;
195 Res.FinalDefinitionInLinkageUnit = true;
197 Res.VisibleToRegularObj = true;
199 Res.LinkerRedefined = true;
201 llvm::errs() << "invalid character " << C << " in resolution: " << R
206 CommandLineResolutions[{FileName, SymbolName}].push_back(Res);
209 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
212 Conf.DiagHandler = [](const DiagnosticInfo &DI) {
213 DiagnosticPrinterRawOStream DP(errs());
216 if (DI.getSeverity() == DS_Error)
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;
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) {
251 Conf.CGOptLevel = CodeGenOpt::None;
254 Conf.CGOptLevel = CodeGenOpt::Less;
257 Conf.CGOptLevel = CodeGenOpt::Default;
260 Conf.CGOptLevel = CodeGenOpt::Aggressive;
263 llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
267 if (FileType.getNumOccurrences())
268 Conf.CGFileType = FileType;
270 Conf.OverrideTriple = OverrideTriple;
271 Conf.DefaultTriple = DefaultTriple;
272 Conf.StatsFile = StatsFile;
275 if (ThinLTODistributedIndexes)
276 Backend = createWriteIndexesThinBackend(/* OldPrefix */ "",
278 /* ShouldEmitImportsFiles */ true,
279 /* LinkedObjectsFile */ nullptr,
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 (I == CommandLineResolutions.end()) {
295 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
296 << ',' << Sym.getName() << '\n';
299 Res.push_back(I->second.front());
300 I->second.pop_front();
301 if (I->second.empty())
302 CommandLineResolutions.erase(I);
309 MBs.push_back(std::move(MB));
310 check(Lto.add(std::move(Input), Res), F);
313 if (!CommandLineResolutions.empty()) {
315 for (auto UnusedRes : CommandLineResolutions)
316 llvm::errs() << argv[0] << ": unused symbol resolution for "
317 << UnusedRes.first.first << ',' << UnusedRes.first.second
324 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
325 std::string Path = OutputFilename + "." + utostr(Task);
328 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
330 return llvm::make_unique<lto::NativeObjectStream>(std::move(S));
333 auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
334 *AddStream(Task)->OS << MB->getBuffer();
337 NativeObjectCache Cache;
338 if (!CacheDir.empty())
339 Cache = check(localCache(CacheDir, AddBuffer), "failed to create cache");
341 check(Lto.run(AddStream, Cache), "LTO::run failed");
345 static int dumpSymtab(int argc, char **argv) {
346 for (StringRef F : make_range(argv + 1, argv + argc)) {
347 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
348 BitcodeFileContents BFC = check(getBitcodeFileContents(*MB), F);
350 if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
351 auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(
353 outs() << "version: " << Hdr->Version << '\n';
354 if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
355 outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)
359 std::unique_ptr<InputFile> Input =
360 check(InputFile::create(MB->getMemBufferRef()), F);
362 outs() << "target triple: " << Input->getTargetTriple() << '\n';
363 Triple TT(Input->getTargetTriple());
365 outs() << "source filename: " << Input->getSourceFileName() << '\n';
367 if (TT.isOSBinFormatCOFF())
368 outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';
370 if (TT.isOSBinFormatELF()) {
371 outs() << "dependent libraries:";
372 for (auto L : Input->getDependentLibraries())
373 outs() << " \"" << L << "\"";
377 std::vector<StringRef> ComdatTable = Input->getComdatTable();
378 for (const InputFile::Symbol &Sym : Input->symbols()) {
379 switch (Sym.getVisibility()) {
380 case GlobalValue::HiddenVisibility:
383 case GlobalValue::ProtectedVisibility:
386 case GlobalValue::DefaultVisibility:
391 auto PrintBool = [&](char C, bool B) { outs() << (B ? C : '-'); };
392 PrintBool('U', Sym.isUndefined());
393 PrintBool('C', Sym.isCommon());
394 PrintBool('W', Sym.isWeak());
395 PrintBool('I', Sym.isIndirect());
396 PrintBool('O', Sym.canBeOmittedFromSymbolTable());
397 PrintBool('T', Sym.isTLS());
398 PrintBool('X', Sym.isExecutable());
399 outs() << ' ' << Sym.getName() << '\n';
402 outs() << " size " << Sym.getCommonSize() << " align "
403 << Sym.getCommonAlignment() << '\n';
405 int Comdat = Sym.getComdatIndex();
407 outs() << " comdat " << ComdatTable[Comdat] << '\n';
409 if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())
410 outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';
412 if (!Sym.getSectionName().empty())
413 outs() << " section " << Sym.getSectionName() << "\n";
422 int main(int argc, char **argv) {
423 InitLLVM X(argc, argv);
424 InitializeAllTargets();
425 InitializeAllTargetMCs();
426 InitializeAllAsmPrinters();
427 InitializeAllAsmParsers();
429 // FIXME: This should use llvm::cl subcommands, but it isn't currently
430 // possible to pass an argument not associated with a subcommand to a
431 // subcommand (e.g. -use-new-pm).
435 StringRef Subcommand = argv[1];
436 // Ensure that argv[0] is correct after adjusting argv/argc.
438 if (Subcommand == "dump-symtab")
439 return dumpSymtab(argc - 1, argv + 1);
440 if (Subcommand == "run")
441 return run(argc - 1, argv + 1);