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/Errc.h" 23 #include "llvm/Support/Error.h" 24 #include "llvm/Support/ManagedStatic.h" 25 #include "llvm/Support/Path.h" 26 #include "llvm/Support/PrettyStackTrace.h" 27 #include "llvm/Support/Signals.h" 28 #include "llvm/Support/TargetSelect.h" 29 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> InputFilename(cl::Positional, 50 cl::desc("<executable>"), 51 cl::Required, cl::cat(BoltCategory), 52 cl::sub(cl::SubCommand::getAll())); 53 54 static cl::opt<std::string> 55 InputDataFilename("data", 56 cl::desc("<data file>"), 57 cl::Optional, 58 cl::cat(BoltCategory)); 59 60 static cl::alias 61 BoltProfile("b", 62 cl::desc("alias for -data"), 63 cl::aliasopt(InputDataFilename), 64 cl::cat(BoltCategory)); 65 66 cl::opt<std::string> 67 LogFile("log-file", 68 cl::desc("redirect journaling to a file instead of stdout/stderr"), 69 cl::Hidden, cl::cat(BoltCategory)); 70 71 static cl::opt<std::string> 72 InputDataFilename2("data2", 73 cl::desc("<data file>"), 74 cl::Optional, 75 cl::cat(BoltCategory)); 76 77 static cl::opt<std::string> 78 InputFilename2( 79 cl::Positional, 80 cl::desc("<executable>"), 81 cl::Optional, 82 cl::cat(BoltDiffCategory)); 83 84 } // namespace opts 85 86 static StringRef ToolName; 87 88 static void report_error(StringRef Message, std::error_code EC) { 89 assert(EC); 90 errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n"; 91 exit(1); 92 } 93 94 static void report_error(StringRef Message, Error E) { 95 assert(E); 96 errs() << ToolName << ": '" << Message << "': " << toString(std::move(E)) 97 << ".\n"; 98 exit(1); 99 } 100 101 static void printBoltRevision(llvm::raw_ostream &OS) { 102 OS << "BOLT revision " << BoltRevision << "\n"; 103 } 104 105 void perf2boltMode(int argc, char **argv) { 106 cl::HideUnrelatedOptions(ArrayRef(opts::Perf2BoltCategories)); 107 cl::AddExtraVersionPrinter(printBoltRevision); 108 cl::ParseCommandLineOptions( 109 argc, argv, 110 "perf2bolt - BOLT data aggregator\n" 111 "\nEXAMPLE: perf2bolt -p=perf.data executable -o data.fdata\n"); 112 if (opts::PerfData.empty()) { 113 errs() << ToolName << ": expected -perfdata=<filename> option.\n"; 114 exit(1); 115 } 116 if (!opts::InputDataFilename.empty()) { 117 errs() << ToolName << ": unknown -data option.\n"; 118 exit(1); 119 } 120 if (!sys::fs::exists(opts::PerfData)) 121 report_error(opts::PerfData, errc::no_such_file_or_directory); 122 if (!DataAggregator::checkPerfDataMagic(opts::PerfData)) { 123 errs() << ToolName << ": '" << opts::PerfData 124 << "': expected valid perf.data file.\n"; 125 exit(1); 126 } 127 if (opts::OutputFilename.empty()) { 128 errs() << ToolName << ": expected -o=<output file> option.\n"; 129 exit(1); 130 } 131 opts::AggregateOnly = true; 132 opts::ShowDensity = true; 133 } 134 135 void boltDiffMode(int argc, char **argv) { 136 cl::HideUnrelatedOptions(ArrayRef(opts::BoltDiffCategories)); 137 cl::AddExtraVersionPrinter(printBoltRevision); 138 cl::ParseCommandLineOptions( 139 argc, argv, 140 "llvm-boltdiff - BOLT binary diff tool\n" 141 "\nEXAMPLE: llvm-boltdiff -data=a.fdata -data2=b.fdata exec1 exec2\n"); 142 if (opts::InputDataFilename2.empty()) { 143 errs() << ToolName << ": expected -data2=<filename> option.\n"; 144 exit(1); 145 } 146 if (opts::InputDataFilename.empty()) { 147 errs() << ToolName << ": expected -data=<filename> option.\n"; 148 exit(1); 149 } 150 if (opts::InputFilename2.empty()) { 151 errs() << ToolName << ": expected second binary name.\n"; 152 exit(1); 153 } 154 if (opts::InputFilename.empty()) { 155 errs() << ToolName << ": expected binary.\n"; 156 exit(1); 157 } 158 opts::DiffOnly = true; 159 } 160 161 void boltMode(int argc, char **argv) { 162 cl::HideUnrelatedOptions(ArrayRef(opts::BoltCategories)); 163 // Register the target printer for --version. 164 cl::AddExtraVersionPrinter(printBoltRevision); 165 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 166 167 cl::ParseCommandLineOptions(argc, argv, 168 "BOLT - Binary Optimization and Layout Tool\n"); 169 170 if (opts::OutputFilename.empty()) { 171 errs() << ToolName << ": expected -o=<output file> option.\n"; 172 exit(1); 173 } 174 } 175 176 static std::string GetExecutablePath(const char *Argv0) { 177 SmallString<256> ExecutablePath(Argv0); 178 // Do a PATH lookup if Argv0 isn't a valid path. 179 if (!llvm::sys::fs::exists(ExecutablePath)) 180 if (llvm::ErrorOr<std::string> P = 181 llvm::sys::findProgramByName(ExecutablePath)) 182 ExecutablePath = *P; 183 return std::string(ExecutablePath); 184 } 185 186 int main(int argc, char **argv) { 187 // Print a stack trace if we signal out. 188 sys::PrintStackTraceOnErrorSignal(argv[0]); 189 PrettyStackTraceProgram X(argc, argv); 190 191 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 192 193 std::string ToolPath = GetExecutablePath(argv[0]); 194 195 // Initialize targets and assembly printers/parsers. 196 llvm::InitializeAllTargetInfos(); 197 llvm::InitializeAllTargetMCs(); 198 llvm::InitializeAllAsmParsers(); 199 llvm::InitializeAllDisassemblers(); 200 201 llvm::InitializeAllTargets(); 202 llvm::InitializeAllAsmPrinters(); 203 204 ToolName = argv[0]; 205 206 if (llvm::sys::path::filename(ToolName).starts_with("perf2bolt")) 207 perf2boltMode(argc, argv); 208 else if (llvm::sys::path::filename(ToolName).starts_with("llvm-boltdiff")) 209 boltDiffMode(argc, argv); 210 else 211 boltMode(argc, argv); 212 213 if (!sys::fs::exists(opts::InputFilename)) 214 report_error(opts::InputFilename, errc::no_such_file_or_directory); 215 216 // Initialize journaling streams 217 raw_ostream *BOLTJournalOut = &outs(); 218 raw_ostream *BOLTJournalErr = &errs(); 219 // RAII obj to keep log file open throughout execution 220 std::unique_ptr<raw_fd_ostream> LogFileStream; 221 if (!opts::LogFile.empty()) { 222 std::error_code LogEC; 223 LogFileStream = std::make_unique<raw_fd_ostream>( 224 opts::LogFile, LogEC, sys::fs::OpenFlags::OF_None); 225 if (LogEC) { 226 errs() << "BOLT-ERROR: cannot open requested log file for writing: " 227 << LogEC.message() << "\n"; 228 exit(1); 229 } 230 BOLTJournalOut = LogFileStream.get(); 231 BOLTJournalErr = LogFileStream.get(); 232 } 233 234 // Attempt to open the binary. 235 if (!opts::DiffOnly) { 236 Expected<OwningBinary<Binary>> BinaryOrErr = 237 createBinary(opts::InputFilename); 238 if (Error E = BinaryOrErr.takeError()) 239 report_error(opts::InputFilename, std::move(E)); 240 Binary &Binary = *BinaryOrErr.get().getBinary(); 241 242 if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) { 243 auto RIOrErr = RewriteInstance::create(e, argc, argv, ToolPath, 244 *BOLTJournalOut, *BOLTJournalErr); 245 if (Error E = RIOrErr.takeError()) 246 report_error(opts::InputFilename, std::move(E)); 247 RewriteInstance &RI = *RIOrErr.get(); 248 if (!opts::PerfData.empty()) { 249 if (!opts::AggregateOnly) { 250 errs() << ToolName 251 << ": WARNING: reading perf data directly is unsupported, " 252 "please use " 253 "-aggregate-only or perf2bolt.\n!!! Proceed on your own " 254 "risk. !!!\n"; 255 } 256 if (Error E = RI.setProfile(opts::PerfData)) 257 report_error(opts::PerfData, std::move(E)); 258 } 259 if (!opts::InputDataFilename.empty()) { 260 if (Error E = RI.setProfile(opts::InputDataFilename)) 261 report_error(opts::InputDataFilename, std::move(E)); 262 } 263 if (opts::AggregateOnly && opts::PerfData.empty()) { 264 errs() << ToolName << ": missing required -perfdata option.\n"; 265 exit(1); 266 } 267 268 if (Error E = RI.run()) 269 report_error(opts::InputFilename, std::move(E)); 270 } else if (auto *O = dyn_cast<MachOObjectFile>(&Binary)) { 271 auto MachORIOrErr = MachORewriteInstance::create(O, ToolPath); 272 if (Error E = MachORIOrErr.takeError()) 273 report_error(opts::InputFilename, std::move(E)); 274 MachORewriteInstance &MachORI = *MachORIOrErr.get(); 275 276 if (!opts::InputDataFilename.empty()) 277 if (Error E = MachORI.setProfile(opts::InputDataFilename)) 278 report_error(opts::InputDataFilename, std::move(E)); 279 280 MachORI.run(); 281 } else { 282 report_error(opts::InputFilename, object_error::invalid_file_type); 283 } 284 285 return EXIT_SUCCESS; 286 } 287 288 // Bolt-diff 289 Expected<OwningBinary<Binary>> BinaryOrErr1 = 290 createBinary(opts::InputFilename); 291 Expected<OwningBinary<Binary>> BinaryOrErr2 = 292 createBinary(opts::InputFilename2); 293 if (Error E = BinaryOrErr1.takeError()) 294 report_error(opts::InputFilename, std::move(E)); 295 if (Error E = BinaryOrErr2.takeError()) 296 report_error(opts::InputFilename2, std::move(E)); 297 Binary &Binary1 = *BinaryOrErr1.get().getBinary(); 298 Binary &Binary2 = *BinaryOrErr2.get().getBinary(); 299 if (auto *ELFObj1 = dyn_cast<ELFObjectFileBase>(&Binary1)) { 300 if (auto *ELFObj2 = dyn_cast<ELFObjectFileBase>(&Binary2)) { 301 auto RI1OrErr = RewriteInstance::create(ELFObj1, argc, argv, ToolPath); 302 if (Error E = RI1OrErr.takeError()) 303 report_error(opts::InputFilename, std::move(E)); 304 RewriteInstance &RI1 = *RI1OrErr.get(); 305 if (Error E = RI1.setProfile(opts::InputDataFilename)) 306 report_error(opts::InputDataFilename, std::move(E)); 307 auto RI2OrErr = RewriteInstance::create(ELFObj2, argc, argv, ToolPath); 308 if (Error E = RI2OrErr.takeError()) 309 report_error(opts::InputFilename2, std::move(E)); 310 RewriteInstance &RI2 = *RI2OrErr.get(); 311 if (Error E = RI2.setProfile(opts::InputDataFilename2)) 312 report_error(opts::InputDataFilename2, std::move(E)); 313 outs() << "BOLT-DIFF: *** Analyzing binary 1: " << opts::InputFilename 314 << "\n"; 315 outs() << "BOLT-DIFF: *** Binary 1 fdata: " << opts::InputDataFilename 316 << "\n"; 317 if (Error E = RI1.run()) 318 report_error(opts::InputFilename, std::move(E)); 319 outs() << "BOLT-DIFF: *** Analyzing binary 2: " << opts::InputFilename2 320 << "\n"; 321 outs() << "BOLT-DIFF: *** Binary 2 fdata: " 322 << opts::InputDataFilename2 << "\n"; 323 if (Error E = RI2.run()) 324 report_error(opts::InputFilename2, std::move(E)); 325 RI1.compare(RI2); 326 } else { 327 report_error(opts::InputFilename2, object_error::invalid_file_type); 328 } 329 } else { 330 report_error(opts::InputFilename, object_error::invalid_file_type); 331 } 332 333 return EXIT_SUCCESS; 334 } 335