blob: 7e2caf3198e3b842d76c702ff2361f09c3aaf05c [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 Courbet0e69e2d2018-05-17 10:52:18 +000089static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) {
90 if (OpcodeName.empty() && (OpcodeIndex == 0))
91 llvm::report_fatal_error(
92 "please provide one and only one of 'opcode-index' or 'opcode-name'");
93 if (OpcodeIndex > 0)
94 return OpcodeIndex;
95 // Resolve opcode name -> opcode.
96 for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
97 if (MCInstrInfo.getName(I) == OpcodeName)
98 return I;
99 llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName));
100}
101
Clement Courbet53d35d22018-06-05 10:56:19 +0000102static BenchmarkResultContext
103getBenchmarkResultContext(const LLVMState &State) {
104 BenchmarkResultContext Ctx;
105
106 const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo();
107 for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I)
108 Ctx.addInstrEntry(I, InstrInfo.getName(I).data());
109
110 const llvm::MCRegisterInfo &RegInfo = State.getRegInfo();
111 for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I)
112 Ctx.addRegEntry(I, RegInfo.getName(I));
113
114 return Ctx;
115}
116
Clement Courbet37f0ca02018-05-15 12:08:00 +0000117void benchmarkMain() {
118 if (exegesis::pfm::pfmInitialize())
119 llvm::report_fatal_error("cannot initialize libpfm");
120
Clement Courbetac74acd2018-04-04 11:37:06 +0000121 llvm::InitializeNativeTarget();
122 llvm::InitializeNativeTargetAsmPrinter();
123
124 // FIXME: Target-specific filter.
125 X86Filter Filter;
126
127 const LLVMState State;
Clement Courbete752fd62018-06-18 11:27:47 +0000128 const auto Opcode = GetOpcodeOrDie(State.getInstrInfo());
129
130 // Ignore instructions without a sched class if -ignore-invalid-sched-class is
131 // passed.
132 if (IgnoreInvalidSchedClass &&
133 State.getInstrInfo().get(Opcode).getSchedClass() == 0) {
134 llvm::errs() << "ignoring instruction without sched class\n";
135 return;
136 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000137
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000138 // FIXME: Do not require SchedModel for latency.
Simon Pilgrim656444b2018-04-18 14:46:54 +0000139 if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo())
140 llvm::report_fatal_error("sched model is missing extra processor info!");
141
Clement Courbetac74acd2018-04-04 11:37:06 +0000142 std::unique_ptr<BenchmarkRunner> Runner;
143 switch (BenchmarkMode) {
144 case BenchmarkModeE::Latency:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000145 Runner = llvm::make_unique<LatencyBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000146 break;
147 case BenchmarkModeE::Uops:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000148 Runner = llvm::make_unique<UopsBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000149 break;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000150 case BenchmarkModeE::Analysis:
151 llvm_unreachable("not a benchmark");
Clement Courbetac74acd2018-04-04 11:37:06 +0000152 }
153
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000154 if (NumRepetitions == 0)
155 llvm::report_fatal_error("--num-repetitions must be greater than zero");
156
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000157 // Write to standard output if file is not set.
158 if (BenchmarkFile.empty())
159 BenchmarkFile = "-";
160
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000161 const BenchmarkResultContext Context = getBenchmarkResultContext(State);
Clement Courbete752fd62018-06-18 11:27:47 +0000162 std::vector<InstructionBenchmark> Results =
163 ExitOnErr(Runner->run(Opcode, Filter, NumRepetitions));
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000164 for (InstructionBenchmark &Result : Results)
Guillaume Chatelet015b3e52018-06-11 14:10:10 +0000165 ExitOnErr(Result.writeYaml(Context, BenchmarkFile));
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000166
Clement Courbet37f0ca02018-05-15 12:08:00 +0000167 exegesis::pfm::pfmTerminate();
168}
169
Clement Courbetcf210742018-05-17 13:41:28 +0000170// Prints the results of running analysis pass `Pass` to file `OutputFilename`
171// if OutputFilename is non-empty.
172template <typename Pass>
173static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name,
Clement Courbet53d35d22018-06-05 10:56:19 +0000174 const std::string &OutputFilename) {
Clement Courbetcf210742018-05-17 13:41:28 +0000175 if (OutputFilename.empty())
176 return;
177 if (OutputFilename != "-") {
178 llvm::errs() << "Printing " << Name << " results to file '"
179 << OutputFilename << "'\n";
180 }
181 std::error_code ErrorCode;
182 llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode,
Zachary Turner1f67a3c2018-06-07 19:58:58 +0000183 llvm::sys::fs::FA_Read |
184 llvm::sys::fs::FA_Write);
185 if (ErrorCode)
186 llvm::report_fatal_error("cannot open out file: " + OutputFilename);
187 if (auto Err = Analyzer.run<Pass>(ClustersOS))
188 llvm::report_fatal_error(std::move(Err));
Clement Courbetcf210742018-05-17 13:41:28 +0000189}
190
191static void analysisMain() {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000192 if (BenchmarkFile.empty())
193 llvm::report_fatal_error("--benchmarks-file must be set.");
194
Clement Courbet53d35d22018-06-05 10:56:19 +0000195 llvm::InitializeNativeTarget();
196 llvm::InitializeNativeTargetAsmPrinter();
Clement Courbet4273e1e2018-06-15 07:30:45 +0000197 llvm::InitializeNativeTargetDisassembler();
Clement Courbet37f0ca02018-05-15 12:08:00 +0000198 // Read benchmarks.
Clement Courbet53d35d22018-06-05 10:56:19 +0000199 const LLVMState State;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000200 const std::vector<InstructionBenchmark> Points =
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000201 ExitOnErr(InstructionBenchmark::readYamls(
202 getBenchmarkResultContext(State), BenchmarkFile));
Clement Courbet37f0ca02018-05-15 12:08:00 +0000203 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
204 if (Points.empty()) {
205 llvm::errs() << "no benchmarks to analyze\n";
206 return;
207 }
208 // FIXME: Check that all points have the same triple/cpu.
209 // FIXME: Merge points from several runs (latency and uops).
210
Clement Courbet37f0ca02018-05-15 12:08:00 +0000211 std::string Error;
212 const auto *TheTarget =
213 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
214 if (!TheTarget) {
215 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
216 return;
217 }
Guillaume Chatelet64165922018-06-11 09:18:01 +0000218 const auto Clustering = ExitOnErr(InstructionBenchmarkClustering::create(
Clement Courbet37f0ca02018-05-15 12:08:00 +0000219 Points, AnalysisNumPoints, AnalysisEpsilon));
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000220
221 const Analysis Analyzer(*TheTarget, Clustering);
222
Clement Courbetcf210742018-05-17 13:41:28 +0000223 maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters",
224 AnalysisClustersOutputFile);
225 maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>(
226 Analyzer, "sched class consistency analysis",
227 AnalysisInconsistenciesOutputFile);
Clement Courbetac74acd2018-04-04 11:37:06 +0000228}
229
230} // namespace exegesis
231
232int main(int Argc, char **Argv) {
233 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
234
Guillaume Chatelet64165922018-06-11 09:18:01 +0000235 exegesis::ExitOnErr.setExitCodeMapper([](const llvm::Error &Err) {
236 if (Err.isA<llvm::StringError>())
237 return EXIT_SUCCESS;
238 return EXIT_FAILURE;
239 });
240
Clement Courbet37f0ca02018-05-15 12:08:00 +0000241 if (BenchmarkMode == BenchmarkModeE::Analysis) {
242 exegesis::analysisMain();
243 } else {
244 exegesis::benchmarkMain();
Clement Courbetac74acd2018-04-04 11:37:06 +0000245 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000246 return EXIT_SUCCESS;
247}