Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H |
| 16 | #define LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H |
| 17 | |
| 18 | #include "Clustering.h" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame^] | 19 | #include "llvm/MC/MCContext.h" |
| 20 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 21 | #include "llvm/MC/MCInstPrinter.h" |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstrInfo.h" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame^] | 23 | #include "llvm/MC/MCObjectFileInfo.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSubtargetInfo.h" |
| 25 | #include "llvm/Support/Error.h" |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 26 | #include "llvm/Support/TargetRegistry.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame^] | 28 | #include <memory> |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 29 | #include <set> |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <unordered_map> |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 32 | |
| 33 | namespace exegesis { |
| 34 | |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 35 | // A helper class to analyze benchmark results for a target. |
| 36 | class Analysis { |
| 37 | public: |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 38 | Analysis(const llvm::Target &Target, |
| 39 | const InstructionBenchmarkClustering &Clustering); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 40 | |
| 41 | // Prints a csv of instructions for each cluster. |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 42 | struct PrintClusters {}; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 43 | // Find potential errors in the scheduling information given measurements. |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 44 | struct PrintSchedClassInconsistencies {}; |
| 45 | |
| 46 | template <typename Pass> llvm::Error run(llvm::raw_ostream &OS) const; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 47 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 48 | private: |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 49 | using ClusterId = InstructionBenchmarkClustering::ClusterId; |
| 50 | |
| 51 | // An llvm::MCSchedClassDesc augmented with some additional data. |
| 52 | struct SchedClass { |
| 53 | SchedClass(const llvm::MCSchedClassDesc &SD, |
| 54 | const llvm::MCSubtargetInfo &STI); |
| 55 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame^] | 56 | const llvm::MCSchedClassDesc *const SCDesc; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 57 | const llvm::SmallVector<llvm::MCWriteProcResEntry, 8> |
| 58 | NonRedundantWriteProcRes; |
| 59 | const std::vector<std::pair<uint16_t, float>> IdealizedProcResPressure; |
| 60 | }; |
| 61 | |
| 62 | // Represents the intersection of a sched class and a cluster. |
| 63 | class SchedClassCluster { |
| 64 | public: |
| 65 | const InstructionBenchmarkClustering::ClusterId &id() const { |
| 66 | return ClusterId; |
| 67 | } |
| 68 | |
| 69 | const std::vector<size_t> &getPointIds() const { return PointIds; } |
| 70 | |
| 71 | // Return the cluster centroid. |
| 72 | const std::vector<BenchmarkMeasureStats> &getRepresentative() const { |
| 73 | return Representative; |
| 74 | } |
| 75 | |
| 76 | // Returns true if the cluster representative measurements match that of SC. |
| 77 | bool |
| 78 | measurementsMatch(const llvm::MCSubtargetInfo &STI, const SchedClass &SC, |
| 79 | const InstructionBenchmarkClustering &Clustering) const; |
| 80 | |
| 81 | void addPoint(size_t PointId, |
| 82 | const InstructionBenchmarkClustering &Clustering); |
| 83 | |
| 84 | private: |
| 85 | InstructionBenchmarkClustering::ClusterId ClusterId; |
| 86 | std::vector<size_t> PointIds; |
| 87 | // Measurement stats for the points in the SchedClassCluster. |
| 88 | std::vector<BenchmarkMeasureStats> Representative; |
| 89 | }; |
| 90 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 91 | void printInstructionRowCsv(size_t PointId, llvm::raw_ostream &OS) const; |
| 92 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 93 | void |
| 94 | printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters, |
| 95 | const SchedClass &SC, |
| 96 | llvm::raw_ostream &OS) const; |
| 97 | void printSchedClassDescHtml(const SchedClass &SC, |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 98 | llvm::raw_ostream &OS) const; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 99 | |
| 100 | // Builds a map of Sched Class -> indices of points that belong to the sched |
| 101 | // class. |
| 102 | std::unordered_map<unsigned, std::vector<size_t>> |
| 103 | makePointsPerSchedClass() const; |
| 104 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame^] | 105 | template <typename EscapeTag, EscapeTag Tag> |
| 106 | void writeSnippet(llvm::raw_ostream &OS, llvm::ArrayRef<uint8_t> Bytes, |
| 107 | const char *Separator) const; |
| 108 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 109 | const InstructionBenchmarkClustering &Clustering_; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame^] | 110 | llvm::MCObjectFileInfo ObjectFileInfo_; |
| 111 | std::unique_ptr<llvm::MCContext> Context_; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 112 | std::unique_ptr<llvm::MCSubtargetInfo> SubtargetInfo_; |
| 113 | std::unique_ptr<llvm::MCInstrInfo> InstrInfo_; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame^] | 114 | std::unique_ptr<llvm::MCRegisterInfo> RegInfo_; |
| 115 | std::unique_ptr<llvm::MCAsmInfo> AsmInfo_; |
| 116 | std::unique_ptr<llvm::MCInstPrinter> InstPrinter_; |
| 117 | std::unique_ptr<llvm::MCDisassembler> Disasm_; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 118 | std::unordered_map<std::string, unsigned> MnemonicToOpcode_; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 119 | }; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 120 | |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 121 | // Computes the idealized ProcRes Unit pressure. This is the expected |
| 122 | // distribution if the CPU scheduler can distribute the load as evenly as |
| 123 | // possible. |
| 124 | std::vector<std::pair<uint16_t, float>> computeIdealizedProcResPressure( |
| 125 | const llvm::MCSchedModel &SM, |
| 126 | llvm::SmallVector<llvm::MCWriteProcResEntry, 8> WPRS); |
| 127 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 128 | } // namespace exegesis |
| 129 | |
| 130 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |