[NFC][Py Reformat] Reformat python files in llvm
[llvm-project.git] / llvm / tools / llvm-profgen / llvm-profgen.cpp
blob3b974e25103ad4bfc40604a02a96920a5d6571fa
1 //===- llvm-profgen.cpp - LLVM SPGO profile generation tool -----*- C++ -*-===//
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 // llvm-profgen generates SPGO profiles from perf script ouput.
11 //===----------------------------------------------------------------------===//
13 #include "ErrorHandling.h"
14 #include "PerfReader.h"
15 #include "ProfileGenerator.h"
16 #include "ProfiledBinary.h"
17 #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/InitLLVM.h"
21 #include "llvm/Support/TargetSelect.h"
22 #include "llvm/Support/VirtualFileSystem.h"
24 static cl::OptionCategory ProfGenCategory("ProfGen Options");
26 static cl::opt<std::string> PerfScriptFilename(
27 "perfscript", cl::value_desc("perfscript"),
28 cl::desc("Path of perf-script trace created by Linux perf tool with "
29 "`script` command(the raw perf.data should be profiled with -b)"),
30 cl::cat(ProfGenCategory));
31 static cl::alias PSA("ps", cl::desc("Alias for --perfscript"),
32 cl::aliasopt(PerfScriptFilename));
34 static cl::opt<std::string> PerfDataFilename(
35 "perfdata", cl::value_desc("perfdata"),
36 cl::desc("Path of raw perf data created by Linux perf tool (it should be "
37 "profiled with -b)"),
38 cl::cat(ProfGenCategory));
39 static cl::alias PDA("pd", cl::desc("Alias for --perfdata"),
40 cl::aliasopt(PerfDataFilename));
42 static cl::opt<std::string> UnsymbolizedProfFilename(
43 "unsymbolized-profile", cl::value_desc("unsymbolized profile"),
44 cl::desc("Path of the unsymbolized profile created by "
45 "`llvm-profgen` with `--skip-symbolization`"),
46 cl::cat(ProfGenCategory));
47 static cl::alias UPA("up", cl::desc("Alias for --unsymbolized-profile"),
48 cl::aliasopt(UnsymbolizedProfFilename));
50 static cl::opt<std::string> SampleProfFilename(
51 "llvm-sample-profile", cl::value_desc("llvm sample profile"),
52 cl::desc("Path of the LLVM sample profile"), cl::cat(ProfGenCategory));
54 static cl::opt<std::string>
55 BinaryPath("binary", cl::value_desc("binary"), cl::Required,
56 cl::desc("Path of profiled executable binary."),
57 cl::cat(ProfGenCategory));
59 static cl::opt<uint32_t>
60 ProcessId("pid", cl::value_desc("process Id"), cl::init(0),
61 cl::desc("Process Id for the profiled executable binary."),
62 cl::cat(ProfGenCategory));
64 static cl::opt<std::string> DebugBinPath(
65 "debug-binary", cl::value_desc("debug-binary"),
66 cl::desc("Path of debug info binary, llvm-profgen will load the DWARF info "
67 "from it instead of the executable binary."),
68 cl::cat(ProfGenCategory));
70 extern cl::opt<bool> ShowDisassemblyOnly;
71 extern cl::opt<bool> ShowSourceLocations;
72 extern cl::opt<bool> SkipSymbolization;
74 using namespace llvm;
75 using namespace sampleprof;
77 // Validate the command line input.
78 static void validateCommandLine() {
79 // Allow the missing perfscript if we only use to show binary disassembly.
80 if (!ShowDisassemblyOnly) {
81 // Validate input profile is provided only once
82 bool HasPerfData = PerfDataFilename.getNumOccurrences() > 0;
83 bool HasPerfScript = PerfScriptFilename.getNumOccurrences() > 0;
84 bool HasUnsymbolizedProfile =
85 UnsymbolizedProfFilename.getNumOccurrences() > 0;
86 bool HasSampleProfile = SampleProfFilename.getNumOccurrences() > 0;
87 uint16_t S =
88 HasPerfData + HasPerfScript + HasUnsymbolizedProfile + HasSampleProfile;
89 if (S != 1) {
90 std::string Msg =
91 S > 1
92 ? "`--perfscript`, `--perfdata` and `--unsymbolized-profile` "
93 "cannot be used together."
94 : "Perf input file is missing, please use one of `--perfscript`, "
95 "`--perfdata` and `--unsymbolized-profile` for the input.";
96 exitWithError(Msg);
99 auto CheckFileExists = [](bool H, StringRef File) {
100 if (H && !llvm::sys::fs::exists(File)) {
101 std::string Msg = "Input perf file(" + File.str() + ") doesn't exist.";
102 exitWithError(Msg);
106 CheckFileExists(HasPerfData, PerfDataFilename);
107 CheckFileExists(HasPerfScript, PerfScriptFilename);
108 CheckFileExists(HasUnsymbolizedProfile, UnsymbolizedProfFilename);
109 CheckFileExists(HasSampleProfile, SampleProfFilename);
112 if (!llvm::sys::fs::exists(BinaryPath)) {
113 std::string Msg = "Input binary(" + BinaryPath + ") doesn't exist.";
114 exitWithError(Msg);
117 if (CSProfileGenerator::MaxCompressionSize < -1) {
118 exitWithError("Value of --compress-recursion should >= -1");
120 if (ShowSourceLocations && !ShowDisassemblyOnly) {
121 exitWithError("--show-source-locations should work together with "
122 "--show-disassembly-only!");
126 static PerfInputFile getPerfInputFile() {
127 PerfInputFile File;
128 if (PerfDataFilename.getNumOccurrences()) {
129 File.InputFile = PerfDataFilename;
130 File.Format = PerfFormat::PerfData;
131 } else if (PerfScriptFilename.getNumOccurrences()) {
132 File.InputFile = PerfScriptFilename;
133 File.Format = PerfFormat::PerfScript;
134 } else if (UnsymbolizedProfFilename.getNumOccurrences()) {
135 File.InputFile = UnsymbolizedProfFilename;
136 File.Format = PerfFormat::UnsymbolizedProfile;
138 return File;
141 int main(int argc, const char *argv[]) {
142 InitLLVM X(argc, argv);
144 // Initialize targets and assembly printers/parsers.
145 InitializeAllTargetInfos();
146 InitializeAllTargetMCs();
147 InitializeAllDisassemblers();
149 cl::HideUnrelatedOptions({&ProfGenCategory, &getColorCategory()});
150 cl::ParseCommandLineOptions(argc, argv, "llvm SPGO profile generator\n");
151 validateCommandLine();
153 // Load symbols and disassemble the code of a given binary.
154 std::unique_ptr<ProfiledBinary> Binary =
155 std::make_unique<ProfiledBinary>(BinaryPath, DebugBinPath);
156 if (ShowDisassemblyOnly)
157 return EXIT_SUCCESS;
159 if (SampleProfFilename.getNumOccurrences()) {
160 LLVMContext Context;
161 auto FS = vfs::getRealFileSystem();
162 auto ReaderOrErr =
163 SampleProfileReader::create(SampleProfFilename, Context, *FS);
164 std::unique_ptr<sampleprof::SampleProfileReader> Reader =
165 std::move(ReaderOrErr.get());
166 Reader->read();
167 std::unique_ptr<ProfileGeneratorBase> Generator =
168 ProfileGeneratorBase::create(Binary.get(), Reader->getProfiles(),
169 Reader->profileIsCS());
170 Generator->generateProfile();
171 Generator->write();
172 } else {
173 std::optional<uint32_t> PIDFilter;
174 if (ProcessId.getNumOccurrences())
175 PIDFilter = ProcessId;
176 PerfInputFile PerfFile = getPerfInputFile();
177 std::unique_ptr<PerfReaderBase> Reader =
178 PerfReaderBase::create(Binary.get(), PerfFile, PIDFilter);
179 // Parse perf events and samples
180 Reader->parsePerfTraces();
182 if (SkipSymbolization)
183 return EXIT_SUCCESS;
185 std::unique_ptr<ProfileGeneratorBase> Generator =
186 ProfileGeneratorBase::create(Binary.get(), &Reader->getSampleCounters(),
187 Reader->profileIsCS());
188 Generator->generateProfile();
189 Generator->write();
192 return EXIT_SUCCESS;