1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
//===-- Analysis.cpp --------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Analysis.h"
#include "BenchmarkResult.h"
#include "llvm/Support/FormatVariadic.h"
#include <unordered_set>
#include <vector>
namespace exegesis {
static const char kCsvSep = ',';
static void writeCsvEscaped(llvm::raw_ostream &OS, const std::string &S) {
if (std::find(S.begin(), S.end(), kCsvSep) == S.end()) {
OS << S;
} else {
// Needs escaping.
OS << '"';
for (const char C : S) {
if (C == '"')
OS << "\"\"";
else
OS << C;
}
OS << '"';
}
}
// Prints a row representing an instruction, along with scheduling info and
// point coordinates (measurements).
void Analysis::printInstructionRow(const bool PrintSchedClass,
const size_t PointId,
llvm::raw_ostream &OS) const {
const InstructionBenchmark &Point = Clustering_.getPoints()[PointId];
const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId);
if (ClusterId.isNoise())
OS << "[noise]";
else if (ClusterId.isError())
OS << "[error]";
else
OS << ClusterId.getId();
OS << kCsvSep;
writeCsvEscaped(OS, Point.Key.OpcodeName);
OS << kCsvSep;
writeCsvEscaped(OS, Point.Key.Config);
if (PrintSchedClass) {
OS << kCsvSep;
const auto OpcodeIt = MnemonicToOpcode_.find(Point.Key.OpcodeName);
if (OpcodeIt != MnemonicToOpcode_.end()) {
const unsigned SchedClassId =
InstrInfo_->get(OpcodeIt->second).getSchedClass();
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
const auto &SchedModel = SubtargetInfo_->getSchedModel();
const llvm::MCSchedClassDesc *const SCDesc =
SchedModel.getSchedClassDesc(SchedClassId);
writeCsvEscaped(OS, SCDesc->Name);
#else
OS << SchedClassId;
#endif
}
}
// FIXME: Print the sched class once InstructionBenchmark separates key into
// (mnemonic, mode, opaque).
for (const auto &Measurement : Point.Measurements) {
OS << kCsvSep;
writeCsvEscaped(OS, llvm::formatv("{0:F}", Measurement.Value));
}
OS << "\n";
}
Analysis::Analysis(const llvm::Target &Target,
const InstructionBenchmarkClustering &Clustering)
: Clustering_(Clustering) {
if (Clustering.getPoints().empty())
return;
InstrInfo_.reset(Target.createMCInstrInfo());
const InstructionBenchmark &FirstPoint = Clustering.getPoints().front();
SubtargetInfo_.reset(Target.createMCSubtargetInfo(FirstPoint.LLVMTriple,
FirstPoint.CpuName, ""));
// Build an index of mnemonic->opcode.
for (int I = 0, E = InstrInfo_->getNumOpcodes(); I < E; ++I)
MnemonicToOpcode_.emplace(InstrInfo_->getName(I), I);
}
llvm::Error Analysis::printClusters(llvm::raw_ostream &OS) const {
if (Clustering_.getPoints().empty())
return llvm::Error::success();
// Write the header.
OS << "cluster_id" << kCsvSep << "opcode_name" << kCsvSep << "config"
<< kCsvSep << "sched_class";
for (const auto &Measurement : Clustering_.getPoints().front().Measurements) {
OS << kCsvSep;
writeCsvEscaped(OS, Measurement.Key);
}
OS << "\n";
// Write the points.
const auto &Clusters = Clustering_.getValidClusters();
for (size_t I = 0, E = Clusters.size(); I < E; ++I) {
for (const size_t PointId : Clusters[I].PointIndices) {
printInstructionRow(/*PrintSchedClass*/ true, PointId, OS);
}
OS << "\n\n";
}
return llvm::Error::success();
}
std::unordered_map<unsigned, std::vector<size_t>>
Analysis::makePointsPerSchedClass() const {
std::unordered_map<unsigned, std::vector<size_t>> PointsPerSchedClass;
const auto &Points = Clustering_.getPoints();
for (size_t PointId = 0, E = Points.size(); PointId < E; ++PointId) {
const InstructionBenchmark &Point = Points[PointId];
if (!Point.Error.empty())
continue;
const auto OpcodeIt = MnemonicToOpcode_.find(Point.Key.OpcodeName);
if (OpcodeIt == MnemonicToOpcode_.end())
continue;
const unsigned SchedClassId =
InstrInfo_->get(OpcodeIt->second).getSchedClass();
PointsPerSchedClass[SchedClassId].push_back(PointId);
}
return PointsPerSchedClass;
}
llvm::Error
Analysis::printSchedClassInconsistencies(llvm::raw_ostream &OS) const {
// All the points in a scheduling class should be in the same cluster.
// Print any scheduling class for which this is not the case.
for (const auto &SchedClassAndPoints : makePointsPerSchedClass()) {
std::unordered_set<size_t> ClustersForSchedClass;
for (const size_t PointId : SchedClassAndPoints.second) {
const auto &ClusterId = Clustering_.getClusterIdForPoint(PointId);
if (!ClusterId.isValid())
continue; // Ignore noise and errors.
ClustersForSchedClass.insert(ClusterId.getId());
}
if (ClustersForSchedClass.size() <= 1)
continue; // Nothing weird.
OS << "\nSched Class ";
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
const auto &SchedModel = SubtargetInfo_->getSchedModel();
const llvm::MCSchedClassDesc *const SCDesc =
SchedModel.getSchedClassDesc(SchedClassAndPoints.first);
OS << SCDesc->Name;
#else
OS << SchedClassAndPoints.first;
#endif
OS << " contains instructions with distinct performance "
"characteristics, falling into "
<< ClustersForSchedClass.size() << " clusters:\n";
for (const size_t PointId : SchedClassAndPoints.second) {
printInstructionRow(/*PrintSchedClass*/ false, PointId, OS);
}
}
return llvm::Error::success();
}
} // namespace exegesis
|