Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 1 | //===-- 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 Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 10 | #include "Analysis.h" |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 11 | #include "BenchmarkResult.h" |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 14 | #include "llvm/Support/FormatVariadic.h" |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 15 | #include <unordered_set> |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
| 18 | namespace exegesis { |
| 19 | |
| 20 | static const char kCsvSep = ','; |
| 21 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 24 | enum EscapeTag { kEscapeCsv, kEscapeHtml, kEscapeHtmlString }; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 25 | |
| 26 | template <EscapeTag Tag> |
| 27 | void writeEscaped(llvm::raw_ostream &OS, const llvm::StringRef S); |
| 28 | |
| 29 | template <> |
| 30 | void writeEscaped<kEscapeCsv>(llvm::raw_ostream &OS, const llvm::StringRef S) { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 31 | if (std::find(S.begin(), S.end(), kCsvSep) == S.end()) { |
| 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 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 46 | template <> |
| 47 | void writeEscaped<kEscapeHtml>(llvm::raw_ostream &OS, const llvm::StringRef S) { |
| 48 | for (const char C : S) { |
| 49 | if (C == '<') |
| 50 | OS << "<"; |
| 51 | else if (C == '>') |
| 52 | OS << ">"; |
| 53 | else if (C == '&') |
| 54 | OS << "&"; |
| 55 | else |
| 56 | OS << C; |
| 57 | } |
| 58 | } |
| 59 | |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 60 | template <> |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 61 | void writeEscaped<kEscapeHtmlString>(llvm::raw_ostream &OS, |
| 62 | const llvm::StringRef S) { |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 63 | for (const char C : S) { |
| 64 | if (C == '"') |
| 65 | OS << "\\\""; |
| 66 | else |
| 67 | OS << C; |
| 68 | } |
| 69 | } |
| 70 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 71 | } // namespace |
| 72 | |
| 73 | template <EscapeTag Tag> |
| 74 | static void |
| 75 | writeClusterId(llvm::raw_ostream &OS, |
| 76 | const InstructionBenchmarkClustering::ClusterId &CID) { |
| 77 | if (CID.isNoise()) |
| 78 | writeEscaped<Tag>(OS, "[noise]"); |
| 79 | else if (CID.isError()) |
| 80 | writeEscaped<Tag>(OS, "[error]"); |
| 81 | else |
| 82 | OS << CID.getId(); |
| 83 | } |
| 84 | |
| 85 | template <EscapeTag Tag> |
| 86 | static void writeMeasurementValue(llvm::raw_ostream &OS, const double Value) { |
| 87 | writeEscaped<Tag>(OS, llvm::formatv("{0:F}", Value).str()); |
| 88 | } |
| 89 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 90 | template <typename EscapeTag, EscapeTag Tag> |
| 91 | void Analysis::writeSnippet(llvm::raw_ostream &OS, |
| 92 | llvm::ArrayRef<uint8_t> Bytes, |
| 93 | const char *Separator) const { |
| 94 | llvm::SmallVector<std::string, 3> Lines; |
| 95 | // Parse the asm snippet and print it. |
| 96 | while (!Bytes.empty()) { |
| 97 | llvm::MCInst MI; |
| 98 | uint64_t MISize = 0; |
| 99 | if (!Disasm_->getInstruction(MI, MISize, Bytes, 0, llvm::nulls(), |
| 100 | llvm::nulls())) { |
| 101 | writeEscaped<Tag>(OS, llvm::join(Lines, Separator)); |
| 102 | writeEscaped<Tag>(OS, Separator); |
| 103 | writeEscaped<Tag>(OS, "[error decoding asm snippet]"); |
| 104 | return; |
| 105 | } |
| 106 | Lines.emplace_back(); |
| 107 | std::string &Line = Lines.back(); |
| 108 | llvm::raw_string_ostream OSS(Line); |
| 109 | InstPrinter_->printInst(&MI, OSS, "", *SubtargetInfo_); |
| 110 | Bytes = Bytes.drop_front(MISize); |
| 111 | OSS.flush(); |
| 112 | Line = llvm::StringRef(Line).trim().str(); |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 113 | } |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 114 | writeEscaped<Tag>(OS, llvm::join(Lines, Separator)); |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 117 | // Prints a row representing an instruction, along with scheduling info and |
| 118 | // point coordinates (measurements). |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 119 | void Analysis::printInstructionRowCsv(const size_t PointId, |
| 120 | llvm::raw_ostream &OS) const { |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 121 | const InstructionBenchmark &Point = Clustering_.getPoints()[PointId]; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 122 | writeClusterId<kEscapeCsv>(OS, Clustering_.getClusterIdForPoint(PointId)); |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 123 | OS << kCsvSep; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 124 | writeSnippet<EscapeTag, kEscapeCsv>(OS, Point.AssembledSnippet, "; "); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 125 | OS << kCsvSep; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 126 | writeEscaped<kEscapeCsv>(OS, Point.Key.Config); |
| 127 | OS << kCsvSep; |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 128 | assert(!Point.Key.Instructions.empty()); |
| 129 | // FIXME: Resolve variant classes. |
| 130 | const unsigned SchedClassId = |
| 131 | InstrInfo_->get(Point.Key.Instructions[0].getOpcode()).getSchedClass(); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 132 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 133 | const auto &SchedModel = SubtargetInfo_->getSchedModel(); |
| 134 | const llvm::MCSchedClassDesc *const SCDesc = |
| 135 | SchedModel.getSchedClassDesc(SchedClassId); |
| 136 | writeEscaped<kEscapeCsv>(OS, SCDesc->Name); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 137 | #else |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 138 | OS << SchedClassId; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 139 | #endif |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 140 | for (const auto &Measurement : Point.Measurements) { |
| 141 | OS << kCsvSep; |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 142 | writeMeasurementValue<kEscapeCsv>(OS, Measurement.PerInstructionValue); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 143 | } |
| 144 | OS << "\n"; |
| 145 | } |
| 146 | |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 147 | Analysis::Analysis(const llvm::Target &Target, |
| 148 | const InstructionBenchmarkClustering &Clustering) |
| 149 | : Clustering_(Clustering) { |
| 150 | if (Clustering.getPoints().empty()) |
| 151 | return; |
| 152 | |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 153 | const InstructionBenchmark &FirstPoint = Clustering.getPoints().front(); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 154 | InstrInfo_.reset(Target.createMCInstrInfo()); |
| 155 | RegInfo_.reset(Target.createMCRegInfo(FirstPoint.LLVMTriple)); |
| 156 | AsmInfo_.reset(Target.createMCAsmInfo(*RegInfo_, FirstPoint.LLVMTriple)); |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 157 | SubtargetInfo_.reset(Target.createMCSubtargetInfo(FirstPoint.LLVMTriple, |
| 158 | FirstPoint.CpuName, "")); |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 159 | InstPrinter_.reset(Target.createMCInstPrinter( |
| 160 | llvm::Triple(FirstPoint.LLVMTriple), 0 /*default variant*/, *AsmInfo_, |
| 161 | *InstrInfo_, *RegInfo_)); |
| 162 | |
| 163 | Context_ = llvm::make_unique<llvm::MCContext>(AsmInfo_.get(), RegInfo_.get(), |
| 164 | &ObjectFileInfo_); |
| 165 | Disasm_.reset(Target.createMCDisassembler(*SubtargetInfo_, *Context_)); |
| 166 | assert(Disasm_ && "cannot create MCDisassembler. missing call to " |
| 167 | "InitializeXXXTargetDisassembler ?"); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 170 | template <> |
| 171 | llvm::Error |
| 172 | Analysis::run<Analysis::PrintClusters>(llvm::raw_ostream &OS) const { |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 173 | if (Clustering_.getPoints().empty()) |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 174 | return llvm::Error::success(); |
| 175 | |
| 176 | // Write the header. |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 177 | OS << "cluster_id" << kCsvSep << "opcode_name" << kCsvSep << "config" |
| 178 | << kCsvSep << "sched_class"; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 179 | for (const auto &Measurement : Clustering_.getPoints().front().Measurements) { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 180 | OS << kCsvSep; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 181 | writeEscaped<kEscapeCsv>(OS, Measurement.Key); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 182 | } |
| 183 | OS << "\n"; |
| 184 | |
| 185 | // Write the points. |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 186 | const auto &Clusters = Clustering_.getValidClusters(); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 187 | for (size_t I = 0, E = Clusters.size(); I < E; ++I) { |
| 188 | for (const size_t PointId : Clusters[I].PointIndices) { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 189 | printInstructionRowCsv(PointId, OS); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 190 | } |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 191 | OS << "\n\n"; |
| 192 | } |
| 193 | return llvm::Error::success(); |
| 194 | } |
| 195 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 196 | std::unordered_map<unsigned, std::vector<size_t>> |
| 197 | Analysis::makePointsPerSchedClass() const { |
| 198 | std::unordered_map<unsigned, std::vector<size_t>> PointsPerSchedClass; |
| 199 | const auto &Points = Clustering_.getPoints(); |
| 200 | for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) { |
| 201 | const InstructionBenchmark &Point = Points[PointId]; |
| 202 | if (!Point.Error.empty()) |
| 203 | continue; |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 204 | assert(!Point.Key.Instructions.empty()); |
| 205 | const auto Opcode = Point.Key.Instructions[0].getOpcode(); |
| 206 | // FIXME: Resolve variant classes. |
| 207 | PointsPerSchedClass[InstrInfo_->get(Opcode).getSchedClass()].push_back( |
| 208 | PointId); |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 209 | } |
| 210 | return PointsPerSchedClass; |
| 211 | } |
| 212 | |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 213 | // Uops repeat the same opcode over again. Just show this opcode and show the |
| 214 | // whole snippet only on hover. |
| 215 | static void writeUopsSnippetHtml(llvm::raw_ostream &OS, |
| 216 | const std::vector<llvm::MCInst> &Instructions, |
| 217 | const llvm::MCInstrInfo &InstrInfo) { |
| 218 | if (Instructions.empty()) |
| 219 | return; |
| 220 | writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instructions[0].getOpcode())); |
| 221 | if (Instructions.size() > 1) |
| 222 | OS << " (x" << Instructions.size() << ")"; |
| 223 | } |
| 224 | |
| 225 | // Latency tries to find a serial path. Just show the opcode path and show the |
| 226 | // whole snippet only on hover. |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 227 | static void |
| 228 | writeLatencySnippetHtml(llvm::raw_ostream &OS, |
| 229 | const std::vector<llvm::MCInst> &Instructions, |
| 230 | const llvm::MCInstrInfo &InstrInfo) { |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 231 | bool First = true; |
| 232 | for (const llvm::MCInst &Instr : Instructions) { |
| 233 | if (First) |
| 234 | First = false; |
| 235 | else |
| 236 | OS << " → "; |
| 237 | writeEscaped<kEscapeHtml>(OS, InstrInfo.getName(Instr.getOpcode())); |
| 238 | } |
| 239 | } |
| 240 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 241 | void Analysis::printSchedClassClustersHtml( |
| 242 | const std::vector<SchedClassCluster> &Clusters, const SchedClass &SC, |
| 243 | llvm::raw_ostream &OS) const { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 244 | const auto &Points = Clustering_.getPoints(); |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 245 | OS << "<table class=\"sched-class-clusters\">"; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 246 | OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 247 | assert(!Clusters.empty()); |
| 248 | for (const auto &Measurement : |
| 249 | Points[Clusters[0].getPointIds()[0]].Measurements) { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 250 | OS << "<th>"; |
Clement Courbet | b1f1b50 | 2018-05-24 11:26:00 +0000 | [diff] [blame] | 251 | if (Measurement.DebugString.empty()) |
| 252 | writeEscaped<kEscapeHtml>(OS, Measurement.Key); |
| 253 | else |
| 254 | writeEscaped<kEscapeHtml>(OS, Measurement.DebugString); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 255 | OS << "</th>"; |
| 256 | } |
| 257 | OS << "</tr>"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 258 | for (const SchedClassCluster &Cluster : Clusters) { |
| 259 | OS << "<tr class=\"" |
| 260 | << (Cluster.measurementsMatch(*SubtargetInfo_, SC, Clustering_) |
| 261 | ? "good-cluster" |
| 262 | : "bad-cluster") |
| 263 | << "\"><td>"; |
| 264 | writeClusterId<kEscapeHtml>(OS, Cluster.id()); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 265 | OS << "</td><td><ul>"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 266 | for (const size_t PointId : Cluster.getPointIds()) { |
| 267 | const auto &Point = Points[PointId]; |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 268 | OS << "<li><span class=\"mono\" title=\""; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 269 | writeSnippet<EscapeTag, kEscapeHtmlString>(OS, Point.AssembledSnippet, |
| 270 | "\n"); |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 271 | OS << "\">"; |
| 272 | switch (Point.Mode) { |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 273 | case InstructionBenchmark::Latency: |
| 274 | writeLatencySnippetHtml(OS, Point.Key.Instructions, *InstrInfo_); |
| 275 | break; |
| 276 | case InstructionBenchmark::Uops: |
| 277 | writeUopsSnippetHtml(OS, Point.Key.Instructions, *InstrInfo_); |
| 278 | break; |
| 279 | default: |
| 280 | llvm_unreachable("invalid mode"); |
Clement Courbet | 49fad1c | 2018-06-14 06:57:52 +0000 | [diff] [blame] | 281 | } |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 282 | OS << "</span> <span class=\"mono\">"; |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 283 | writeEscaped<kEscapeHtml>(OS, Point.Key.Config); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 284 | OS << "</span></li>"; |
| 285 | } |
| 286 | OS << "</ul></td>"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 287 | for (const auto &Stats : Cluster.getRepresentative()) { |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 288 | OS << "<td class=\"measurement\">"; |
| 289 | writeMeasurementValue<kEscapeHtml>(OS, Stats.avg()); |
| 290 | OS << "<br><span class=\"minmax\">["; |
| 291 | writeMeasurementValue<kEscapeHtml>(OS, Stats.min()); |
| 292 | OS << ";"; |
| 293 | writeMeasurementValue<kEscapeHtml>(OS, Stats.max()); |
| 294 | OS << "]</span></td>"; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 295 | } |
| 296 | OS << "</tr>"; |
| 297 | } |
| 298 | OS << "</table>"; |
| 299 | } |
| 300 | |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 301 | // Return the non-redundant list of WriteProcRes used by the given sched class. |
| 302 | // The scheduling model for LLVM is such that each instruction has a certain |
| 303 | // number of uops which consume resources which are described by WriteProcRes |
| 304 | // entries. Each entry describe how many cycles are spent on a specific ProcRes |
| 305 | // kind. |
| 306 | // For example, an instruction might have 3 uOps, one dispatching on P0 |
| 307 | // (ProcResIdx=1) and two on P06 (ProcResIdx = 7). |
| 308 | // Note that LLVM additionally denormalizes resource consumption to include |
| 309 | // usage of super resources by subresources. So in practice if there exists a |
| 310 | // P016 (ProcResIdx=10), then the cycles consumed by P0 are also consumed by |
| 311 | // P06 (ProcResIdx = 7) and P016 (ProcResIdx = 10), and the resources consumed |
| 312 | // by P06 are also consumed by P016. In the figure below, parenthesized cycles |
| 313 | // denote implied usage of superresources by subresources: |
| 314 | // P0 P06 P016 |
| 315 | // uOp1 1 (1) (1) |
| 316 | // uOp2 1 (1) |
| 317 | // uOp3 1 (1) |
| 318 | // ============================= |
| 319 | // 1 3 3 |
| 320 | // Eventually we end up with three entries for the WriteProcRes of the |
| 321 | // instruction: |
| 322 | // {ProcResIdx=1, Cycles=1} // P0 |
| 323 | // {ProcResIdx=7, Cycles=3} // P06 |
| 324 | // {ProcResIdx=10, Cycles=3} // P016 |
| 325 | // |
| 326 | // Note that in this case, P016 does not contribute any cycles, so it would |
| 327 | // be removed by this function. |
| 328 | // FIXME: Move this to MCSubtargetInfo and use it in llvm-mca. |
| 329 | static llvm::SmallVector<llvm::MCWriteProcResEntry, 8> |
| 330 | getNonRedundantWriteProcRes(const llvm::MCSchedClassDesc &SCDesc, |
| 331 | const llvm::MCSubtargetInfo &STI) { |
| 332 | llvm::SmallVector<llvm::MCWriteProcResEntry, 8> Result; |
| 333 | const auto &SM = STI.getSchedModel(); |
| 334 | const unsigned NumProcRes = SM.getNumProcResourceKinds(); |
| 335 | |
| 336 | // This assumes that the ProcResDescs are sorted in topological order, which |
| 337 | // is guaranteed by the tablegen backend. |
| 338 | llvm::SmallVector<float, 32> ProcResUnitUsage(NumProcRes); |
| 339 | for (const auto *WPR = STI.getWriteProcResBegin(&SCDesc), |
| 340 | *const WPREnd = STI.getWriteProcResEnd(&SCDesc); |
| 341 | WPR != WPREnd; ++WPR) { |
| 342 | const llvm::MCProcResourceDesc *const ProcResDesc = |
| 343 | SM.getProcResource(WPR->ProcResourceIdx); |
| 344 | if (ProcResDesc->SubUnitsIdxBegin == nullptr) { |
| 345 | // This is a ProcResUnit. |
| 346 | Result.push_back({WPR->ProcResourceIdx, WPR->Cycles}); |
| 347 | ProcResUnitUsage[WPR->ProcResourceIdx] += WPR->Cycles; |
| 348 | } else { |
| 349 | // This is a ProcResGroup. First see if it contributes any cycles or if |
| 350 | // it has cycles just from subunits. |
| 351 | float RemainingCycles = WPR->Cycles; |
| 352 | for (const auto *SubResIdx = ProcResDesc->SubUnitsIdxBegin; |
| 353 | SubResIdx != ProcResDesc->SubUnitsIdxBegin + ProcResDesc->NumUnits; |
| 354 | ++SubResIdx) { |
| 355 | RemainingCycles -= ProcResUnitUsage[*SubResIdx]; |
| 356 | } |
| 357 | if (RemainingCycles < 0.01f) { |
| 358 | // The ProcResGroup contributes no cycles of its own. |
| 359 | continue; |
| 360 | } |
| 361 | // The ProcResGroup contributes `RemainingCycles` cycles of its own. |
| 362 | Result.push_back({WPR->ProcResourceIdx, |
| 363 | static_cast<uint16_t>(std::round(RemainingCycles))}); |
| 364 | // Spread the remaining cycles over all subunits. |
| 365 | for (const auto *SubResIdx = ProcResDesc->SubUnitsIdxBegin; |
| 366 | SubResIdx != ProcResDesc->SubUnitsIdxBegin + ProcResDesc->NumUnits; |
| 367 | ++SubResIdx) { |
| 368 | ProcResUnitUsage[*SubResIdx] += RemainingCycles / ProcResDesc->NumUnits; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | return Result; |
| 373 | } |
| 374 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 375 | Analysis::SchedClass::SchedClass(const llvm::MCSchedClassDesc &SD, |
| 376 | const llvm::MCSubtargetInfo &STI) |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 377 | : SCDesc(&SD), |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 378 | NonRedundantWriteProcRes(getNonRedundantWriteProcRes(SD, STI)), |
| 379 | IdealizedProcResPressure(computeIdealizedProcResPressure( |
| 380 | STI.getSchedModel(), NonRedundantWriteProcRes)) {} |
| 381 | |
| 382 | void Analysis::SchedClassCluster::addPoint( |
| 383 | size_t PointId, const InstructionBenchmarkClustering &Clustering) { |
| 384 | PointIds.push_back(PointId); |
| 385 | const auto &Point = Clustering.getPoints()[PointId]; |
| 386 | if (ClusterId.isUndef()) { |
| 387 | ClusterId = Clustering.getClusterIdForPoint(PointId); |
| 388 | Representative.resize(Point.Measurements.size()); |
| 389 | } |
| 390 | for (size_t I = 0, E = Point.Measurements.size(); I < E; ++I) { |
| 391 | Representative[I].push(Point.Measurements[I]); |
| 392 | } |
| 393 | assert(ClusterId == Clustering.getClusterIdForPoint(PointId)); |
| 394 | } |
| 395 | |
| 396 | bool Analysis::SchedClassCluster::measurementsMatch( |
| 397 | const llvm::MCSubtargetInfo &STI, const SchedClass &SC, |
| 398 | const InstructionBenchmarkClustering &Clustering) const { |
| 399 | const size_t NumMeasurements = Representative.size(); |
| 400 | std::vector<BenchmarkMeasure> ClusterCenterPoint(NumMeasurements); |
| 401 | std::vector<BenchmarkMeasure> SchedClassPoint(NumMeasurements); |
| 402 | // Latency case. |
| 403 | assert(!Clustering.getPoints().empty()); |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 404 | const InstructionBenchmark::ModeE Mode = Clustering.getPoints()[0].Mode; |
| 405 | if (Mode == InstructionBenchmark::Latency) { |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 406 | if (NumMeasurements != 1) { |
| 407 | llvm::errs() |
| 408 | << "invalid number of measurements in latency mode: expected 1, got " |
| 409 | << NumMeasurements << "\n"; |
| 410 | return false; |
| 411 | } |
| 412 | // Find the latency. |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 413 | SchedClassPoint[0].PerInstructionValue = 0.0; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 414 | for (unsigned I = 0; I < SC.SCDesc->NumWriteLatencyEntries; ++I) { |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 415 | const llvm::MCWriteLatencyEntry *const WLE = |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 416 | STI.getWriteLatencyEntry(SC.SCDesc, I); |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 417 | SchedClassPoint[0].PerInstructionValue = |
| 418 | std::max<double>(SchedClassPoint[0].PerInstructionValue, WLE->Cycles); |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 419 | } |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 420 | ClusterCenterPoint[0].PerInstructionValue = Representative[0].avg(); |
Clement Courbet | 62b34fa | 2018-06-06 09:42:36 +0000 | [diff] [blame] | 421 | } else if (Mode == InstructionBenchmark::Uops) { |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 422 | for (int I = 0, E = Representative.size(); I < E; ++I) { |
| 423 | // Find the pressure on ProcResIdx `Key`. |
| 424 | uint16_t ProcResIdx = 0; |
| 425 | if (!llvm::to_integer(Representative[I].key(), ProcResIdx, 10)) { |
| 426 | llvm::errs() << "expected ProcResIdx key, got " |
| 427 | << Representative[I].key() << "\n"; |
| 428 | return false; |
| 429 | } |
| 430 | const auto ProcResPressureIt = |
| 431 | std::find_if(SC.IdealizedProcResPressure.begin(), |
| 432 | SC.IdealizedProcResPressure.end(), |
| 433 | [ProcResIdx](const std::pair<uint16_t, float> &WPR) { |
| 434 | return WPR.first == ProcResIdx; |
| 435 | }); |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 436 | SchedClassPoint[I].PerInstructionValue = |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 437 | ProcResPressureIt == SC.IdealizedProcResPressure.end() |
| 438 | ? 0.0 |
| 439 | : ProcResPressureIt->second; |
Clement Courbet | 684a5f6 | 2018-09-26 08:37:21 +0000 | [diff] [blame] | 440 | ClusterCenterPoint[I].PerInstructionValue = Representative[I].avg(); |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 441 | } |
| 442 | } else { |
Clement Courbet | 2cb97b9 | 2018-06-04 11:43:40 +0000 | [diff] [blame] | 443 | llvm::errs() << "unimplemented measurement matching for mode " << Mode |
| 444 | << "\n"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | return Clustering.isNeighbour(ClusterCenterPoint, SchedClassPoint); |
| 448 | } |
| 449 | |
| 450 | void Analysis::printSchedClassDescHtml(const SchedClass &SC, |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 451 | llvm::raw_ostream &OS) const { |
| 452 | OS << "<table class=\"sched-class-desc\">"; |
| 453 | OS << "<tr><th>Valid</th><th>Variant</th><th>uOps</th><th>Latency</" |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 454 | "th><th>WriteProcRes</th><th title=\"This is the idealized unit " |
| 455 | "resource (port) pressure assuming ideal distribution\">Idealized " |
| 456 | "Resource Pressure</th></tr>"; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 457 | if (SC.SCDesc->isValid()) { |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 458 | const auto &SM = SubtargetInfo_->getSchedModel(); |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 459 | OS << "<tr><td>✔</td>"; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 460 | OS << "<td>" << (SC.SCDesc->isVariant() ? "✔" : "✕") |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 461 | << "</td>"; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 462 | OS << "<td>" << SC.SCDesc->NumMicroOps << "</td>"; |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 463 | // Latencies. |
| 464 | OS << "<td><ul>"; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 465 | for (int I = 0, E = SC.SCDesc->NumWriteLatencyEntries; I < E; ++I) { |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 466 | const auto *const Entry = |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 467 | SubtargetInfo_->getWriteLatencyEntry(SC.SCDesc, I); |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 468 | OS << "<li>" << Entry->Cycles; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 469 | if (SC.SCDesc->NumWriteLatencyEntries > 1) { |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 470 | // Dismabiguate if more than 1 latency. |
| 471 | OS << " (WriteResourceID " << Entry->WriteResourceID << ")"; |
| 472 | } |
| 473 | OS << "</li>"; |
| 474 | } |
| 475 | OS << "</ul></td>"; |
| 476 | // WriteProcRes. |
| 477 | OS << "<td><ul>"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 478 | for (const auto &WPR : SC.NonRedundantWriteProcRes) { |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 479 | OS << "<li><span class=\"mono\">"; |
| 480 | writeEscaped<kEscapeHtml>(OS, |
| 481 | SM.getProcResource(WPR.ProcResourceIdx)->Name); |
| 482 | OS << "</span>: " << WPR.Cycles << "</li>"; |
| 483 | } |
| 484 | OS << "</ul></td>"; |
| 485 | // Idealized port pressure. |
| 486 | OS << "<td><ul>"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 487 | for (const auto &Pressure : SC.IdealizedProcResPressure) { |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 488 | OS << "<li><span class=\"mono\">"; |
| 489 | writeEscaped<kEscapeHtml>(OS, SubtargetInfo_->getSchedModel() |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 490 | .getProcResource(Pressure.first) |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 491 | ->Name); |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 492 | OS << "</span>: "; |
| 493 | writeMeasurementValue<kEscapeHtml>(OS, Pressure.second); |
| 494 | OS << "</li>"; |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 495 | } |
| 496 | OS << "</ul></td>"; |
| 497 | OS << "</tr>"; |
| 498 | } else { |
| 499 | OS << "<tr><td>✕</td><td></td><td></td></tr>"; |
| 500 | } |
| 501 | OS << "</table>"; |
| 502 | } |
| 503 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 504 | static constexpr const char kHtmlHead[] = R"( |
| 505 | <head> |
| 506 | <title>llvm-exegesis Analysis Results</title> |
| 507 | <style> |
| 508 | body { |
| 509 | font-family: sans-serif |
| 510 | } |
| 511 | span.sched-class-name { |
| 512 | font-weight: bold; |
| 513 | font-family: monospace; |
| 514 | } |
| 515 | span.opcode { |
| 516 | font-family: monospace; |
| 517 | } |
| 518 | span.config { |
| 519 | font-family: monospace; |
| 520 | } |
| 521 | div.inconsistency { |
| 522 | margin-top: 50px; |
| 523 | } |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 524 | table { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 525 | margin-left: 50px; |
| 526 | border-collapse: collapse; |
| 527 | } |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 528 | table, table tr,td,th { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 529 | border: 1px solid #444; |
| 530 | } |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 531 | table ul { |
| 532 | padding-left: 0px; |
| 533 | margin: 0px; |
| 534 | list-style-type: none; |
| 535 | } |
| 536 | table.sched-class-clusters td { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 537 | padding-left: 10px; |
| 538 | padding-right: 10px; |
| 539 | padding-top: 10px; |
| 540 | padding-bottom: 10px; |
| 541 | } |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 542 | table.sched-class-desc td { |
| 543 | padding-left: 10px; |
| 544 | padding-right: 10px; |
| 545 | padding-top: 2px; |
| 546 | padding-bottom: 2px; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 547 | } |
| 548 | span.mono { |
| 549 | font-family: monospace; |
| 550 | } |
Clement Courbet | ae8ae5dc | 2018-05-24 12:41:02 +0000 | [diff] [blame] | 551 | td.measurement { |
| 552 | text-align: center; |
| 553 | } |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 554 | tr.good-cluster td.measurement { |
| 555 | color: #292 |
| 556 | } |
| 557 | tr.bad-cluster td.measurement { |
| 558 | color: #922 |
| 559 | } |
| 560 | tr.good-cluster td.measurement span.minmax { |
| 561 | color: #888; |
| 562 | } |
| 563 | tr.bad-cluster td.measurement span.minmax { |
| 564 | color: #888; |
| 565 | } |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 566 | </style> |
| 567 | </head> |
| 568 | )"; |
| 569 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 570 | template <> |
| 571 | llvm::Error Analysis::run<Analysis::PrintSchedClassInconsistencies>( |
| 572 | llvm::raw_ostream &OS) const { |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 573 | const auto &FirstPoint = Clustering_.getPoints()[0]; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 574 | // Print the header. |
| 575 | OS << "<!DOCTYPE html><html>" << kHtmlHead << "<body>"; |
| 576 | OS << "<h1><span class=\"mono\">llvm-exegesis</span> Analysis Results</h1>"; |
| 577 | OS << "<h3>Triple: <span class=\"mono\">"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 578 | writeEscaped<kEscapeHtml>(OS, FirstPoint.LLVMTriple); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 579 | OS << "</span></h3><h3>Cpu: <span class=\"mono\">"; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 580 | writeEscaped<kEscapeHtml>(OS, FirstPoint.CpuName); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 581 | OS << "</span></h3>"; |
| 582 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 583 | for (const auto &SchedClassAndPoints : makePointsPerSchedClass()) { |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 584 | const auto SchedClassId = SchedClassAndPoints.first; |
| 585 | const std::vector<size_t> &SchedClassPoints = SchedClassAndPoints.second; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 586 | const auto &SchedModel = SubtargetInfo_->getSchedModel(); |
| 587 | const llvm::MCSchedClassDesc *const SCDesc = |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 588 | SchedModel.getSchedClassDesc(SchedClassId); |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 589 | if (!SCDesc) |
| 590 | continue; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 591 | const SchedClass SC(*SCDesc, *SubtargetInfo_); |
| 592 | |
| 593 | // Bucket sched class points into sched class clusters. |
| 594 | std::vector<SchedClassCluster> SchedClassClusters; |
| 595 | for (const size_t PointId : SchedClassPoints) { |
| 596 | const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId); |
| 597 | if (!ClusterId.isValid()) |
| 598 | continue; // Ignore noise and errors. FIXME: take noise into account ? |
| 599 | auto SchedClassClusterIt = |
| 600 | std::find_if(SchedClassClusters.begin(), SchedClassClusters.end(), |
| 601 | [ClusterId](const SchedClassCluster &C) { |
| 602 | return C.id() == ClusterId; |
| 603 | }); |
| 604 | if (SchedClassClusterIt == SchedClassClusters.end()) { |
| 605 | SchedClassClusters.emplace_back(); |
| 606 | SchedClassClusterIt = std::prev(SchedClassClusters.end()); |
| 607 | } |
| 608 | SchedClassClusterIt->addPoint(PointId, Clustering_); |
| 609 | } |
| 610 | |
| 611 | // Print any scheduling class that has at least one cluster that does not |
| 612 | // match the checked-in data. |
| 613 | if (std::all_of(SchedClassClusters.begin(), SchedClassClusters.end(), |
| 614 | [this, &SC](const SchedClassCluster &C) { |
| 615 | return C.measurementsMatch(*SubtargetInfo_, SC, |
| 616 | Clustering_); |
| 617 | })) |
| 618 | continue; // Nothing weird. |
| 619 | |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 620 | OS << "<div class=\"inconsistency\"><p>Sched Class <span " |
| 621 | "class=\"sched-class-name\">"; |
| 622 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 623 | writeEscaped<kEscapeHtml>(OS, SCDesc->Name); |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 624 | #else |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 625 | OS << SchedClassId; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 626 | #endif |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 627 | OS << "</span> contains instructions whose performance characteristics do" |
| 628 | " not match that of LLVM:</p>"; |
| 629 | printSchedClassClustersHtml(SchedClassClusters, SC, OS); |
| 630 | OS << "<p>llvm SchedModel data:</p>"; |
| 631 | printSchedClassDescHtml(SC, OS); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 632 | OS << "</div>"; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 633 | } |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 634 | |
| 635 | OS << "</body></html>"; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 636 | return llvm::Error::success(); |
| 637 | } |
| 638 | |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 639 | // Distributes a pressure budget as evenly as possible on the provided subunits |
| 640 | // given the already existing port pressure distribution. |
| 641 | // |
| 642 | // The algorithm is as follows: while there is remaining pressure to |
| 643 | // distribute, find the subunits with minimal pressure, and distribute |
| 644 | // remaining pressure equally up to the pressure of the unit with |
| 645 | // second-to-minimal pressure. |
| 646 | // For example, let's assume we want to distribute 2*P1256 |
| 647 | // (Subunits = [P1,P2,P5,P6]), and the starting DensePressure is: |
| 648 | // DensePressure = P0 P1 P2 P3 P4 P5 P6 P7 |
| 649 | // 0.1 0.3 0.2 0.0 0.0 0.5 0.5 0.5 |
| 650 | // RemainingPressure = 2.0 |
| 651 | // We sort the subunits by pressure: |
| 652 | // Subunits = [(P2,p=0.2), (P1,p=0.3), (P5,p=0.5), (P6, p=0.5)] |
| 653 | // We'll first start by the subunits with minimal pressure, which are at |
| 654 | // the beginning of the sorted array. In this example there is one (P2). |
| 655 | // The subunit with second-to-minimal pressure is the next one in the |
| 656 | // array (P1). So we distribute 0.1 pressure to P2, and remove 0.1 cycles |
| 657 | // from the budget. |
| 658 | // Subunits = [(P2,p=0.3), (P1,p=0.3), (P5,p=0.5), (P5,p=0.5)] |
| 659 | // RemainingPressure = 1.9 |
| 660 | // We repeat this process: distribute 0.2 pressure on each of the minimal |
| 661 | // P2 and P1, decrease budget by 2*0.2: |
| 662 | // Subunits = [(P2,p=0.5), (P1,p=0.5), (P5,p=0.5), (P5,p=0.5)] |
| 663 | // RemainingPressure = 1.5 |
| 664 | // There are no second-to-minimal subunits so we just share the remaining |
| 665 | // budget (1.5 cycles) equally: |
| 666 | // Subunits = [(P2,p=0.875), (P1,p=0.875), (P5,p=0.875), (P5,p=0.875)] |
| 667 | // RemainingPressure = 0.0 |
| 668 | // We stop as there is no remaining budget to distribute. |
| 669 | void distributePressure(float RemainingPressure, |
| 670 | llvm::SmallVector<uint16_t, 32> Subunits, |
| 671 | llvm::SmallVector<float, 32> &DensePressure) { |
| 672 | // Find the number of subunits with minimal pressure (they are at the |
| 673 | // front). |
| 674 | llvm::sort(Subunits.begin(), Subunits.end(), |
| 675 | [&DensePressure](const uint16_t A, const uint16_t B) { |
| 676 | return DensePressure[A] < DensePressure[B]; |
| 677 | }); |
| 678 | const auto getPressureForSubunit = [&DensePressure, |
| 679 | &Subunits](size_t I) -> float & { |
| 680 | return DensePressure[Subunits[I]]; |
| 681 | }; |
| 682 | size_t NumMinimalSU = 1; |
| 683 | while (NumMinimalSU < Subunits.size() && |
| 684 | getPressureForSubunit(NumMinimalSU) == getPressureForSubunit(0)) { |
| 685 | ++NumMinimalSU; |
| 686 | } |
| 687 | while (RemainingPressure > 0.0f) { |
| 688 | if (NumMinimalSU == Subunits.size()) { |
| 689 | // All units are minimal, just distribute evenly and be done. |
| 690 | for (size_t I = 0; I < NumMinimalSU; ++I) { |
| 691 | getPressureForSubunit(I) += RemainingPressure / NumMinimalSU; |
| 692 | } |
| 693 | return; |
| 694 | } |
| 695 | // Distribute the remaining pressure equally. |
| 696 | const float MinimalPressure = getPressureForSubunit(NumMinimalSU - 1); |
| 697 | const float SecondToMinimalPressure = getPressureForSubunit(NumMinimalSU); |
| 698 | assert(MinimalPressure < SecondToMinimalPressure); |
| 699 | const float Increment = SecondToMinimalPressure - MinimalPressure; |
| 700 | if (RemainingPressure <= NumMinimalSU * Increment) { |
| 701 | // There is not enough remaining pressure. |
| 702 | for (size_t I = 0; I < NumMinimalSU; ++I) { |
| 703 | getPressureForSubunit(I) += RemainingPressure / NumMinimalSU; |
| 704 | } |
| 705 | return; |
| 706 | } |
| 707 | // Bump all minimal pressure subunits to `SecondToMinimalPressure`. |
| 708 | for (size_t I = 0; I < NumMinimalSU; ++I) { |
| 709 | getPressureForSubunit(I) = SecondToMinimalPressure; |
| 710 | RemainingPressure -= SecondToMinimalPressure; |
| 711 | } |
| 712 | while (NumMinimalSU < Subunits.size() && |
| 713 | getPressureForSubunit(NumMinimalSU) == SecondToMinimalPressure) { |
| 714 | ++NumMinimalSU; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | std::vector<std::pair<uint16_t, float>> computeIdealizedProcResPressure( |
| 720 | const llvm::MCSchedModel &SM, |
| 721 | llvm::SmallVector<llvm::MCWriteProcResEntry, 8> WPRS) { |
| 722 | // DensePressure[I] is the port pressure for Proc Resource I. |
| 723 | llvm::SmallVector<float, 32> DensePressure(SM.getNumProcResourceKinds()); |
| 724 | llvm::sort(WPRS.begin(), WPRS.end(), |
| 725 | [](const llvm::MCWriteProcResEntry &A, |
| 726 | const llvm::MCWriteProcResEntry &B) { |
| 727 | return A.ProcResourceIdx < B.ProcResourceIdx; |
| 728 | }); |
| 729 | for (const llvm::MCWriteProcResEntry &WPR : WPRS) { |
| 730 | // Get units for the entry. |
| 731 | const llvm::MCProcResourceDesc *const ProcResDesc = |
| 732 | SM.getProcResource(WPR.ProcResourceIdx); |
| 733 | if (ProcResDesc->SubUnitsIdxBegin == nullptr) { |
| 734 | // This is a ProcResUnit. |
| 735 | DensePressure[WPR.ProcResourceIdx] += WPR.Cycles; |
| 736 | } else { |
| 737 | // This is a ProcResGroup. |
| 738 | llvm::SmallVector<uint16_t, 32> Subunits(ProcResDesc->SubUnitsIdxBegin, |
| 739 | ProcResDesc->SubUnitsIdxBegin + |
| 740 | ProcResDesc->NumUnits); |
| 741 | distributePressure(WPR.Cycles, Subunits, DensePressure); |
| 742 | } |
| 743 | } |
| 744 | // Turn dense pressure into sparse pressure by removing zero entries. |
| 745 | std::vector<std::pair<uint16_t, float>> Pressure; |
| 746 | for (unsigned I = 0, E = SM.getNumProcResourceKinds(); I < E; ++I) { |
| 747 | if (DensePressure[I] > 0.0f) |
| 748 | Pressure.emplace_back(I, DensePressure[I]); |
| 749 | } |
| 750 | return Pressure; |
| 751 | } |
| 752 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 753 | } // namespace exegesis |