1 //===-- Analysis.h ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Analysis output for benchmark results.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
15 #define LLVM_TOOLS_LLVM_EXEGESIS_ANALYSIS_H
17 #include "Clustering.h"
18 #include "SchedClassResolution.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"
31 #include <unordered_map>
36 // A helper class to analyze benchmark results for a target.
39 Analysis(const llvm::Target
&Target
,
40 std::unique_ptr
<llvm::MCInstrInfo
> InstrInfo
,
41 const InstructionBenchmarkClustering
&Clustering
,
42 double AnalysisInconsistencyEpsilon
,
43 bool AnalysisDisplayUnstableOpcodes
);
45 // Prints a csv of instructions for each cluster.
46 struct PrintClusters
{};
47 // Find potential errors in the scheduling information given measurements.
48 struct PrintSchedClassInconsistencies
{};
50 template <typename Pass
> llvm::Error
run(llvm::raw_ostream
&OS
) const;
53 using ClusterId
= InstructionBenchmarkClustering::ClusterId
;
55 // Represents the intersection of a sched class and a cluster.
56 class SchedClassCluster
{
58 const InstructionBenchmarkClustering::ClusterId
&id() const {
62 const std::vector
<size_t> &getPointIds() const { return PointIds
; }
64 void addPoint(size_t PointId
,
65 const InstructionBenchmarkClustering
&Clustering
);
67 // Return the cluster centroid.
68 const SchedClassClusterCentroid
&getCentroid() const { return Centroid
; }
70 // Returns true if the cluster representative measurements match that of SC.
72 measurementsMatch(const llvm::MCSubtargetInfo
&STI
,
73 const ResolvedSchedClass
&SC
,
74 const InstructionBenchmarkClustering
&Clustering
,
75 const double AnalysisInconsistencyEpsilonSquared_
) const;
78 InstructionBenchmarkClustering::ClusterId ClusterId
;
79 std::vector
<size_t> PointIds
;
80 // Measurement stats for the points in the SchedClassCluster.
81 SchedClassClusterCentroid Centroid
;
84 void printInstructionRowCsv(size_t PointId
, llvm::raw_ostream
&OS
) const;
87 printSchedClassClustersHtml(const std::vector
<SchedClassCluster
> &Clusters
,
88 const ResolvedSchedClass
&SC
,
89 llvm::raw_ostream
&OS
) const;
90 void printSchedClassDescHtml(const ResolvedSchedClass
&SC
,
91 llvm::raw_ostream
&OS
) const;
93 // A pair of (Sched Class, indices of points that belong to the sched
95 struct ResolvedSchedClassAndPoints
{
96 explicit ResolvedSchedClassAndPoints(ResolvedSchedClass
&&RSC
);
98 ResolvedSchedClass RSC
;
99 std::vector
<size_t> PointIds
;
102 // Builds a list of ResolvedSchedClassAndPoints.
103 std::vector
<ResolvedSchedClassAndPoints
> makePointsPerSchedClass() const;
105 template <typename EscapeTag
, EscapeTag Tag
>
106 void writeSnippet(llvm::raw_ostream
&OS
, llvm::ArrayRef
<uint8_t> Bytes
,
107 const char *Separator
) const;
109 const InstructionBenchmarkClustering
&Clustering_
;
110 llvm::MCObjectFileInfo ObjectFileInfo_
;
111 std::unique_ptr
<llvm::MCContext
> Context_
;
112 std::unique_ptr
<llvm::MCSubtargetInfo
> SubtargetInfo_
;
113 std::unique_ptr
<llvm::MCInstrInfo
> InstrInfo_
;
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_
;
118 const double AnalysisInconsistencyEpsilonSquared_
;
119 const bool AnalysisDisplayUnstableOpcodes_
;
122 } // namespace exegesis
125 #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H