xref: /llvm-project/llvm/tools/llvm-exegesis/lib/Analysis.cpp (revision 3d479fe81cbc533ea4bbe74e51c3a1dd3ae8ffef)
1 
2 #include "Analysis.h"
3 #include "llvm/Support/Format.h"
4 
5 namespace exegesis {
6 
7 namespace {
8 
9 // Prints a row representing an instruction, along with scheduling info and
10 // point coordinates (measurements).
11 void renderInstructionRow(const InstructionBenchmark &Point,
12                           const size_t NameLen, llvm::raw_ostream &OS) {
13   OS << llvm::format("%*s", NameLen, Point.AsmTmpl.Name.c_str());
14   for (const auto &Measurement : Point.Measurements) {
15     OS << llvm::format("   %*.2f", Measurement.Key.size(), Measurement.Value);
16   }
17   OS << "\n";
18 }
19 
20 void analyzeCluster(const std::vector<InstructionBenchmark> &Points,
21                     const llvm::MCSubtargetInfo &STI,
22                     const InstructionBenchmarkClustering::Cluster &Cluster,
23                     llvm::raw_ostream &OS) {
24   // TODO:
25   // std::sort(Cluster.PointIndices.begin(), Cluster.PointIndices.end(),
26   // [](int PointIdA, int PointIdB) { return GetSchedClass(Points[PointIdA]) <
27   // GetSchedClass(Points[PointIdB]); });
28   OS << "Cluster:\n";
29   // Get max length of the name for alignement.
30   size_t NameLen = 0;
31   for (const auto &PointId : Cluster.PointIndices) {
32     NameLen = std::max(NameLen, Points[PointId].AsmTmpl.Name.size());
33   }
34 
35   // Print all points.
36   for (const auto &PointId : Cluster.PointIndices) {
37     renderInstructionRow(Points[PointId], NameLen, OS);
38   }
39 }
40 
41 } // namespace
42 
43 llvm::Error
44 printAnalysisClusters(const InstructionBenchmarkClustering &Clustering,
45                       const llvm::MCSubtargetInfo &STI, llvm::raw_ostream &OS) {
46 
47   for (const auto &Cluster : Clustering.getValidClusters()) {
48     analyzeCluster(Clustering.getPoints(), STI, Cluster, OS);
49     OS << "\n\n\n";
50   }
51 
52   return llvm::Error::success();
53 }
54 
55 } // namespace exegesis
56