blob: c6218052fae5b14e9dc3574046bf47ad7da03a02 [file] [log] [blame]
Clement Courbet37f0ca02018-05-15 12:08:00 +00001//===-- 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 Courbet6d6c1a92018-05-16 08:47:21 +000019#include "llvm/MC/MCInstrInfo.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000020#include "llvm/MC/MCSubtargetInfo.h"
21#include "llvm/Support/Error.h"
Clement Courbet448550d2018-05-17 12:25:18 +000022#include "llvm/Support/TargetRegistry.h"
Clement Courbet37f0ca02018-05-15 12:08:00 +000023#include "llvm/Support/raw_ostream.h"
Clement Courbet72287212018-06-04 11:11:55 +000024#include <set>
Clement Courbet6d6c1a92018-05-16 08:47:21 +000025#include <string>
26#include <unordered_map>
Clement Courbet37f0ca02018-05-15 12:08:00 +000027
28namespace exegesis {
29
Clement Courbet6d6c1a92018-05-16 08:47:21 +000030// A helper class to analyze benchmark results for a target.
31class Analysis {
32public:
Clement Courbet448550d2018-05-17 12:25:18 +000033 Analysis(const llvm::Target &Target,
34 const InstructionBenchmarkClustering &Clustering);
Clement Courbet6d6c1a92018-05-16 08:47:21 +000035
36 // Prints a csv of instructions for each cluster.
Clement Courbetcf210742018-05-17 13:41:28 +000037 struct PrintClusters {};
Clement Courbet448550d2018-05-17 12:25:18 +000038 // Find potential errors in the scheduling information given measurements.
Clement Courbetcf210742018-05-17 13:41:28 +000039 struct PrintSchedClassInconsistencies {};
40
41 template <typename Pass> llvm::Error run(llvm::raw_ostream &OS) const;
Clement Courbet6d6c1a92018-05-16 08:47:21 +000042
Clement Courbet448550d2018-05-17 12:25:18 +000043private:
Clement Courbet72287212018-06-04 11:11:55 +000044 using ClusterId = InstructionBenchmarkClustering::ClusterId;
45
46 // An llvm::MCSchedClassDesc augmented with some additional data.
47 struct SchedClass {
48 SchedClass(const llvm::MCSchedClassDesc &SD,
49 const llvm::MCSubtargetInfo &STI);
50
51 const llvm::MCSchedClassDesc &SCDesc;
52 const llvm::SmallVector<llvm::MCWriteProcResEntry, 8>
53 NonRedundantWriteProcRes;
54 const std::vector<std::pair<uint16_t, float>> IdealizedProcResPressure;
55 };
56
57 // Represents the intersection of a sched class and a cluster.
58 class SchedClassCluster {
59 public:
60 const InstructionBenchmarkClustering::ClusterId &id() const {
61 return ClusterId;
62 }
63
64 const std::vector<size_t> &getPointIds() const { return PointIds; }
65
66 // Return the cluster centroid.
67 const std::vector<BenchmarkMeasureStats> &getRepresentative() const {
68 return Representative;
69 }
70
71 // Returns true if the cluster representative measurements match that of SC.
72 bool
73 measurementsMatch(const llvm::MCSubtargetInfo &STI, const SchedClass &SC,
74 const InstructionBenchmarkClustering &Clustering) const;
75
76 void addPoint(size_t PointId,
77 const InstructionBenchmarkClustering &Clustering);
78
79 private:
80 InstructionBenchmarkClustering::ClusterId ClusterId;
81 std::vector<size_t> PointIds;
82 // Measurement stats for the points in the SchedClassCluster.
83 std::vector<BenchmarkMeasureStats> Representative;
84 };
85
Clement Courbet17d3c252018-05-22 13:31:29 +000086 void printInstructionRowCsv(size_t PointId, llvm::raw_ostream &OS) const;
87
Clement Courbet72287212018-06-04 11:11:55 +000088 void
89 printSchedClassClustersHtml(const std::vector<SchedClassCluster> &Clusters,
90 const SchedClass &SC,
91 llvm::raw_ostream &OS) const;
92 void printSchedClassDescHtml(const SchedClass &SC,
Clement Courbet2637e5f2018-05-24 10:47:05 +000093 llvm::raw_ostream &OS) const;
Clement Courbet448550d2018-05-17 12:25:18 +000094
95 // Builds a map of Sched Class -> indices of points that belong to the sched
96 // class.
97 std::unordered_map<unsigned, std::vector<size_t>>
98 makePointsPerSchedClass() const;
99
100 const InstructionBenchmarkClustering &Clustering_;
101 std::unique_ptr<llvm::MCSubtargetInfo> SubtargetInfo_;
102 std::unique_ptr<llvm::MCInstrInfo> InstrInfo_;
103 std::unordered_map<std::string, unsigned> MnemonicToOpcode_;
Clement Courbet6d6c1a92018-05-16 08:47:21 +0000104};
Clement Courbet37f0ca02018-05-15 12:08:00 +0000105
Clement Courbetdf79e792018-06-01 14:18:02 +0000106// Computes the idealized ProcRes Unit pressure. This is the expected
107// distribution if the CPU scheduler can distribute the load as evenly as
108// possible.
109std::vector<std::pair<uint16_t, float>> computeIdealizedProcResPressure(
110 const llvm::MCSchedModel &SM,
111 llvm::SmallVector<llvm::MCWriteProcResEntry, 8> WPRS);
112
Clement Courbet37f0ca02018-05-15 12:08:00 +0000113} // namespace exegesis
114
115#endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H