blob: a1848c3f14ff6f460ff4d3c27cf98f7ace9773bf [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>
48 BenchmarkFile("benchmarks-file", llvm::cl::desc(""), llvm::cl::init("-"));
49
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 Courbet37f0ca02018-05-15 12:08:00 +000063static llvm::cl::opt<unsigned> AnalysisNumPoints(
64 "analysis-numpoints",
65 llvm::cl::desc("minimum number of points in an analysis cluster"),
66 llvm::cl::init(3));
67
68static llvm::cl::opt<float>
69 AnalysisEpsilon("analysis-epsilon",
70 llvm::cl::desc("dbscan epsilon for analysis clustering"),
71 llvm::cl::init(0.1));
72
Clement Courbetcf210742018-05-17 13:41:28 +000073static llvm::cl::opt<std::string>
74 AnalysisClustersOutputFile("analysis-clusters-output-file",
75 llvm::cl::desc(""), llvm::cl::init("-"));
76static llvm::cl::opt<std::string>
77 AnalysisInconsistenciesOutputFile("analysis-inconsistencies-output-file",
78 llvm::cl::desc(""), llvm::cl::init("-"));
Clement Courbetcaa163e2018-05-16 09:50:04 +000079
Clement Courbetac74acd2018-04-04 11:37:06 +000080namespace exegesis {
81
Clement Courbet0e69e2d2018-05-17 10:52:18 +000082static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) {
83 if (OpcodeName.empty() && (OpcodeIndex == 0))
84 llvm::report_fatal_error(
85 "please provide one and only one of 'opcode-index' or 'opcode-name'");
86 if (OpcodeIndex > 0)
87 return OpcodeIndex;
88 // Resolve opcode name -> opcode.
89 for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
90 if (MCInstrInfo.getName(I) == OpcodeName)
91 return I;
92 llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName));
93}
94
Clement Courbet53d35d22018-06-05 10:56:19 +000095static BenchmarkResultContext
96getBenchmarkResultContext(const LLVMState &State) {
97 BenchmarkResultContext Ctx;
98
99 const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo();
100 for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I)
101 Ctx.addInstrEntry(I, InstrInfo.getName(I).data());
102
103 const llvm::MCRegisterInfo &RegInfo = State.getRegInfo();
104 for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I)
105 Ctx.addRegEntry(I, RegInfo.getName(I));
106
107 return Ctx;
108}
109
Clement Courbet37f0ca02018-05-15 12:08:00 +0000110void benchmarkMain() {
111 if (exegesis::pfm::pfmInitialize())
112 llvm::report_fatal_error("cannot initialize libpfm");
113
Clement Courbetac74acd2018-04-04 11:37:06 +0000114 llvm::InitializeNativeTarget();
115 llvm::InitializeNativeTargetAsmPrinter();
116
117 // FIXME: Target-specific filter.
118 X86Filter Filter;
119
120 const LLVMState State;
121
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000122 // FIXME: Do not require SchedModel for latency.
Simon Pilgrim656444b2018-04-18 14:46:54 +0000123 if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo())
124 llvm::report_fatal_error("sched model is missing extra processor info!");
125
Clement Courbetac74acd2018-04-04 11:37:06 +0000126 std::unique_ptr<BenchmarkRunner> Runner;
127 switch (BenchmarkMode) {
128 case BenchmarkModeE::Latency:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000129 Runner = llvm::make_unique<LatencyBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000130 break;
131 case BenchmarkModeE::Uops:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000132 Runner = llvm::make_unique<UopsBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000133 break;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000134 case BenchmarkModeE::Analysis:
135 llvm_unreachable("not a benchmark");
Clement Courbetac74acd2018-04-04 11:37:06 +0000136 }
137
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000138 if (NumRepetitions == 0)
139 llvm::report_fatal_error("--num-repetitions must be greater than zero");
140
141 Runner->run(GetOpcodeOrDie(State.getInstrInfo()), Filter, NumRepetitions)
Clement Courbet53d35d22018-06-05 10:56:19 +0000142 .writeYamlOrDie(getBenchmarkResultContext(State), BenchmarkFile);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000143 exegesis::pfm::pfmTerminate();
144}
145
Clement Courbetcf210742018-05-17 13:41:28 +0000146// Prints the results of running analysis pass `Pass` to file `OutputFilename`
147// if OutputFilename is non-empty.
148template <typename Pass>
149static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name,
Clement Courbet53d35d22018-06-05 10:56:19 +0000150 const std::string &OutputFilename) {
Clement Courbetcf210742018-05-17 13:41:28 +0000151 if (OutputFilename.empty())
152 return;
153 if (OutputFilename != "-") {
154 llvm::errs() << "Printing " << Name << " results to file '"
155 << OutputFilename << "'\n";
156 }
157 std::error_code ErrorCode;
158 llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode,
159 llvm::sys::fs::F_RW);
160 if (ErrorCode)
161 llvm::report_fatal_error("cannot open out file: " + OutputFilename);
162 if (auto Err = Analyzer.run<Pass>(ClustersOS))
163 llvm::report_fatal_error(std::move(Err));
164}
165
166static void analysisMain() {
Clement Courbet53d35d22018-06-05 10:56:19 +0000167 llvm::InitializeNativeTarget();
168 llvm::InitializeNativeTargetAsmPrinter();
169
Clement Courbet37f0ca02018-05-15 12:08:00 +0000170 // Read benchmarks.
Clement Courbet53d35d22018-06-05 10:56:19 +0000171 const LLVMState State;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000172 const std::vector<InstructionBenchmark> Points =
Clement Courbet53d35d22018-06-05 10:56:19 +0000173 InstructionBenchmark::readYamlsOrDie(getBenchmarkResultContext(State),
174 BenchmarkFile);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000175 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
176 if (Points.empty()) {
177 llvm::errs() << "no benchmarks to analyze\n";
178 return;
179 }
180 // FIXME: Check that all points have the same triple/cpu.
181 // FIXME: Merge points from several runs (latency and uops).
182
Clement Courbet37f0ca02018-05-15 12:08:00 +0000183 std::string Error;
184 const auto *TheTarget =
185 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
186 if (!TheTarget) {
187 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
188 return;
189 }
Clement Courbet37f0ca02018-05-15 12:08:00 +0000190 const auto Clustering = llvm::cantFail(InstructionBenchmarkClustering::create(
191 Points, AnalysisNumPoints, AnalysisEpsilon));
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000192
193 const Analysis Analyzer(*TheTarget, Clustering);
194
Clement Courbetcf210742018-05-17 13:41:28 +0000195 maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters",
196 AnalysisClustersOutputFile);
197 maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>(
198 Analyzer, "sched class consistency analysis",
199 AnalysisInconsistenciesOutputFile);
Clement Courbetac74acd2018-04-04 11:37:06 +0000200}
201
202} // namespace exegesis
203
204int main(int Argc, char **Argv) {
205 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
206
Clement Courbet37f0ca02018-05-15 12:08:00 +0000207 if (BenchmarkMode == BenchmarkModeE::Analysis) {
208 exegesis::analysisMain();
209 } else {
210 exegesis::benchmarkMain();
Clement Courbetac74acd2018-04-04 11:37:06 +0000211 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000212 return EXIT_SUCCESS;
213}