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 10 #include "Analysis.h" 11 #include "BenchmarkResult.h" 12 #include "llvm/Support/FormatVariadic.h" 13 #include <unordered_set> 14 #include <vector> 15 16 namespace exegesis { 17 18 static const char kCsvSep = ','; 19 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) { 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 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 77 // Prints a row representing an instruction, along with scheduling info and 78 // point coordinates (measurements). 79 void Analysis::printInstructionRowCsv(const size_t PointId, 80 llvm::raw_ostream &OS) const { 81 const InstructionBenchmark &Point = Clustering_.getPoints()[PointId]; 82 writeClusterId<kEscapeCsv>(OS, Clustering_.getClusterIdForPoint(PointId)); 83 OS << kCsvSep; 84 writeEscaped<kEscapeCsv>(OS, Point.Key.OpcodeName); 85 OS << kCsvSep; 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(); 92 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 93 const auto &SchedModel = SubtargetInfo_->getSchedModel(); 94 const llvm::MCSchedClassDesc *const SCDesc = 95 SchedModel.getSchedClassDesc(SchedClassId); 96 writeEscaped<kEscapeCsv>(OS, SCDesc->Name); 97 #else 98 OS << SchedClassId; 99 #endif 100 } 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; 105 writeMeasurementValue<kEscapeCsv>(OS, Measurement.Value); 106 } 107 OS << "\n"; 108 } 109 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(); 118 SubtargetInfo_.reset(Target.createMCSubtargetInfo(FirstPoint.LLVMTriple, 119 FirstPoint.CpuName, "")); 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); 124 } 125 126 template <> 127 llvm::Error 128 Analysis::run<Analysis::PrintClusters>(llvm::raw_ostream &OS) const { 129 if (Clustering_.getPoints().empty()) 130 return llvm::Error::success(); 131 132 // Write the header. 133 OS << "cluster_id" << kCsvSep << "opcode_name" << kCsvSep << "config" 134 << kCsvSep << "sched_class"; 135 for (const auto &Measurement : Clustering_.getPoints().front().Measurements) { 136 OS << kCsvSep; 137 writeEscaped<kEscapeCsv>(OS, Measurement.Key); 138 } 139 OS << "\n"; 140 141 // Write the points. 142 const auto &Clusters = Clustering_.getValidClusters(); 143 for (size_t I = 0, E = Clusters.size(); I < E; ++I) { 144 for (const size_t PointId : Clusters[I].PointIndices) { 145 printInstructionRowCsv(PointId, OS); 146 } 147 OS << "\n\n"; 148 } 149 return llvm::Error::success(); 150 } 151 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 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 262 template <> 263 llvm::Error Analysis::run<Analysis::PrintSchedClassInconsistencies>( 264 llvm::raw_ostream &OS) const { 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 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 287 OS << "<div class=\"inconsistency\"><p>Sched Class <span " 288 "class=\"sched-class-name\">"; 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); 293 writeEscaped<kEscapeHtml>(OS, SCDesc->Name); 294 #else 295 OS << SchedClassAndPoints.first; 296 #endif 297 OS << "</span> contains instructions with distinct performance " 298 "characteristics, falling into " 299 << ClustersForSchedClass.size() << " clusters:</p>"; 300 printSchedClassHtml(SchedClassAndPoints.second, OS); 301 OS << "</div>"; 302 } 303 304 OS << "</body></html>"; 305 return llvm::Error::success(); 306 } 307 308 } // namespace exegesis 309