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 Target
&Target
, std::unique_ptr
<MCInstrInfo
> InstrInfo
,
40 const InstructionBenchmarkClustering
&Clustering
,
41 double AnalysisInconsistencyEpsilon
,
42 bool AnalysisDisplayUnstableOpcodes
);
44 // Prints a csv of instructions for each cluster.
45 struct PrintClusters
{};
46 // Find potential errors in the scheduling information given measurements.
47 struct PrintSchedClassInconsistencies
{};
49 template <typename Pass
> Error
run(raw_ostream
&OS
) const;
52 using ClusterId
= InstructionBenchmarkClustering::ClusterId
;
54 // Represents the intersection of a sched class and a cluster.
55 class SchedClassCluster
{
57 const InstructionBenchmarkClustering::ClusterId
&id() const {
61 const std::vector
<size_t> &getPointIds() const { return PointIds
; }
63 void addPoint(size_t PointId
,
64 const InstructionBenchmarkClustering
&Clustering
);
66 // Return the cluster centroid.
67 const SchedClassClusterCentroid
&getCentroid() const { return Centroid
; }
69 // Returns true if the cluster representative measurements match that of SC.
71 measurementsMatch(const MCSubtargetInfo
&STI
, const ResolvedSchedClass
&SC
,
72 const InstructionBenchmarkClustering
&Clustering
,
73 const double AnalysisInconsistencyEpsilonSquared_
) const;
76 InstructionBenchmarkClustering::ClusterId ClusterId
;
77 std::vector
<size_t> PointIds
;
78 // Measurement stats for the points in the SchedClassCluster.
79 SchedClassClusterCentroid Centroid
;
82 void printInstructionRowCsv(size_t PointId
, raw_ostream
&OS
) const;
84 void printClusterRawHtml(const InstructionBenchmarkClustering::ClusterId
&Id
,
85 StringRef display_name
, llvm::raw_ostream
&OS
) const;
87 void printPointHtml(const InstructionBenchmark
&Point
,
88 llvm::raw_ostream
&OS
) const;
91 printSchedClassClustersHtml(const std::vector
<SchedClassCluster
> &Clusters
,
92 const ResolvedSchedClass
&SC
,
93 raw_ostream
&OS
) const;
94 void printSchedClassDescHtml(const ResolvedSchedClass
&SC
,
95 raw_ostream
&OS
) const;
97 // A pair of (Sched Class, indices of points that belong to the sched
99 struct ResolvedSchedClassAndPoints
{
100 explicit ResolvedSchedClassAndPoints(ResolvedSchedClass
&&RSC
);
102 ResolvedSchedClass RSC
;
103 std::vector
<size_t> PointIds
;
106 // Builds a list of ResolvedSchedClassAndPoints.
107 std::vector
<ResolvedSchedClassAndPoints
> makePointsPerSchedClass() const;
109 template <typename EscapeTag
, EscapeTag Tag
>
110 void writeSnippet(raw_ostream
&OS
, ArrayRef
<uint8_t> Bytes
,
111 const char *Separator
) const;
113 const InstructionBenchmarkClustering
&Clustering_
;
114 MCObjectFileInfo ObjectFileInfo_
;
115 std::unique_ptr
<MCContext
> Context_
;
116 std::unique_ptr
<MCSubtargetInfo
> SubtargetInfo_
;
117 std::unique_ptr
<MCInstrInfo
> InstrInfo_
;
118 std::unique_ptr
<MCRegisterInfo
> RegInfo_
;
119 std::unique_ptr
<MCAsmInfo
> AsmInfo_
;
120 std::unique_ptr
<MCInstPrinter
> InstPrinter_
;
121 std::unique_ptr
<MCDisassembler
> Disasm_
;
122 const double AnalysisInconsistencyEpsilonSquared_
;
123 const bool AnalysisDisplayUnstableOpcodes_
;
126 } // namespace exegesis
129 #endif // LLVM_TOOLS_LLVM_EXEGESIS_CLUSTERING_H