blob: 47f2dd9bebd5575ed3fe102db248c7ba2b8686d0 [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 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
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +000082static llvm::ExitOnError ExitOnErr;
83
Clement Courbet0e69e2d2018-05-17 10:52:18 +000084static unsigned GetOpcodeOrDie(const llvm::MCInstrInfo &MCInstrInfo) {
85 if (OpcodeName.empty() && (OpcodeIndex == 0))
86 llvm::report_fatal_error(
87 "please provide one and only one of 'opcode-index' or 'opcode-name'");
88 if (OpcodeIndex > 0)
89 return OpcodeIndex;
90 // Resolve opcode name -> opcode.
91 for (unsigned I = 0, E = MCInstrInfo.getNumOpcodes(); I < E; ++I)
92 if (MCInstrInfo.getName(I) == OpcodeName)
93 return I;
94 llvm::report_fatal_error(llvm::Twine("unknown opcode ").concat(OpcodeName));
95}
96
Clement Courbet53d35d22018-06-05 10:56:19 +000097static BenchmarkResultContext
98getBenchmarkResultContext(const LLVMState &State) {
99 BenchmarkResultContext Ctx;
100
101 const llvm::MCInstrInfo &InstrInfo = State.getInstrInfo();
102 for (unsigned E = InstrInfo.getNumOpcodes(), I = 0; I < E; ++I)
103 Ctx.addInstrEntry(I, InstrInfo.getName(I).data());
104
105 const llvm::MCRegisterInfo &RegInfo = State.getRegInfo();
106 for (unsigned E = RegInfo.getNumRegs(), I = 0; I < E; ++I)
107 Ctx.addRegEntry(I, RegInfo.getName(I));
108
109 return Ctx;
110}
111
Clement Courbet37f0ca02018-05-15 12:08:00 +0000112void benchmarkMain() {
113 if (exegesis::pfm::pfmInitialize())
114 llvm::report_fatal_error("cannot initialize libpfm");
115
Clement Courbetac74acd2018-04-04 11:37:06 +0000116 llvm::InitializeNativeTarget();
117 llvm::InitializeNativeTargetAsmPrinter();
118
119 // FIXME: Target-specific filter.
120 X86Filter Filter;
121
122 const LLVMState State;
123
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000124 // FIXME: Do not require SchedModel for latency.
Simon Pilgrim656444b2018-04-18 14:46:54 +0000125 if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo())
126 llvm::report_fatal_error("sched model is missing extra processor info!");
127
Clement Courbetac74acd2018-04-04 11:37:06 +0000128 std::unique_ptr<BenchmarkRunner> Runner;
129 switch (BenchmarkMode) {
130 case BenchmarkModeE::Latency:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000131 Runner = llvm::make_unique<LatencyBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000132 break;
133 case BenchmarkModeE::Uops:
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000134 Runner = llvm::make_unique<UopsBenchmarkRunner>(State);
Clement Courbetac74acd2018-04-04 11:37:06 +0000135 break;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000136 case BenchmarkModeE::Analysis:
137 llvm_unreachable("not a benchmark");
Clement Courbetac74acd2018-04-04 11:37:06 +0000138 }
139
Clement Courbet0e69e2d2018-05-17 10:52:18 +0000140 if (NumRepetitions == 0)
141 llvm::report_fatal_error("--num-repetitions must be greater than zero");
142
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000143 // Write to standard output if file is not set.
144 if (BenchmarkFile.empty())
145 BenchmarkFile = "-";
146
Guillaume Chateletb4f15822018-06-07 14:00:29 +0000147 const BenchmarkResultContext Context = getBenchmarkResultContext(State);
148 std::vector<InstructionBenchmark> Results = ExitOnErr(Runner->run(
149 GetOpcodeOrDie(State.getInstrInfo()), Filter, NumRepetitions));
150 for (InstructionBenchmark &Result : Results)
151 Result.writeYaml(Context, BenchmarkFile);
152
Clement Courbet37f0ca02018-05-15 12:08:00 +0000153 exegesis::pfm::pfmTerminate();
154}
155
Clement Courbetcf210742018-05-17 13:41:28 +0000156// Prints the results of running analysis pass `Pass` to file `OutputFilename`
157// if OutputFilename is non-empty.
158template <typename Pass>
159static void maybeRunAnalysis(const Analysis &Analyzer, const std::string &Name,
Clement Courbet53d35d22018-06-05 10:56:19 +0000160 const std::string &OutputFilename) {
Clement Courbetcf210742018-05-17 13:41:28 +0000161 if (OutputFilename.empty())
162 return;
163 if (OutputFilename != "-") {
164 llvm::errs() << "Printing " << Name << " results to file '"
165 << OutputFilename << "'\n";
166 }
167 std::error_code ErrorCode;
168 llvm::raw_fd_ostream ClustersOS(OutputFilename, ErrorCode,
169 llvm::sys::fs::F_RW);
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000170 ExitOnErr(llvm::errorCodeToError(ErrorCode));
171 ExitOnErr(Analyzer.run<Pass>(ClustersOS));
Clement Courbetcf210742018-05-17 13:41:28 +0000172}
173
174static void analysisMain() {
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000175 if (BenchmarkFile.empty())
176 llvm::report_fatal_error("--benchmarks-file must be set.");
177
Clement Courbet53d35d22018-06-05 10:56:19 +0000178 llvm::InitializeNativeTarget();
179 llvm::InitializeNativeTargetAsmPrinter();
Clement Courbet37f0ca02018-05-15 12:08:00 +0000180 // Read benchmarks.
Clement Courbet53d35d22018-06-05 10:56:19 +0000181 const LLVMState State;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000182 const std::vector<InstructionBenchmark> Points =
Guillaume Chatelet8c91d4c2018-06-07 07:51:16 +0000183 ExitOnErr(InstructionBenchmark::readYamls(
184 getBenchmarkResultContext(State), BenchmarkFile));
Clement Courbet37f0ca02018-05-15 12:08:00 +0000185 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
186 if (Points.empty()) {
187 llvm::errs() << "no benchmarks to analyze\n";
188 return;
189 }
190 // FIXME: Check that all points have the same triple/cpu.
191 // FIXME: Merge points from several runs (latency and uops).
192
Clement Courbet37f0ca02018-05-15 12:08:00 +0000193 std::string Error;
194 const auto *TheTarget =
195 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
196 if (!TheTarget) {
197 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
198 return;
199 }
Clement Courbet37f0ca02018-05-15 12:08:00 +0000200 const auto Clustering = llvm::cantFail(InstructionBenchmarkClustering::create(
201 Points, AnalysisNumPoints, AnalysisEpsilon));
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000202
203 const Analysis Analyzer(*TheTarget, Clustering);
204
Clement Courbetcf210742018-05-17 13:41:28 +0000205 maybeRunAnalysis<Analysis::PrintClusters>(Analyzer, "analysis clusters",
206 AnalysisClustersOutputFile);
207 maybeRunAnalysis<Analysis::PrintSchedClassInconsistencies>(
208 Analyzer, "sched class consistency analysis",
209 AnalysisInconsistenciesOutputFile);
Clement Courbetac74acd2018-04-04 11:37:06 +0000210}
211
212} // namespace exegesis
213
214int main(int Argc, char **Argv) {
215 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
216
Clement Courbet37f0ca02018-05-15 12:08:00 +0000217 if (BenchmarkMode == BenchmarkModeE::Analysis) {
218 exegesis::analysisMain();
219 } else {
220 exegesis::benchmarkMain();
Clement Courbetac74acd2018-04-04 11:37:06 +0000221 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000222 return EXIT_SUCCESS;
223}