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" |
Roman Lebedev | 1d1330c | 2019-03-29 14:24:27 +0000 | [diff] [blame^] | 18 | #include "SchedClassResolution.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 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 33 | namespace llvm { |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 34 | namespace exegesis { |
| 35 | |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 36 | // A helper class to analyze benchmark results for a target. |
| 37 | class Analysis { |
| 38 | public: |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 39 | Analysis(const llvm::Target &Target, |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 40 | std::unique_ptr<llvm::MCInstrInfo> InstrInfo, |
| 41 | const InstructionBenchmarkClustering &Clustering, |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 42 | double AnalysisInconsistencyEpsilon, |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 43 | bool AnalysisDisplayUnstableOpcodes); |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 44 | |
| 45 | // Prints a csv of instructions for each cluster. |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 46 | struct PrintClusters {}; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 47 | // Find potential errors in the scheduling information given measurements. |
Clement Courbet | cf21074 | 2018-05-17 13:41:28 +0000 | [diff] [blame] | 48 | struct PrintSchedClassInconsistencies {}; |
| 49 | |
| 50 | template <typename Pass> llvm::Error run(llvm::raw_ostream &OS) const; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 51 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 52 | private: |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 53 | using ClusterId = InstructionBenchmarkClustering::ClusterId; |
| 54 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 55 | // Represents the intersection of a sched class and a cluster. |
| 56 | class SchedClassCluster { |
| 57 | public: |
| 58 | const InstructionBenchmarkClustering::ClusterId &id() const { |
| 59 | return ClusterId; |
| 60 | } |
| 61 | |
| 62 | const std::vector<size_t> &getPointIds() const { return PointIds; } |
| 63 | |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame] | 64 | void addPoint(size_t PointId, |
| 65 | const InstructionBenchmarkClustering &Clustering); |
| 66 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 67 | // Return the cluster centroid. |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame] | 68 | const SchedClassClusterCentroid &getCentroid() const { return Centroid; } |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 69 | |
Roman Lebedev | b8fb15d | 2019-03-29 11:36:08 +0000 | [diff] [blame] | 70 | std::vector<BenchmarkMeasure> |
| 71 | getSchedClassPoint(InstructionBenchmark::ModeE Mode, |
| 72 | const llvm::MCSubtargetInfo &STI, |
| 73 | const ResolvedSchedClass &SC, |
| 74 | ArrayRef<PerInstructionStats> Representative) const; |
| 75 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 76 | // Returns true if the cluster representative measurements match that of SC. |
| 77 | bool |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 78 | measurementsMatch(const llvm::MCSubtargetInfo &STI, |
| 79 | const ResolvedSchedClass &SC, |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 80 | const InstructionBenchmarkClustering &Clustering, |
| 81 | const double AnalysisInconsistencyEpsilonSquared_) const; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 82 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 83 | private: |
| 84 | InstructionBenchmarkClustering::ClusterId ClusterId; |
| 85 | std::vector<size_t> PointIds; |
| 86 | // Measurement stats for the points in the SchedClassCluster. |
Roman Lebedev | c2423fe | 2019-03-28 08:55:01 +0000 | [diff] [blame] | 87 | SchedClassClusterCentroid Centroid; |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
Clement Courbet | 17d3c25 | 2018-05-22 13:31:29 +0000 | [diff] [blame] | 90 | void printInstructionRowCsv(size_t PointId, llvm::raw_ostream &OS) const; |
| 91 | |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 92 | void |
| 93 | printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters, |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 94 | const ResolvedSchedClass &SC, |
Clement Courbet | 7228721 | 2018-06-04 11:11:55 +0000 | [diff] [blame] | 95 | llvm::raw_ostream &OS) const; |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 96 | void printSchedClassDescHtml(const ResolvedSchedClass &SC, |
Clement Courbet | 2637e5f | 2018-05-24 10:47:05 +0000 | [diff] [blame] | 97 | llvm::raw_ostream &OS) const; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 98 | |
Clement Courbet | d5a3955 | 2018-10-03 11:50:25 +0000 | [diff] [blame] | 99 | // A pair of (Sched Class, indices of points that belong to the sched |
| 100 | // class). |
| 101 | struct ResolvedSchedClassAndPoints { |
| 102 | explicit ResolvedSchedClassAndPoints(ResolvedSchedClass &&RSC); |
| 103 | |
| 104 | ResolvedSchedClass RSC; |
| 105 | std::vector<size_t> PointIds; |
| 106 | }; |
| 107 | |
| 108 | // Builds a list of ResolvedSchedClassAndPoints. |
| 109 | std::vector<ResolvedSchedClassAndPoints> makePointsPerSchedClass() const; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 110 | |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 111 | template <typename EscapeTag, EscapeTag Tag> |
| 112 | void writeSnippet(llvm::raw_ostream &OS, llvm::ArrayRef<uint8_t> Bytes, |
| 113 | const char *Separator) const; |
| 114 | |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 115 | const InstructionBenchmarkClustering &Clustering_; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 116 | llvm::MCObjectFileInfo ObjectFileInfo_; |
| 117 | std::unique_ptr<llvm::MCContext> Context_; |
Clement Courbet | 448550d | 2018-05-17 12:25:18 +0000 | [diff] [blame] | 118 | std::unique_ptr<llvm::MCSubtargetInfo> SubtargetInfo_; |
| 119 | std::unique_ptr<llvm::MCInstrInfo> InstrInfo_; |
Clement Courbet | 4273e1e | 2018-06-15 07:30:45 +0000 | [diff] [blame] | 120 | std::unique_ptr<llvm::MCRegisterInfo> RegInfo_; |
| 121 | std::unique_ptr<llvm::MCAsmInfo> AsmInfo_; |
| 122 | std::unique_ptr<llvm::MCInstPrinter> InstPrinter_; |
| 123 | std::unique_ptr<llvm::MCDisassembler> Disasm_; |
Roman Lebedev | 542e5d7 | 2019-02-25 09:36:12 +0000 | [diff] [blame] | 124 | const double AnalysisInconsistencyEpsilonSquared_; |
Roman Lebedev | 6971639 | 2019-02-20 09:14:04 +0000 | [diff] [blame] | 125 | const bool AnalysisDisplayUnstableOpcodes_; |
Clement Courbet | 6d6c1a9 | 2018-05-16 08:47:21 +0000 | [diff] [blame] | 126 | }; |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 127 | |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 128 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 129 | } // namespace llvm |
Clement Courbet | 37f0ca0 | 2018-05-15 12:08:00 +0000 | [diff] [blame] | 130 | |
| 131 | #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H |