• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Analysis.h ----------------------------------------------*- 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 /// \file
10 /// Analysis output for benchmark results.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
15 #define LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
16 
17 #include "Clustering.h"
18 #include "SchedClassResolution.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
21 #include "llvm/MC/MCInstPrinter.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <unordered_map>
32 
33 namespace llvm {
34 namespace exegesis {
35 
36 // A helper class to analyze benchmark results for a target.
37 class Analysis {
38 public:
39   Analysis(const Target &Target, std::unique_ptr<MCInstrInfo> InstrInfo,
40            const InstructionBenchmarkClustering &Clustering,
41            double AnalysisInconsistencyEpsilon,
42            bool AnalysisDisplayUnstableOpcodes);
43 
44   // Prints a csv of instructions for each cluster.
45   struct PrintClusters {};
46   // Find potential errors in the scheduling information given measurements.
47   struct PrintSchedClassInconsistencies {};
48 
49   template <typename Pass> Error run(raw_ostream &OS) const;
50 
51 private:
52   using ClusterId = InstructionBenchmarkClustering::ClusterId;
53 
54   // Represents the intersection of a sched class and a cluster.
55   class SchedClassCluster {
56   public:
id()57     const InstructionBenchmarkClustering::ClusterId &id() const {
58       return ClusterId;
59     }
60 
getPointIds()61     const std::vector<size_t> &getPointIds() const { return PointIds; }
62 
63     void addPoint(size_t PointId,
64                   const InstructionBenchmarkClustering &Clustering);
65 
66     // Return the cluster centroid.
getCentroid()67     const SchedClassClusterCentroid &getCentroid() const { return Centroid; }
68 
69     // Returns true if the cluster representative measurements match that of SC.
70     bool
71     measurementsMatch(const MCSubtargetInfo &STI, const ResolvedSchedClass &SC,
72                       const InstructionBenchmarkClustering &Clustering,
73                       const double AnalysisInconsistencyEpsilonSquared_) const;
74 
75   private:
76     InstructionBenchmarkClustering::ClusterId ClusterId;
77     std::vector<size_t> PointIds;
78     // Measurement stats for the points in the SchedClassCluster.
79     SchedClassClusterCentroid Centroid;
80   };
81 
82   void printInstructionRowCsv(size_t PointId, raw_ostream &OS) const;
83 
84   void printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId &Id,
85                            StringRef display_name, llvm::raw_ostream &OS) const;
86 
87   void printPointHtml(const InstructionBenchmark &Point,
88                       llvm::raw_ostream &OS) const;
89 
90   void
91   printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters,
92                               const ResolvedSchedClass &SC,
93                               raw_ostream &OS) const;
94   void printSchedClassDescHtml(const ResolvedSchedClass &SC,
95                                raw_ostream &OS) const;
96 
97   // A pair of (Sched Class, indices of points that belong to the sched
98   // class).
99   struct ResolvedSchedClassAndPoints {
100     explicit ResolvedSchedClassAndPoints(ResolvedSchedClass &&RSC);
101 
102     ResolvedSchedClass RSC;
103     std::vector<size_t> PointIds;
104   };
105 
106   // Builds a list of ResolvedSchedClassAndPoints.
107   std::vector<ResolvedSchedClassAndPoints> makePointsPerSchedClass() const;
108 
109   template <typename EscapeTag, EscapeTag Tag>
110   void writeSnippet(raw_ostream &OS, ArrayRef<uint8_t> Bytes,
111                     const char *Separator) const;
112 
113   const InstructionBenchmarkClustering &Clustering_;
114   MCObjectFileInfo ObjectFileInfo_;
115   std::unique_ptr<MCContext> Context_;
116   std::unique_ptr<MCSubtargetInfo> SubtargetInfo_;
117   std::unique_ptr<MCInstrInfo> InstrInfo_;
118   std::unique_ptr<MCRegisterInfo> RegInfo_;
119   std::unique_ptr<MCAsmInfo> AsmInfo_;
120   std::unique_ptr<MCInstPrinter> InstPrinter_;
121   std::unique_ptr<MCDisassembler> Disasm_;
122   const double AnalysisInconsistencyEpsilonSquared_;
123   const bool AnalysisDisplayUnstableOpcodes_;
124 };
125 
126 } // namespace exegesis
127 } // namespace llvm
128 
129 #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H
130