blob: a872c75909310bf13ba9d11f2e35f7335a679d3c [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(
52 "benchmark-mode", llvm::cl::desc("the benchmark mode to run"),
53 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 Courbetcaa163e2018-05-16 09:50:04 +000073static llvm::cl::opt<std::string> AnalysisClustersFile("analysis-clusters-file",
74 llvm::cl::desc(""),
75 llvm::cl::init("-"));
76
Clement Courbetac74acd2018-04-04 11:37:06 +000077namespace exegesis {
78
Clement Courbet37f0ca02018-05-15 12:08:00 +000079void benchmarkMain() {
80 if (exegesis::pfm::pfmInitialize())
81 llvm::report_fatal_error("cannot initialize libpfm");
82
Clement Courbet295a5542018-05-17 08:12:29 +000083 if (OpcodeName.empty() == (OpcodeIndex == 0))
84 llvm::report_fatal_error(
85 "please provide one and only one of 'opcode-index' or 'opcode-name'");
86
Clement Courbetac74acd2018-04-04 11:37:06 +000087 llvm::InitializeNativeTarget();
88 llvm::InitializeNativeTargetAsmPrinter();
89
90 // FIXME: Target-specific filter.
91 X86Filter Filter;
92
93 const LLVMState State;
94
Simon Pilgrim656444b2018-04-18 14:46:54 +000095 if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo())
96 llvm::report_fatal_error("sched model is missing extra processor info!");
97
Clement Courbet295a5542018-05-17 08:12:29 +000098 unsigned Opcode = OpcodeIndex;
99 if (Opcode == 0) {
100 // Resolve opcode name -> opcode.
101 for (unsigned I = 0, E = State.getInstrInfo().getNumOpcodes(); I < E; ++I) {
102 if (State.getInstrInfo().getName(I) == OpcodeName) {
103 Opcode = I;
104 break;
105 }
106 }
107 if (Opcode == 0) {
108 llvm::report_fatal_error(
109 llvm::Twine("unknown opcode ").concat(OpcodeName));
110 }
111 }
112
Clement Courbetac74acd2018-04-04 11:37:06 +0000113 std::unique_ptr<BenchmarkRunner> Runner;
114 switch (BenchmarkMode) {
115 case BenchmarkModeE::Latency:
Clement Courbet295a5542018-05-17 08:12:29 +0000116 Runner = llvm::make_unique<LatencyBenchmarkRunner>();
Clement Courbetac74acd2018-04-04 11:37:06 +0000117 break;
118 case BenchmarkModeE::Uops:
Clement Courbet295a5542018-05-17 08:12:29 +0000119 Runner = llvm::make_unique<UopsBenchmarkRunner>();
Clement Courbetac74acd2018-04-04 11:37:06 +0000120 break;
Clement Courbet37f0ca02018-05-15 12:08:00 +0000121 case BenchmarkModeE::Analysis:
122 llvm_unreachable("not a benchmark");
Clement Courbetac74acd2018-04-04 11:37:06 +0000123 }
124
Clement Courbet295a5542018-05-17 08:12:29 +0000125 Runner->run(State, Opcode, NumRepetitions > 0 ? NumRepetitions : 1, Filter)
Clement Courbet37f0ca02018-05-15 12:08:00 +0000126 .writeYamlOrDie(BenchmarkFile);
127 exegesis::pfm::pfmTerminate();
128}
129
130void analysisMain() {
131 // Read benchmarks.
132 const std::vector<InstructionBenchmark> Points =
133 InstructionBenchmark::readYamlsOrDie(BenchmarkFile);
134 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
135 if (Points.empty()) {
136 llvm::errs() << "no benchmarks to analyze\n";
137 return;
138 }
139 // FIXME: Check that all points have the same triple/cpu.
140 // FIXME: Merge points from several runs (latency and uops).
141
Clement Courbet7b19ffc2018-05-15 12:27:36 +0000142 llvm::InitializeNativeTarget();
143 llvm::InitializeNativeTargetAsmPrinter();
144
Clement Courbet37f0ca02018-05-15 12:08:00 +0000145 std::string Error;
146 const auto *TheTarget =
147 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
148 if (!TheTarget) {
149 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
150 return;
151 }
Clement Courbet37f0ca02018-05-15 12:08:00 +0000152 const auto Clustering = llvm::cantFail(InstructionBenchmarkClustering::create(
153 Points, AnalysisNumPoints, AnalysisEpsilon));
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000154
155 const Analysis Analyzer(*TheTarget, Clustering);
156
Clement Courbetcaa163e2018-05-16 09:50:04 +0000157 std::error_code ErrorCode;
158 llvm::raw_fd_ostream ClustersOS(AnalysisClustersFile, ErrorCode,
159 llvm::sys::fs::F_RW);
160 if (ErrorCode)
161 llvm::report_fatal_error("cannot open out file: " + AnalysisClustersFile);
162
163 if (auto Err = Analyzer.printClusters(ClustersOS))
Clement Courbet37f0ca02018-05-15 12:08:00 +0000164 llvm::report_fatal_error(std::move(Err));
Clement Courbetac74acd2018-04-04 11:37:06 +0000165}
166
167} // namespace exegesis
168
169int main(int Argc, char **Argv) {
170 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
171
Clement Courbet37f0ca02018-05-15 12:08:00 +0000172 if (BenchmarkMode == BenchmarkModeE::Analysis) {
173 exegesis::analysisMain();
174 } else {
175 exegesis::benchmarkMain();
Clement Courbetac74acd2018-04-04 11:37:06 +0000176 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000177 return EXIT_SUCCESS;
178}