blob: cb3800e7db5eda7583f64ace8f9b5499896cdb29 [file] [log] [blame]
Clement Courbet37f0ca02018-05-15 12:08:00 +00001//===-- Analysis.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
Clement Courbet37f0ca02018-05-15 12:08:00 +000010#include "Analysis.h"
Clement Courbeta66bfaa42018-05-15 13:07:05 +000011#include "BenchmarkResult.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000012#include "llvm/Support/FormatVariadic.h"
Clement Courbet448550d2018-05-17 12:25:18 +000013#include <unordered_set>
Clement Courbet37f0ca02018-05-15 12:08:00 +000014#include <vector>
15
16namespace exegesis {
17
18static const char kCsvSep = ',';
19
Clement Courbet17d3c252018-05-22 13:31:29 +000020namespace {
21
22enum EscapeTag { kEscapeCsv, kEscapeHtml };
23
24template <EscapeTag Tag>
25void writeEscaped(llvm::raw_ostream &OS, const llvm::StringRef S);
26
27template <>
28void writeEscaped<kEscapeCsv>(llvm::raw_ostream &OS, const llvm::StringRef S) {
Clement Courbet37f0ca02018-05-15 12:08:00 +000029 if (std::find(S.begin(), S.end(), kCsvSep) == S.end()) {
30 OS << S;
31 } else {
32 // Needs escaping.
33 OS << '"';
34 for (const char C : S) {
35 if (C == '"')
36 OS << "\"\"";
37 else
38 OS << C;
39 }
40 OS << '"';
41 }
42}
43
Clement Courbet17d3c252018-05-22 13:31:29 +000044template <>
45void writeEscaped<kEscapeHtml>(llvm::raw_ostream &OS, const llvm::StringRef S) {
46 for (const char C : S) {
47 if (C == '<')
48 OS << "&lt;";
49 else if (C == '>')
50 OS << "&gt;";
51 else if (C == '&')
52 OS << "&amp;";
53 else
54 OS << C;
55 }
56}
57
58} // namespace
59
60template <EscapeTag Tag>
61static void
62writeClusterId(llvm::raw_ostream &OS,
63 const InstructionBenchmarkClustering::ClusterId &CID) {
64 if (CID.isNoise())
65 writeEscaped<Tag>(OS, "[noise]");
66 else if (CID.isError())
67 writeEscaped<Tag>(OS, "[error]");
68 else
69 OS << CID.getId();
70}
71
72template <EscapeTag Tag>
73static void writeMeasurementValue(llvm::raw_ostream &OS, const double Value) {
74 writeEscaped<Tag>(OS, llvm::formatv("{0:F}", Value).str());
75}
76
Clement Courbet37f0ca02018-05-15 12:08:00 +000077// Prints a row representing an instruction, along with scheduling info and
78// point coordinates (measurements).
Clement Courbet17d3c252018-05-22 13:31:29 +000079void Analysis::printInstructionRowCsv(const size_t PointId,
80 llvm::raw_ostream &OS) const {
Clement Courbet6d6c1a92018-05-16 08:47:21 +000081 const InstructionBenchmark &Point = Clustering_.getPoints()[PointId];
Clement Courbet17d3c252018-05-22 13:31:29 +000082 writeClusterId<kEscapeCsv>(OS, Clustering_.getClusterIdForPoint(PointId));
Clement Courbet448550d2018-05-17 12:25:18 +000083 OS << kCsvSep;
Clement Courbet17d3c252018-05-22 13:31:29 +000084 writeEscaped<kEscapeCsv>(OS, Point.Key.OpcodeName);
Clement Courbeta66bfaa42018-05-15 13:07:05 +000085 OS << kCsvSep;
Clement Courbet17d3c252018-05-22 13:31:29 +000086 writeEscaped<kEscapeCsv>(OS, Point.Key.Config);
87 OS << kCsvSep;
88 const auto OpcodeIt = MnemonicToOpcode_.find(Point.Key.OpcodeName);
89 if (OpcodeIt != MnemonicToOpcode_.end()) {
90 const unsigned SchedClassId =
91 InstrInfo_->get(OpcodeIt->second).getSchedClass();
Clement Courbet6d6c1a92018-05-16 08:47:21 +000092#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Clement Courbet17d3c252018-05-22 13:31:29 +000093 const auto &SchedModel = SubtargetInfo_->getSchedModel();
94 const llvm::MCSchedClassDesc *const SCDesc =
95 SchedModel.getSchedClassDesc(SchedClassId);
96 writeEscaped<kEscapeCsv>(OS, SCDesc->Name);
Clement Courbet6d6c1a92018-05-16 08:47:21 +000097#else
Clement Courbet17d3c252018-05-22 13:31:29 +000098 OS << SchedClassId;
Clement Courbet6d6c1a92018-05-16 08:47:21 +000099#endif
100 }
Clement Courbet37f0ca02018-05-15 12:08:00 +0000101 // FIXME: Print the sched class once InstructionBenchmark separates key into
102 // (mnemonic, mode, opaque).
103 for (const auto &Measurement : Point.Measurements) {
104 OS << kCsvSep;
Clement Courbet17d3c252018-05-22 13:31:29 +0000105 writeMeasurementValue<kEscapeCsv>(OS, Measurement.Value);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000106 }
107 OS << "\n";
108}
109
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000110Analysis::Analysis(const llvm::Target &Target,
111 const InstructionBenchmarkClustering &Clustering)
112 : Clustering_(Clustering) {
113 if (Clustering.getPoints().empty())
114 return;
115
116 InstrInfo_.reset(Target.createMCInstrInfo());
117 const InstructionBenchmark &FirstPoint = Clustering.getPoints().front();
Clement Courbet448550d2018-05-17 12:25:18 +0000118 SubtargetInfo_.reset(Target.createMCSubtargetInfo(FirstPoint.LLVMTriple,
119 FirstPoint.CpuName, ""));
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000120
121 // Build an index of mnemonic->opcode.
122 for (int I = 0, E = InstrInfo_->getNumOpcodes(); I < E; ++I)
123 MnemonicToOpcode_.emplace(InstrInfo_->getName(I), I);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000124}
125
Clement Courbetcf210742018-05-17 13:41:28 +0000126template <>
127llvm::Error
128Analysis::run<Analysis::PrintClusters>(llvm::raw_ostream &OS) const {
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000129 if (Clustering_.getPoints().empty())
Clement Courbet37f0ca02018-05-15 12:08:00 +0000130 return llvm::Error::success();
131
132 // Write the header.
Clement Courbeta66bfaa42018-05-15 13:07:05 +0000133 OS << "cluster_id" << kCsvSep << "opcode_name" << kCsvSep << "config"
134 << kCsvSep << "sched_class";
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000135 for (const auto &Measurement : Clustering_.getPoints().front().Measurements) {
Clement Courbet37f0ca02018-05-15 12:08:00 +0000136 OS << kCsvSep;
Clement Courbet17d3c252018-05-22 13:31:29 +0000137 writeEscaped<kEscapeCsv>(OS, Measurement.Key);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000138 }
139 OS << "\n";
140
141 // Write the points.
Clement Courbet448550d2018-05-17 12:25:18 +0000142 const auto &Clusters = Clustering_.getValidClusters();
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000143 for (size_t I = 0, E = Clusters.size(); I < E; ++I) {
144 for (const size_t PointId : Clusters[I].PointIndices) {
Clement Courbet17d3c252018-05-22 13:31:29 +0000145 printInstructionRowCsv(PointId, OS);
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000146 }
Clement Courbet37f0ca02018-05-15 12:08:00 +0000147 OS << "\n\n";
148 }
149 return llvm::Error::success();
150}
151
Clement Courbet448550d2018-05-17 12:25:18 +0000152std::unordered_map<unsigned, std::vector<size_t>>
153Analysis::makePointsPerSchedClass() const {
154 std::unordered_map<unsigned, std::vector<size_t>> PointsPerSchedClass;
155 const auto &Points = Clustering_.getPoints();
156 for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) {
157 const InstructionBenchmark &Point = Points[PointId];
158 if (!Point.Error.empty())
159 continue;
160 const auto OpcodeIt = MnemonicToOpcode_.find(Point.Key.OpcodeName);
161 if (OpcodeIt == MnemonicToOpcode_.end())
162 continue;
163 const unsigned SchedClassId =
164 InstrInfo_->get(OpcodeIt->second).getSchedClass();
165 PointsPerSchedClass[SchedClassId].push_back(PointId);
166 }
167 return PointsPerSchedClass;
168}
169
Clement Courbet17d3c252018-05-22 13:31:29 +0000170void Analysis::printSchedClassHtml(std::vector<size_t> PointIds,
171 llvm::raw_ostream &OS) const {
172 assert(!PointIds.empty());
173 // Sort the points by cluster id so that we can display them grouped by
174 // cluster.
175 std::sort(PointIds.begin(), PointIds.end(),
176 [this](const size_t A, const size_t B) {
177 return Clustering_.getClusterIdForPoint(A) <
178 Clustering_.getClusterIdForPoint(B);
179 });
180 const auto &Points = Clustering_.getPoints();
181 OS << "<table class=\"sched-class\">";
182 OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>";
183 for (const auto &Measurement : Points[PointIds[0]].Measurements) {
184 OS << "<th>";
185 writeEscaped<kEscapeHtml>(OS, Measurement.Key);
186 OS << "</th>";
187 }
188 OS << "</tr>";
189 for (size_t I = 0, E = PointIds.size(); I < E;) {
190 const auto &CurrentClusterId =
191 Clustering_.getClusterIdForPoint(PointIds[I]);
192 OS << "<tr><td>";
193 writeClusterId<kEscapeHtml>(OS, CurrentClusterId);
194 OS << "</td><td><ul>";
195 const auto &ClusterRepresentative =
196 Points[PointIds[I]]; // FIXME: average measurements.
197 for (; I < E &&
198 Clustering_.getClusterIdForPoint(PointIds[I]) == CurrentClusterId;
199 ++I) {
200 OS << "<li><span class=\"mono\">";
201 writeEscaped<kEscapeHtml>(OS, Points[PointIds[I]].Key.OpcodeName);
202 OS << "</span> <span class=\"mono\">";
203 writeEscaped<kEscapeHtml>(OS, Points[PointIds[I]].Key.Config);
204 OS << "</span></li>";
205 }
206 OS << "</ul></td>";
207 for (const auto &Measurement : ClusterRepresentative.Measurements) {
208 OS << "<td>";
209 writeMeasurementValue<kEscapeHtml>(OS, Measurement.Value);
210 OS << "</td>";
211 }
212 OS << "</tr>";
213 }
214 OS << "</table>";
215}
216
217static constexpr const char kHtmlHead[] = R"(
218<head>
219<title>llvm-exegesis Analysis Results</title>
220<style>
221body {
222 font-family: sans-serif
223}
224span.sched-class-name {
225 font-weight: bold;
226 font-family: monospace;
227}
228span.opcode {
229 font-family: monospace;
230}
231span.config {
232 font-family: monospace;
233}
234div.inconsistency {
235 margin-top: 50px;
236}
237table.sched-class {
238 margin-left: 50px;
239 border-collapse: collapse;
240}
241table.sched-class, table.sched-class tr,td,th {
242 border: 1px solid #444;
243}
244table.sched-class td {
245 padding-left: 10px;
246 padding-right: 10px;
247 padding-top: 10px;
248 padding-bottom: 10px;
249}
250table.sched-class ul {
251 padding-left: 0px;
252 margin: 0px;
253 list-style-type: none;
254}
255span.mono {
256 font-family: monospace;
257}
258</style>
259</head>
260)";
261
Clement Courbetcf210742018-05-17 13:41:28 +0000262template <>
263llvm::Error Analysis::run<Analysis::PrintSchedClassInconsistencies>(
264 llvm::raw_ostream &OS) const {
Clement Courbet17d3c252018-05-22 13:31:29 +0000265 // Print the header.
266 OS << "<!DOCTYPE html><html>" << kHtmlHead << "<body>";
267 OS << "<h1><span class=\"mono\">llvm-exegesis</span> Analysis Results</h1>";
268 OS << "<h3>Triple: <span class=\"mono\">";
269 writeEscaped<kEscapeHtml>(OS, Clustering_.getPoints()[0].LLVMTriple);
270 OS << "</span></h3><h3>Cpu: <span class=\"mono\">";
271 writeEscaped<kEscapeHtml>(OS, Clustering_.getPoints()[0].CpuName);
272 OS << "</span></h3>";
273
Clement Courbet448550d2018-05-17 12:25:18 +0000274 // All the points in a scheduling class should be in the same cluster.
275 // Print any scheduling class for which this is not the case.
276 for (const auto &SchedClassAndPoints : makePointsPerSchedClass()) {
277 std::unordered_set<size_t> ClustersForSchedClass;
278 for (const size_t PointId : SchedClassAndPoints.second) {
279 const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId);
280 if (!ClusterId.isValid())
281 continue; // Ignore noise and errors.
282 ClustersForSchedClass.insert(ClusterId.getId());
283 }
284 if (ClustersForSchedClass.size() <= 1)
285 continue; // Nothing weird.
286
Clement Courbet17d3c252018-05-22 13:31:29 +0000287 OS << "<div class=\"inconsistency\"><p>Sched Class <span "
288 "class=\"sched-class-name\">";
Clement Courbet448550d2018-05-17 12:25:18 +0000289#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
290 const auto &SchedModel = SubtargetInfo_->getSchedModel();
291 const llvm::MCSchedClassDesc *const SCDesc =
292 SchedModel.getSchedClassDesc(SchedClassAndPoints.first);
Clement Courbet17d3c252018-05-22 13:31:29 +0000293 writeEscaped<kEscapeHtml>(OS, SCDesc->Name);
Clement Courbet448550d2018-05-17 12:25:18 +0000294#else
295 OS << SchedClassAndPoints.first;
296#endif
Clement Courbet17d3c252018-05-22 13:31:29 +0000297 OS << "</span> contains instructions with distinct performance "
Clement Courbet448550d2018-05-17 12:25:18 +0000298 "characteristics, falling into "
Clement Courbet17d3c252018-05-22 13:31:29 +0000299 << ClustersForSchedClass.size() << " clusters:</p>";
300 printSchedClassHtml(SchedClassAndPoints.second, OS);
301 OS << "</div>";
Clement Courbet448550d2018-05-17 12:25:18 +0000302 }
Clement Courbet17d3c252018-05-22 13:31:29 +0000303
304 OS << "</body></html>";
Clement Courbet448550d2018-05-17 12:25:18 +0000305 return llvm::Error::success();
306}
307
Clement Courbet37f0ca02018-05-15 12:08:00 +0000308} // namespace exegesis