xref: /llvm-project/bolt/tools/heatmap/heatmap.cpp (revision fd38366e4525c5507bbb2a2fc1f7d113a964224e)
1 //===- bolt/tools/heatmap/heatmap.cpp - Profile heatmap visualization tool ===//
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 #include "bolt/Rewrite/RewriteInstance.h"
10 #include "bolt/Utils/CommandLineOpts.h"
11 #include "llvm/MC/TargetRegistry.h"
12 #include "llvm/Object/Binary.h"
13 #include "llvm/Support/CommandLine.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/Error.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Program.h"
18 #include "llvm/Support/TargetSelect.h"
19 
20 using namespace llvm;
21 using namespace bolt;
22 
23 namespace opts {
24 
25 static cl::OptionCategory *HeatmapCategories[] = {&HeatmapCategory,
26                                                   &BoltOutputCategory};
27 
28 static cl::opt<std::string> InputFilename(cl::Positional,
29                                           cl::desc("<executable>"),
30                                           cl::Required,
31                                           cl::cat(HeatmapCategory));
32 
33 } // namespace opts
34 
35 static StringRef ToolName;
36 
report_error(StringRef Message,std::error_code EC)37 static void report_error(StringRef Message, std::error_code EC) {
38   assert(EC);
39   errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n";
40   exit(1);
41 }
42 
report_error(StringRef Message,Error E)43 static void report_error(StringRef Message, Error E) {
44   assert(E);
45   errs() << ToolName << ": '" << Message << "': " << toString(std::move(E))
46          << ".\n";
47   exit(1);
48 }
49 
GetExecutablePath(const char * Argv0)50 static std::string GetExecutablePath(const char *Argv0) {
51   SmallString<256> ExecutablePath(Argv0);
52   // Do a PATH lookup if Argv0 isn't a valid path.
53   if (!llvm::sys::fs::exists(ExecutablePath))
54     if (llvm::ErrorOr<std::string> P =
55             llvm::sys::findProgramByName(ExecutablePath))
56       ExecutablePath = *P;
57   return std::string(ExecutablePath);
58 }
59 
main(int argc,char ** argv)60 int main(int argc, char **argv) {
61   cl::HideUnrelatedOptions(ArrayRef(opts::HeatmapCategories));
62   cl::ParseCommandLineOptions(argc, argv, "");
63 
64   if (opts::PerfData.empty()) {
65     errs() << ToolName << ": expected -perfdata=<filename> option.\n";
66     exit(1);
67   }
68 
69   opts::HeatmapMode = true;
70   opts::AggregateOnly = true;
71   if (!sys::fs::exists(opts::InputFilename))
72     report_error(opts::InputFilename, errc::no_such_file_or_directory);
73 
74   // Output to stdout by default
75   if (opts::OutputFilename.empty())
76     opts::OutputFilename = "-";
77 
78   // Initialize targets and assembly printers/parsers.
79   llvm::InitializeAllTargetInfos();
80   llvm::InitializeAllTargetMCs();
81   llvm::InitializeAllAsmParsers();
82   llvm::InitializeAllDisassemblers();
83 
84   llvm::InitializeAllTargets();
85   llvm::InitializeAllAsmPrinters();
86 
87   ToolName = argv[0];
88   std::string ToolPath = GetExecutablePath(argv[0]);
89   Expected<OwningBinary<Binary>> BinaryOrErr =
90       createBinary(opts::InputFilename);
91   if (Error E = BinaryOrErr.takeError())
92     report_error(opts::InputFilename, std::move(E));
93   Binary &Binary = *BinaryOrErr.get().getBinary();
94 
95   if (auto *e = dyn_cast<ELFObjectFileBase>(&Binary)) {
96     auto RIOrErr = RewriteInstance::create(e, argc, argv, ToolPath);
97     if (Error E = RIOrErr.takeError())
98       report_error("RewriteInstance", std::move(E));
99 
100     RewriteInstance &RI = *RIOrErr.get();
101     if (Error E = RI.setProfile(opts::PerfData))
102       report_error(opts::PerfData, std::move(E));
103 
104     if (Error E = RI.run())
105       report_error(opts::InputFilename, std::move(E));
106   } else {
107     report_error(opts::InputFilename, object_error::invalid_file_type);
108   }
109 
110   return EXIT_SUCCESS;
111 }
112