1 //===-- llvm-exegesis.cpp ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Measures execution properties (latencies/uops) of an instruction.
12 //===----------------------------------------------------------------------===//
14 #include "lib/Analysis.h"
15 #include "lib/BenchmarkResult.h"
16 #include "lib/BenchmarkRunner.h"
17 #include "lib/Clustering.h"
18 #include "lib/Error.h"
19 #include "lib/LlvmState.h"
20 #include "lib/PerfHelper.h"
21 #include "lib/SnippetFile.h"
22 #include "lib/SnippetRepetitor.h"
23 #include "lib/Target.h"
24 #include "lib/TargetSelect.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/MC/MCInstBuilder.h"
28 #include "llvm/MC/MCObjectFileInfo.h"
29 #include "llvm/MC/MCParser/MCAsmParser.h"
30 #include "llvm/MC/MCParser/MCTargetAsmParser.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCSubtargetInfo.h"
33 #include "llvm/Object/ObjectFile.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Format.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Support/SourceMgr.h"
38 #include "llvm/Support/TargetRegistry.h"
39 #include "llvm/Support/TargetSelect.h"
46 static cl::OptionCategory
Options("llvm-exegesis options");
47 static cl::OptionCategory
BenchmarkOptions("llvm-exegesis benchmark options");
48 static cl::OptionCategory
AnalysisOptions("llvm-exegesis analysis options");
50 static cl::opt
<int> OpcodeIndex("opcode-index",
51 cl::desc("opcode to measure, by index"),
52 cl::cat(BenchmarkOptions
), cl::init(0));
54 static cl::opt
<std::string
>
55 OpcodeNames("opcode-name",
56 cl::desc("comma-separated list of opcodes to measure, by name"),
57 cl::cat(BenchmarkOptions
), cl::init(""));
59 static cl::opt
<std::string
> SnippetsFile("snippets-file",
60 cl::desc("code snippets to measure"),
61 cl::cat(BenchmarkOptions
),
64 static cl::opt
<std::string
>
65 BenchmarkFile("benchmarks-file",
66 cl::desc("File to read (analysis mode) or write "
67 "(latency/uops/inverse_throughput modes) benchmark "
68 "results. “-” uses stdin/stdout."),
69 cl::cat(Options
), cl::init(""));
71 static cl::opt
<exegesis::InstructionBenchmark::ModeE
> BenchmarkMode(
72 "mode", cl::desc("the mode to run"), cl::cat(Options
),
73 cl::values(clEnumValN(exegesis::InstructionBenchmark::Latency
, "latency",
74 "Instruction Latency"),
75 clEnumValN(exegesis::InstructionBenchmark::InverseThroughput
,
77 "Instruction Inverse Throughput"),
78 clEnumValN(exegesis::InstructionBenchmark::Uops
, "uops",
80 // When not asking for a specific benchmark mode,
81 // we'll analyse the results.
82 clEnumValN(exegesis::InstructionBenchmark::Unknown
, "analysis",
85 static cl::opt
<exegesis::InstructionBenchmark::RepetitionModeE
> RepetitionMode(
86 "repetition-mode", cl::desc("how to repeat the instruction snippet"),
87 cl::cat(BenchmarkOptions
),
88 cl::values(clEnumValN(exegesis::InstructionBenchmark::Duplicate
,
89 "duplicate", "Duplicate the snippet"),
90 clEnumValN(exegesis::InstructionBenchmark::Loop
, "loop",
91 "Loop over the snippet")));
93 static cl::opt
<unsigned>
94 NumRepetitions("num-repetitions",
95 cl::desc("number of time to repeat the asm snippet"),
96 cl::cat(BenchmarkOptions
), cl::init(10000));
98 static cl::opt
<unsigned> MaxConfigsPerOpcode(
99 "max-configs-per-opcode",
101 "allow to snippet generator to generate at most that many configs"),
102 cl::cat(BenchmarkOptions
), cl::init(1));
104 static cl::opt
<bool> IgnoreInvalidSchedClass(
105 "ignore-invalid-sched-class",
106 cl::desc("ignore instructions that do not define a sched class"),
107 cl::cat(BenchmarkOptions
), cl::init(false));
109 static cl::opt
<exegesis::InstructionBenchmarkClustering::ModeE
>
110 AnalysisClusteringAlgorithm(
111 "analysis-clustering", cl::desc("the clustering algorithm to use"),
112 cl::cat(AnalysisOptions
),
113 cl::values(clEnumValN(exegesis::InstructionBenchmarkClustering::Dbscan
,
114 "dbscan", "use DBSCAN/OPTICS algorithm"),
115 clEnumValN(exegesis::InstructionBenchmarkClustering::Naive
,
116 "naive", "one cluster per opcode")),
117 cl::init(exegesis::InstructionBenchmarkClustering::Dbscan
));
119 static cl::opt
<unsigned> AnalysisDbscanNumPoints(
120 "analysis-numpoints",
121 cl::desc("minimum number of points in an analysis cluster (dbscan only)"),
122 cl::cat(AnalysisOptions
), cl::init(3));
124 static cl::opt
<float> AnalysisClusteringEpsilon(
125 "analysis-clustering-epsilon",
126 cl::desc("epsilon for benchmark point clustering"),
127 cl::cat(AnalysisOptions
), cl::init(0.1));
129 static cl::opt
<float> AnalysisInconsistencyEpsilon(
130 "analysis-inconsistency-epsilon",
131 cl::desc("epsilon for detection of when the cluster is different from the "
132 "LLVM schedule profile values"),
133 cl::cat(AnalysisOptions
), cl::init(0.1));
135 static cl::opt
<std::string
>
136 AnalysisClustersOutputFile("analysis-clusters-output-file", cl::desc(""),
137 cl::cat(AnalysisOptions
), cl::init(""));
138 static cl::opt
<std::string
>
139 AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file",
140 cl::desc(""), cl::cat(AnalysisOptions
),
143 static cl::opt
<bool> AnalysisDisplayUnstableOpcodes(
144 "analysis-display-unstable-clusters",
145 cl::desc("if there is more than one benchmark for an opcode, said "
146 "benchmarks may end up not being clustered into the same cluster "
147 "if the measured performance characteristics are different. by "
148 "default all such opcodes are filtered out. this flag will "
149 "instead show only such unstable opcodes"),
150 cl::cat(AnalysisOptions
), cl::init(false));
152 static cl::opt
<std::string
> CpuName(
154 cl::desc("cpu name to use for pfm counters, leave empty to autodetect"),
155 cl::cat(Options
), cl::init(""));
158 DumpObjectToDisk("dump-object-to-disk",
159 cl::desc("dumps the generated benchmark object to disk "
160 "and prints a message to access it"),
161 cl::cat(BenchmarkOptions
), cl::init(true));
163 static ExitOnError ExitOnErr
;
165 // Checks that only one of OpcodeNames, OpcodeIndex or SnippetsFile is provided,
166 // and returns the opcode indices or {} if snippets should be read from
168 static std::vector
<unsigned>
169 getOpcodesOrDie(const llvm::MCInstrInfo
&MCInstrInfo
) {
170 const size_t NumSetFlags
= (OpcodeNames
.empty() ? 0 : 1) +
171 (OpcodeIndex
== 0 ? 0 : 1) +
172 (SnippetsFile
.empty() ? 0 : 1);
173 if (NumSetFlags
!= 1)
174 llvm::report_fatal_error(
175 "please provide one and only one of 'opcode-index', 'opcode-name' or "
177 if (!SnippetsFile
.empty())
180 return {static_cast<unsigned>(OpcodeIndex
)};
181 if (OpcodeIndex
< 0) {
182 std::vector
<unsigned> Result
;
183 for (unsigned I
= 1, E
= MCInstrInfo
.getNumOpcodes(); I
< E
; ++I
)
187 // Resolve opcode name -> opcode.
188 const auto ResolveName
=
189 [&MCInstrInfo
](llvm::StringRef OpcodeName
) -> unsigned {
190 for (unsigned I
= 1, E
= MCInstrInfo
.getNumOpcodes(); I
< E
; ++I
)
191 if (MCInstrInfo
.getName(I
) == OpcodeName
)
195 llvm::SmallVector
<llvm::StringRef
, 2> Pieces
;
196 llvm::StringRef(OpcodeNames
.getValue())
197 .split(Pieces
, ",", /* MaxSplit */ -1, /* KeepEmpty */ false);
198 std::vector
<unsigned> Result
;
199 for (const llvm::StringRef OpcodeName
: Pieces
) {
200 if (unsigned Opcode
= ResolveName(OpcodeName
))
201 Result
.push_back(Opcode
);
203 llvm::report_fatal_error(
204 llvm::Twine("unknown opcode ").concat(OpcodeName
));
209 // Generates code snippets for opcode `Opcode`.
210 static llvm::Expected
<std::vector
<BenchmarkCode
>>
211 generateSnippets(const LLVMState
&State
, unsigned Opcode
,
212 const llvm::BitVector
&ForbiddenRegs
) {
213 const Instruction
&Instr
= State
.getIC().getInstr(Opcode
);
214 const llvm::MCInstrDesc
&InstrDesc
= *Instr
.Description
;
215 // Ignore instructions that we cannot run.
216 if (InstrDesc
.isPseudo())
217 return make_error
<Failure
>("Unsupported opcode: isPseudo");
218 if (InstrDesc
.isBranch() || InstrDesc
.isIndirectBranch())
219 return make_error
<Failure
>("Unsupported opcode: isBranch/isIndirectBranch");
220 if (InstrDesc
.isCall() || InstrDesc
.isReturn())
221 return make_error
<Failure
>("Unsupported opcode: isCall/isReturn");
223 SnippetGenerator::Options Options
;
224 Options
.MaxConfigsPerOpcode
= MaxConfigsPerOpcode
;
225 const std::unique_ptr
<SnippetGenerator
> Generator
=
226 State
.getExegesisTarget().createSnippetGenerator(BenchmarkMode
, State
,
229 llvm::report_fatal_error("cannot create snippet generator");
230 return Generator
->generateConfigurations(Instr
, ForbiddenRegs
);
233 void benchmarkMain() {
235 llvm::report_fatal_error(
236 "benchmarking unavailable, LLVM was built without libpfm.");
239 if (exegesis::pfm::pfmInitialize())
240 llvm::report_fatal_error("cannot initialize libpfm");
242 llvm::InitializeNativeTarget();
243 llvm::InitializeNativeTargetAsmPrinter();
244 llvm::InitializeNativeTargetAsmParser();
245 InitializeNativeExegesisTarget();
247 const LLVMState
State(CpuName
);
248 const auto Opcodes
= getOpcodesOrDie(State
.getInstrInfo());
250 const auto Repetitor
= SnippetRepetitor::Create(RepetitionMode
, State
);
252 std::vector
<BenchmarkCode
> Configurations
;
253 if (!Opcodes
.empty()) {
254 for (const unsigned Opcode
: Opcodes
) {
255 // Ignore instructions without a sched class if
256 // -ignore-invalid-sched-class is passed.
257 if (IgnoreInvalidSchedClass
&&
258 State
.getInstrInfo().get(Opcode
).getSchedClass() == 0) {
259 llvm::errs() << State
.getInstrInfo().getName(Opcode
)
260 << ": ignoring instruction without sched class\n";
263 auto ConfigsForInstr
=
264 generateSnippets(State
, Opcode
, Repetitor
->getReservedRegs());
265 if (!ConfigsForInstr
) {
266 llvm::logAllUnhandledErrors(
267 ConfigsForInstr
.takeError(), llvm::errs(),
268 llvm::Twine(State
.getInstrInfo().getName(Opcode
)).concat(": "));
271 std::move(ConfigsForInstr
->begin(), ConfigsForInstr
->end(),
272 std::back_inserter(Configurations
));
275 Configurations
= ExitOnErr(readSnippets(State
, SnippetsFile
));
278 const std::unique_ptr
<BenchmarkRunner
> Runner
=
279 State
.getExegesisTarget().createBenchmarkRunner(BenchmarkMode
, State
);
281 llvm::report_fatal_error("cannot create benchmark runner");
284 if (NumRepetitions
== 0)
285 llvm::report_fatal_error("--num-repetitions must be greater than zero");
287 // Write to standard output if file is not set.
288 if (BenchmarkFile
.empty())
291 for (const BenchmarkCode
&Conf
: Configurations
) {
292 InstructionBenchmark Result
= Runner
->runConfiguration(
293 Conf
, NumRepetitions
, *Repetitor
, DumpObjectToDisk
);
294 ExitOnErr(Result
.writeYaml(State
, BenchmarkFile
));
296 exegesis::pfm::pfmTerminate();
299 // Prints the results of running analysis pass `Pass` to file `OutputFilename`
300 // if OutputFilename is non-empty.
301 template <typename Pass
>
302 static void maybeRunAnalysis(const Analysis
&Analyzer
, const std::string
&Name
,
303 const std::string
&OutputFilename
) {
304 if (OutputFilename
.empty())
306 if (OutputFilename
!= "-") {
307 llvm::errs() << "Printing " << Name
<< " results to file '"
308 << OutputFilename
<< "'\n";
310 std::error_code ErrorCode
;
311 llvm::raw_fd_ostream
ClustersOS(OutputFilename
, ErrorCode
,
312 llvm::sys::fs::FA_Read
|
313 llvm::sys::fs::FA_Write
);
315 llvm::report_fatal_error("cannot open out file: " + OutputFilename
);
316 if (auto Err
= Analyzer
.run
<Pass
>(ClustersOS
))
317 llvm::report_fatal_error(std::move(Err
));
320 static void analysisMain() {
321 if (BenchmarkFile
.empty())
322 llvm::report_fatal_error("--benchmarks-file must be set.");
324 if (AnalysisClustersOutputFile
.empty() &&
325 AnalysisInconsistenciesOutputFile
.empty()) {
326 llvm::report_fatal_error(
327 "At least one of --analysis-clusters-output-file and "
328 "--analysis-inconsistencies-output-file must be specified.");
331 llvm::InitializeNativeTarget();
332 llvm::InitializeNativeTargetAsmPrinter();
333 llvm::InitializeNativeTargetDisassembler();
335 const LLVMState
State("");
336 const std::vector
<InstructionBenchmark
> Points
=
337 ExitOnErr(InstructionBenchmark::readYamls(State
, BenchmarkFile
));
338 llvm::outs() << "Parsed " << Points
.size() << " benchmark points\n";
339 if (Points
.empty()) {
340 llvm::errs() << "no benchmarks to analyze\n";
343 // FIXME: Check that all points have the same triple/cpu.
344 // FIXME: Merge points from several runs (latency and uops).
347 const auto *TheTarget
=
348 llvm::TargetRegistry::lookupTarget(Points
[0].LLVMTriple
, Error
);
350 llvm::errs() << "unknown target '" << Points
[0].LLVMTriple
<< "'\n";
354 std::unique_ptr
<llvm::MCInstrInfo
> InstrInfo(TheTarget
->createMCInstrInfo());
356 const auto Clustering
= ExitOnErr(InstructionBenchmarkClustering::create(
357 Points
, AnalysisClusteringAlgorithm
, AnalysisDbscanNumPoints
,
358 AnalysisClusteringEpsilon
, InstrInfo
->getNumOpcodes()));
360 const Analysis
Analyzer(*TheTarget
, std::move(InstrInfo
), Clustering
,
361 AnalysisInconsistencyEpsilon
,
362 AnalysisDisplayUnstableOpcodes
);
364 maybeRunAnalysis
<Analysis::PrintClusters
>(Analyzer
, "analysis clusters",
365 AnalysisClustersOutputFile
);
366 maybeRunAnalysis
<Analysis::PrintSchedClassInconsistencies
>(
367 Analyzer
, "sched class consistency analysis",
368 AnalysisInconsistenciesOutputFile
);
371 } // namespace exegesis
374 int main(int Argc
, char **Argv
) {
375 using namespace llvm
;
376 cl::ParseCommandLineOptions(Argc
, Argv
, "");
378 exegesis::ExitOnErr
.setExitCodeMapper([](const llvm::Error
&Err
) {
379 if (Err
.isA
<llvm::StringError
>())
384 if (exegesis::BenchmarkMode
== exegesis::InstructionBenchmark::Unknown
) {
385 exegesis::analysisMain();
387 exegesis::benchmarkMain();