[llvm-exegesis] Implements a cache of Instruction objects.
[llvm-core.git] / tools / llvm-exegesis / lib / Analysis.h
blob9ee1493f4e051939aa0e846c5527da2d13f7f774
1 //===-- Analysis.h ----------------------------------------------*- 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 /// \file
11 /// Analysis output for benchmark results.
12 ///
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
16 #define LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
18 #include "Clustering.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>
33 namespace llvm {
34 namespace exegesis {
36 // A helper class to analyze benchmark results for a target.
37 class Analysis {
38 public:
39 Analysis(const llvm::Target &Target,
40 const InstructionBenchmarkClustering &Clustering);
42 // Prints a csv of instructions for each cluster.
43 struct PrintClusters {};
44 // Find potential errors in the scheduling information given measurements.
45 struct PrintSchedClassInconsistencies {};
47 template <typename Pass> llvm::Error run(llvm::raw_ostream &OS) const;
49 private:
50 using ClusterId = InstructionBenchmarkClustering::ClusterId;
52 // An llvm::MCSchedClassDesc augmented with some additional data.
53 struct ResolvedSchedClass {
54 ResolvedSchedClass(const llvm::MCSubtargetInfo &STI,
55 unsigned ResolvedSchedClassId, bool WasVariant);
57 const unsigned SchedClassId;
58 const llvm::MCSchedClassDesc *const SCDesc;
59 const bool WasVariant; // Whether the original class was variant.
60 const llvm::SmallVector<llvm::MCWriteProcResEntry, 8>
61 NonRedundantWriteProcRes;
62 const std::vector<std::pair<uint16_t, float>> IdealizedProcResPressure;
65 // Represents the intersection of a sched class and a cluster.
66 class SchedClassCluster {
67 public:
68 const InstructionBenchmarkClustering::ClusterId &id() const {
69 return ClusterId;
72 const std::vector<size_t> &getPointIds() const { return PointIds; }
74 // Return the cluster centroid.
75 const std::vector<PerInstructionStats> &getRepresentative() const {
76 return Representative;
79 // Returns true if the cluster representative measurements match that of SC.
80 bool
81 measurementsMatch(const llvm::MCSubtargetInfo &STI,
82 const ResolvedSchedClass &SC,
83 const InstructionBenchmarkClustering &Clustering) const;
85 void addPoint(size_t PointId,
86 const InstructionBenchmarkClustering &Clustering);
88 private:
89 InstructionBenchmarkClustering::ClusterId ClusterId;
90 std::vector<size_t> PointIds;
91 // Measurement stats for the points in the SchedClassCluster.
92 std::vector<PerInstructionStats> Representative;
95 void printInstructionRowCsv(size_t PointId, llvm::raw_ostream &OS) const;
97 void
98 printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters,
99 const ResolvedSchedClass &SC,
100 llvm::raw_ostream &OS) const;
101 void printSchedClassDescHtml(const ResolvedSchedClass &SC,
102 llvm::raw_ostream &OS) const;
104 // A pair of (Sched Class, indices of points that belong to the sched
105 // class).
106 struct ResolvedSchedClassAndPoints {
107 explicit ResolvedSchedClassAndPoints(ResolvedSchedClass &&RSC);
109 ResolvedSchedClass RSC;
110 std::vector<size_t> PointIds;
113 // Builds a list of ResolvedSchedClassAndPoints.
114 std::vector<ResolvedSchedClassAndPoints> makePointsPerSchedClass() const;
116 template <typename EscapeTag, EscapeTag Tag>
117 void writeSnippet(llvm::raw_ostream &OS, llvm::ArrayRef<uint8_t> Bytes,
118 const char *Separator) const;
120 const InstructionBenchmarkClustering &Clustering_;
121 llvm::MCObjectFileInfo ObjectFileInfo_;
122 std::unique_ptr<llvm::MCContext> Context_;
123 std::unique_ptr<llvm::MCSubtargetInfo> SubtargetInfo_;
124 std::unique_ptr<llvm::MCInstrInfo> InstrInfo_;
125 std::unique_ptr<llvm::MCRegisterInfo> RegInfo_;
126 std::unique_ptr<llvm::MCAsmInfo> AsmInfo_;
127 std::unique_ptr<llvm::MCInstPrinter> InstPrinter_;
128 std::unique_ptr<llvm::MCDisassembler> Disasm_;
131 // Computes the idealized ProcRes Unit pressure. This is the expected
132 // distribution if the CPU scheduler can distribute the load as evenly as
133 // possible.
134 std::vector<std::pair<uint16_t, float>> computeIdealizedProcResPressure(
135 const llvm::MCSchedModel &SM,
136 llvm::SmallVector<llvm::MCWriteProcResEntry, 8> WPRS);
138 } // namespace exegesis
139 } // namespace llvm
141 #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H