1 //===-- llvm-lto2: test harness for the resolution-based LTO interface ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 // This program is intended to eventually replace llvm-lto which uses the legacy
15 // LTO interface.
16 //
17 //===----------------------------------------------------------------------===//
18
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"
29
30 using namespace llvm;
31 using namespace lto;
32
33 static cl::opt<char>
34 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
35 "(default = '-O2')"),
36 cl::Prefix, cl::ZeroOrMore, cl::init('2'));
37
38 static cl::opt<char> CGOptLevel(
39 "cg-opt-level",
40 cl::desc("Codegen optimization level (0, 1, 2 or 3, default = '2')"),
41 cl::init('2'));
42
43 static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
44 cl::desc("<input bitcode files>"));
45
46 static cl::opt<std::string> OutputFilename("o", cl::Required,
47 cl::desc("Output filename"),
48 cl::value_desc("filename"));
49
50 static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
51 cl::value_desc("directory"));
52
53 static cl::opt<std::string> OptPipeline("opt-pipeline",
54 cl::desc("Optimizer Pipeline"),
55 cl::value_desc("pipeline"));
56
57 static cl::opt<std::string> AAPipeline("aa-pipeline",
58 cl::desc("Alias Analysis Pipeline"),
59 cl::value_desc("aapipeline"));
60
61 static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
62
63 static cl::opt<bool>
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"));
68
69 static cl::opt<int> Threads("thinlto-threads",
70 cl::init(llvm::heavyweight_hardware_concurrency()));
71
72 static cl::list<std::string> SymbolResolutions(
73 "r",
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"
78 " symbol\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."),
84 cl::ZeroOrMore);
85
86 static cl::opt<std::string> OverrideTriple(
87 "override-triple",
88 cl::desc("Replace target triples in input files with this triple"));
89
90 static cl::opt<std::string> DefaultTriple(
91 "default-triple",
92 cl::desc(
93 "Replace unspecified target triples in input files with this triple"));
94
95 static cl::opt<std::string>
96 OptRemarksOutput("pass-remarks-output",
97 cl::desc("YAML output file for optimization remarks"));
98
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."));
103
104 static cl::opt<std::string>
105 SamplePGOFile("lto-sample-profile-file",
106 cl::desc("Specify a SamplePGO profile file"));
107
108 static cl::opt<bool>
109 UseNewPM("use-new-pm",
110 cl::desc("Run LTO passes using the new pass manager"),
111 cl::init(false), cl::Hidden);
112
113 static cl::opt<bool>
114 DebugPassManager("debug-pass-manager", cl::init(false), cl::Hidden,
115 cl::desc("Print pass management debugging information"));
116
117 static cl::opt<std::string>
118 StatsFile("stats-file", cl::desc("Filename to write statistics to"));
119
check(Error E,std::string Msg)120 static void check(Error E, std::string Msg) {
121 if (!E)
122 return;
123 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
124 errs() << "llvm-lto2: " << Msg << ": " << EIB.message().c_str() << '\n';
125 });
126 exit(1);
127 }
128
check(Expected<T> E,std::string Msg)129 template <typename T> static T check(Expected<T> E, std::string Msg) {
130 if (E)
131 return std::move(*E);
132 check(E.takeError(), Msg);
133 return T();
134 }
135
check(std::error_code EC,std::string Msg)136 static void check(std::error_code EC, std::string Msg) {
137 check(errorCodeToError(EC), Msg);
138 }
139
check(ErrorOr<T> E,std::string Msg)140 template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
141 if (E)
142 return std::move(*E);
143 check(E.getError(), Msg);
144 return T();
145 }
146
usage()147 static int usage() {
148 errs() << "Available subcommands: dump-symtab run\n";
149 return 1;
150 }
151
run(int argc,char ** argv)152 static int run(int argc, char **argv) {
153 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
154
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) {
162 StringRef Rest = R;
163 StringRef FileName, SymbolName;
164 std::tie(FileName, Rest) = Rest.split(',');
165 if (Rest.empty()) {
166 llvm::errs() << "invalid resolution: " << R << '\n';
167 return 1;
168 }
169 std::tie(SymbolName, Rest) = Rest.split(',');
170 SymbolResolution Res;
171 for (char C : Rest) {
172 if (C == 'p')
173 Res.Prevailing = true;
174 else if (C == 'l')
175 Res.FinalDefinitionInLinkageUnit = true;
176 else if (C == 'x')
177 Res.VisibleToRegularObj = true;
178 else if (C == 'r')
179 Res.LinkerRedefined = true;
180 else {
181 llvm::errs() << "invalid character " << C << " in resolution: " << R
182 << '\n';
183 return 1;
184 }
185 }
186 CommandLineResolutions[{FileName, SymbolName}].push_back(Res);
187 }
188
189 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
190
191 Config Conf;
192 Conf.DiagHandler = [](const DiagnosticInfo &DI) {
193 DiagnosticPrinterRawOStream DP(errs());
194 DI.print(DP);
195 errs() << '\n';
196 if (DI.getSeverity() == DS_Error)
197 exit(1);
198 };
199
200 Conf.CPU = MCPU;
201 Conf.Options = InitTargetOptionsFromCodeGenFlags();
202 Conf.MAttrs = MAttrs;
203 if (auto RM = getRelocModel())
204 Conf.RelocModel = *RM;
205 Conf.CodeModel = getCodeModel();
206
207 Conf.DebugPassManager = DebugPassManager;
208
209 if (SaveTemps)
210 check(Conf.addSaveTemps(OutputFilename + "."),
211 "Config::addSaveTemps failed");
212
213 // Optimization remarks.
214 Conf.RemarksFilename = OptRemarksOutput;
215 Conf.RemarksWithHotness = OptRemarksWithHotness;
216
217 Conf.SampleProfile = SamplePGOFile;
218
219 // Run a custom pipeline, if asked for.
220 Conf.OptPipeline = OptPipeline;
221 Conf.AAPipeline = AAPipeline;
222
223 Conf.OptLevel = OptLevel - '0';
224 Conf.UseNewPM = UseNewPM;
225 switch (CGOptLevel) {
226 case '0':
227 Conf.CGOptLevel = CodeGenOpt::None;
228 break;
229 case '1':
230 Conf.CGOptLevel = CodeGenOpt::Less;
231 break;
232 case '2':
233 Conf.CGOptLevel = CodeGenOpt::Default;
234 break;
235 case '3':
236 Conf.CGOptLevel = CodeGenOpt::Aggressive;
237 break;
238 default:
239 llvm::errs() << "invalid cg optimization level: " << CGOptLevel << '\n';
240 return 1;
241 }
242
243 if (FileType.getNumOccurrences())
244 Conf.CGFileType = FileType;
245
246 Conf.OverrideTriple = OverrideTriple;
247 Conf.DefaultTriple = DefaultTriple;
248 Conf.StatsFile = StatsFile;
249
250 ThinBackend Backend;
251 if (ThinLTODistributedIndexes)
252 Backend = createWriteIndexesThinBackend(/* OldPrefix */ "",
253 /* NewPrefix */ "",
254 /* ShouldEmitImportsFiles */ true,
255 /* LinkedObjectsFile */ nullptr,
256 /* OnWrite */ {});
257 else
258 Backend = createInProcessThinBackend(Threads);
259 LTO Lto(std::move(Conf), std::move(Backend));
260
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);
266
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';
273 HasErrors = true;
274 } else {
275 Res.push_back(I->second.front());
276 I->second.pop_front();
277 if (I->second.empty())
278 CommandLineResolutions.erase(I);
279 }
280 }
281
282 if (HasErrors)
283 continue;
284
285 MBs.push_back(std::move(MB));
286 check(Lto.add(std::move(Input), Res), F);
287 }
288
289 if (!CommandLineResolutions.empty()) {
290 HasErrors = true;
291 for (auto UnusedRes : CommandLineResolutions)
292 llvm::errs() << argv[0] << ": unused symbol resolution for "
293 << UnusedRes.first.first << ',' << UnusedRes.first.second
294 << '\n';
295 }
296 if (HasErrors)
297 return 1;
298
299 auto AddStream =
300 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
301 std::string Path = OutputFilename + "." + utostr(Task);
302
303 std::error_code EC;
304 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
305 check(EC, Path);
306 return llvm::make_unique<lto::NativeObjectStream>(std::move(S));
307 };
308
309 auto AddBuffer = [&](size_t Task, std::unique_ptr<MemoryBuffer> MB) {
310 *AddStream(Task)->OS << MB->getBuffer();
311 };
312
313 NativeObjectCache Cache;
314 if (!CacheDir.empty())
315 Cache = check(localCache(CacheDir, AddBuffer), "failed to create cache");
316
317 check(Lto.run(AddStream, Cache), "LTO::run failed");
318 return 0;
319 }
320
dumpSymtab(int argc,char ** argv)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);
325
326 if (BFC.Symtab.size() >= sizeof(irsymtab::storage::Header)) {
327 auto *Hdr = reinterpret_cast<const irsymtab::storage::Header *>(
328 BFC.Symtab.data());
329 outs() << "version: " << Hdr->Version << '\n';
330 if (Hdr->Version == irsymtab::storage::Header::kCurrentVersion)
331 outs() << "producer: " << Hdr->Producer.get(BFC.StrtabForSymtab)
332 << '\n';
333 }
334
335 std::unique_ptr<InputFile> Input =
336 check(InputFile::create(MB->getMemBufferRef()), F);
337
338 outs() << "target triple: " << Input->getTargetTriple() << '\n';
339 Triple TT(Input->getTargetTriple());
340
341 outs() << "source filename: " << Input->getSourceFileName() << '\n';
342
343 if (TT.isOSBinFormatCOFF())
344 outs() << "linker opts: " << Input->getCOFFLinkerOpts() << '\n';
345
346 std::vector<StringRef> ComdatTable = Input->getComdatTable();
347 for (const InputFile::Symbol &Sym : Input->symbols()) {
348 switch (Sym.getVisibility()) {
349 case GlobalValue::HiddenVisibility:
350 outs() << 'H';
351 break;
352 case GlobalValue::ProtectedVisibility:
353 outs() << 'P';
354 break;
355 case GlobalValue::DefaultVisibility:
356 outs() << 'D';
357 break;
358 }
359
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';
369
370 if (Sym.isCommon())
371 outs() << " size " << Sym.getCommonSize() << " align "
372 << Sym.getCommonAlignment() << '\n';
373
374 int Comdat = Sym.getComdatIndex();
375 if (Comdat != -1)
376 outs() << " comdat " << ComdatTable[Comdat] << '\n';
377
378 if (TT.isOSBinFormatCOFF() && Sym.isWeak() && Sym.isIndirect())
379 outs() << " fallback " << Sym.getCOFFWeakExternalFallback() << '\n';
380
381 if (!Sym.getSectionName().empty())
382 outs() << " section " << Sym.getSectionName() << "\n";
383 }
384
385 outs() << '\n';
386 }
387
388 return 0;
389 }
390
main(int argc,char ** argv)391 int main(int argc, char **argv) {
392 InitLLVM X(argc, argv);
393 InitializeAllTargets();
394 InitializeAllTargetMCs();
395 InitializeAllAsmPrinters();
396 InitializeAllAsmParsers();
397
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).
401 if (argc < 2)
402 return usage();
403
404 StringRef Subcommand = argv[1];
405 // Ensure that argv[0] is correct after adjusting argv/argc.
406 argv[1] = argv[0];
407 if (Subcommand == "dump-symtab")
408 return dumpSymtab(argc - 1, argv + 1);
409 if (Subcommand == "run")
410 return run(argc - 1, argv + 1);
411 return usage();
412 }
413