xref: /llvm-project/bolt/tools/driver/llvm-bolt.cpp (revision a34c753fe709a624f5b087397fb05adeac2311e4)
1 //===-- llvm-bolt.cpp - Feedback-directed layout optimizer ----------------===//
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 is a binary optimizer that will take 'perf' output and change
10 // basic block layout for better performance (a.k.a. branch straightening),
11 // plus some other optimizations that are better performed on a binary.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "bolt/Profile/DataAggregator.h"
16 #include "bolt/Rewrite/MachORewriteInstance.h"
17 #include "bolt/Rewrite/RewriteInstance.h"
18 #include "bolt/Utils/CommandLineOpts.h"
19 #include "llvm/MC/TargetRegistry.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/Support/TargetSelect.h"
28 
29 #undef  DEBUG_TYPE
30 #define DEBUG_TYPE "bolt"
31 
32 using namespace llvm;
33 using namespace object;
34 using namespace bolt;
35 
36 namespace opts {
37 
38 static cl::OptionCategory *BoltCategories[] = {&BoltCategory,
39                                                &BoltOptCategory,
40                                                &BoltRelocCategory,
41                                                &BoltInstrCategory,
42                                                &BoltOutputCategory};
43 
44 static cl::OptionCategory *BoltDiffCategories[] = {&BoltDiffCategory};
45 
46 static cl::OptionCategory *Perf2BoltCategories[] = {&AggregatorCategory,
47                                                     &BoltOutputCategory};
48 
49 static cl::opt<std::string>
50 InputDataFilename("data",
51   cl::desc("<data file>"),
52   cl::Optional,
53   cl::cat(BoltCategory));
54 
55 static cl::alias
56 BoltProfile("b",
57   cl::desc("alias for -data"),
58   cl::aliasopt(InputDataFilename),
59   cl::cat(BoltCategory));
60 
61 static cl::opt<std::string>
62 InputDataFilename2("data2",
63   cl::desc("<data file>"),
64   cl::Optional,
65   cl::cat(BoltCategory));
66 
67 static cl::opt<std::string>
68 InputFilename2(
69   cl::Positional,
70   cl::desc("<executable>"),
71   cl::Optional,
72   cl::cat(BoltDiffCategory));
73 
74 } // namespace opts
75 
76 static StringRef ToolName;
77 
78 static void report_error(StringRef Message, std::error_code EC) {
79   assert(EC);
80   errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n";
81   exit(1);
82 }
83 
84 static void report_error(StringRef Message, Error E) {
85   assert(E);
86   errs() << ToolName << ": '" << Message << "': " << toString(std::move(E))
87          << ".\n";
88   exit(1);
89 }
90 
91 static void printBoltRevision(llvm::raw_ostream &OS) {
92   OS << "BOLT revision " << BoltRevision << "\n";
93 }
94 
95 void perf2boltMode(int argc, char **argv) {
96   cl::HideUnrelatedOptions(makeArrayRef(opts::Perf2BoltCategories));
97   cl::AddExtraVersionPrinter(printBoltRevision);
98   cl::ParseCommandLineOptions(
99       argc, argv,
100       "perf2bolt - BOLT data aggregator\n"
101       "\nEXAMPLE: perf2bolt -p=perf.data executable -o data.fdata\n");
102   if (opts::PerfData.empty()) {
103     errs() << ToolName << ": expected -perfdata=<filename> option.\n";
104     exit(1);
105   }
106   if (!opts::InputDataFilename.empty()) {
107     errs() << ToolName << ": unknown -data option.\n";
108     exit(1);
109   }
110   if (!sys::fs::exists(opts::PerfData))
111     report_error(opts::PerfData, errc::no_such_file_or_directory);
112   if (!DataAggregator::checkPerfDataMagic(opts::PerfData)) {
113     errs() << ToolName << ": '" << opts::PerfData
114            << "': expected valid perf.data file.\n";
115     exit(1);
116   }
117   if (opts::OutputFilename.empty()) {
118     errs() << ToolName << ": expected -o=<output file> option.\n";
119     exit(1);
120   }
121   opts::AggregateOnly = true;
122 }
123 
124 void heatmapMode(int argc, char **argv) {
125   // Insert a fake subcommand if invoked via a command alias.
126   std::unique_ptr<char *[]> FakeArgv;
127   if (argc == 1 || strcmp(argv[1], "heatmap")) {
128     ++argc;
129     FakeArgv.reset(new char *[argc+1]);
130     FakeArgv[0] = argv[0];
131     FakeArgv[1] = const_cast<char *>("heatmap");
132     for (int I = 2; I < argc; ++I)
133       FakeArgv[I] = argv[I - 1];
134     FakeArgv[argc] = nullptr;
135     argv = FakeArgv.get();
136   }
137 
138   cl::ParseCommandLineOptions(argc, argv, "");
139 
140   if (!sys::fs::exists(opts::InputFilename))
141     report_error(opts::InputFilename, errc::no_such_file_or_directory);
142 
143   if (opts::PerfData.empty()) {
144     errs() << ToolName << ": expected -perfdata=<filename> option.\n";
145     exit(1);
146   }
147 
148   opts::HeatmapMode = true;
149   opts::AggregateOnly = true;
150 }
151 
152 void boltDiffMode(int argc, char **argv) {
153   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltDiffCategories));
154   cl::AddExtraVersionPrinter(printBoltRevision);
155   cl::ParseCommandLineOptions(
156       argc, argv,
157       "llvm-boltdiff - BOLT binary diff tool\n"
158       "\nEXAMPLE: llvm-boltdiff -data=a.fdata -data2=b.fdata exec1 exec2\n");
159   if (opts::InputDataFilename2.empty()) {
160     errs() << ToolName << ": expected -data2=<filename> option.\n";
161     exit(1);
162   }
163   if (opts::InputDataFilename.empty()) {
164     errs() << ToolName << ": expected -data=<filename> option.\n";
165     exit(1);
166   }
167   if (opts::InputFilename2.empty()) {
168     errs() << ToolName << ": expected second binary name.\n";
169     exit(1);
170   }
171   if (opts::InputFilename.empty()) {
172     errs() << ToolName << ": expected binary.\n";
173     exit(1);
174   }
175   opts::DiffOnly = true;
176 }
177 
178 void boltMode(int argc, char **argv) {
179   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltCategories));
180   // Register the target printer for --version.
181   cl::AddExtraVersionPrinter(printBoltRevision);
182   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
183 
184   cl::ParseCommandLineOptions(argc, argv,
185                               "BOLT - Binary Optimization and Layout Tool\n");
186 
187   if (opts::OutputFilename.empty()) {
188     errs() << ToolName << ": expected -o=<output file> option.\n";
189     exit(1);
190   }
191 }
192 
193 std::string GetExecutablePath(const char *Argv0) {
194   SmallString<128> ExecutablePath(Argv0);
195   // Do a PATH lookup if Argv0 isn't a valid path.
196   if (!llvm::sys::fs::exists(ExecutablePath))
197     if (llvm::ErrorOr<std::string> P =
198             llvm::sys::findProgramByName(ExecutablePath))
199       ExecutablePath = *P;
200   return std::string(ExecutablePath.str());
201 }
202 
203 int main(int argc, char **argv) {
204   // Print a stack trace if we signal out.
205   sys::PrintStackTraceOnErrorSignal(argv[0]);
206   PrettyStackTraceProgram X(argc, argv);
207 
208   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
209 
210   std::string ToolPath = GetExecutablePath(argv[0]);
211 
212   // Initialize targets and assembly printers/parsers.
213   llvm::InitializeAllTargetInfos();
214   llvm::InitializeAllTargetMCs();
215   llvm::InitializeAllAsmParsers();
216   llvm::InitializeAllDisassemblers();
217 
218   llvm::InitializeAllTargets();
219   llvm::InitializeAllAsmPrinters();
220 
221   ToolName = argv[0];
222 
223   // Pre-process subcommands.
224   if (argc > 1 && *argv[1] != '-') {
225     if (!strcmp(argv[1], "heatmap"))
226       opts::HeatmapMode = true;
227   }
228 
229   if (llvm::sys::path::filename(ToolName) == "perf2bolt")
230     perf2boltMode(argc, argv);
231   else if (llvm::sys::path::filename(ToolName) == "llvm-boltdiff")
232     boltDiffMode(argc, argv);
233   else if (llvm::sys::path::filename(ToolName) == "llvm-bolt-heatmap" ||
234            opts::HeatmapMode)
235     heatmapMode(argc, argv);
236   else
237     boltMode(argc, argv);
238 
239 
240   if (!sys::fs::exists(opts::InputFilename))
241     report_error(opts::InputFilename, errc::no_such_file_or_directory);
242 
243   // Attempt to open the binary.
244   if (!opts::DiffOnly) {
245     Expected<OwningBinary<Binary>> BinaryOrErr =
246         createBinary(opts::InputFilename);
247     if (Error E = BinaryOrErr.takeError())
248       report_error(opts::InputFilename, std::move(E));
249     Binary &Binary = *BinaryOrErr.get().getBinary();
250 
251     if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) {
252       RewriteInstance RI(e, argc, argv, ToolPath);
253       if (!opts::PerfData.empty()) {
254         if (!opts::AggregateOnly) {
255           errs() << ToolName
256             << ": WARNING: reading perf data directly is unsupported, please use "
257             "-aggregate-only or perf2bolt.\n!!! Proceed on your own risk. !!!\n";
258         }
259         if (Error E = RI.setProfile(opts::PerfData))
260           report_error(opts::PerfData, std::move(E));
261       }
262       if (!opts::InputDataFilename.empty()) {
263         if (Error E = RI.setProfile(opts::InputDataFilename))
264           report_error(opts::InputDataFilename, std::move(E));
265       }
266       if (opts::AggregateOnly && opts::PerfData.empty()) {
267         errs() << ToolName << ": missing required -perfdata option.\n";
268         exit(1);
269       }
270 
271       RI.run();
272     } else if (auto *O = dyn_cast<MachOObjectFile>(&Binary)) {
273       MachORewriteInstance MachORI(O, ToolPath);
274 
275       if (!opts::InputDataFilename.empty())
276         if (Error E = MachORI.setProfile(opts::InputDataFilename))
277           report_error(opts::InputDataFilename, std::move(E));
278 
279       MachORI.run();
280     } else {
281       report_error(opts::InputFilename, object_error::invalid_file_type);
282     }
283 
284     return EXIT_SUCCESS;
285   }
286 
287   // Bolt-diff
288   Expected<OwningBinary<Binary>> BinaryOrErr1 =
289       createBinary(opts::InputFilename);
290   Expected<OwningBinary<Binary>> BinaryOrErr2 =
291       createBinary(opts::InputFilename2);
292   if (Error E = BinaryOrErr1.takeError())
293     report_error(opts::InputFilename, std::move(E));
294   if (Error E = BinaryOrErr2.takeError())
295     report_error(opts::InputFilename2, std::move(E));
296   Binary &Binary1 = *BinaryOrErr1.get().getBinary();
297   Binary &Binary2 = *BinaryOrErr2.get().getBinary();
298   if (auto *ELFObj1 = dyn_cast<ELFObjectFileBase>(&Binary1)) {
299     if (auto *ELFObj2 = dyn_cast<ELFObjectFileBase>(&Binary2)) {
300       RewriteInstance RI1(ELFObj1, argc, argv, ToolPath);
301       if (Error E = RI1.setProfile(opts::InputDataFilename))
302         report_error(opts::InputDataFilename, std::move(E));
303       RewriteInstance RI2(ELFObj2, argc, argv, ToolPath);
304       if (Error E = RI2.setProfile(opts::InputDataFilename2))
305         report_error(opts::InputDataFilename2, std::move(E));
306       outs() << "BOLT-DIFF: *** Analyzing binary 1: " << opts::InputFilename
307              << "\n";
308       outs() << "BOLT-DIFF: *** Binary 1 fdata:     " << opts::InputDataFilename
309              << "\n";
310       RI1.run();
311       outs() << "BOLT-DIFF: *** Analyzing binary 2: " << opts::InputFilename2
312              << "\n";
313       outs() << "BOLT-DIFF: *** Binary 2 fdata:     "
314              << opts::InputDataFilename2 << "\n";
315       RI2.run();
316       RI1.compare(RI2);
317     } else {
318       report_error(opts::InputFilename2, object_error::invalid_file_type);
319     }
320   } else {
321     report_error(opts::InputFilename, object_error::invalid_file_type);
322   }
323 
324   return EXIT_SUCCESS;
325 }
326