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 | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FormatVariadic.h" |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 13 | #include <unordered_set> |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | namespace exegesis { |
| 17 | |
| 18 | static const char kCsvSep = ','; |
| 19 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 20 | namespace { |
| 21 | |
| 22 | enum EscapeTag { kEscapeCsv, kEscapeHtml }; |
| 23 | |
| 24 | template <EscapeTag Tag> |
| 25 | void writeEscaped(llvm::raw_ostream &OS, const llvm::StringRef S); |
| 26 | |
| 27 | template <> |
| 28 | void writeEscaped<kEscapeCsv>(llvm::raw_ostream &OS, const llvm::StringRef S) { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 29 | if (std::find(S.begin(), S.end(), kCsvSep) == S.end()) { |
| 30 | OS << S; |
| 31 | } else { |
| 32 | // Needs escaping. |
| 33 | OS << '"'; |
| 34 | for (const char C : S) { |
| 35 | if (C == '"') |
| 36 | OS << "\"\""; |
| 37 | else |
| 38 | OS << C; |
| 39 | } |
| 40 | OS << '"'; |
| 41 | } |
| 42 | } |
| 43 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 44 | template <> |
| 45 | void writeEscaped<kEscapeHtml>(llvm::raw_ostream &OS, const llvm::StringRef S) { |
| 46 | for (const char C : S) { |
| 47 | if (C == '<') |
| 48 | OS << "<"; |
| 49 | else if (C == '>') |
| 50 | OS << ">"; |
| 51 | else if (C == '&') |
| 52 | OS << "&"; |
| 53 | else |
| 54 | OS << C; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
| 60 | template <EscapeTag Tag> |
| 61 | static void |
| 62 | writeClusterId(llvm::raw_ostream &OS, |
| 63 | const InstructionBenchmarkClustering::ClusterId &CID) { |
| 64 | if (CID.isNoise()) |
| 65 | writeEscaped<Tag>(OS, "[noise]"); |
| 66 | else if (CID.isError()) |
| 67 | writeEscaped<Tag>(OS, "[error]"); |
| 68 | else |
| 69 | OS << CID.getId(); |
| 70 | } |
| 71 | |
| 72 | template <EscapeTag Tag> |
| 73 | static void writeMeasurementValue(llvm::raw_ostream &OS, const double Value) { |
| 74 | writeEscaped<Tag>(OS, llvm::formatv("{0:F}", Value).str()); |
| 75 | } |
| 76 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 77 | // Prints a row representing an instruction, along with scheduling info and |
| 78 | // point coordinates (measurements). |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 79 | void Analysis::printInstructionRowCsv(const size_t PointId, |
| 80 | llvm::raw_ostream &OS) const { |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 81 | const InstructionBenchmark &Point = Clustering_.getPoints()[PointId]; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 82 | writeClusterId<kEscapeCsv>(OS, Clustering_.getClusterIdForPoint(PointId)); |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 83 | OS << kCsvSep; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 84 | writeEscaped<kEscapeCsv>(OS, Point.Key.OpcodeName); |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 85 | OS << kCsvSep; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 86 | writeEscaped<kEscapeCsv>(OS, Point.Key.Config); |
| 87 | OS << kCsvSep; |
| 88 | const auto OpcodeIt = MnemonicToOpcode_.find(Point.Key.OpcodeName); |
| 89 | if (OpcodeIt != MnemonicToOpcode_.end()) { |
| 90 | const unsigned SchedClassId = |
| 91 | InstrInfo_->get(OpcodeIt->second).getSchedClass(); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 92 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 93 | const auto &SchedModel = SubtargetInfo_->getSchedModel(); |
| 94 | const llvm::MCSchedClassDesc *const SCDesc = |
| 95 | SchedModel.getSchedClassDesc(SchedClassId); |
| 96 | writeEscaped<kEscapeCsv>(OS, SCDesc->Name); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 97 | #else |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 98 | OS << SchedClassId; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 99 | #endif |
| 100 | } |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 101 | // FIXME: Print the sched class once InstructionBenchmark separates key into |
| 102 | // (mnemonic, mode, opaque). |
| 103 | for (const auto &Measurement : Point.Measurements) { |
| 104 | OS << kCsvSep; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 105 | writeMeasurementValue<kEscapeCsv>(OS, Measurement.Value); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 106 | } |
| 107 | OS << "\n"; |
| 108 | } |
| 109 | |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 110 | Analysis::Analysis(const llvm::Target &Target, |
| 111 | const InstructionBenchmarkClustering &Clustering) |
| 112 | : Clustering_(Clustering) { |
| 113 | if (Clustering.getPoints().empty()) |
| 114 | return; |
| 115 | |
| 116 | InstrInfo_.reset(Target.createMCInstrInfo()); |
| 117 | const InstructionBenchmark &FirstPoint = Clustering.getPoints().front(); |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 118 | SubtargetInfo_.reset(Target.createMCSubtargetInfo(FirstPoint.LLVMTriple, |
| 119 | FirstPoint.CpuName, "")); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 120 | |
| 121 | // Build an index of mnemonic->opcode. |
| 122 | for (int I = 0, E = InstrInfo_->getNumOpcodes(); I < E; ++I) |
| 123 | MnemonicToOpcode_.emplace(InstrInfo_->getName(I), I); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 126 | template <> |
| 127 | llvm::Error |
| 128 | Analysis::run<Analysis::PrintClusters>(llvm::raw_ostream &OS) const { |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 129 | if (Clustering_.getPoints().empty()) |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 130 | return llvm::Error::success(); |
| 131 | |
| 132 | // Write the header. |
Clement Courbet | a66bfaa4 | 2018-05-15 13:07:05 +0000 | [diff] [blame] | 133 | OS << "cluster_id" << kCsvSep << "opcode_name" << kCsvSep << "config" |
| 134 | << kCsvSep << "sched_class"; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 135 | for (const auto &Measurement : Clustering_.getPoints().front().Measurements) { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 136 | OS << kCsvSep; |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 137 | writeEscaped<kEscapeCsv>(OS, Measurement.Key); |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 138 | } |
| 139 | OS << "\n"; |
| 140 | |
| 141 | // Write the points. |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 142 | const auto &Clusters = Clustering_.getValidClusters(); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 143 | for (size_t I = 0, E = Clusters.size(); I < E; ++I) { |
| 144 | for (const size_t PointId : Clusters[I].PointIndices) { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 145 | printInstructionRowCsv(PointId, OS); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 146 | } |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 147 | OS << "\n\n"; |
| 148 | } |
| 149 | return llvm::Error::success(); |
| 150 | } |
| 151 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 152 | std::unordered_map<unsigned, std::vector<size_t>> |
| 153 | Analysis::makePointsPerSchedClass() const { |
| 154 | std::unordered_map<unsigned, std::vector<size_t>> PointsPerSchedClass; |
| 155 | const auto &Points = Clustering_.getPoints(); |
| 156 | for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) { |
| 157 | const InstructionBenchmark &Point = Points[PointId]; |
| 158 | if (!Point.Error.empty()) |
| 159 | continue; |
| 160 | const auto OpcodeIt = MnemonicToOpcode_.find(Point.Key.OpcodeName); |
| 161 | if (OpcodeIt == MnemonicToOpcode_.end()) |
| 162 | continue; |
| 163 | const unsigned SchedClassId = |
| 164 | InstrInfo_->get(OpcodeIt->second).getSchedClass(); |
| 165 | PointsPerSchedClass[SchedClassId].push_back(PointId); |
| 166 | } |
| 167 | return PointsPerSchedClass; |
| 168 | } |
| 169 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 170 | void Analysis::printSchedClassHtml(std::vector<size_t> PointIds, |
| 171 | llvm::raw_ostream &OS) const { |
| 172 | assert(!PointIds.empty()); |
| 173 | // Sort the points by cluster id so that we can display them grouped by |
| 174 | // cluster. |
| 175 | std::sort(PointIds.begin(), PointIds.end(), |
| 176 | [this](const size_t A, const size_t B) { |
| 177 | return Clustering_.getClusterIdForPoint(A) < |
| 178 | Clustering_.getClusterIdForPoint(B); |
| 179 | }); |
| 180 | const auto &Points = Clustering_.getPoints(); |
| 181 | OS << "<table class=\"sched-class\">"; |
| 182 | OS << "<tr><th>ClusterId</th><th>Opcode/Config</th>"; |
| 183 | for (const auto &Measurement : Points[PointIds[0]].Measurements) { |
| 184 | OS << "<th>"; |
| 185 | writeEscaped<kEscapeHtml>(OS, Measurement.Key); |
| 186 | OS << "</th>"; |
| 187 | } |
| 188 | OS << "</tr>"; |
| 189 | for (size_t I = 0, E = PointIds.size(); I < E;) { |
| 190 | const auto &CurrentClusterId = |
| 191 | Clustering_.getClusterIdForPoint(PointIds[I]); |
| 192 | OS << "<tr><td>"; |
| 193 | writeClusterId<kEscapeHtml>(OS, CurrentClusterId); |
| 194 | OS << "</td><td><ul>"; |
| 195 | const auto &ClusterRepresentative = |
| 196 | Points[PointIds[I]]; // FIXME: average measurements. |
| 197 | for (; I < E && |
| 198 | Clustering_.getClusterIdForPoint(PointIds[I]) == CurrentClusterId; |
| 199 | ++I) { |
| 200 | OS << "<li><span class=\"mono\">"; |
| 201 | writeEscaped<kEscapeHtml>(OS, Points[PointIds[I]].Key.OpcodeName); |
| 202 | OS << "</span> <span class=\"mono\">"; |
| 203 | writeEscaped<kEscapeHtml>(OS, Points[PointIds[I]].Key.Config); |
| 204 | OS << "</span></li>"; |
| 205 | } |
| 206 | OS << "</ul></td>"; |
| 207 | for (const auto &Measurement : ClusterRepresentative.Measurements) { |
| 208 | OS << "<td>"; |
| 209 | writeMeasurementValue<kEscapeHtml>(OS, Measurement.Value); |
| 210 | OS << "</td>"; |
| 211 | } |
| 212 | OS << "</tr>"; |
| 213 | } |
| 214 | OS << "</table>"; |
| 215 | } |
| 216 | |
| 217 | static constexpr const char kHtmlHead[] = R"( |
| 218 | <head> |
| 219 | <title>llvm-exegesis Analysis Results</title> |
| 220 | <style> |
| 221 | body { |
| 222 | font-family: sans-serif |
| 223 | } |
| 224 | span.sched-class-name { |
| 225 | font-weight: bold; |
| 226 | font-family: monospace; |
| 227 | } |
| 228 | span.opcode { |
| 229 | font-family: monospace; |
| 230 | } |
| 231 | span.config { |
| 232 | font-family: monospace; |
| 233 | } |
| 234 | div.inconsistency { |
| 235 | margin-top: 50px; |
| 236 | } |
| 237 | table.sched-class { |
| 238 | margin-left: 50px; |
| 239 | border-collapse: collapse; |
| 240 | } |
| 241 | table.sched-class, table.sched-class tr,td,th { |
| 242 | border: 1px solid #444; |
| 243 | } |
| 244 | table.sched-class td { |
| 245 | padding-left: 10px; |
| 246 | padding-right: 10px; |
| 247 | padding-top: 10px; |
| 248 | padding-bottom: 10px; |
| 249 | } |
| 250 | table.sched-class ul { |
| 251 | padding-left: 0px; |
| 252 | margin: 0px; |
| 253 | list-style-type: none; |
| 254 | } |
| 255 | span.mono { |
| 256 | font-family: monospace; |
| 257 | } |
| 258 | </style> |
| 259 | </head> |
| 260 | )"; |
| 261 | |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 262 | template <> |
| 263 | llvm::Error Analysis::run<Analysis::PrintSchedClassInconsistencies>( |
| 264 | llvm::raw_ostream &OS) const { |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 265 | // Print the header. |
| 266 | OS << "<!DOCTYPE html><html>" << kHtmlHead << "<body>"; |
| 267 | OS << "<h1><span class=\"mono\">llvm-exegesis</span> Analysis Results</h1>"; |
| 268 | OS << "<h3>Triple: <span class=\"mono\">"; |
| 269 | writeEscaped<kEscapeHtml>(OS, Clustering_.getPoints()[0].LLVMTriple); |
| 270 | OS << "</span></h3><h3>Cpu: <span class=\"mono\">"; |
| 271 | writeEscaped<kEscapeHtml>(OS, Clustering_.getPoints()[0].CpuName); |
| 272 | OS << "</span></h3>"; |
| 273 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 274 | // All the points in a scheduling class should be in the same cluster. |
| 275 | // Print any scheduling class for which this is not the case. |
| 276 | for (const auto &SchedClassAndPoints : makePointsPerSchedClass()) { |
| 277 | std::unordered_set<size_t> ClustersForSchedClass; |
| 278 | for (const size_t PointId : SchedClassAndPoints.second) { |
| 279 | const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId); |
| 280 | if (!ClusterId.isValid()) |
| 281 | continue; // Ignore noise and errors. |
| 282 | ClustersForSchedClass.insert(ClusterId.getId()); |
| 283 | } |
| 284 | if (ClustersForSchedClass.size() <= 1) |
| 285 | continue; // Nothing weird. |
| 286 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 287 | OS << "<div class=\"inconsistency\"><p>Sched Class <span " |
| 288 | "class=\"sched-class-name\">"; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 289 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 290 | const auto &SchedModel = SubtargetInfo_->getSchedModel(); |
| 291 | const llvm::MCSchedClassDesc *const SCDesc = |
| 292 | SchedModel.getSchedClassDesc(SchedClassAndPoints.first); |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 293 | writeEscaped<kEscapeHtml>(OS, SCDesc->Name); |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 294 | #else |
| 295 | OS << SchedClassAndPoints.first; |
| 296 | #endif |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 297 | OS << "</span> contains instructions with distinct performance " |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 298 | "characteristics, falling into " |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 299 | << ClustersForSchedClass.size() << " clusters:</p>"; |
| 300 | printSchedClassHtml(SchedClassAndPoints.second, OS); |
| 301 | OS << "</div>"; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 302 | } |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame^] | 303 | |
| 304 | OS << "</body></html>"; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 305 | return llvm::Error::success(); |
| 306 | } |
| 307 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 308 | } // namespace exegesis |