blob: 6b626b0eaa341cd3d8526364f571937d3eb07708 [file] [log] [blame]
Clement Courbetac74acd2018-04-04 11:37:06 +00001//===-- llvm-exegesis.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// Measures execution properties (latencies/uops) of an instruction.
12///
13//===----------------------------------------------------------------------===//
14
Clement Courbet37f0ca02018-05-15 12:08:00 +000015#include "lib/Analysis.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000016#include "lib/BenchmarkResult.h"
17#include "lib/BenchmarkRunner.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000018#include "lib/Clustering.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000019#include "lib/LlvmState.h"
20#include "lib/PerfHelper.h"
Clement Courbet4860b982018-06-26 08:49:30 +000021#include "lib/Target.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000022#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/Twine.h"
24#include "llvm/MC/MCInstBuilder.h"
25#include "llvm/MC/MCRegisterInfo.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000026#include "llvm/MC/MCSubtargetInfo.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000027#include "llvm/Support/CommandLine.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000028#include "llvm/Support/Format.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000029#include "llvm/Support/Path.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000030#include "llvm/Support/TargetRegistry.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000031#include "llvm/Support/TargetSelect.h"
32#include <algorithm>
33#include <random>
34#include <string>
35#include <unordered_map>
36
37static llvm::cl::opt<unsigned>
38 OpcodeIndex("opcode-index", llvm::cl::desc("opcode to measure, by index"),
39 llvm::cl::init(0));
40
41static llvm::cl::opt<std::string>
42 OpcodeName("opcode-name", llvm::cl::desc("opcode to measure, by name"),
43 llvm::cl::init(""));
44
Clement Courbet37f0ca02018-05-15 12:08:00 +000045static llvm::cl::opt<std::string>
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000046 BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init(""));
Clement Courbet37f0ca02018-05-15 12:08:00 +000047
Clement Courbet4860b982018-06-26 08:49:30 +000048static llvm::cl::opt<exegesis::InstructionBenchmark::ModeE> BenchmarkMode(
Clement Courbet5ec03cd2018-05-18 12:33:57 +000049 "mode", llvm::cl::desc("the mode to run"),
Clement Courbet4860b982018-06-26 08:49:30 +000050 llvm::cl::values(clEnumValN(exegesis::InstructionBenchmark::Latency,
51 "latency", "Instruction Latency"),
52 clEnumValN(exegesis::InstructionBenchmark::Uops, "uops",
53 "Uop Decomposition"),
54 // When not asking for a specific benchmark mode, we'll
55 // analyse the results.
56 clEnumValN(exegesis::InstructionBenchmark::Unknown,
57 "analysis", "Analysis")));
Clement Courbetac74acd2018-04-04 11:37:06 +000058
59static llvm::cl::opt<unsigned>
60 NumRepetitions("num-repetitions",
61 llvm::cl::desc("number of time to repeat the asm snippet"),
62 llvm::cl::init(10000));
63
Clement Courbete752fd62018-06-18 11:27:47 +000064static llvm::cl::opt<bool> IgnoreInvalidSchedClass(
65 "ignore-invalid-sched-class",
66 llvm::cl::desc("ignore instructions that do not define a sched class"),
67 llvm::cl::init(false));
68
Clement Courbet37f0ca02018-05-15 12:08:00 +000069static llvm::cl::opt<unsigned> AnalysisNumPoints(
70 "analysis-numpoints",
71 llvm::cl::desc("minimum number of points in an analysis cluster"),
72 llvm::cl::init(3));
73
74static llvm::cl::opt<float>
75 AnalysisEpsilon("analysis-epsilon",
76 llvm::cl::desc("dbscan epsilon for analysis clustering"),
77 llvm::cl::init(0.1));
78
Clement Courbetcf210742018-05-17 13:41:28 +000079static llvm::cl::opt<std::string>
80 AnalysisClustersOutputFile("analysis-clusters-output-file",
81 llvm::cl::desc(""), llvm::cl::init("-"));
82static llvm::cl::opt<std::string>
83 AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file",
84 llvm::cl::desc(""), llvm::cl::init("-"));
Clement Courbetcaa163e2018-05-16 09:50:04 +000085
Clement Courbetac74acd2018-04-04 11:37:06 +000086namespace exegesis {
87
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000088static llvm::ExitOnError ExitOnErr;
89
Clement Courbet44b4c542018-06-19 11:28:59 +000090#ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET
91void LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET();
92#endif
93
Clement Courbet0e69e2d2018-05-17 10:52:18 +000094static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) {
95 if (OpcodeName.empty() && (OpcodeIndex == 0))
96 llvm::report_fatal_error(
97 "please provide one and only one of 'opcode-index' or 'opcode-name'");
98 if (OpcodeIndex > 0)
99 return OpcodeIndex;
100 // Resolve opcode name -> opcode.
101 for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
102 if (MCInstrInfo.getName(I) == OpcodeName)
103 return I;
104 llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName));
105}
106
Clement Courbet53d35d22018-06-05 10:56:19 +0000107static BenchmarkResultContext
108getBenchmarkResultContext(const LLVMState &State) {
109 BenchmarkResultContext Ctx;
110
111 const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo();
112 for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I)
113 Ctx.addInstrEntry(I, InstrInfo.getName(I).data());
114
115 const llvm::MCRegisterInfo &RegInfo = State.getRegInfo();
116 for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I)
117 Ctx.addRegEntry(I, RegInfo.getName(I));
118
119 return Ctx;
120}
121
Clement Courbet37f0ca02018-05-15 12:08:00 +0000122void benchmarkMain() {
123 if (exegesis::pfm::pfmInitialize())
124 llvm::report_fatal_error("cannot initialize libpfm");
125
Clement Courbetac74acd2018-04-04 11:37:06 +0000126 llvm::InitializeNativeTarget();
127 llvm::InitializeNativeTargetAsmPrinter();
Clement Courbet44b4c542018-06-19 11:28:59 +0000128#ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET
129 LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET();
130#endif
Clement Courbetac74acd2018-04-04 11:37:06 +0000131
Clement Courbetac74acd2018-04-04 11:37:06 +0000132 const LLVMState State;
Clement Courbete752fd62018-06-18 11:27:47 +0000133 const auto Opcode = GetOpcodeOrDie(State.getInstrInfo());
134
135 // Ignore instructions without a sched class if -ignore-invalid-sched-class is
136 // passed.
137 if (IgnoreInvalidSchedClass &&
138 State.getInstrInfo().get(Opcode).getSchedClass() == 0) {
139 llvm::errs() << "ignoring instruction without sched class\n";
140 return;
141 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000142
Clement Courbet4860b982018-06-26 08:49:30 +0000143 const std::unique_ptr<BenchmarkRunner> Runner =
144 State.getExegesisTarget().createBenchmarkRunner(BenchmarkMode, State);
145 if (!Runner) {
146 llvm::report_fatal_error("cannot create benchmark runner");
Clement Courbetac74acd2018-04-04 11:37:06 +0000147 }
148
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000149 if (NumRepetitions == 0)
150 llvm::report_fatal_error("--num-repetitions must be greater than zero");
151
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000152 // Write to standard output if file is not set.
153 if (BenchmarkFile.empty())
154 BenchmarkFile = "-";
155
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000156 const BenchmarkResultContext Context = getBenchmarkResultContext(State);
Clement Courbete752fd62018-06-18 11:27:47 +0000157 std::vector<InstructionBenchmark> Results =
Clement Courbet4860b982018-06-26 08:49:30 +0000158 ExitOnErr(Runner->run(Opcode, NumRepetitions));
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000159 for (InstructionBenchmark &Result : Results)
Guillaume Chatelet015b3e52018-06-11 14:10:10 +0000160 ExitOnErr(Result.writeYaml(Context, BenchmarkFile));
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000161
Clement Courbet37f0ca02018-05-15 12:08:00 +0000162 exegesis::pfm::pfmTerminate();
163}
164
Clement Courbetcf210742018-05-17 13:41:28 +0000165// Prints the results of running analysis pass `Pass` to file `OutputFilename`
166// if OutputFilename is non-empty.
167template <typename Pass>
168static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name,
Clement Courbet53d35d22018-06-05 10:56:19 +0000169 const std::string &OutputFilename) {
Clement Courbetcf210742018-05-17 13:41:28 +0000170 if (OutputFilename.empty())
171 return;
172 if (OutputFilename != "-") {
173 llvm::errs() << "Printing " << Name << " results to file '"
174 << OutputFilename << "'\n";
175 }
176 std::error_code ErrorCode;
177 llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode,
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000178 llvm::sys::fs::FA_Read |
179 llvm::sys::fs::FA_Write);
180 if (ErrorCode)
181 llvm::report_fatal_error("cannot open out file: " + OutputFilename);
182 if (auto Err = Analyzer.run<Pass>(ClustersOS))
183 llvm::report_fatal_error(std::move(Err));
Clement Courbetcf210742018-05-17 13:41:28 +0000184}
185
186static void analysisMain() {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000187 if (BenchmarkFile.empty())
188 llvm::report_fatal_error("--benchmarks-file must be set.");
189
Clement Courbet53d35d22018-06-05 10:56:19 +0000190 llvm::InitializeNativeTarget();
191 llvm::InitializeNativeTargetAsmPrinter();
Clement Courbet4273e1e2018-06-15 07:30:45 +0000192 llvm::InitializeNativeTargetDisassembler();
Clement Courbet37f0ca02018-05-15 12:08:00 +0000193 // Read benchmarks.
Clement Courbet53d35d22018-06-05 10:56:19 +0000194 const LLVMState State;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000195 const std::vector<InstructionBenchmark> Points =
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000196 ExitOnErr(InstructionBenchmark::readYamls(
197 getBenchmarkResultContext(State), BenchmarkFile));
Clement Courbet37f0ca02018-05-15 12:08:00 +0000198 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
199 if (Points.empty()) {
200 llvm::errs() << "no benchmarks to analyze\n";
201 return;
202 }
203 // FIXME: Check that all points have the same triple/cpu.
204 // FIXME: Merge points from several runs (latency and uops).
205
Clement Courbet37f0ca02018-05-15 12:08:00 +0000206 std::string Error;
207 const auto *TheTarget =
208 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
209 if (!TheTarget) {
210 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
211 return;
212 }
Guillaume Chatelet64165922018-06-11 09:18:01 +0000213 const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create(
Clement Courbet37f0ca02018-05-15 12:08:00 +0000214 Points, AnalysisNumPoints, AnalysisEpsilon));
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000215
216 const Analysis Analyzer(*TheTarget, Clustering);
217
Clement Courbetcf210742018-05-17 13:41:28 +0000218 maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters",
219 AnalysisClustersOutputFile);
220 maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>(
221 Analyzer, "sched class consistency analysis",
222 AnalysisInconsistenciesOutputFile);
Clement Courbetac74acd2018-04-04 11:37:06 +0000223}
224
225} // namespace exegesis
226
227int main(int Argc, char **Argv) {
228 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
229
Guillaume Chatelet64165922018-06-11 09:18:01 +0000230 exegesis::ExitOnErr.setExitCodeMapper([](const llvm::Error &Err) {
231 if (Err.isA<llvm::StringError>())
232 return EXIT_SUCCESS;
233 return EXIT_FAILURE;
234 });
235
Clement Courbet4860b982018-06-26 08:49:30 +0000236 if (BenchmarkMode == exegesis::InstructionBenchmark::Unknown) {
Clement Courbet37f0ca02018-05-15 12:08:00 +0000237 exegesis::analysisMain();
238 } else {
239 exegesis::benchmarkMain();
Clement Courbetac74acd2018-04-04 11:37:06 +0000240 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000241 return EXIT_SUCCESS;
242}