xref: /llvm-project/bolt/tools/driver/llvm-bolt.cpp (revision 2f09f445b2d6b3ef197aecd8d1e06d08140380f3)
1 //===- bolt/tools/driver/llvm-bolt.cpp - Feedback-directed 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 #define DEBUG_TYPE "bolt"
30 
31 using namespace llvm;
32 using namespace object;
33 using namespace bolt;
34 
35 namespace opts {
36 
37 static cl::OptionCategory *BoltCategories[] = {&BoltCategory,
38                                                &BoltOptCategory,
39                                                &BoltRelocCategory,
40                                                &BoltInstrCategory,
41                                                &BoltOutputCategory};
42 
43 static cl::OptionCategory *BoltDiffCategories[] = {&BoltDiffCategory};
44 
45 static cl::OptionCategory *Perf2BoltCategories[] = {&AggregatorCategory,
46                                                     &BoltOutputCategory};
47 
48 static cl::opt<std::string>
49 InputDataFilename("data",
50   cl::desc("<data file>"),
51   cl::Optional,
52   cl::cat(BoltCategory));
53 
54 static cl::alias
55 BoltProfile("b",
56   cl::desc("alias for -data"),
57   cl::aliasopt(InputDataFilename),
58   cl::cat(BoltCategory));
59 
60 static cl::opt<std::string>
61 InputDataFilename2("data2",
62   cl::desc("<data file>"),
63   cl::Optional,
64   cl::cat(BoltCategory));
65 
66 static cl::opt<std::string>
67 InputFilename2(
68   cl::Positional,
69   cl::desc("<executable>"),
70   cl::Optional,
71   cl::cat(BoltDiffCategory));
72 
73 } // namespace opts
74 
75 static StringRef ToolName;
76 
77 static void report_error(StringRef Message, std::error_code EC) {
78   assert(EC);
79   errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n";
80   exit(1);
81 }
82 
83 static void report_error(StringRef Message, Error E) {
84   assert(E);
85   errs() << ToolName << ": '" << Message << "': " << toString(std::move(E))
86          << ".\n";
87   exit(1);
88 }
89 
90 static void printBoltRevision(llvm::raw_ostream &OS) {
91   OS << "BOLT revision " << BoltRevision << "\n";
92 }
93 
94 void perf2boltMode(int argc, char **argv) {
95   cl::HideUnrelatedOptions(makeArrayRef(opts::Perf2BoltCategories));
96   cl::AddExtraVersionPrinter(printBoltRevision);
97   cl::ParseCommandLineOptions(
98       argc, argv,
99       "perf2bolt - BOLT data aggregator\n"
100       "\nEXAMPLE: perf2bolt -p=perf.data executable -o data.fdata\n");
101   if (opts::PerfData.empty()) {
102     errs() << ToolName << ": expected -perfdata=<filename> option.\n";
103     exit(1);
104   }
105   if (!opts::InputDataFilename.empty()) {
106     errs() << ToolName << ": unknown -data option.\n";
107     exit(1);
108   }
109   if (!sys::fs::exists(opts::PerfData))
110     report_error(opts::PerfData, errc::no_such_file_or_directory);
111   if (!DataAggregator::checkPerfDataMagic(opts::PerfData)) {
112     errs() << ToolName << ": '" << opts::PerfData
113            << "': expected valid perf.data file.\n";
114     exit(1);
115   }
116   if (opts::OutputFilename.empty()) {
117     errs() << ToolName << ": expected -o=<output file> option.\n";
118     exit(1);
119   }
120   opts::AggregateOnly = true;
121 }
122 
123 void heatmapMode(int argc, char **argv) {
124   // Insert a fake subcommand if invoked via a command alias.
125   std::unique_ptr<char *[]> FakeArgv;
126   if (argc == 1 || strcmp(argv[1], "heatmap")) {
127     ++argc;
128     FakeArgv.reset(new char *[argc + 1]);
129     FakeArgv[0] = argv[0];
130     FakeArgv[1] = const_cast<char *>("heatmap");
131     for (int I = 2; I < argc; ++I)
132       FakeArgv[I] = argv[I - 1];
133     FakeArgv[argc] = nullptr;
134     argv = FakeArgv.get();
135   }
136 
137   cl::ParseCommandLineOptions(argc, argv, "");
138 
139   if (!sys::fs::exists(opts::InputFilename))
140     report_error(opts::InputFilename, errc::no_such_file_or_directory);
141 
142   if (opts::PerfData.empty()) {
143     errs() << ToolName << ": expected -perfdata=<filename> option.\n";
144     exit(1);
145   }
146 
147   opts::HeatmapMode = true;
148   opts::AggregateOnly = true;
149 }
150 
151 void boltDiffMode(int argc, char **argv) {
152   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltDiffCategories));
153   cl::AddExtraVersionPrinter(printBoltRevision);
154   cl::ParseCommandLineOptions(
155       argc, argv,
156       "llvm-boltdiff - BOLT binary diff tool\n"
157       "\nEXAMPLE: llvm-boltdiff -data=a.fdata -data2=b.fdata exec1 exec2\n");
158   if (opts::InputDataFilename2.empty()) {
159     errs() << ToolName << ": expected -data2=<filename> option.\n";
160     exit(1);
161   }
162   if (opts::InputDataFilename.empty()) {
163     errs() << ToolName << ": expected -data=<filename> option.\n";
164     exit(1);
165   }
166   if (opts::InputFilename2.empty()) {
167     errs() << ToolName << ": expected second binary name.\n";
168     exit(1);
169   }
170   if (opts::InputFilename.empty()) {
171     errs() << ToolName << ": expected binary.\n";
172     exit(1);
173   }
174   opts::DiffOnly = true;
175 }
176 
177 void boltMode(int argc, char **argv) {
178   cl::HideUnrelatedOptions(makeArrayRef(opts::BoltCategories));
179   // Register the target printer for --version.
180   cl::AddExtraVersionPrinter(printBoltRevision);
181   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
182 
183   cl::ParseCommandLineOptions(argc, argv,
184                               "BOLT - Binary Optimization and Layout Tool\n");
185 
186   if (opts::OutputFilename.empty()) {
187     errs() << ToolName << ": expected -o=<output file> option.\n";
188     exit(1);
189   }
190 }
191 
192 std::string GetExecutablePath(const char *Argv0) {
193   SmallString<128> ExecutablePath(Argv0);
194   // Do a PATH lookup if Argv0 isn't a valid path.
195   if (!llvm::sys::fs::exists(ExecutablePath))
196     if (llvm::ErrorOr<std::string> P =
197             llvm::sys::findProgramByName(ExecutablePath))
198       ExecutablePath = *P;
199   return std::string(ExecutablePath.str());
200 }
201 
202 int main(int argc, char **argv) {
203   // Print a stack trace if we signal out.
204   sys::PrintStackTraceOnErrorSignal(argv[0]);
205   PrettyStackTraceProgram X(argc, argv);
206 
207   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
208 
209   std::string ToolPath = GetExecutablePath(argv[0]);
210 
211   // Initialize targets and assembly printers/parsers.
212   llvm::InitializeAllTargetInfos();
213   llvm::InitializeAllTargetMCs();
214   llvm::InitializeAllAsmParsers();
215   llvm::InitializeAllDisassemblers();
216 
217   llvm::InitializeAllTargets();
218   llvm::InitializeAllAsmPrinters();
219 
220   ToolName = argv[0];
221 
222   // Pre-process subcommands.
223   if (argc > 1 && *argv[1] != '-') {
224     if (!strcmp(argv[1], "heatmap"))
225       opts::HeatmapMode = true;
226   }
227 
228   if (llvm::sys::path::filename(ToolName) == "perf2bolt")
229     perf2boltMode(argc, argv);
230   else if (llvm::sys::path::filename(ToolName) == "llvm-boltdiff")
231     boltDiffMode(argc, argv);
232   else if (llvm::sys::path::filename(ToolName) == "llvm-bolt-heatmap" ||
233            opts::HeatmapMode)
234     heatmapMode(argc, argv);
235   else
236     boltMode(argc, argv);
237 
238   if (!sys::fs::exists(opts::InputFilename))
239     report_error(opts::InputFilename, errc::no_such_file_or_directory);
240 
241   // Attempt to open the binary.
242   if (!opts::DiffOnly) {
243     Expected<OwningBinary<Binary>> BinaryOrErr =
244         createBinary(opts::InputFilename);
245     if (Error E = BinaryOrErr.takeError())
246       report_error(opts::InputFilename, std::move(E));
247     Binary &Binary = *BinaryOrErr.get().getBinary();
248 
249     if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) {
250       RewriteInstance RI(e, argc, argv, ToolPath);
251       if (!opts::PerfData.empty()) {
252         if (!opts::AggregateOnly) {
253           errs() << ToolName
254                  << ": WARNING: reading perf data directly is unsupported, "
255                     "please use "
256                     "-aggregate-only or perf2bolt.\n!!! Proceed on your own "
257                     "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