blob: 2ac92965a96f0f7c9fcaa3b28f1081a3847d4cc8 [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/Latency.h"
20#include "lib/LlvmState.h"
21#include "lib/PerfHelper.h"
22#include "lib/Uops.h"
23#include "lib/X86.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/Twine.h"
26#include "llvm/MC/MCInstBuilder.h"
27#include "llvm/MC/MCRegisterInfo.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000028#include "llvm/MC/MCSubtargetInfo.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000029#include "llvm/Support/CommandLine.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000030#include "llvm/Support/Format.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000031#include "llvm/Support/Path.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000032#include "llvm/Support/TargetRegistry.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000033#include "llvm/Support/TargetSelect.h"
34#include <algorithm>
35#include <random>
36#include <string>
37#include <unordered_map>
38
39static llvm::cl::opt<unsigned>
40 OpcodeIndex("opcode-index", llvm::cl::desc("opcode to measure, by index"),
41 llvm::cl::init(0));
42
43static llvm::cl::opt<std::string>
44 OpcodeName("opcode-name", llvm::cl::desc("opcode to measure, by name"),
45 llvm::cl::init(""));
46
Clement Courbet37f0ca02018-05-15 12:08:00 +000047static llvm::cl::opt<std::string>
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000048 BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init(""));
Clement Courbet37f0ca02018-05-15 12:08:00 +000049
50enum class BenchmarkModeE { Latency, Uops, Analysis };
51static llvm::cl::opt<BenchmarkModeE> BenchmarkMode(
Clement Courbet5ec03cd2018-05-18 12:33:57 +000052 "mode", llvm::cl::desc("the mode to run"),
Clement Courbet37f0ca02018-05-15 12:08:00 +000053 llvm::cl::values(
54 clEnumValN(BenchmarkModeE::Latency, "latency", "Instruction Latency"),
55 clEnumValN(BenchmarkModeE::Uops, "uops", "Uop Decomposition"),
56 clEnumValN(BenchmarkModeE::Analysis, "analysis", "Analysis")));
Clement Courbetac74acd2018-04-04 11:37:06 +000057
58static llvm::cl::opt<unsigned>
59 NumRepetitions("num-repetitions",
60 llvm::cl::desc("number of time to repeat the asm snippet"),
61 llvm::cl::init(10000));
62
Clement Courbete752fd62018-06-18 11:27:47 +000063static llvm::cl::opt<bool> IgnoreInvalidSchedClass(
64 "ignore-invalid-sched-class",
65 llvm::cl::desc("ignore instructions that do not define a sched class"),
66 llvm::cl::init(false));
67
Clement Courbet37f0ca02018-05-15 12:08:00 +000068static llvm::cl::opt<unsigned> AnalysisNumPoints(
69 "analysis-numpoints",
70 llvm::cl::desc("minimum number of points in an analysis cluster"),
71 llvm::cl::init(3));
72
73static llvm::cl::opt<float>
74 AnalysisEpsilon("analysis-epsilon",
75 llvm::cl::desc("dbscan epsilon for analysis clustering"),
76 llvm::cl::init(0.1));
77
Clement Courbetcf210742018-05-17 13:41:28 +000078static llvm::cl::opt<std::string>
79 AnalysisClustersOutputFile("analysis-clusters-output-file",
80 llvm::cl::desc(""), llvm::cl::init("-"));
81static llvm::cl::opt<std::string>
82 AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file",
83 llvm::cl::desc(""), llvm::cl::init("-"));
Clement Courbetcaa163e2018-05-16 09:50:04 +000084
Clement Courbetac74acd2018-04-04 11:37:06 +000085namespace exegesis {
86
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000087static llvm::ExitOnError ExitOnErr;
88
Clement Courbet44b4c542018-06-19 11:28:59 +000089#ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET
90void LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET();
91#endif
92
Clement Courbet0e69e2d2018-05-17 10:52:18 +000093static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) {
94 if (OpcodeName.empty() && (OpcodeIndex == 0))
95 llvm::report_fatal_error(
96 "please provide one and only one of 'opcode-index' or 'opcode-name'");
97 if (OpcodeIndex > 0)
98 return OpcodeIndex;
99 // Resolve opcode name -> opcode.
100 for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
101 if (MCInstrInfo.getName(I) == OpcodeName)
102 return I;
103 llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName));
104}
105
Clement Courbet53d35d22018-06-05 10:56:19 +0000106static BenchmarkResultContext
107getBenchmarkResultContext(const LLVMState &State) {
108 BenchmarkResultContext Ctx;
109
110 const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo();
111 for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I)
112 Ctx.addInstrEntry(I, InstrInfo.getName(I).data());
113
114 const llvm::MCRegisterInfo &RegInfo = State.getRegInfo();
115 for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I)
116 Ctx.addRegEntry(I, RegInfo.getName(I));
117
118 return Ctx;
119}
120
Clement Courbet37f0ca02018-05-15 12:08:00 +0000121void benchmarkMain() {
122 if (exegesis::pfm::pfmInitialize())
123 llvm::report_fatal_error("cannot initialize libpfm");
124
Clement Courbetac74acd2018-04-04 11:37:06 +0000125 llvm::InitializeNativeTarget();
126 llvm::InitializeNativeTargetAsmPrinter();
Clement Courbet44b4c542018-06-19 11:28:59 +0000127#ifdef LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET
128 LLVM_EXEGESIS_INITIALIZE_NATIVE_TARGET();
129#endif
Clement Courbetac74acd2018-04-04 11:37:06 +0000130
131 // FIXME: Target-specific filter.
132 X86Filter Filter;
133
134 const LLVMState State;
Clement Courbete752fd62018-06-18 11:27:47 +0000135 const auto Opcode = GetOpcodeOrDie(State.getInstrInfo());
136
137 // Ignore instructions without a sched class if -ignore-invalid-sched-class is
138 // passed.
139 if (IgnoreInvalidSchedClass &&
140 State.getInstrInfo().get(Opcode).getSchedClass() == 0) {
141 llvm::errs() << "ignoring instruction without sched class\n";
142 return;
143 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000144
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000145 // FIXME: Do not require SchedModel for latency.
Simon Pilgrim656444b2018-04-18 14:46:54 +0000146 if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo())
147 llvm::report_fatal_error("sched model is missing extra processor info!");
148
Clement Courbetac74acd2018-04-04 11:37:06 +0000149 std::unique_ptr<BenchmarkRunner> Runner;
150 switch (BenchmarkMode) {
151 case BenchmarkModeE::Latency:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000152 Runner = llvm::make_unique<LatencyBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000153 break;
154 case BenchmarkModeE::Uops:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000155 Runner = llvm::make_unique<UopsBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000156 break;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000157 case BenchmarkModeE::Analysis:
158 llvm_unreachable("not a benchmark");
Clement Courbetac74acd2018-04-04 11:37:06 +0000159 }
160
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000161 if (NumRepetitions == 0)
162 llvm::report_fatal_error("--num-repetitions must be greater than zero");
163
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000164 // Write to standard output if file is not set.
165 if (BenchmarkFile.empty())
166 BenchmarkFile = "-";
167
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000168 const BenchmarkResultContext Context = getBenchmarkResultContext(State);
Clement Courbete752fd62018-06-18 11:27:47 +0000169 std::vector<InstructionBenchmark> Results =
170 ExitOnErr(Runner->run(Opcode, Filter, NumRepetitions));
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000171 for (InstructionBenchmark &Result : Results)
Guillaume Chatelet015b3e52018-06-11 14:10:10 +0000172 ExitOnErr(Result.writeYaml(Context, BenchmarkFile));
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000173
Clement Courbet37f0ca02018-05-15 12:08:00 +0000174 exegesis::pfm::pfmTerminate();
175}
176
Clement Courbetcf210742018-05-17 13:41:28 +0000177// Prints the results of running analysis pass `Pass` to file `OutputFilename`
178// if OutputFilename is non-empty.
179template <typename Pass>
180static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name,
Clement Courbet53d35d22018-06-05 10:56:19 +0000181 const std::string &OutputFilename) {
Clement Courbetcf210742018-05-17 13:41:28 +0000182 if (OutputFilename.empty())
183 return;
184 if (OutputFilename != "-") {
185 llvm::errs() << "Printing " << Name << " results to file '"
186 << OutputFilename << "'\n";
187 }
188 std::error_code ErrorCode;
189 llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode,
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000190 llvm::sys::fs::FA_Read |
191 llvm::sys::fs::FA_Write);
192 if (ErrorCode)
193 llvm::report_fatal_error("cannot open out file: " + OutputFilename);
194 if (auto Err = Analyzer.run<Pass>(ClustersOS))
195 llvm::report_fatal_error(std::move(Err));
Clement Courbetcf210742018-05-17 13:41:28 +0000196}
197
198static void analysisMain() {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000199 if (BenchmarkFile.empty())
200 llvm::report_fatal_error("--benchmarks-file must be set.");
201
Clement Courbet53d35d22018-06-05 10:56:19 +0000202 llvm::InitializeNativeTarget();
203 llvm::InitializeNativeTargetAsmPrinter();
Clement Courbet4273e1e2018-06-15 07:30:45 +0000204 llvm::InitializeNativeTargetDisassembler();
Clement Courbet37f0ca02018-05-15 12:08:00 +0000205 // Read benchmarks.
Clement Courbet53d35d22018-06-05 10:56:19 +0000206 const LLVMState State;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000207 const std::vector<InstructionBenchmark> Points =
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000208 ExitOnErr(InstructionBenchmark::readYamls(
209 getBenchmarkResultContext(State), BenchmarkFile));
Clement Courbet37f0ca02018-05-15 12:08:00 +0000210 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
211 if (Points.empty()) {
212 llvm::errs() << "no benchmarks to analyze\n";
213 return;
214 }
215 // FIXME: Check that all points have the same triple/cpu.
216 // FIXME: Merge points from several runs (latency and uops).
217
Clement Courbet37f0ca02018-05-15 12:08:00 +0000218 std::string Error;
219 const auto *TheTarget =
220 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
221 if (!TheTarget) {
222 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
223 return;
224 }
Guillaume Chatelet64165922018-06-11 09:18:01 +0000225 const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create(
Clement Courbet37f0ca02018-05-15 12:08:00 +0000226 Points, AnalysisNumPoints, AnalysisEpsilon));
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000227
228 const Analysis Analyzer(*TheTarget, Clustering);
229
Clement Courbetcf210742018-05-17 13:41:28 +0000230 maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters",
231 AnalysisClustersOutputFile);
232 maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>(
233 Analyzer, "sched class consistency analysis",
234 AnalysisInconsistenciesOutputFile);
Clement Courbetac74acd2018-04-04 11:37:06 +0000235}
236
237} // namespace exegesis
238
239int main(int Argc, char **Argv) {
240 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
241
Guillaume Chatelet64165922018-06-11 09:18:01 +0000242 exegesis::ExitOnErr.setExitCodeMapper([](const llvm::Error &Err) {
243 if (Err.isA<llvm::StringError>())
244 return EXIT_SUCCESS;
245 return EXIT_FAILURE;
246 });
247
Clement Courbet37f0ca02018-05-15 12:08:00 +0000248 if (BenchmarkMode == BenchmarkModeE::Analysis) {
249 exegesis::analysisMain();
250 } else {
251 exegesis::benchmarkMain();
Clement Courbetac74acd2018-04-04 11:37:06 +0000252 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000253 return EXIT_SUCCESS;
254}