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(InstPrinterStr.str().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<MCSubtargetInfo> SubtargetInfo,std::unique_ptr<MCInstrInfo> InstrInfo,const InstructionBenchmarkClustering & Clustering,double AnalysisInconsistencyEpsilon,bool AnalysisDisplayUnstableOpcodes,const std::string & ForceCpuName)154 Analysis::Analysis(const Target &Target,
155                    std::unique_ptr<MCSubtargetInfo> SubtargetInfo,
156                    std::unique_ptr<MCInstrInfo> InstrInfo,
157                    const InstructionBenchmarkClustering &Clustering,
158                    double AnalysisInconsistencyEpsilon,
159                    bool AnalysisDisplayUnstableOpcodes,
160                    const std::string &ForceCpuName)
161     : Clustering_(Clustering), SubtargetInfo_(std::move(SubtargetInfo)),
162       InstrInfo_(std::move(InstrInfo)),
163       AnalysisInconsistencyEpsilonSquared_(AnalysisInconsistencyEpsilon *
164                                            AnalysisInconsistencyEpsilon),
165       AnalysisDisplayUnstableOpcodes_(AnalysisDisplayUnstableOpcodes) {
166   if (Clustering.getPoints().empty())
167     return;
168 
169   const InstructionBenchmark &FirstPoint = Clustering.getPoints().front();
170   const std::string CpuName =
171       ForceCpuName.empty() ? FirstPoint.CpuName : ForceCpuName;
172   RegInfo_.reset(Target.createMCRegInfo(FirstPoint.LLVMTriple));
173   MCTargetOptions MCOptions;
174   AsmInfo_.reset(
175       Target.createMCAsmInfo(*RegInfo_, FirstPoint.LLVMTriple, MCOptions));
176   SubtargetInfo_.reset(
177       Target.createMCSubtargetInfo(FirstPoint.LLVMTriple, CpuName, ""));
178   InstPrinter_.reset(Target.createMCInstPrinter(
179       Triple(FirstPoint.LLVMTriple), 0 /*default variant*/, *AsmInfo_,
180       *InstrInfo_, *RegInfo_));
181 
182   Context_ =
183       std::make_unique<MCContext>(Triple(FirstPoint.LLVMTriple), AsmInfo_.get(),
184                                   RegInfo_.get(), SubtargetInfo_.get());
185   Disasm_.reset(Target.createMCDisassembler(*SubtargetInfo_, *Context_));
186   assert(Disasm_ && "cannot create MCDisassembler. missing call to "
187                     "InitializeXXXTargetDisassembler ?");
188 }
189 
190 template <>
run(raw_ostream & OS) const191 Error Analysis::run<Analysis::PrintClusters>(raw_ostream &OS) const {
192   if (Clustering_.getPoints().empty())
193     return Error::success();
194 
195   // Write the header.
196   OS << "cluster_id" << kCsvSep << "opcode_name" << kCsvSep << "config"
197      << kCsvSep << "sched_class";
198   for (const auto &Measurement : Clustering_.getPoints().front().Measurements) {
199     OS << kCsvSep;
200     writeEscaped<kEscapeCsv>(OS, Measurement.Key);
201   }
202   OS << "\n";
203 
204   // Write the points.
205   for (const auto &ClusterIt : Clustering_.getValidClusters()) {
206     for (const size_t PointId : ClusterIt.PointIndices) {
207       printInstructionRowCsv(PointId, OS);
208     }
209     OS << "\n\n";
210   }
211   return Error::success();
212 }
213 
ResolvedSchedClassAndPoints(ResolvedSchedClass && RSC)214 Analysis::ResolvedSchedClassAndPoints::ResolvedSchedClassAndPoints(
215     ResolvedSchedClass &&RSC)
216     : RSC(std::move(RSC)) {}
217 
218 std::vector<Analysis::ResolvedSchedClassAndPoints>
makePointsPerSchedClass() const219 Analysis::makePointsPerSchedClass() const {
220   std::vector<ResolvedSchedClassAndPoints> Entries;
221   // Maps SchedClassIds to index in result.
222   std::unordered_map<unsigned, size_t> SchedClassIdToIndex;
223   const auto &Points = Clustering_.getPoints();
224   for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) {
225     const InstructionBenchmark &Point = Points[PointId];
226     if (!Point.Error.empty())
227       continue;
228     assert(!Point.Key.Instructions.empty());
229     // FIXME: we should be using the tuple of classes for instructions in the
230     // snippet as key.
231     const MCInst &MCI = Point.keyInstruction();
232     unsigned SchedClassId;
233     bool WasVariant;
234     std::tie(SchedClassId, WasVariant) =
235         ResolvedSchedClass::resolveSchedClassId(*SubtargetInfo_, *InstrInfo_,
236                                                 MCI);
237     const auto IndexIt = SchedClassIdToIndex.find(SchedClassId);
238     if (IndexIt == SchedClassIdToIndex.end()) {
239       // Create a new entry.
240       SchedClassIdToIndex.emplace(SchedClassId, Entries.size());
241       ResolvedSchedClassAndPoints Entry(
242           ResolvedSchedClass(*SubtargetInfo_, SchedClassId, WasVariant));
243       Entry.PointIds.push_back(PointId);
244       Entries.push_back(std::move(Entry));
245     } else {
246       // Append to the existing entry.
247       Entries[IndexIt->second].PointIds.push_back(PointId);
248     }
249   }
250   return Entries;
251 }
252 
253 // Parallel benchmarks repeat the same opcode multiple times. Just show this
254 // opcode and show the whole snippet only on hover.
writeParallelSnippetHtml(raw_ostream & OS,const std::vector<MCInst> & Instructions,const MCInstrInfo & InstrInfo)255 static void writeParallelSnippetHtml(raw_ostream &OS,
256                                  const std::vector<MCInst> &Instructions,
257                                  const MCInstrInfo &InstrInfo) {
258   if (Instructions.empty())
259     return;
260   writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instructions[0].getOpcode()));
261   if (Instructions.size() > 1)
262     OS << " (x" << Instructions.size() << ")";
263 }
264 
265 // Latency tries to find a serial path. Just show the opcode path and show the
266 // whole snippet only on hover.
writeLatencySnippetHtml(raw_ostream & OS,const std::vector<MCInst> & Instructions,const MCInstrInfo & InstrInfo)267 static void writeLatencySnippetHtml(raw_ostream &OS,
268                                     const std::vector<MCInst> &Instructions,
269                                     const MCInstrInfo &InstrInfo) {
270   bool First = true;
271   for (const MCInst &Instr : Instructions) {
272     if (First)
273       First = false;
274     else
275       OS << " &rarr; ";
276     writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instr.getOpcode()));
277   }
278 }
279 
printPointHtml(const InstructionBenchmark & Point,llvm::raw_ostream & OS) const280 void Analysis::printPointHtml(const InstructionBenchmark &Point,
281                               llvm::raw_ostream &OS) const {
282   OS << "<li><span class=\"mono\" title=\"";
283   writeSnippet<EscapeTag, kEscapeHtmlString>(OS, Point.AssembledSnippet, "\n");
284   OS << "\">";
285   switch (Point.Mode) {
286   case InstructionBenchmark::Latency:
287     writeLatencySnippetHtml(OS, Point.Key.Instructions, *InstrInfo_);
288     break;
289   case InstructionBenchmark::Uops:
290   case InstructionBenchmark::InverseThroughput:
291     writeParallelSnippetHtml(OS, Point.Key.Instructions, *InstrInfo_);
292     break;
293   default:
294     llvm_unreachable("invalid mode");
295   }
296   OS << "</span> <span class=\"mono\">";
297   writeEscaped<kEscapeHtml>(OS, Point.Key.Config);
298   OS << "</span></li>";
299 }
300 
printSchedClassClustersHtml(const std::vector<SchedClassCluster> & Clusters,const ResolvedSchedClass & RSC,raw_ostream & OS) const301 void Analysis::printSchedClassClustersHtml(
302     const std::vector<SchedClassCluster> &Clusters,
303     const ResolvedSchedClass &RSC, raw_ostream &OS) const {
304   const auto &Points = Clustering_.getPoints();
305   OS << "<table class=\"sched-class-clusters\">";
306   OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>";
307   assert(!Clusters.empty());
308   for (const auto &Measurement :
309        Points[Clusters[0].getPointIds()[0]].Measurements) {
310     OS << "<th>";
311     writeEscaped<kEscapeHtml>(OS, Measurement.Key);
312     OS << "</th>";
313   }
314   OS << "</tr>";
315   for (const SchedClassCluster &Cluster : Clusters) {
316     OS << "<tr class=\""
317        << (Cluster.measurementsMatch(*SubtargetInfo_, RSC, Clustering_,
318                                      AnalysisInconsistencyEpsilonSquared_)
319                ? "good-cluster"
320                : "bad-cluster")
321        << "\"><td>";
322     writeClusterId<kEscapeHtml>(OS, Cluster.id());
323     OS << "</td><td><ul>";
324     for (const size_t PointId : Cluster.getPointIds()) {
325       printPointHtml(Points[PointId], OS);
326     }
327     OS << "</ul></td>";
328     for (const auto &Stats : Cluster.getCentroid().getStats()) {
329       OS << "<td class=\"measurement\">";
330       writeMeasurementValue<kEscapeHtml>(OS, Stats.avg());
331       OS << "<br><span class=\"minmax\">[";
332       writeMeasurementValue<kEscapeHtml>(OS, Stats.min());
333       OS << ";";
334       writeMeasurementValue<kEscapeHtml>(OS, Stats.max());
335       OS << "]</span></td>";
336     }
337     OS << "</tr>";
338   }
339   OS << "</table>";
340 }
341 
addPoint(size_t PointId,const InstructionBenchmarkClustering & Clustering)342 void Analysis::SchedClassCluster::addPoint(
343     size_t PointId, const InstructionBenchmarkClustering &Clustering) {
344   PointIds.push_back(PointId);
345   const auto &Point = Clustering.getPoints()[PointId];
346   if (ClusterId.isUndef())
347     ClusterId = Clustering.getClusterIdForPoint(PointId);
348   assert(ClusterId == Clustering.getClusterIdForPoint(PointId));
349 
350   Centroid.addPoint(Point.Measurements);
351 }
352 
measurementsMatch(const MCSubtargetInfo & STI,const ResolvedSchedClass & RSC,const InstructionBenchmarkClustering & Clustering,const double AnalysisInconsistencyEpsilonSquared_) const353 bool Analysis::SchedClassCluster::measurementsMatch(
354     const MCSubtargetInfo &STI, const ResolvedSchedClass &RSC,
355     const InstructionBenchmarkClustering &Clustering,
356     const double AnalysisInconsistencyEpsilonSquared_) const {
357   assert(!Clustering.getPoints().empty());
358   const InstructionBenchmark::ModeE Mode = Clustering.getPoints()[0].Mode;
359 
360   if (!Centroid.validate(Mode))
361     return false;
362 
363   const std::vector<BenchmarkMeasure> ClusterCenterPoint =
364       Centroid.getAsPoint();
365 
366   const std::vector<BenchmarkMeasure> SchedClassPoint =
367       RSC.getAsPoint(Mode, STI, Centroid.getStats());
368   if (SchedClassPoint.empty())
369     return false; // In Uops mode validate() may not be enough.
370 
371   assert(ClusterCenterPoint.size() == SchedClassPoint.size() &&
372          "Expected measured/sched data dimensions to match.");
373 
374   return Clustering.isNeighbour(ClusterCenterPoint, SchedClassPoint,
375                                 AnalysisInconsistencyEpsilonSquared_);
376 }
377 
printSchedClassDescHtml(const ResolvedSchedClass & RSC,raw_ostream & OS) const378 void Analysis::printSchedClassDescHtml(const ResolvedSchedClass &RSC,
379                                        raw_ostream &OS) const {
380   OS << "<table class=\"sched-class-desc\">";
381   OS << "<tr><th>Valid</th><th>Variant</th><th>NumMicroOps</th><th>Latency</"
382         "th><th>RThroughput</th><th>WriteProcRes</th><th title=\"This is the "
383         "idealized unit resource (port) pressure assuming ideal "
384         "distribution\">Idealized Resource Pressure</th></tr>";
385   if (RSC.SCDesc->isValid()) {
386     const auto &SM = SubtargetInfo_->getSchedModel();
387     OS << "<tr><td>&#10004;</td>";
388     OS << "<td>" << (RSC.WasVariant ? "&#10004;" : "&#10005;") << "</td>";
389     OS << "<td>" << RSC.SCDesc->NumMicroOps << "</td>";
390     // Latencies.
391     OS << "<td><ul>";
392     for (int I = 0, E = RSC.SCDesc->NumWriteLatencyEntries; I < E; ++I) {
393       const auto *const Entry =
394           SubtargetInfo_->getWriteLatencyEntry(RSC.SCDesc, I);
395       OS << "<li>" << Entry->Cycles;
396       if (RSC.SCDesc->NumWriteLatencyEntries > 1) {
397         // Dismabiguate if more than 1 latency.
398         OS << " (WriteResourceID " << Entry->WriteResourceID << ")";
399       }
400       OS << "</li>";
401     }
402     OS << "</ul></td>";
403     // inverse throughput.
404     OS << "<td>";
405     writeMeasurementValue<kEscapeHtml>(
406         OS,
407         MCSchedModel::getReciprocalThroughput(*SubtargetInfo_, *RSC.SCDesc));
408     OS << "</td>";
409     // WriteProcRes.
410     OS << "<td><ul>";
411     for (const auto &WPR : RSC.NonRedundantWriteProcRes) {
412       OS << "<li><span class=\"mono\">";
413       writeEscaped<kEscapeHtml>(OS,
414                                 SM.getProcResource(WPR.ProcResourceIdx)->Name);
415       OS << "</span>: " << WPR.Cycles << "</li>";
416     }
417     OS << "</ul></td>";
418     // Idealized port pressure.
419     OS << "<td><ul>";
420     for (const auto &Pressure : RSC.IdealizedProcResPressure) {
421       OS << "<li><span class=\"mono\">";
422       writeEscaped<kEscapeHtml>(OS, SubtargetInfo_->getSchedModel()
423                                         .getProcResource(Pressure.first)
424                                         ->Name);
425       OS << "</span>: ";
426       writeMeasurementValue<kEscapeHtml>(OS, Pressure.second);
427       OS << "</li>";
428     }
429     OS << "</ul></td>";
430     OS << "</tr>";
431   } else {
432     OS << "<tr><td>&#10005;</td><td></td><td></td></tr>";
433   }
434   OS << "</table>";
435 }
436 
printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId & Id,StringRef display_name,llvm::raw_ostream & OS) const437 void Analysis::printClusterRawHtml(
438     const InstructionBenchmarkClustering::ClusterId &Id, StringRef display_name,
439     llvm::raw_ostream &OS) const {
440   const auto &Points = Clustering_.getPoints();
441   const auto &Cluster = Clustering_.getCluster(Id);
442   if (Cluster.PointIndices.empty())
443     return;
444 
445   OS << "<div class=\"inconsistency\"><p>" << display_name << " Cluster ("
446      << Cluster.PointIndices.size() << " points)</p>";
447   OS << "<table class=\"sched-class-clusters\">";
448   // Table Header.
449   OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>";
450   for (const auto &Measurement : Points[Cluster.PointIndices[0]].Measurements) {
451     OS << "<th>";
452     writeEscaped<kEscapeHtml>(OS, Measurement.Key);
453     OS << "</th>";
454   }
455   OS << "</tr>";
456 
457   // Point data.
458   for (const auto &PointId : Cluster.PointIndices) {
459     OS << "<tr class=\"bad-cluster\"><td>" << display_name << "</td><td><ul>";
460     printPointHtml(Points[PointId], OS);
461     OS << "</ul></td>";
462     for (const auto &Measurement : Points[PointId].Measurements) {
463       OS << "<td class=\"measurement\">";
464       writeMeasurementValue<kEscapeHtml>(OS, Measurement.PerInstructionValue);
465     }
466     OS << "</tr>";
467   }
468   OS << "</table>";
469 
470   OS << "</div>";
471 
472 } // namespace exegesis
473 
474 static constexpr const char kHtmlHead[] = R"(
475 <head>
476 <title>llvm-exegesis Analysis Results</title>
477 <style>
478 body {
479   font-family: sans-serif
480 }
481 span.sched-class-name {
482   font-weight: bold;
483   font-family: monospace;
484 }
485 span.opcode {
486   font-family: monospace;
487 }
488 span.config {
489   font-family: monospace;
490 }
491 div.inconsistency {
492   margin-top: 50px;
493 }
494 table {
495   margin-left: 50px;
496   border-collapse: collapse;
497 }
498 table, table tr,td,th {
499   border: 1px solid #444;
500 }
501 table ul {
502   padding-left: 0px;
503   margin: 0px;
504   list-style-type: none;
505 }
506 table.sched-class-clusters td {
507   padding-left: 10px;
508   padding-right: 10px;
509   padding-top: 10px;
510   padding-bottom: 10px;
511 }
512 table.sched-class-desc td {
513   padding-left: 10px;
514   padding-right: 10px;
515   padding-top: 2px;
516   padding-bottom: 2px;
517 }
518 span.mono {
519   font-family: monospace;
520 }
521 td.measurement {
522   text-align: center;
523 }
524 tr.good-cluster td.measurement {
525   color: #292
526 }
527 tr.bad-cluster td.measurement {
528   color: #922
529 }
530 tr.good-cluster td.measurement span.minmax {
531   color: #888;
532 }
533 tr.bad-cluster td.measurement span.minmax {
534   color: #888;
535 }
536 </style>
537 </head>
538 )";
539 
540 template <>
run(raw_ostream & OS) const541 Error Analysis::run<Analysis::PrintSchedClassInconsistencies>(
542     raw_ostream &OS) const {
543   const auto &FirstPoint = Clustering_.getPoints()[0];
544   // Print the header.
545   OS << "<!DOCTYPE html><html>" << kHtmlHead << "<body>";
546   OS << "<h1><span class=\"mono\">llvm-exegesis</span> Analysis Results</h1>";
547   OS << "<h3>Triple: <span class=\"mono\">";
548   writeEscaped<kEscapeHtml>(OS, FirstPoint.LLVMTriple);
549   OS << "</span></h3><h3>Cpu: <span class=\"mono\">";
550   writeEscaped<kEscapeHtml>(OS, FirstPoint.CpuName);
551   OS << "</span></h3>";
552 
553   for (const auto &RSCAndPoints : makePointsPerSchedClass()) {
554     if (!RSCAndPoints.RSC.SCDesc)
555       continue;
556     // Bucket sched class points into sched class clusters.
557     std::vector<SchedClassCluster> SchedClassClusters;
558     for (const size_t PointId : RSCAndPoints.PointIds) {
559       const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId);
560       if (!ClusterId.isValid())
561         continue; // Ignore noise and errors. FIXME: take noise into account ?
562       if (ClusterId.isUnstable() ^ AnalysisDisplayUnstableOpcodes_)
563         continue; // Either display stable or unstable clusters only.
564       auto SchedClassClusterIt = llvm::find_if(
565           SchedClassClusters, [ClusterId](const SchedClassCluster &C) {
566             return C.id() == ClusterId;
567           });
568       if (SchedClassClusterIt == SchedClassClusters.end()) {
569         SchedClassClusters.emplace_back();
570         SchedClassClusterIt = std::prev(SchedClassClusters.end());
571       }
572       SchedClassClusterIt->addPoint(PointId, Clustering_);
573     }
574 
575     // Print any scheduling class that has at least one cluster that does not
576     // match the checked-in data.
577     if (all_of(SchedClassClusters, [this,
578                                     &RSCAndPoints](const SchedClassCluster &C) {
579           return C.measurementsMatch(*SubtargetInfo_, RSCAndPoints.RSC,
580                                      Clustering_,
581                                      AnalysisInconsistencyEpsilonSquared_);
582         }))
583       continue; // Nothing weird.
584 
585     OS << "<div class=\"inconsistency\"><p>Sched Class <span "
586           "class=\"sched-class-name\">";
587 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
588     writeEscaped<kEscapeHtml>(OS, RSCAndPoints.RSC.SCDesc->Name);
589 #else
590     OS << RSCAndPoints.RSC.SchedClassId;
591 #endif
592     OS << "</span> contains instructions whose performance characteristics do"
593           " not match that of LLVM:</p>";
594     printSchedClassClustersHtml(SchedClassClusters, RSCAndPoints.RSC, OS);
595     OS << "<p>llvm SchedModel data:</p>";
596     printSchedClassDescHtml(RSCAndPoints.RSC, OS);
597     OS << "</div>";
598   }
599 
600   printClusterRawHtml(InstructionBenchmarkClustering::ClusterId::noise(),
601                       "[noise]", OS);
602 
603   OS << "</body></html>";
604   return Error::success();
605 }
606 
607 } // namespace exegesis
608 } // namespace llvm
609