1 //===-- llvm-lto2: test harness for the resolution-based LTO interface ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This program takes in a list of bitcode files, links them and performs
11 // link-time optimization according to the provided symbol resolutions using the
12 // resolution-based LTO interface, and outputs one or more object files.
14 // This program is intended to eventually replace llvm-lto which uses the legacy
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Bitcode/BitcodeReader.h"
20 #include "llvm/CodeGen/CommandFlags.inc"
21 #include "llvm/IR/DiagnosticPrinter.h"
22 #include "llvm/LTO/Caching.h"
23 #include "llvm/LTO/LTO.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/InitLLVM.h"
27 #include "llvm/Support/TargetSelect.h"
28 #include "llvm/Support/Threading.h"
34 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
36 cl::Prefix, cl::ZeroOrMore, cl::init('2'));
38 static cl::opt<char> CGOptLevel(
40 cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"),
43 static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
44 cl::desc("<input bitcode files>"));
46 static cl::opt<std::string> OutputFilename("o", cl::Required,
47 cl::desc("Output filename"),
48 cl::value_desc("filename"));
50 static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
51 cl::value_desc("directory"));
53 static cl::opt<std::string> OptPipeline("opt-pipeline",
54 cl::desc("Optimizer Pipeline"),
55 cl::value_desc("pipeline"));
57 static cl::opt<std::string> AAPipeline("aa-pipeline",
58 cl::desc("Alias Analysis Pipeline"),
59 cl::value_desc("aapipeline"));
61 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
64 ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false),
65 cl::desc("Write out individual index and "
66 "import files for the "
67 "distributed backend case"));
69 static cl::opt<int> Threads("thinlto-threads",
70 cl::init(llvm::heavyweight_hardware_concurrency()));
72 static cl::list<std::string> SymbolResolutions(
74 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
75 "where \"resolution\" is a sequence (which may be empty) of the\n"
76 "following characters:\n"
77 " p - prevailing: the linker has chosen this definition of the\n"
79 " l - local: the definition of this symbol is unpreemptable at\n"
80 " runtime and is known to be in this linkage unit\n"
81 " x - externally visible: the definition of this symbol is\n"
82 " visible outside of the LTO unit\n"
83 "A resolution for each symbol must be specified."),
86 static cl::opt<std::string> OverrideTriple(
88 cl::desc("Replace target triples in input files with this triple"));
90 static cl::opt<std::string> DefaultTriple(
93 "Replace unspecified target triples in input files with this triple"));
95 static cl::opt<std::string>
96 OptRemarksOutput("pass-remarks-output",
97 cl::desc("YAML output file for optimization remarks"));
99 static cl::opt<bool> OptRemarksWithHotness(
100 "pass-remarks-with-hotness",
101 cl::desc("Whether to include hotness informations in the remarks.\n"
102 "Has effect only if -pass-remarks-output is specified."));
104 static cl::opt<std::string>
105 SamplePGOFile("lto-sample-profile-file",
106 cl::desc("Specify a SamplePGO profile file"));
109 UseNewPM("use-new-pm",
110 cl::desc("Run LTO passes using the new pass manager"),
111 cl::init(false), cl::Hidden);
114 DebugPassManager("debug-pass-manager", cl::init(false), cl::Hidden,
115 cl::desc("Print pass management debugging information"));
117 static cl::opt<std::string>
118 StatsFile("stats-file", cl::desc("Filename to write statistics to"));
120 static void check(Error E, std::string Msg) {
123 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
124 errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';
129 template <typename T> static T check(Expected<T> E, std::string Msg) {
131 return std::move(*E);
132 check(E.takeError(), Msg);
136 static void check(std::error_code EC, std::string Msg) {
137 check(errorCodeToError(EC), Msg);
140 template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
142 return std::move(*E);
143 check(E.getError(), Msg);
148 errs() << "Available subcommands: dump-symtab run\n";
152 static int run(int argc, char **argv) {
153 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
155 // FIXME: Workaround PR30396 which means that a symbol can appear
156 // more than once if it is defined in module-level assembly and
157 // has a GV declaration. We allow (file, symbol) pairs to have multiple
158 // resolutions and apply them in the order observed.
159 std::map<std::pair<std::string, std::string>, std::list<SymbolResolution>>
160 CommandLineResolutions;
161 for (std::string R : SymbolResolutions) {
163 StringRef FileName, SymbolName;
164 std::tie(FileName, Rest) = Rest.split(',');
166 llvm::errs() << "invalid resolution: " << R << '\n';
169 std::tie(SymbolName, Rest) = Rest.split(',');
170 SymbolResolution Res;
171 for (char C : Rest) {
173 Res.Prevailing = true;
175 Res.FinalDefinitionInLinkageUnit = true;
177 Res.VisibleToRegularObj = true;
179 Res.LinkerRedefined = true;
181 llvm::errs() << "invalid character " << C << " in resolution: " << R
186 CommandLineResolutions[{FileName, SymbolName}].push_back(Res);
189 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
192 Conf.DiagHandler = [](const DiagnosticInfo &DI) {
193 DiagnosticPrinterRawOStream DP(errs());
196 if (DI.getSeverity() == DS_Error)
201 Conf.Options = InitTargetOptionsFromCodeGenFlags();
202 Conf.MAttrs = MAttrs;
203 if (auto RM = getRelocModel())
204 Conf.RelocModel = *RM;
205 Conf.CodeModel = getCodeModel();
207 Conf.DebugPassManager = DebugPassManager;
210 check(Conf.addSaveTemps(OutputFilename + "."),
211 "Config::addSaveTemps failed");
213 // Optimization remarks.
214 Conf.RemarksFilename = OptRemarksOutput;
215 Conf.RemarksWithHotness = OptRemarksWithHotness;
217 Conf.SampleProfile = SamplePGOFile;
219 // Run a custom pipeline, if asked for.
220 Conf.OptPipeline = OptPipeline;
221 Conf.AAPipeline = AAPipeline;
223 Conf.OptLevel = OptLevel - '0';
224 Conf.UseNewPM = UseNewPM;
225 switch (CGOptLevel) {
227 Conf.CGOptLevel = CodeGenOpt::None;
230 Conf.CGOptLevel = CodeGenOpt::Less;
233 Conf.CGOptLevel = CodeGenOpt::Default;
236 Conf.CGOptLevel = CodeGenOpt::Aggressive;
239 llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
243 if (FileType.getNumOccurrences())
244 Conf.CGFileType = FileType;
246 Conf.OverrideTriple = OverrideTriple;
247 Conf.DefaultTriple = DefaultTriple;
248 Conf.StatsFile = StatsFile;
251 if (ThinLTODistributedIndexes)
252 Backend = createWriteIndexesThinBackend(/* OldPrefix */ "",
254 /* ShouldEmitImportsFiles */ true,
255 /* LinkedObjectsFile */ nullptr,
258 Backend = createInProcessThinBackend(Threads);
259 LTO Lto(std::move(Conf), std::move(Backend));
261 bool HasErrors = false;
262 for (std::string F : InputFilenames) {
263 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
264 std::unique_ptr<InputFile> Input =
265 check(InputFile::create(MB->getMemBufferRef()), F);
267 std::vector<SymbolResolution> Res;
268 for (const InputFile::Symbol &Sym : Input->symbols()) {
269 auto I = CommandLineResolutions.find({F, Sym.getName()});
270 if (I == CommandLineResolutions.end()) {
271 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
272 << ',' << Sym.getName() << '\n';
275 Res.push_back(I->second.front());
276 I->second.pop_front();
277 if (I->second.empty())
278 CommandLineResolutions.erase(I);
285 MBs.push_back(std::move(MB));
286 check(Lto.add(std::move(Input), Res), F);
289 if (!CommandLineResolutions.empty()) {
291 for (auto UnusedRes : CommandLineResolutions)
292 llvm::errs() << argv[0] << ": unused symbol resolution for "
293 << UnusedRes.first.first << ',' << UnusedRes.first.second
300 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
301 std::string Path = OutputFilename + "." + utostr(Task);
304 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
306 return llvm::make_unique<lto::NativeObjectStream>(std::move(S));
309 auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
310 *AddStream(Task)->OS << MB->getBuffer();
313 NativeObjectCache Cache;
314 if (!CacheDir.empty())
315 Cache = check(localCache(CacheDir, AddBuffer), "failed to create cache");
317 check(Lto.run(AddStream, Cache), "LTO::run failed");
321 static int dumpSymtab(int argc, char **argv) {
322 for (StringRef F : make_range(argv + 1, argv + argc)) {
323 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
324 BitcodeFileContents BFC = check(getBitcodeFileContents(*MB), F);
326 if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
327 auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(
329 outs() << "version: " << Hdr->Version << '\n';
330 if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
331 outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)
335 std::unique_ptr<InputFile> Input =
336 check(InputFile::create(MB->getMemBufferRef()), F);
338 outs() << "target triple: " << Input->getTargetTriple() << '\n';
339 Triple TT(Input->getTargetTriple());
341 outs() << "source filename: " << Input->getSourceFileName() << '\n';
343 if (TT.isOSBinFormatCOFF())
344 outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';
346 std::vector<StringRef> ComdatTable = Input->getComdatTable();
347 for (const InputFile::Symbol &Sym : Input->symbols()) {
348 switch (Sym.getVisibility()) {
349 case GlobalValue::HiddenVisibility:
352 case GlobalValue::ProtectedVisibility:
355 case GlobalValue::DefaultVisibility:
360 auto PrintBool = [&](char C, bool B) { outs() << (B ? C : '-'); };
361 PrintBool('U', Sym.isUndefined());
362 PrintBool('C', Sym.isCommon());
363 PrintBool('W', Sym.isWeak());
364 PrintBool('I', Sym.isIndirect());
365 PrintBool('O', Sym.canBeOmittedFromSymbolTable());
366 PrintBool('T', Sym.isTLS());
367 PrintBool('X', Sym.isExecutable());
368 outs() << ' ' << Sym.getName() << '\n';
371 outs() << " size " << Sym.getCommonSize() << " align "
372 << Sym.getCommonAlignment() << '\n';
374 int Comdat = Sym.getComdatIndex();
376 outs() << " comdat " << ComdatTable[Comdat] << '\n';
378 if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())
379 outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';
381 if (!Sym.getSectionName().empty())
382 outs() << " section " << Sym.getSectionName() << "\n";
391 int main(int argc, char **argv) {
392 InitLLVM X(argc, argv);
393 InitializeAllTargets();
394 InitializeAllTargetMCs();
395 InitializeAllAsmPrinters();
396 InitializeAllAsmParsers();
398 // FIXME: This should use llvm::cl subcommands, but it isn't currently
399 // possible to pass an argument not associated with a subcommand to a
400 // subcommand (e.g. -use-new-pm).
404 StringRef Subcommand = argv[1];
405 // Ensure that argv[0] is correct after adjusting argv/argc.
407 if (Subcommand == "dump-symtab")
408 return dumpSymtab(argc - 1, argv + 1);
409 if (Subcommand == "run")
410 return run(argc - 1, argv + 1);