blob: 0c8a4173c96a36eb6b9c352e7ff1195581dafd55 [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 Courbet3d479fe2018-05-14 11:30:56 +000015#include "lib/Analysis.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000016#include "lib/BenchmarkResult.h"
17#include "lib/BenchmarkRunner.h"
Clement Courbet3d479fe2018-05-14 11:30:56 +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 Courbet3d479fe2018-05-14 11:30:56 +000028#include "llvm/MC/MCSubtargetInfo.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000029#include "llvm/Support/CommandLine.h"
Clement Courbet3d479fe2018-05-14 11:30:56 +000030#include "llvm/Support/Format.h"
Clement Courbetac74acd2018-04-04 11:37:06 +000031#include "llvm/Support/Path.h"
Clement Courbet3d479fe2018-05-14 11:30:56 +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 Courbet3d479fe2018-05-14 11:30:56 +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 Courbet3d479fe2018-05-14 11:30:56 +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 Courbetac74acd2018-04-04 11:37:06 +000073namespace exegesis {
74
Clement Courbet3d479fe2018-05-14 11:30:56 +000075void benchmarkMain() {
76 if (exegesis::pfm::pfmInitialize())
77 llvm::report_fatal_error("cannot initialize libpfm");
78
79 if (OpcodeName.empty() == (OpcodeIndex == 0))
Clement Courbetac74acd2018-04-04 11:37:06 +000080 llvm::report_fatal_error(
Simon Pilgrim656444b2018-04-18 14:46:54 +000081 "please provide one and only one of 'opcode-index' or 'opcode-name'");
Clement Courbetac74acd2018-04-04 11:37:06 +000082
83 llvm::InitializeNativeTarget();
84 llvm::InitializeNativeTargetAsmPrinter();
85
86 // FIXME: Target-specific filter.
87 X86Filter Filter;
88
89 const LLVMState State;
90
Simon Pilgrim656444b2018-04-18 14:46:54 +000091 if (!State.getSubtargetInfo().getSchedModel().hasExtraProcessorInfo())
92 llvm::report_fatal_error("sched model is missing extra processor info!");
93
Clement Courbetac74acd2018-04-04 11:37:06 +000094 unsigned Opcode = OpcodeIndex;
95 if (Opcode == 0) {
96 // Resolve opcode name -> opcode.
97 for (unsigned I = 0, E = State.getInstrInfo().getNumOpcodes(); I < E; ++I) {
98 if (State.getInstrInfo().getName(I) == OpcodeName) {
99 Opcode = I;
100 break;
101 }
102 }
103 if (Opcode == 0) {
104 llvm::report_fatal_error(
105 llvm::Twine("unknown opcode ").concat(OpcodeName));
106 }
107 }
108
109 std::unique_ptr<BenchmarkRunner> Runner;
110 switch (BenchmarkMode) {
111 case BenchmarkModeE::Latency:
112 Runner = llvm::make_unique<LatencyBenchmarkRunner>();
113 break;
114 case BenchmarkModeE::Uops:
115 Runner = llvm::make_unique<UopsBenchmarkRunner>();
116 break;
Clement Courbet3d479fe2018-05-14 11:30:56 +0000117 case BenchmarkModeE::Analysis:
118 llvm_unreachable("not a benchmark");
Clement Courbetac74acd2018-04-04 11:37:06 +0000119 }
120
121 Runner->run(State, Opcode, NumRepetitions > 0 ? NumRepetitions : 1, Filter)
Clement Courbet3d479fe2018-05-14 11:30:56 +0000122 .writeYamlOrDie(BenchmarkFile);
123 exegesis::pfm::pfmTerminate();
124}
125
126void analysisMain() {
127 // Read benchmarks.
128 const std::vector<InstructionBenchmark> Points =
129 InstructionBenchmark::readYamlsOrDie(BenchmarkFile);
130 llvm::outs() << "Parsed " << Points.size() << " benchmark points\n";
131 if (Points.empty()) {
132 llvm::errs() << "no benchmarks to analyze\n";
133 return;
134 }
135 // TODO: Merge points from several runs (latency and uops).
136
137 // FIXME: Check that all points have the same triple/cpu.
138 llvm::InitializeAllTargets();
139 std::string Error;
140 const auto *TheTarget =
141 llvm::TargetRegistry::lookupTarget(Points[0].LLVMTriple, Error);
142 if (!TheTarget) {
143 llvm::errs() << "unknown target '" << Points[0].LLVMTriple << "'\n";
144 return;
145 }
146 std::unique_ptr<llvm::MCSubtargetInfo> STI(TheTarget->createMCSubtargetInfo(
147 Points[0].LLVMTriple, Points[0].CpuName, ""));
148
149 const auto Clustering = llvm::cantFail(InstructionBenchmarkClustering::create(
150 Points, AnalysisNumPoints, AnalysisEpsilon));
151 if (auto Err = printAnalysisClusters(Clustering, *STI, llvm::outs())) {
152 llvm::report_fatal_error(std::move(Err));
153 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000154}
155
156} // namespace exegesis
157
158int main(int Argc, char **Argv) {
159 llvm::cl::ParseCommandLineOptions(Argc, Argv, "");
160
Clement Courbet3d479fe2018-05-14 11:30:56 +0000161 if (BenchmarkMode == BenchmarkModeE::Analysis) {
162 exegesis::analysisMain();
163 } else {
164 exegesis::benchmarkMain();
Clement Courbetac74acd2018-04-04 11:37:06 +0000165 }
Clement Courbetac74acd2018-04-04 11:37:06 +0000166 return EXIT_SUCCESS;
167}