xref: /openbsd-src/gnu/llvm/llvm/tools/llvm-exegesis/lib/Analysis.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
1 //===-- Analysis.cpp --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Analysis.h"
10 #include "BenchmarkResult.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCTargetOptions.h"
14 #include "llvm/Support/FormatVariadic.h"
15 #include <limits>
16 #include <unordered_set>
17 #include <vector>
18 
19 namespace llvm {
20 namespace exegesis {
21 
22 static const char kCsvSep = ',';
23 
24 namespace {
25 
26 enum EscapeTag { kEscapeCsv, kEscapeHtml, kEscapeHtmlString };
27 
28 template <EscapeTag Tag> void writeEscaped(raw_ostream &OS, const StringRef S);
29 
writeEscaped(raw_ostream & OS,const StringRef S)30 template <> void writeEscaped<kEscapeCsv>(raw_ostream &OS, const StringRef S) {
31   if (!llvm::is_contained(S, kCsvSep)) {
32     OS << S;
33   } else {
34     // Needs escaping.
35     OS << '"';
36     for (const char C : S) {
37       if (C == '"')
38         OS << "\"\"";
39       else
40         OS << C;
41     }
42     OS << '"';
43   }
44 }
45 
writeEscaped(raw_ostream & OS,const StringRef S)46 template <> void writeEscaped<kEscapeHtml>(raw_ostream &OS, const StringRef S) {
47   for (const char C : S) {
48     if (C == '<')
49       OS << "&lt;";
50     else if (C == '>')
51       OS << "&gt;";
52     else if (C == '&')
53       OS << "&amp;";
54     else
55       OS << C;
56   }
57 }
58 
59 template <>
writeEscaped(raw_ostream & OS,const StringRef S)60 void writeEscaped<kEscapeHtmlString>(raw_ostream &OS, const StringRef S) {
61   for (const char C : S) {
62     if (C == '"')
63       OS << "\\\"";
64     else
65       OS << C;
66   }
67 }
68 
69 } // namespace
70 
71 template <EscapeTag Tag>
72 static void
writeClusterId(raw_ostream & OS,const InstructionBenchmarkClustering::ClusterId & CID)73 writeClusterId(raw_ostream &OS,
74                const InstructionBenchmarkClustering::ClusterId &CID) {
75   if (CID.isNoise())
76     writeEscaped<Tag>(OS, "[noise]");
77   else if (CID.isError())
78     writeEscaped<Tag>(OS, "[error]");
79   else
80     OS << CID.getId();
81 }
82 
83 template <EscapeTag Tag>
writeMeasurementValue(raw_ostream & OS,const double Value)84 static void writeMeasurementValue(raw_ostream &OS, const double Value) {
85   // Given Value, if we wanted to serialize it to a string,
86   // how many base-10 digits will we need to store, max?
87   static constexpr auto MaxDigitCount =
88       std::numeric_limits<decltype(Value)>::max_digits10;
89   // Also, we will need a decimal separator.
90   static constexpr auto DecimalSeparatorLen = 1; // '.' e.g.
91   // So how long of a string will the serialization produce, max?
92   static constexpr auto SerializationLen = MaxDigitCount + DecimalSeparatorLen;
93 
94   // WARNING: when changing the format, also adjust the small-size estimate ^.
95   static constexpr StringLiteral SimpleFloatFormat = StringLiteral("{0:F}");
96 
97   writeEscaped<Tag>(
98       OS, formatv(SimpleFloatFormat.data(), Value).sstr<SerializationLen>());
99 }
100 
101 template <typename EscapeTag, EscapeTag Tag>
writeSnippet(raw_ostream & OS,ArrayRef<uint8_t> Bytes,const char * Separator) const102 void Analysis::writeSnippet(raw_ostream &OS, ArrayRef<uint8_t> Bytes,
103                             const char *Separator) const {
104   SmallVector<std::string, 3> Lines;
105   const auto &SI = State_.getSubtargetInfo();
106   // Parse the asm snippet and print it.
107   while (!Bytes.empty()) {
108     MCInst MI;
109     uint64_t MISize = 0;
110     if (!Disasm_->getInstruction(MI, MISize, Bytes, 0, nulls())) {
111       writeEscaped<Tag>(OS, join(Lines, Separator));
112       writeEscaped<Tag>(OS, Separator);
113       writeEscaped<Tag>(OS, "[error decoding asm snippet]");
114       return;
115     }
116     SmallString<128> InstPrinterStr; // FIXME: magic number.
117     raw_svector_ostream OSS(InstPrinterStr);
118     InstPrinter_->printInst(&MI, 0, "", SI, OSS);
119     Bytes = Bytes.drop_front(MISize);
120     Lines.emplace_back(InstPrinterStr.str().trim());
121   }
122   writeEscaped<Tag>(OS, join(Lines, Separator));
123 }
124 
125 // Prints a row representing an instruction, along with scheduling info and
126 // point coordinates (measurements).
printInstructionRowCsv(const size_t PointId,raw_ostream & OS) const127 void Analysis::printInstructionRowCsv(const size_t PointId,
128                                       raw_ostream &OS) const {
129   const InstructionBenchmark &Point = Clustering_.getPoints()[PointId];
130   writeClusterId<kEscapeCsv>(OS, Clustering_.getClusterIdForPoint(PointId));
131   OS << kCsvSep;
132   writeSnippet<EscapeTag, kEscapeCsv>(OS, Point.AssembledSnippet, "; ");
133   OS << kCsvSep;
134   writeEscaped<kEscapeCsv>(OS, Point.Key.Config);
135   OS << kCsvSep;
136   assert(!Point.Key.Instructions.empty());
137   const MCInst &MCI = Point.keyInstruction();
138   unsigned SchedClassId;
139   std::tie(SchedClassId, std::ignore) = ResolvedSchedClass::resolveSchedClassId(
140       State_.getSubtargetInfo(), State_.getInstrInfo(), MCI);
141 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
142   const MCSchedClassDesc *const SCDesc =
143       State_.getSubtargetInfo().getSchedModel().getSchedClassDesc(SchedClassId);
144   writeEscaped<kEscapeCsv>(OS, SCDesc->Name);
145 #else
146   OS << SchedClassId;
147 #endif
148   for (const auto &Measurement : Point.Measurements) {
149     OS << kCsvSep;
150     writeMeasurementValue<kEscapeCsv>(OS, Measurement.PerInstructionValue);
151   }
152   OS << "\n";
153 }
154 
Analysis(const LLVMState & State,const InstructionBenchmarkClustering & Clustering,double AnalysisInconsistencyEpsilon,bool AnalysisDisplayUnstableOpcodes)155 Analysis::Analysis(const LLVMState &State,
156                    const InstructionBenchmarkClustering &Clustering,
157                    double AnalysisInconsistencyEpsilon,
158                    bool AnalysisDisplayUnstableOpcodes)
159     : Clustering_(Clustering), State_(State),
160       AnalysisInconsistencyEpsilonSquared_(AnalysisInconsistencyEpsilon *
161                                            AnalysisInconsistencyEpsilon),
162       AnalysisDisplayUnstableOpcodes_(AnalysisDisplayUnstableOpcodes) {
163   if (Clustering.getPoints().empty())
164     return;
165 
166   MCTargetOptions MCOptions;
167   const auto &TM = State.getTargetMachine();
168   const auto &Triple = TM.getTargetTriple();
169   AsmInfo_.reset(TM.getTarget().createMCAsmInfo(State_.getRegInfo(),
170                                                 Triple.str(), MCOptions));
171   InstPrinter_.reset(TM.getTarget().createMCInstPrinter(
172       Triple, 0 /*default variant*/, *AsmInfo_, State_.getInstrInfo(),
173       State_.getRegInfo()));
174 
175   Context_ = std::make_unique<MCContext>(
176       Triple, AsmInfo_.get(), &State_.getRegInfo(), &State_.getSubtargetInfo());
177   Disasm_.reset(TM.getTarget().createMCDisassembler(State_.getSubtargetInfo(),
178                                                     *Context_));
179   assert(Disasm_ && "cannot create MCDisassembler. missing call to "
180                     "InitializeXXXTargetDisassembler ?");
181 }
182 
183 template <>
run(raw_ostream & OS) const184 Error Analysis::run<Analysis::PrintClusters>(raw_ostream &OS) const {
185   if (Clustering_.getPoints().empty())
186     return Error::success();
187 
188   // Write the header.
189   OS << "cluster_id" << kCsvSep << "opcode_name" << kCsvSep << "config"
190      << kCsvSep << "sched_class";
191   for (const auto &Measurement : Clustering_.getPoints().front().Measurements) {
192     OS << kCsvSep;
193     writeEscaped<kEscapeCsv>(OS, Measurement.Key);
194   }
195   OS << "\n";
196 
197   // Write the points.
198   for (const auto &ClusterIt : Clustering_.getValidClusters()) {
199     for (const size_t PointId : ClusterIt.PointIndices) {
200       printInstructionRowCsv(PointId, OS);
201     }
202     OS << "\n\n";
203   }
204   return Error::success();
205 }
206 
ResolvedSchedClassAndPoints(ResolvedSchedClass && RSC)207 Analysis::ResolvedSchedClassAndPoints::ResolvedSchedClassAndPoints(
208     ResolvedSchedClass &&RSC)
209     : RSC(std::move(RSC)) {}
210 
211 std::vector<Analysis::ResolvedSchedClassAndPoints>
makePointsPerSchedClass() const212 Analysis::makePointsPerSchedClass() const {
213   std::vector<ResolvedSchedClassAndPoints> Entries;
214   // Maps SchedClassIds to index in result.
215   std::unordered_map<unsigned, size_t> SchedClassIdToIndex;
216   const auto &Points = Clustering_.getPoints();
217   for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) {
218     const InstructionBenchmark &Point = Points[PointId];
219     if (!Point.Error.empty())
220       continue;
221     assert(!Point.Key.Instructions.empty());
222     // FIXME: we should be using the tuple of classes for instructions in the
223     // snippet as key.
224     const MCInst &MCI = Point.keyInstruction();
225     unsigned SchedClassId;
226     bool WasVariant;
227     std::tie(SchedClassId, WasVariant) =
228         ResolvedSchedClass::resolveSchedClassId(State_.getSubtargetInfo(),
229                                                 State_.getInstrInfo(), MCI);
230     const auto IndexIt = SchedClassIdToIndex.find(SchedClassId);
231     if (IndexIt == SchedClassIdToIndex.end()) {
232       // Create a new entry.
233       SchedClassIdToIndex.emplace(SchedClassId, Entries.size());
234       ResolvedSchedClassAndPoints Entry(ResolvedSchedClass(
235           State_.getSubtargetInfo(), SchedClassId, WasVariant));
236       Entry.PointIds.push_back(PointId);
237       Entries.push_back(std::move(Entry));
238     } else {
239       // Append to the existing entry.
240       Entries[IndexIt->second].PointIds.push_back(PointId);
241     }
242   }
243   return Entries;
244 }
245 
246 // Parallel benchmarks repeat the same opcode multiple times. Just show this
247 // opcode and show the whole snippet only on hover.
writeParallelSnippetHtml(raw_ostream & OS,const std::vector<MCInst> & Instructions,const MCInstrInfo & InstrInfo)248 static void writeParallelSnippetHtml(raw_ostream &OS,
249                                  const std::vector<MCInst> &Instructions,
250                                  const MCInstrInfo &InstrInfo) {
251   if (Instructions.empty())
252     return;
253   writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instructions[0].getOpcode()));
254   if (Instructions.size() > 1)
255     OS << " (x" << Instructions.size() << ")";
256 }
257 
258 // Latency tries to find a serial path. Just show the opcode path and show the
259 // whole snippet only on hover.
writeLatencySnippetHtml(raw_ostream & OS,const std::vector<MCInst> & Instructions,const MCInstrInfo & InstrInfo)260 static void writeLatencySnippetHtml(raw_ostream &OS,
261                                     const std::vector<MCInst> &Instructions,
262                                     const MCInstrInfo &InstrInfo) {
263   bool First = true;
264   for (const MCInst &Instr : Instructions) {
265     if (First)
266       First = false;
267     else
268       OS << " &rarr; ";
269     writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instr.getOpcode()));
270   }
271 }
272 
printPointHtml(const InstructionBenchmark & Point,llvm::raw_ostream & OS) const273 void Analysis::printPointHtml(const InstructionBenchmark &Point,
274                               llvm::raw_ostream &OS) const {
275   OS << "<li><span class=\"mono\" title=\"";
276   writeSnippet<EscapeTag, kEscapeHtmlString>(OS, Point.AssembledSnippet, "\n");
277   OS << "\">";
278   switch (Point.Mode) {
279   case InstructionBenchmark::Latency:
280     writeLatencySnippetHtml(OS, Point.Key.Instructions, State_.getInstrInfo());
281     break;
282   case InstructionBenchmark::Uops:
283   case InstructionBenchmark::InverseThroughput:
284     writeParallelSnippetHtml(OS, Point.Key.Instructions, State_.getInstrInfo());
285     break;
286   default:
287     llvm_unreachable("invalid mode");
288   }
289   OS << "</span> <span class=\"mono\">";
290   writeEscaped<kEscapeHtml>(OS, Point.Key.Config);
291   OS << "</span></li>";
292 }
293 
printSchedClassClustersHtml(const std::vector<SchedClassCluster> & Clusters,const ResolvedSchedClass & RSC,raw_ostream & OS) const294 void Analysis::printSchedClassClustersHtml(
295     const std::vector<SchedClassCluster> &Clusters,
296     const ResolvedSchedClass &RSC, raw_ostream &OS) const {
297   const auto &Points = Clustering_.getPoints();
298   OS << "<table class=\"sched-class-clusters\">";
299   OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>";
300   assert(!Clusters.empty());
301   for (const auto &Measurement :
302        Points[Clusters[0].getPointIds()[0]].Measurements) {
303     OS << "<th>";
304     writeEscaped<kEscapeHtml>(OS, Measurement.Key);
305     OS << "</th>";
306   }
307   OS << "</tr>";
308   for (const SchedClassCluster &Cluster : Clusters) {
309     OS << "<tr class=\""
310        << (Cluster.measurementsMatch(State_.getSubtargetInfo(), RSC,
311                                      Clustering_,
312                                      AnalysisInconsistencyEpsilonSquared_)
313                ? "good-cluster"
314                : "bad-cluster")
315        << "\"><td>";
316     writeClusterId<kEscapeHtml>(OS, Cluster.id());
317     OS << "</td><td><ul>";
318     for (const size_t PointId : Cluster.getPointIds()) {
319       printPointHtml(Points[PointId], OS);
320     }
321     OS << "</ul></td>";
322     for (const auto &Stats : Cluster.getCentroid().getStats()) {
323       OS << "<td class=\"measurement\">";
324       writeMeasurementValue<kEscapeHtml>(OS, Stats.avg());
325       OS << "<br><span class=\"minmax\">[";
326       writeMeasurementValue<kEscapeHtml>(OS, Stats.min());
327       OS << ";";
328       writeMeasurementValue<kEscapeHtml>(OS, Stats.max());
329       OS << "]</span></td>";
330     }
331     OS << "</tr>";
332   }
333   OS << "</table>";
334 }
335 
addPoint(size_t PointId,const InstructionBenchmarkClustering & Clustering)336 void Analysis::SchedClassCluster::addPoint(
337     size_t PointId, const InstructionBenchmarkClustering &Clustering) {
338   PointIds.push_back(PointId);
339   const auto &Point = Clustering.getPoints()[PointId];
340   if (ClusterId.isUndef())
341     ClusterId = Clustering.getClusterIdForPoint(PointId);
342   assert(ClusterId == Clustering.getClusterIdForPoint(PointId));
343 
344   Centroid.addPoint(Point.Measurements);
345 }
346 
measurementsMatch(const MCSubtargetInfo & STI,const ResolvedSchedClass & RSC,const InstructionBenchmarkClustering & Clustering,const double AnalysisInconsistencyEpsilonSquared_) const347 bool Analysis::SchedClassCluster::measurementsMatch(
348     const MCSubtargetInfo &STI, const ResolvedSchedClass &RSC,
349     const InstructionBenchmarkClustering &Clustering,
350     const double AnalysisInconsistencyEpsilonSquared_) const {
351   assert(!Clustering.getPoints().empty());
352   const InstructionBenchmark::ModeE Mode = Clustering.getPoints()[0].Mode;
353 
354   if (!Centroid.validate(Mode))
355     return false;
356 
357   const std::vector<BenchmarkMeasure> ClusterCenterPoint =
358       Centroid.getAsPoint();
359 
360   const std::vector<BenchmarkMeasure> SchedClassPoint =
361       RSC.getAsPoint(Mode, STI, Centroid.getStats());
362   if (SchedClassPoint.empty())
363     return false; // In Uops mode validate() may not be enough.
364 
365   assert(ClusterCenterPoint.size() == SchedClassPoint.size() &&
366          "Expected measured/sched data dimensions to match.");
367 
368   return Clustering.isNeighbour(ClusterCenterPoint, SchedClassPoint,
369                                 AnalysisInconsistencyEpsilonSquared_);
370 }
371 
printSchedClassDescHtml(const ResolvedSchedClass & RSC,raw_ostream & OS) const372 void Analysis::printSchedClassDescHtml(const ResolvedSchedClass &RSC,
373                                        raw_ostream &OS) const {
374   OS << "<table class=\"sched-class-desc\">";
375   OS << "<tr><th>Valid</th><th>Variant</th><th>NumMicroOps</th><th>Latency</"
376         "th><th>RThroughput</th><th>WriteProcRes</th><th title=\"This is the "
377         "idealized unit resource (port) pressure assuming ideal "
378         "distribution\">Idealized Resource Pressure</th></tr>";
379   if (RSC.SCDesc->isValid()) {
380     const auto &SI = State_.getSubtargetInfo();
381     const auto &SM = SI.getSchedModel();
382     OS << "<tr><td>&#10004;</td>";
383     OS << "<td>" << (RSC.WasVariant ? "&#10004;" : "&#10005;") << "</td>";
384     OS << "<td>" << RSC.SCDesc->NumMicroOps << "</td>";
385     // Latencies.
386     OS << "<td><ul>";
387     for (int I = 0, E = RSC.SCDesc->NumWriteLatencyEntries; I < E; ++I) {
388       const auto *const Entry = SI.getWriteLatencyEntry(RSC.SCDesc, I);
389       OS << "<li>" << Entry->Cycles;
390       if (RSC.SCDesc->NumWriteLatencyEntries > 1) {
391         // Dismabiguate if more than 1 latency.
392         OS << " (WriteResourceID " << Entry->WriteResourceID << ")";
393       }
394       OS << "</li>";
395     }
396     OS << "</ul></td>";
397     // inverse throughput.
398     OS << "<td>";
399     writeMeasurementValue<kEscapeHtml>(
400         OS, MCSchedModel::getReciprocalThroughput(SI, *RSC.SCDesc));
401     OS << "</td>";
402     // WriteProcRes.
403     OS << "<td><ul>";
404     for (const auto &WPR : RSC.NonRedundantWriteProcRes) {
405       OS << "<li><span class=\"mono\">";
406       writeEscaped<kEscapeHtml>(OS,
407                                 SM.getProcResource(WPR.ProcResourceIdx)->Name);
408       OS << "</span>: " << WPR.Cycles << "</li>";
409     }
410     OS << "</ul></td>";
411     // Idealized port pressure.
412     OS << "<td><ul>";
413     for (const auto &Pressure : RSC.IdealizedProcResPressure) {
414       OS << "<li><span class=\"mono\">";
415       writeEscaped<kEscapeHtml>(
416           OS, SI.getSchedModel().getProcResource(Pressure.first)->Name);
417       OS << "</span>: ";
418       writeMeasurementValue<kEscapeHtml>(OS, Pressure.second);
419       OS << "</li>";
420     }
421     OS << "</ul></td>";
422     OS << "</tr>";
423   } else {
424     OS << "<tr><td>&#10005;</td><td></td><td></td></tr>";
425   }
426   OS << "</table>";
427 }
428 
printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId & Id,StringRef display_name,llvm::raw_ostream & OS) const429 void Analysis::printClusterRawHtml(
430     const InstructionBenchmarkClustering::ClusterId &Id, StringRef display_name,
431     llvm::raw_ostream &OS) const {
432   const auto &Points = Clustering_.getPoints();
433   const auto &Cluster = Clustering_.getCluster(Id);
434   if (Cluster.PointIndices.empty())
435     return;
436 
437   OS << "<div class=\"inconsistency\"><p>" << display_name << " Cluster ("
438      << Cluster.PointIndices.size() << " points)</p>";
439   OS << "<table class=\"sched-class-clusters\">";
440   // Table Header.
441   OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>";
442   for (const auto &Measurement : Points[Cluster.PointIndices[0]].Measurements) {
443     OS << "<th>";
444     writeEscaped<kEscapeHtml>(OS, Measurement.Key);
445     OS << "</th>";
446   }
447   OS << "</tr>";
448 
449   // Point data.
450   for (const auto &PointId : Cluster.PointIndices) {
451     OS << "<tr class=\"bad-cluster\"><td>" << display_name << "</td><td><ul>";
452     printPointHtml(Points[PointId], OS);
453     OS << "</ul></td>";
454     for (const auto &Measurement : Points[PointId].Measurements) {
455       OS << "<td class=\"measurement\">";
456       writeMeasurementValue<kEscapeHtml>(OS, Measurement.PerInstructionValue);
457     }
458     OS << "</tr>";
459   }
460   OS << "</table>";
461 
462   OS << "</div>";
463 
464 } // namespace exegesis
465 
466 static constexpr const char kHtmlHead[] = R"(
467 <head>
468 <title>llvm-exegesis Analysis Results</title>
469 <style>
470 body {
471   font-family: sans-serif
472 }
473 span.sched-class-name {
474   font-weight: bold;
475   font-family: monospace;
476 }
477 span.opcode {
478   font-family: monospace;
479 }
480 span.config {
481   font-family: monospace;
482 }
483 div.inconsistency {
484   margin-top: 50px;
485 }
486 table {
487   margin-left: 50px;
488   border-collapse: collapse;
489 }
490 table, table tr,td,th {
491   border: 1px solid #444;
492 }
493 table ul {
494   padding-left: 0px;
495   margin: 0px;
496   list-style-type: none;
497 }
498 table.sched-class-clusters td {
499   padding-left: 10px;
500   padding-right: 10px;
501   padding-top: 10px;
502   padding-bottom: 10px;
503 }
504 table.sched-class-desc td {
505   padding-left: 10px;
506   padding-right: 10px;
507   padding-top: 2px;
508   padding-bottom: 2px;
509 }
510 span.mono {
511   font-family: monospace;
512 }
513 td.measurement {
514   text-align: center;
515 }
516 tr.good-cluster td.measurement {
517   color: #292
518 }
519 tr.bad-cluster td.measurement {
520   color: #922
521 }
522 tr.good-cluster td.measurement span.minmax {
523   color: #888;
524 }
525 tr.bad-cluster td.measurement span.minmax {
526   color: #888;
527 }
528 </style>
529 </head>
530 )";
531 
532 template <>
run(raw_ostream & OS) const533 Error Analysis::run<Analysis::PrintSchedClassInconsistencies>(
534     raw_ostream &OS) const {
535   const auto &FirstPoint = Clustering_.getPoints()[0];
536   // Print the header.
537   OS << "<!DOCTYPE html><html>" << kHtmlHead << "<body>";
538   OS << "<h1><span class=\"mono\">llvm-exegesis</span> Analysis Results</h1>";
539   OS << "<h3>Triple: <span class=\"mono\">";
540   writeEscaped<kEscapeHtml>(OS, FirstPoint.LLVMTriple);
541   OS << "</span></h3><h3>Cpu: <span class=\"mono\">";
542   writeEscaped<kEscapeHtml>(OS, FirstPoint.CpuName);
543   OS << "</span></h3>";
544 
545   const auto &SI = State_.getSubtargetInfo();
546   for (const auto &RSCAndPoints : makePointsPerSchedClass()) {
547     if (!RSCAndPoints.RSC.SCDesc)
548       continue;
549     // Bucket sched class points into sched class clusters.
550     std::vector<SchedClassCluster> SchedClassClusters;
551     for (const size_t PointId : RSCAndPoints.PointIds) {
552       const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId);
553       if (!ClusterId.isValid())
554         continue; // Ignore noise and errors. FIXME: take noise into account ?
555       if (ClusterId.isUnstable() ^ AnalysisDisplayUnstableOpcodes_)
556         continue; // Either display stable or unstable clusters only.
557       auto SchedClassClusterIt = llvm::find_if(
558           SchedClassClusters, [ClusterId](const SchedClassCluster &C) {
559             return C.id() == ClusterId;
560           });
561       if (SchedClassClusterIt == SchedClassClusters.end()) {
562         SchedClassClusters.emplace_back();
563         SchedClassClusterIt = std::prev(SchedClassClusters.end());
564       }
565       SchedClassClusterIt->addPoint(PointId, Clustering_);
566     }
567 
568     // Print any scheduling class that has at least one cluster that does not
569     // match the checked-in data.
570     if (all_of(SchedClassClusters, [this, &RSCAndPoints,
571                                     &SI](const SchedClassCluster &C) {
572           return C.measurementsMatch(SI, RSCAndPoints.RSC, Clustering_,
573                                      AnalysisInconsistencyEpsilonSquared_);
574         }))
575       continue; // Nothing weird.
576 
577     OS << "<div class=\"inconsistency\"><p>Sched Class <span "
578           "class=\"sched-class-name\">";
579 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
580     writeEscaped<kEscapeHtml>(OS, RSCAndPoints.RSC.SCDesc->Name);
581 #else
582     OS << RSCAndPoints.RSC.SchedClassId;
583 #endif
584     OS << "</span> contains instructions whose performance characteristics do"
585           " not match that of LLVM:</p>";
586     printSchedClassClustersHtml(SchedClassClusters, RSCAndPoints.RSC, OS);
587     OS << "<p>llvm SchedModel data:</p>";
588     printSchedClassDescHtml(RSCAndPoints.RSC, OS);
589     OS << "</div>";
590   }
591 
592   printClusterRawHtml(InstructionBenchmarkClustering::ClusterId::noise(),
593                       "[noise]", OS);
594 
595   OS << "</body></html>";
596   return Error::success();
597 }
598 
599 } // namespace exegesis
600 } // namespace llvm
601