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   // Parse the asm snippet and print it.
106   while (!Bytes.empty()) {
107     MCInst MI;
108     uint64_t MISize = 0;
109     if (!Disasm_->getInstruction(MI, MISize, Bytes, 0, nulls())) {
110       writeEscaped<Tag>(OS, join(Lines, Separator));
111       writeEscaped<Tag>(OS, Separator);
112       writeEscaped<Tag>(OS, "[error decoding asm snippet]");
113       return;
114     }
115     SmallString<128> InstPrinterStr; // FIXME: magic number.
116     raw_svector_ostream OSS(InstPrinterStr);
117     InstPrinter_->printInst(&MI, 0, "", *SubtargetInfo_, OSS);
118     Bytes = Bytes.drop_front(MISize);
119     Lines.emplace_back(StringRef(InstPrinterStr).trim());
120   }
121   writeEscaped<Tag>(OS, join(Lines, Separator));
122 }
123 
124 // Prints a row representing an instruction, along with scheduling info and
125 // point coordinates (measurements).
printInstructionRowCsv(const size_t PointId,raw_ostream & OS) const126 void Analysis::printInstructionRowCsv(const size_t PointId,
127                                       raw_ostream &OS) const {
128   const InstructionBenchmark &Point = Clustering_.getPoints()[PointId];
129   writeClusterId<kEscapeCsv>(OS, Clustering_.getClusterIdForPoint(PointId));
130   OS << kCsvSep;
131   writeSnippet<EscapeTag, kEscapeCsv>(OS, Point.AssembledSnippet, "; ");
132   OS << kCsvSep;
133   writeEscaped<kEscapeCsv>(OS, Point.Key.Config);
134   OS << kCsvSep;
135   assert(!Point.Key.Instructions.empty());
136   const MCInst &MCI = Point.keyInstruction();
137   unsigned SchedClassId;
138   std::tie(SchedClassId, std::ignore) = ResolvedSchedClass::resolveSchedClassId(
139       *SubtargetInfo_, *InstrInfo_, MCI);
140 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
141   const MCSchedClassDesc *const SCDesc =
142       SubtargetInfo_->getSchedModel().getSchedClassDesc(SchedClassId);
143   writeEscaped<kEscapeCsv>(OS, SCDesc->Name);
144 #else
145   OS << SchedClassId;
146 #endif
147   for (const auto &Measurement : Point.Measurements) {
148     OS << kCsvSep;
149     writeMeasurementValue<kEscapeCsv>(OS, Measurement.PerInstructionValue);
150   }
151   OS << "\n";
152 }
153 
Analysis(const Target & Target,std::unique_ptr<MCInstrInfo> InstrInfo,const InstructionBenchmarkClustering & Clustering,double AnalysisInconsistencyEpsilon,bool AnalysisDisplayUnstableOpcodes)154 Analysis::Analysis(const Target &Target, std::unique_ptr<MCInstrInfo> InstrInfo,
155                    const InstructionBenchmarkClustering &Clustering,
156                    double AnalysisInconsistencyEpsilon,
157                    bool AnalysisDisplayUnstableOpcodes)
158     : Clustering_(Clustering), InstrInfo_(std::move(InstrInfo)),
159       AnalysisInconsistencyEpsilonSquared_(AnalysisInconsistencyEpsilon *
160                                            AnalysisInconsistencyEpsilon),
161       AnalysisDisplayUnstableOpcodes_(AnalysisDisplayUnstableOpcodes) {
162   if (Clustering.getPoints().empty())
163     return;
164 
165   const InstructionBenchmark &FirstPoint = Clustering.getPoints().front();
166   RegInfo_.reset(Target.createMCRegInfo(FirstPoint.LLVMTriple));
167   MCTargetOptions MCOptions;
168   AsmInfo_.reset(
169       Target.createMCAsmInfo(*RegInfo_, FirstPoint.LLVMTriple, MCOptions));
170   SubtargetInfo_.reset(Target.createMCSubtargetInfo(FirstPoint.LLVMTriple,
171                                                     FirstPoint.CpuName, ""));
172   InstPrinter_.reset(Target.createMCInstPrinter(
173       Triple(FirstPoint.LLVMTriple), 0 /*default variant*/, *AsmInfo_,
174       *InstrInfo_, *RegInfo_));
175 
176   Context_ = std::make_unique<MCContext>(AsmInfo_.get(), RegInfo_.get(),
177                                          &ObjectFileInfo_);
178   Disasm_.reset(Target.createMCDisassembler(*SubtargetInfo_, *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   const auto &Clusters = Clustering_.getValidClusters();
199   for (size_t I = 0, E = Clusters.size(); I < E; ++I) {
200     for (const size_t PointId : Clusters[I].PointIndices) {
201       printInstructionRowCsv(PointId, OS);
202     }
203     OS << "\n\n";
204   }
205   return Error::success();
206 }
207 
ResolvedSchedClassAndPoints(ResolvedSchedClass && RSC)208 Analysis::ResolvedSchedClassAndPoints::ResolvedSchedClassAndPoints(
209     ResolvedSchedClass &&RSC)
210     : RSC(std::move(RSC)) {}
211 
212 std::vector<Analysis::ResolvedSchedClassAndPoints>
makePointsPerSchedClass() const213 Analysis::makePointsPerSchedClass() const {
214   std::vector<ResolvedSchedClassAndPoints> Entries;
215   // Maps SchedClassIds to index in result.
216   std::unordered_map<unsigned, size_t> SchedClassIdToIndex;
217   const auto &Points = Clustering_.getPoints();
218   for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) {
219     const InstructionBenchmark &Point = Points[PointId];
220     if (!Point.Error.empty())
221       continue;
222     assert(!Point.Key.Instructions.empty());
223     // FIXME: we should be using the tuple of classes for instructions in the
224     // snippet as key.
225     const MCInst &MCI = Point.keyInstruction();
226     unsigned SchedClassId;
227     bool WasVariant;
228     std::tie(SchedClassId, WasVariant) =
229         ResolvedSchedClass::resolveSchedClassId(*SubtargetInfo_, *InstrInfo_,
230                                                 MCI);
231     const auto IndexIt = SchedClassIdToIndex.find(SchedClassId);
232     if (IndexIt == SchedClassIdToIndex.end()) {
233       // Create a new entry.
234       SchedClassIdToIndex.emplace(SchedClassId, Entries.size());
235       ResolvedSchedClassAndPoints Entry(
236           ResolvedSchedClass(*SubtargetInfo_, SchedClassId, WasVariant));
237       Entry.PointIds.push_back(PointId);
238       Entries.push_back(std::move(Entry));
239     } else {
240       // Append to the existing entry.
241       Entries[IndexIt->second].PointIds.push_back(PointId);
242     }
243   }
244   return Entries;
245 }
246 
247 // Parallel benchmarks repeat the same opcode multiple times. Just show this
248 // opcode and show the whole snippet only on hover.
writeParallelSnippetHtml(raw_ostream & OS,const std::vector<MCInst> & Instructions,const MCInstrInfo & InstrInfo)249 static void writeParallelSnippetHtml(raw_ostream &OS,
250                                  const std::vector<MCInst> &Instructions,
251                                  const MCInstrInfo &InstrInfo) {
252   if (Instructions.empty())
253     return;
254   writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instructions[0].getOpcode()));
255   if (Instructions.size() > 1)
256     OS << " (x" << Instructions.size() << ")";
257 }
258 
259 // Latency tries to find a serial path. Just show the opcode path and show the
260 // whole snippet only on hover.
writeLatencySnippetHtml(raw_ostream & OS,const std::vector<MCInst> & Instructions,const MCInstrInfo & InstrInfo)261 static void writeLatencySnippetHtml(raw_ostream &OS,
262                                     const std::vector<MCInst> &Instructions,
263                                     const MCInstrInfo &InstrInfo) {
264   bool First = true;
265   for (const MCInst &Instr : Instructions) {
266     if (First)
267       First = false;
268     else
269       OS << " &rarr; ";
270     writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instr.getOpcode()));
271   }
272 }
273 
printPointHtml(const InstructionBenchmark & Point,llvm::raw_ostream & OS) const274 void Analysis::printPointHtml(const InstructionBenchmark &Point,
275                               llvm::raw_ostream &OS) const {
276   OS << "<li><span class=\"mono\" title=\"";
277   writeSnippet<EscapeTag, kEscapeHtmlString>(OS, Point.AssembledSnippet, "\n");
278   OS << "\">";
279   switch (Point.Mode) {
280   case InstructionBenchmark::Latency:
281     writeLatencySnippetHtml(OS, Point.Key.Instructions, *InstrInfo_);
282     break;
283   case InstructionBenchmark::Uops:
284   case InstructionBenchmark::InverseThroughput:
285     writeParallelSnippetHtml(OS, Point.Key.Instructions, *InstrInfo_);
286     break;
287   default:
288     llvm_unreachable("invalid mode");
289   }
290   OS << "</span> <span class=\"mono\">";
291   writeEscaped<kEscapeHtml>(OS, Point.Key.Config);
292   OS << "</span></li>";
293 }
294 
printSchedClassClustersHtml(const std::vector<SchedClassCluster> & Clusters,const ResolvedSchedClass & RSC,raw_ostream & OS) const295 void Analysis::printSchedClassClustersHtml(
296     const std::vector<SchedClassCluster> &Clusters,
297     const ResolvedSchedClass &RSC, raw_ostream &OS) const {
298   const auto &Points = Clustering_.getPoints();
299   OS << "<table class=\"sched-class-clusters\">";
300   OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>";
301   assert(!Clusters.empty());
302   for (const auto &Measurement :
303        Points[Clusters[0].getPointIds()[0]].Measurements) {
304     OS << "<th>";
305     writeEscaped<kEscapeHtml>(OS, Measurement.Key);
306     OS << "</th>";
307   }
308   OS << "</tr>";
309   for (const SchedClassCluster &Cluster : Clusters) {
310     OS << "<tr class=\""
311        << (Cluster.measurementsMatch(*SubtargetInfo_, RSC, 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 &SM = SubtargetInfo_->getSchedModel();
381     OS << "<tr><td>&#10004;</td>";
382     OS << "<td>" << (RSC.WasVariant ? "&#10004;" : "&#10005;") << "</td>";
383     OS << "<td>" << RSC.SCDesc->NumMicroOps << "</td>";
384     // Latencies.
385     OS << "<td><ul>";
386     for (int I = 0, E = RSC.SCDesc->NumWriteLatencyEntries; I < E; ++I) {
387       const auto *const Entry =
388           SubtargetInfo_->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,
401         MCSchedModel::getReciprocalThroughput(*SubtargetInfo_, *RSC.SCDesc));
402     OS << "</td>";
403     // WriteProcRes.
404     OS << "<td><ul>";
405     for (const auto &WPR : RSC.NonRedundantWriteProcRes) {
406       OS << "<li><span class=\"mono\">";
407       writeEscaped<kEscapeHtml>(OS,
408                                 SM.getProcResource(WPR.ProcResourceIdx)->Name);
409       OS << "</span>: " << WPR.Cycles << "</li>";
410     }
411     OS << "</ul></td>";
412     // Idealized port pressure.
413     OS << "<td><ul>";
414     for (const auto &Pressure : RSC.IdealizedProcResPressure) {
415       OS << "<li><span class=\"mono\">";
416       writeEscaped<kEscapeHtml>(OS, SubtargetInfo_->getSchedModel()
417                                         .getProcResource(Pressure.first)
418                                         ->Name);
419       OS << "</span>: ";
420       writeMeasurementValue<kEscapeHtml>(OS, Pressure.second);
421       OS << "</li>";
422     }
423     OS << "</ul></td>";
424     OS << "</tr>";
425   } else {
426     OS << "<tr><td>&#10005;</td><td></td><td></td></tr>";
427   }
428   OS << "</table>";
429 }
430 
printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId & Id,StringRef display_name,llvm::raw_ostream & OS) const431 void Analysis::printClusterRawHtml(
432     const InstructionBenchmarkClustering::ClusterId &Id, StringRef display_name,
433     llvm::raw_ostream &OS) const {
434   const auto &Points = Clustering_.getPoints();
435   const auto &Cluster = Clustering_.getCluster(Id);
436   if (Cluster.PointIndices.empty())
437     return;
438 
439   OS << "<div class=\"inconsistency\"><p>" << display_name << " Cluster ("
440      << Cluster.PointIndices.size() << " points)</p>";
441   OS << "<table class=\"sched-class-clusters\">";
442   // Table Header.
443   OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>";
444   for (const auto &Measurement : Points[Cluster.PointIndices[0]].Measurements) {
445     OS << "<th>";
446     writeEscaped<kEscapeHtml>(OS, Measurement.Key);
447     OS << "</th>";
448   }
449   OS << "</tr>";
450 
451   // Point data.
452   for (const auto &PointId : Cluster.PointIndices) {
453     OS << "<tr class=\"bad-cluster\"><td>" << display_name << "</td><td><ul>";
454     printPointHtml(Points[PointId], OS);
455     OS << "</ul></td>";
456     for (const auto &Measurement : Points[PointId].Measurements) {
457       OS << "<td class=\"measurement\">";
458       writeMeasurementValue<kEscapeHtml>(OS, Measurement.PerInstructionValue);
459     }
460     OS << "</tr>";
461   }
462   OS << "</table>";
463 
464   OS << "</div>";
465 
466 } // namespace exegesis
467 
468 static constexpr const char kHtmlHead[] = R"(
469 <head>
470 <title>llvm-exegesis Analysis Results</title>
471 <style>
472 body {
473   font-family: sans-serif
474 }
475 span.sched-class-name {
476   font-weight: bold;
477   font-family: monospace;
478 }
479 span.opcode {
480   font-family: monospace;
481 }
482 span.config {
483   font-family: monospace;
484 }
485 div.inconsistency {
486   margin-top: 50px;
487 }
488 table {
489   margin-left: 50px;
490   border-collapse: collapse;
491 }
492 table, table tr,td,th {
493   border: 1px solid #444;
494 }
495 table ul {
496   padding-left: 0px;
497   margin: 0px;
498   list-style-type: none;
499 }
500 table.sched-class-clusters td {
501   padding-left: 10px;
502   padding-right: 10px;
503   padding-top: 10px;
504   padding-bottom: 10px;
505 }
506 table.sched-class-desc td {
507   padding-left: 10px;
508   padding-right: 10px;
509   padding-top: 2px;
510   padding-bottom: 2px;
511 }
512 span.mono {
513   font-family: monospace;
514 }
515 td.measurement {
516   text-align: center;
517 }
518 tr.good-cluster td.measurement {
519   color: #292
520 }
521 tr.bad-cluster td.measurement {
522   color: #922
523 }
524 tr.good-cluster td.measurement span.minmax {
525   color: #888;
526 }
527 tr.bad-cluster td.measurement span.minmax {
528   color: #888;
529 }
530 </style>
531 </head>
532 )";
533 
534 template <>
run(raw_ostream & OS) const535 Error Analysis::run<Analysis::PrintSchedClassInconsistencies>(
536     raw_ostream &OS) const {
537   const auto &FirstPoint = Clustering_.getPoints()[0];
538   // Print the header.
539   OS << "<!DOCTYPE html><html>" << kHtmlHead << "<body>";
540   OS << "<h1><span class=\"mono\">llvm-exegesis</span> Analysis Results</h1>";
541   OS << "<h3>Triple: <span class=\"mono\">";
542   writeEscaped<kEscapeHtml>(OS, FirstPoint.LLVMTriple);
543   OS << "</span></h3><h3>Cpu: <span class=\"mono\">";
544   writeEscaped<kEscapeHtml>(OS, FirstPoint.CpuName);
545   OS << "</span></h3>";
546 
547   for (const auto &RSCAndPoints : makePointsPerSchedClass()) {
548     if (!RSCAndPoints.RSC.SCDesc)
549       continue;
550     // Bucket sched class points into sched class clusters.
551     std::vector<SchedClassCluster> SchedClassClusters;
552     for (const size_t PointId : RSCAndPoints.PointIds) {
553       const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId);
554       if (!ClusterId.isValid())
555         continue; // Ignore noise and errors. FIXME: take noise into account ?
556       if (ClusterId.isUnstable() ^ AnalysisDisplayUnstableOpcodes_)
557         continue; // Either display stable or unstable clusters only.
558       auto SchedClassClusterIt = llvm::find_if(
559           SchedClassClusters, [ClusterId](const SchedClassCluster &C) {
560             return C.id() == ClusterId;
561           });
562       if (SchedClassClusterIt == SchedClassClusters.end()) {
563         SchedClassClusters.emplace_back();
564         SchedClassClusterIt = std::prev(SchedClassClusters.end());
565       }
566       SchedClassClusterIt->addPoint(PointId, Clustering_);
567     }
568 
569     // Print any scheduling class that has at least one cluster that does not
570     // match the checked-in data.
571     if (all_of(SchedClassClusters, [this,
572                                     &RSCAndPoints](const SchedClassCluster &C) {
573           return C.measurementsMatch(*SubtargetInfo_, RSCAndPoints.RSC,
574                                      Clustering_,
575                                      AnalysisInconsistencyEpsilonSquared_);
576         }))
577       continue; // Nothing weird.
578 
579     OS << "<div class=\"inconsistency\"><p>Sched Class <span "
580           "class=\"sched-class-name\">";
581 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
582     writeEscaped<kEscapeHtml>(OS, RSCAndPoints.RSC.SCDesc->Name);
583 #else
584     OS << RSCAndPoints.RSC.SchedClassId;
585 #endif
586     OS << "</span> contains instructions whose performance characteristics do"
587           " not match that of LLVM:</p>";
588     printSchedClassClustersHtml(SchedClassClusters, RSCAndPoints.RSC, OS);
589     OS << "<p>llvm SchedModel data:</p>";
590     printSchedClassDescHtml(RSCAndPoints.RSC, OS);
591     OS << "</div>";
592   }
593 
594   printClusterRawHtml(InstructionBenchmarkClustering::ClusterId::noise(),
595                       "[noise]", OS);
596 
597   OS << "</body></html>";
598   return Error::success();
599 }
600 
601 } // namespace exegesis
602 } // namespace llvm
603