Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 1 | //===-- Analysis.h ----------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 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" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" |
| 19 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 20 | #include "llvm/MC/MCInstPrinter.h" |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCInstrInfo.h" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCObjectFileInfo.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSubtargetInfo.h" |
| 24 | #include "llvm/Support/Error.h" |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 25 | #include "llvm/Support/TargetRegistry.h" |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 27 | #include <memory> |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 28 | #include <set> |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <unordered_map> |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 31 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 32 | namespace llvm { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 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, |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 39 | std::unique_ptr<llvm::MCInstrInfo> InstrInfo, |
| 40 | const InstructionBenchmarkClustering &Clustering, |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 41 | double AnalysisInconsistencyEpsilon, |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 42 | bool AnalysisDisplayUnstableOpcodes); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 43 | |
| 44 | // Prints a csv of instructions for each cluster. |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 45 | struct PrintClusters {}; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 46 | // Find potential errors in the scheduling information given measurements. |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 47 | struct PrintSchedClassInconsistencies {}; |
| 48 | |
| 49 | template <typename Pass> llvm::Error run(llvm::raw_ostream &OS) const; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 50 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 51 | private: |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 52 | using ClusterId = InstructionBenchmarkClustering::ClusterId; |
| 53 | |
| 54 | // An llvm::MCSchedClassDesc augmented with some additional data. |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 55 | struct ResolvedSchedClass { |
| 56 | ResolvedSchedClass(const llvm::MCSubtargetInfo &STI, |
| 57 | unsigned ResolvedSchedClassId, bool WasVariant); |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 58 | |
Clement Courbet | 8a5a6be | 2018-10-03 12:35:35 +0000 | [diff] [blame] | 59 | const unsigned SchedClassId; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 60 | const llvm::MCSchedClassDesc *const SCDesc; |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 61 | const bool WasVariant; // Whether the original class was variant. |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 62 | const llvm::SmallVector<llvm::MCWriteProcResEntry, 8> |
| 63 | NonRedundantWriteProcRes; |
| 64 | const std::vector<std::pair<uint16_t, float>> IdealizedProcResPressure; |
| 65 | }; |
| 66 | |
| 67 | // Represents the intersection of a sched class and a cluster. |
| 68 | class SchedClassCluster { |
| 69 | public: |
| 70 | const InstructionBenchmarkClustering::ClusterId &id() const { |
| 71 | return ClusterId; |
| 72 | } |
| 73 | |
| 74 | const std::vector<size_t> &getPointIds() const { return PointIds; } |
| 75 | |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame] | 76 | void addPoint(size_t PointId, |
| 77 | const InstructionBenchmarkClustering &Clustering); |
| 78 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 79 | // Return the cluster centroid. |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame] | 80 | const SchedClassClusterCentroid &getCentroid() const { return Centroid; } |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 81 | |
Roman Lebedev | b8fb15d | 2019-03-29 11:36:08 +0000 | [diff] [blame^] | 82 | std::vector<BenchmarkMeasure> |
| 83 | getSchedClassPoint(InstructionBenchmark::ModeE Mode, |
| 84 | const llvm::MCSubtargetInfo &STI, |
| 85 | const ResolvedSchedClass &SC, |
| 86 | ArrayRef<PerInstructionStats> Representative) const; |
| 87 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 88 | // Returns true if the cluster representative measurements match that of SC. |
| 89 | bool |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 90 | measurementsMatch(const llvm::MCSubtargetInfo &STI, |
| 91 | const ResolvedSchedClass &SC, |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 92 | const InstructionBenchmarkClustering &Clustering, |
| 93 | const double AnalysisInconsistencyEpsilonSquared_) const; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 94 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 95 | private: |
| 96 | InstructionBenchmarkClustering::ClusterId ClusterId; |
| 97 | std::vector<size_t> PointIds; |
| 98 | // Measurement stats for the points in the SchedClassCluster. |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame] | 99 | SchedClassClusterCentroid Centroid; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 102 | void printInstructionRowCsv(size_t PointId, llvm::raw_ostream &OS) const; |
| 103 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 104 | void |
| 105 | printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters, |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 106 | const ResolvedSchedClass &SC, |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 107 | llvm::raw_ostream &OS) const; |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 108 | void printSchedClassDescHtml(const ResolvedSchedClass &SC, |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 109 | llvm::raw_ostream &OS) const; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 110 | |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 111 | // A pair of (Sched Class, indices of points that belong to the sched |
| 112 | // class). |
| 113 | struct ResolvedSchedClassAndPoints { |
| 114 | explicit ResolvedSchedClassAndPoints(ResolvedSchedClass &&RSC); |
| 115 | |
| 116 | ResolvedSchedClass RSC; |
| 117 | std::vector<size_t> PointIds; |
| 118 | }; |
| 119 | |
| 120 | // Builds a list of ResolvedSchedClassAndPoints. |
| 121 | std::vector<ResolvedSchedClassAndPoints> makePointsPerSchedClass() const; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 122 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 123 | template <typename EscapeTag, EscapeTag Tag> |
| 124 | void writeSnippet(llvm::raw_ostream &OS, llvm::ArrayRef<uint8_t> Bytes, |
| 125 | const char *Separator) const; |
| 126 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 127 | const InstructionBenchmarkClustering &Clustering_; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 128 | llvm::MCObjectFileInfo ObjectFileInfo_; |
| 129 | std::unique_ptr<llvm::MCContext> Context_; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 130 | std::unique_ptr<llvm::MCSubtargetInfo> SubtargetInfo_; |
| 131 | std::unique_ptr<llvm::MCInstrInfo> InstrInfo_; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 132 | std::unique_ptr<llvm::MCRegisterInfo> RegInfo_; |
| 133 | std::unique_ptr<llvm::MCAsmInfo> AsmInfo_; |
| 134 | std::unique_ptr<llvm::MCInstPrinter> InstPrinter_; |
| 135 | std::unique_ptr<llvm::MCDisassembler> Disasm_; |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 136 | const double AnalysisInconsistencyEpsilonSquared_; |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 137 | const bool AnalysisDisplayUnstableOpcodes_; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 138 | }; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 139 | |
Clement Courbet | df79e79 | 2018-06-01 14:18:02 +0000 | [diff] [blame] | 140 | // Computes the idealized ProcRes Unit pressure. This is the expected |
| 141 | // distribution if the CPU scheduler can distribute the load as evenly as |
| 142 | // possible. |
| 143 | std::vector<std::pair<uint16_t, float>> computeIdealizedProcResPressure( |
| 144 | const llvm::MCSchedModel &SM, |
| 145 | llvm::SmallVector<llvm::MCWriteProcResEntry, 8> WPRS); |
| 146 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 147 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 148 | } // namespace llvm |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 149 | |
| 150 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |