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