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 | |
37 | static void report_error(StringRef Message, std::error_code EC) { |
38 | assert(EC); |
39 | errs() << ToolName << ": '" << Message << "': " << EC.message() << ".\n" ; |
40 | exit(status: 1); |
41 | } |
42 | |
43 | static void report_error(StringRef Message, Error E) { |
44 | assert(E); |
45 | errs() << ToolName << ": '" << Message << "': " << toString(E: std::move(E)) |
46 | << ".\n" ; |
47 | exit(status: 1); |
48 | } |
49 | |
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(Path: ExecutablePath)) |
54 | if (llvm::ErrorOr<std::string> P = |
55 | llvm::sys::findProgramByName(Name: ExecutablePath)) |
56 | ExecutablePath = *P; |
57 | return std::string(ExecutablePath); |
58 | } |
59 | |
60 | int main(int argc, char **argv) { |
61 | cl::HideUnrelatedOptions(Categories: ArrayRef(opts::HeatmapCategories)); |
62 | cl::ParseCommandLineOptions(argc, argv, Overview: "" ); |
63 | |
64 | if (opts::PerfData.empty()) { |
65 | errs() << ToolName << ": expected -perfdata=<filename> option.\n" ; |
66 | exit(status: 1); |
67 | } |
68 | |
69 | opts::HeatmapMode = true; |
70 | opts::AggregateOnly = true; |
71 | if (!sys::fs::exists(Path: opts::InputFilename)) |
72 | report_error(Message: opts::InputFilename, EC: 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(Argv0: argv[0]); |
89 | Expected<OwningBinary<Binary>> BinaryOrErr = |
90 | createBinary(Path: opts::InputFilename); |
91 | if (Error E = BinaryOrErr.takeError()) |
92 | report_error(Message: opts::InputFilename, E: std::move(E)); |
93 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
94 | |
95 | if (auto *e = dyn_cast<ELFObjectFileBase>(Val: &Binary)) { |
96 | auto RIOrErr = RewriteInstance::create(File: e, Argc: argc, Argv: argv, ToolPath); |
97 | if (Error E = RIOrErr.takeError()) |
98 | report_error(Message: "RewriteInstance" , E: std::move(E)); |
99 | |
100 | RewriteInstance &RI = *RIOrErr.get(); |
101 | if (Error E = RI.setProfile(opts::PerfData)) |
102 | report_error(Message: opts::PerfData, E: std::move(E)); |
103 | |
104 | if (Error E = RI.run()) |
105 | report_error(Message: opts::InputFilename, E: std::move(E)); |
106 | } else { |
107 | report_error(Message: opts::InputFilename, EC: object_error::invalid_file_type); |
108 | } |
109 | |
110 | return EXIT_SUCCESS; |
111 | } |
112 | |