blob: bebb535f431b39f406870af95b1fd443a9851a43 [file] [log] [blame]
Clement Courbet96715412018-05-07 09:09:48 +00001//===-- Clustering.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Courbet96715412018-05-07 09:09:48 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Clustering.h"
Clement Courbet176388c2019-01-02 09:21:00 +000010#include "llvm/ADT/SetVector.h"
Roman Lebedev69716392019-02-20 09:14:04 +000011#include "llvm/ADT/SmallSet.h"
Roman Lebedev8aecb0c2018-11-19 13:28:22 +000012#include "llvm/ADT/SmallVector.h"
Roman Lebedev69716392019-02-20 09:14:04 +000013#include <algorithm>
Clement Courbet96715412018-05-07 09:09:48 +000014#include <string>
Roman Lebedev69716392019-02-20 09:14:04 +000015#include <vector>
Clement Courbet96715412018-05-07 09:09:48 +000016
Fangrui Song32401af2018-10-22 17:10:47 +000017namespace llvm {
Clement Courbet96715412018-05-07 09:09:48 +000018namespace exegesis {
19
20// The clustering problem has the following characteristics:
21// (A) - Low dimension (dimensions are typically proc resource units,
22// typically < 10).
23// (B) - Number of points : ~thousands (points are measurements of an MCInst)
24// (C) - Number of clusters: ~tens.
25// (D) - The number of clusters is not known /a priory/.
Clement Courbetdffc4ca2018-05-14 11:35:37 +000026// (E) - The amount of noise is relatively small.
Clement Courbet96715412018-05-07 09:09:48 +000027// The problem is rather small. In terms of algorithms, (D) disqualifies
28// k-means and makes algorithms such as DBSCAN[1] or OPTICS[2] more applicable.
29//
30// We've used DBSCAN here because it's simple to implement. This is a pretty
31// straightforward and inefficient implementation of the pseudocode in [2].
32//
33// [1] https://en.wikipedia.org/wiki/DBSCAN
34// [2] https://en.wikipedia.org/wiki/OPTICS_algorithm
35
Clement Courbet96715412018-05-07 09:09:48 +000036// Finds the points at distance less than sqrt(EpsilonSquared) of Q (not
37// including Q).
Roman Lebedev666d8552018-11-19 13:28:31 +000038void InstructionBenchmarkClustering::rangeQuery(
Roman Lebedev71fdb572018-11-19 13:28:41 +000039 const size_t Q, std::vector<size_t> &Neighbors) const {
Roman Lebedev666d8552018-11-19 13:28:31 +000040 Neighbors.clear();
Roman Lebedev71fdb572018-11-19 13:28:41 +000041 Neighbors.reserve(Points_.size() - 1); // The Q itself isn't a neighbor.
Clement Courbet72287212018-06-04 11:11:55 +000042 const auto &QMeasurements = Points_[Q].Measurements;
43 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000044 if (P == Q)
45 continue;
Clement Courbet72287212018-06-04 11:11:55 +000046 const auto &PMeasurements = Points_[P].Measurements;
Clement Courbet96715412018-05-07 09:09:48 +000047 if (PMeasurements.empty()) // Error point.
48 continue;
Clement Courbet72287212018-06-04 11:11:55 +000049 if (isNeighbour(PMeasurements, QMeasurements)) {
Clement Courbet96715412018-05-07 09:09:48 +000050 Neighbors.push_back(P);
51 }
52 }
Clement Courbet96715412018-05-07 09:09:48 +000053}
54
Clement Courbet37f0ca02018-05-15 12:08:00 +000055InstructionBenchmarkClustering::InstructionBenchmarkClustering(
Clement Courbet72287212018-06-04 11:11:55 +000056 const std::vector<InstructionBenchmark> &Points,
57 const double EpsilonSquared)
58 : Points_(Points), EpsilonSquared_(EpsilonSquared),
59 NoiseCluster_(ClusterId::noise()), ErrorCluster_(ClusterId::error()) {}
Clement Courbet96715412018-05-07 09:09:48 +000060
Clement Courbet37f0ca02018-05-15 12:08:00 +000061llvm::Error InstructionBenchmarkClustering::validateAndSetup() {
62 ClusterIdForPoint_.resize(Points_.size());
Clement Courbet96715412018-05-07 09:09:48 +000063 // Mark erroneous measurements out.
64 // All points must have the same number of dimensions, in the same order.
65 const std::vector<BenchmarkMeasure> *LastMeasurement = nullptr;
Clement Courbet37f0ca02018-05-15 12:08:00 +000066 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
67 const auto &Point = Points_[P];
Clement Courbet96715412018-05-07 09:09:48 +000068 if (!Point.Error.empty()) {
69 ClusterIdForPoint_[P] = ClusterId::error();
70 ErrorCluster_.PointIndices.push_back(P);
71 continue;
72 }
73 const auto *CurMeasurement = &Point.Measurements;
74 if (LastMeasurement) {
75 if (LastMeasurement->size() != CurMeasurement->size()) {
76 return llvm::make_error<llvm::StringError>(
77 "inconsistent measurement dimensions",
78 llvm::inconvertibleErrorCode());
79 }
80 for (size_t I = 0, E = LastMeasurement->size(); I < E; ++I) {
81 if (LastMeasurement->at(I).Key != CurMeasurement->at(I).Key) {
82 return llvm::make_error<llvm::StringError>(
83 "inconsistent measurement dimensions keys",
84 llvm::inconvertibleErrorCode());
85 }
86 }
87 }
88 LastMeasurement = CurMeasurement;
89 }
90 if (LastMeasurement) {
91 NumDimensions_ = LastMeasurement->size();
92 }
93 return llvm::Error::success();
94}
95
Clement Courbet72287212018-06-04 11:11:55 +000096void InstructionBenchmarkClustering::dbScan(const size_t MinPts) {
Clement Courbet176388c2019-01-02 09:21:00 +000097 std::vector<size_t> Neighbors; // Persistent buffer to avoid allocs.
98 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
Clement Courbet96715412018-05-07 09:09:48 +000099 if (!ClusterIdForPoint_[P].isUndef())
100 continue; // Previously processed in inner loop.
Roman Lebedev666d8552018-11-19 13:28:31 +0000101 rangeQuery(P, Neighbors);
Clement Courbet96715412018-05-07 09:09:48 +0000102 if (Neighbors.size() + 1 < MinPts) { // Density check.
103 // The region around P is not dense enough to create a new cluster, mark
104 // as noise for now.
105 ClusterIdForPoint_[P] = ClusterId::noise();
106 continue;
107 }
108
109 // Create a new cluster, add P.
110 Clusters_.emplace_back(ClusterId::makeValid(Clusters_.size()));
111 Cluster &CurrentCluster = Clusters_.back();
112 ClusterIdForPoint_[P] = CurrentCluster.Id; /* Label initial point */
113 CurrentCluster.PointIndices.push_back(P);
114
Clement Courbet176388c2019-01-02 09:21:00 +0000115 // Process P's neighbors.
116 llvm::SetVector<size_t, std::deque<size_t>> ToProcess;
117 ToProcess.insert(Neighbors.begin(), Neighbors.end());
118 while (!ToProcess.empty()) {
119 // Retrieve a point from the set.
120 const size_t Q = *ToProcess.begin();
121 ToProcess.erase(ToProcess.begin());
Clement Courbet96715412018-05-07 09:09:48 +0000122
Clement Courbet176388c2019-01-02 09:21:00 +0000123 if (ClusterIdForPoint_[Q].isNoise()) {
124 // Change noise point to border point.
125 ClusterIdForPoint_[Q] = CurrentCluster.Id;
126 CurrentCluster.PointIndices.push_back(Q);
Clement Courbet96715412018-05-07 09:09:48 +0000127 continue;
Clement Courbet176388c2019-01-02 09:21:00 +0000128 }
129 if (!ClusterIdForPoint_[Q].isUndef()) {
130 continue; // Previously processed.
131 }
132 // Add Q to the current custer.
133 ClusterIdForPoint_[Q] = CurrentCluster.Id;
134 CurrentCluster.PointIndices.push_back(Q);
135 // And extend to the neighbors of Q if the region is dense enough.
136 rangeQuery(Q, Neighbors);
137 if (Neighbors.size() + 1 >= MinPts) {
138 ToProcess.insert(Neighbors.begin(), Neighbors.end());
139 }
Clement Courbet96715412018-05-07 09:09:48 +0000140 }
141 }
Clement Courbet176388c2019-01-02 09:21:00 +0000142 // assert(Neighbors.capacity() == (Points_.size() - 1));
143 // ^ True, but it is not quaranteed to be true in all the cases.
Clement Courbet96715412018-05-07 09:09:48 +0000144
145 // Add noisy points to noise cluster.
Clement Courbet176388c2019-01-02 09:21:00 +0000146 for (size_t P = 0, NumPoints = Points_.size(); P < NumPoints; ++P) {
147 if (ClusterIdForPoint_[P].isNoise()) {
Clement Courbet96715412018-05-07 09:09:48 +0000148 NoiseCluster_.PointIndices.push_back(P);
Clement Courbet176388c2019-01-02 09:21:00 +0000149 }
150 }
Clement Courbet96715412018-05-07 09:09:48 +0000151}
152
Roman Lebedev69716392019-02-20 09:14:04 +0000153// Given an instruction Opcode, we can make benchmarks (measurements) of the
154// instruction characteristics/performance. Then, to facilitate further analysis
155// we group the benchmarks with *similar* characteristics into clusters.
156// Now, this is all not entirely deterministic. Some instructions have variable
157// characteristics, depending on their arguments. And thus, if we do several
158// benchmarks of the same instruction Opcode, we may end up with *different*
159// performance characteristics measurements. And when we then do clustering,
160// these several benchmarks of the same instruction Opcode may end up being
161// clustered into *different* clusters. This is not great for further analysis.
162// We shall find every opcode with benchmarks not in just one cluster, and move
163// *all* the benchmarks of said Opcode into one new unstable cluster per Opcode.
164void InstructionBenchmarkClustering::stabilize(unsigned NumOpcodes) {
165 // Given an instruction Opcode, in which clusters do benchmarks of this
166 // instruction lie? Normally, they all should be in the same cluster.
167 std::vector<llvm::SmallSet<ClusterId, 1>> OpcodeToClusterIDs;
168 OpcodeToClusterIDs.resize(NumOpcodes);
169 // The list of opcodes that have more than one cluster.
170 llvm::SetVector<size_t> UnstableOpcodes;
171 // Populate OpcodeToClusterIDs and UnstableOpcodes data structures.
172 assert(ClusterIdForPoint_.size() == Points_.size() && "size mismatch");
173 for (const auto &Point : zip(Points_, ClusterIdForPoint_)) {
174 const ClusterId &ClusterIdOfPoint = std::get<1>(Point);
175 if (!ClusterIdOfPoint.isValid())
176 continue; // Only process fully valid clusters.
177 const unsigned Opcode = std::get<0>(Point).keyInstruction().getOpcode();
178 assert(Opcode < NumOpcodes && "NumOpcodes is incorrect (too small)");
179 llvm::SmallSet<ClusterId, 1> &ClusterIDsOfOpcode =
180 OpcodeToClusterIDs[Opcode];
181 ClusterIDsOfOpcode.insert(ClusterIdOfPoint);
182 // Is there more than one ClusterID for this opcode?.
183 if (ClusterIDsOfOpcode.size() < 2)
184 continue; // If not, then at this moment this Opcode is stable.
185 // Else let's record this unstable opcode for future use.
186 UnstableOpcodes.insert(Opcode);
187 }
188 assert(OpcodeToClusterIDs.size() == NumOpcodes && "sanity check");
189
190 // We know with how many [new] clusters we will end up with.
191 const auto NewTotalClusterCount = Clusters_.size() + UnstableOpcodes.size();
192 Clusters_.reserve(NewTotalClusterCount);
193 for (const size_t UnstableOpcode : UnstableOpcodes.getArrayRef()) {
194 const llvm::SmallSet<ClusterId, 1> &ClusterIDs =
195 OpcodeToClusterIDs[UnstableOpcode];
196 assert(ClusterIDs.size() > 1 &&
197 "Should only have Opcodes with more than one cluster.");
198
199 // Create a new unstable cluster, one per Opcode.
200 Clusters_.emplace_back(ClusterId::makeValidUnstable(Clusters_.size()));
201 Cluster &UnstableCluster = Clusters_.back();
202 // We will find *at least* one point in each of these clusters.
203 UnstableCluster.PointIndices.reserve(ClusterIDs.size());
204
205 // Go through every cluster which we recorded as containing benchmarks
206 // of this UnstableOpcode. NOTE: we only recorded valid clusters.
207 for (const ClusterId &CID : ClusterIDs) {
208 assert(CID.isValid() &&
209 "We only recorded valid clusters, not noise/error clusters.");
210 Cluster &OldCluster = Clusters_[CID.getId()]; // Valid clusters storage.
211 // Within each cluster, go through each point, and either move it to the
212 // new unstable cluster, or 'keep' it.
213 // In this case, we'll reshuffle OldCluster.PointIndices vector
214 // so that all the points that are *not* for UnstableOpcode are first,
215 // and the rest of the points is for the UnstableOpcode.
216 const auto it = std::stable_partition(
217 OldCluster.PointIndices.begin(), OldCluster.PointIndices.end(),
218 [this, UnstableOpcode](size_t P) {
219 return Points_[P].keyInstruction().getOpcode() != UnstableOpcode;
220 });
221 assert(std::distance(it, OldCluster.PointIndices.end()) > 0 &&
222 "Should have found at least one bad point");
223 // Mark to-be-moved points as belonging to the new cluster.
224 std::for_each(it, OldCluster.PointIndices.end(),
225 [this, &UnstableCluster](size_t P) {
226 ClusterIdForPoint_[P] = UnstableCluster.Id;
227 });
228 // Actually append to-be-moved points to the new cluster.
229 UnstableCluster.PointIndices.insert(UnstableCluster.PointIndices.cend(),
230 it, OldCluster.PointIndices.end());
231 // And finally, remove "to-be-moved" points form the old cluster.
232 OldCluster.PointIndices.erase(it, OldCluster.PointIndices.cend());
233 // Now, the old cluster may end up being empty, but let's just keep it
234 // in whatever state it ended up. Purging empty clusters isn't worth it.
235 };
236 assert(UnstableCluster.PointIndices.size() > 1 &&
237 "New unstable cluster should end up with more than one point.");
238 assert(UnstableCluster.PointIndices.size() >= ClusterIDs.size() &&
239 "New unstable cluster should end up with no less points than there "
240 "was clusters");
241 }
242 assert(Clusters_.size() == NewTotalClusterCount && "sanity check");
243}
244
Clement Courbet96715412018-05-07 09:09:48 +0000245llvm::Expected<InstructionBenchmarkClustering>
246InstructionBenchmarkClustering::create(
247 const std::vector<InstructionBenchmark> &Points, const size_t MinPts,
Roman Lebedev69716392019-02-20 09:14:04 +0000248 const double Epsilon, llvm::Optional<unsigned> NumOpcodes) {
Clement Courbet72287212018-06-04 11:11:55 +0000249 InstructionBenchmarkClustering Clustering(Points, Epsilon * Epsilon);
Clement Courbet37f0ca02018-05-15 12:08:00 +0000250 if (auto Error = Clustering.validateAndSetup()) {
Clement Courbetcdb0eb82018-05-15 12:38:06 +0000251 return std::move(Error);
Clement Courbet96715412018-05-07 09:09:48 +0000252 }
253 if (Clustering.ErrorCluster_.PointIndices.size() == Points.size()) {
254 return Clustering; // Nothing to cluster.
255 }
256
Clement Courbet72287212018-06-04 11:11:55 +0000257 Clustering.dbScan(MinPts);
Roman Lebedev69716392019-02-20 09:14:04 +0000258
259 if (NumOpcodes.hasValue())
260 Clustering.stabilize(NumOpcodes.getValue());
261
Clement Courbet96715412018-05-07 09:09:48 +0000262 return Clustering;
263}
264
265} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000266} // namespace llvm