-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathRegAlloc.h
More file actions
348 lines (300 loc) · 11.5 KB
/
RegAlloc.h
File metadata and controls
348 lines (300 loc) · 11.5 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// Copyright 2025 The Wave Authors
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef WaveASM_TRANSFORMS_REGALLOC_H
#define WaveASM_TRANSFORMS_REGALLOC_H
#include "mlir/IR/Value.h"
#include "waveasm/Dialect/WaveASMOps.h"
#include "waveasm/Dialect/WaveASMTypes.h"
#include "waveasm/Transforms/Liveness.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include <algorithm>
#include <memory>
#include <optional>
namespace waveasm {
//===----------------------------------------------------------------------===//
// Spill Record
//===----------------------------------------------------------------------===//
/// Records a cross-class spill decision made during register allocation.
/// The victim value is evicted from its original register class into the
/// alternate class (e.g. VGPR -> AGPR). The pass uses these records to
/// insert spill/reload ops after allocation completes.
struct SpillRecord {
mlir::Value victim; ///< The SSA value being spilled.
int64_t sourcePhysReg; ///< Physical register freed in original class.
int64_t targetPhysReg; ///< Physical register allocated in target class.
RegClass sourceClass; ///< Original register class (e.g. VGPR).
RegClass targetClass; ///< Target register class (e.g. AGPR).
};
//===----------------------------------------------------------------------===//
// Allocation Statistics
//===----------------------------------------------------------------------===//
/// Statistics from register allocation
struct AllocationStats {
int64_t peakVGPRs = 0;
int64_t peakSGPRs = 0;
int64_t peakAGPRs = 0;
int64_t totalVRegs = 0;
int64_t totalSRegs = 0;
int64_t totalARegs = 0;
int64_t rangesAllocated = 0;
int64_t rangesExpired = 0;
};
//===----------------------------------------------------------------------===//
// Physical Mapping (Pure SSA)
//===----------------------------------------------------------------------===//
/// Maps SSA Values to physical register indices
class PhysicalMapping {
public:
/// Get physical register index for a Value
int64_t getPhysReg(mlir::Value value) const {
auto it = valueToPhysReg.find(value);
return it != valueToPhysReg.end() ? it->second : -1;
}
/// Set mapping for a Value
void setPhysReg(mlir::Value value, int64_t physIdx) {
valueToPhysReg[value] = physIdx;
}
/// Check if a Value is mapped
bool hasMapping(mlir::Value value) const {
return valueToPhysReg.contains(value);
}
/// Direct access for LinearScanRegAlloc
llvm::DenseMap<mlir::Value, int64_t> valueToPhysReg;
};
//===----------------------------------------------------------------------===//
// Register Pool
//===----------------------------------------------------------------------===//
/// Pool of available physical registers.
/// Uses a BitVector for O(1) per-register operations and cache-friendly
/// scanning. Typical GPU register files (256 VGPRs, 102 SGPRs, 256 AGPRs)
/// fit in a few 64-bit words.
class RegPool {
public:
RegPool(RegClass regClass, int64_t maxRegs,
const llvm::DenseSet<int64_t> &reserved)
: regClass(regClass), maxRegs(maxRegs), free(maxRegs, true) {
for (int64_t reg : reserved) {
if (reg >= 0 && reg < maxRegs) {
free.reset(reg);
++currentUsage;
}
}
updatePeak();
}
/// O(1) free-check via bit test.
bool isFree(int64_t reg) const {
return reg >= 0 && reg < maxRegs && free.test(reg);
}
/// Reserve a specific register range (for precoloring / re-reservation).
void reserve(int64_t reg, int64_t size) {
for (int64_t idx = 0; idx < size; ++idx) {
int64_t target = reg + idx;
if (target >= 0 && target < maxRegs && free.test(target)) {
free.reset(target);
++currentUsage;
}
}
updatePeak();
}
/// Allocate the lowest-numbered free register.
/// Returns -1 if no register is available.
int64_t allocSingle() {
int64_t idx = free.find_first();
if (idx < 0)
return -1;
free.reset(idx);
++currentUsage;
updatePeak();
return idx;
}
/// Allocate a contiguous range of registers with the given alignment.
/// Scans by alignment stride for O(maxRegs/alignment) candidate checks.
/// Returns base register index, or -1 if allocation fails.
int64_t allocRange(int64_t size, int64_t alignment) {
if (size <= 0)
return -1;
assert(alignment > 0 && "alignment must be positive");
for (int64_t candidate = 0; candidate + size <= maxRegs;
candidate += alignment) {
bool allFree = true;
for (int64_t offset = 0; offset < size; ++offset) {
if (!free.test(candidate + offset)) {
allFree = false;
break;
}
}
if (allFree) {
for (int64_t offset = 0; offset < size; ++offset)
free.reset(candidate + offset);
currentUsage += size;
updatePeak();
return candidate;
}
}
return -1;
}
/// Allocate the highest-numbered free register below ceiling.
int64_t allocSingleFromTop(int64_t ceiling = -1) {
int64_t cap = (ceiling > 0 && ceiling <= maxRegs) ? ceiling : maxRegs;
for (int64_t reg = cap - 1; reg >= 0; --reg) {
if (free.test(reg)) {
free.reset(reg);
++currentUsage;
updatePeak();
return reg;
}
}
return -1;
}
/// Allocate a contiguous range from the top of a capped region.
/// Scans from `ceiling` downward to pack long-lived values at the top
/// of the USED range, not the top of the entire register file.
int64_t allocRangeFromTop(int64_t size, int64_t alignment,
int64_t ceiling = -1) {
if (size <= 0)
return -1;
assert(alignment > 0 && "alignment must be positive");
int64_t cap = (ceiling > 0 && ceiling <= maxRegs) ? ceiling : maxRegs;
if (size > cap)
return -1;
int64_t highestBase = ((cap - size) / alignment) * alignment;
for (int64_t candidate = highestBase; candidate >= 0;
candidate -= alignment) {
bool allFree = true;
for (int64_t offset = 0; offset < size; ++offset) {
if (!free.test(candidate + offset)) {
allFree = false;
break;
}
}
if (allFree) {
for (int64_t offset = 0; offset < size; ++offset)
free.reset(candidate + offset);
currentUsage += size;
updatePeak();
return candidate;
}
}
return -1;
}
/// Free a single register back to the pool.
void freeSingle(int64_t reg) {
if (reg < 0 || reg >= maxRegs || free.test(reg))
return;
free.set(reg);
--currentUsage;
}
/// Free a contiguous range of registers.
void freeRange(int64_t base, int64_t size) {
for (int64_t offset = 0; offset < size; ++offset) {
freeSingle(base + offset);
}
}
/// Check if the pool has at least one free register.
bool hasFree() const { return free.any(); }
/// Get peak usage.
int64_t getPeakUsage() const { return peak; }
int64_t getCurrentUsage() const { return currentUsage; }
RegClass getRegClass() const { return regClass; }
private:
void updatePeak() { peak = std::max(peak, currentUsage); }
RegClass regClass;
int64_t maxRegs;
llvm::BitVector free;
int64_t currentUsage = 0;
int64_t peak = 0;
};
//===----------------------------------------------------------------------===//
// Allocation Strategy
//===----------------------------------------------------------------------===//
/// Pluggable strategy for choosing how to allocate a physical register for a
/// given live range. Subclasses override `allocate()` to implement heuristics
/// such as bidirectional allocation, best-fit, etc.
///
/// Returning std::nullopt signals "no preference" and lets the caller fall
/// through to the default bottom-up allocation.
class AllocationStrategy {
public:
virtual ~AllocationStrategy() = default;
/// Try to allocate a physical register for the given live range.
/// Returns the allocated register index, or std::nullopt to fall back to
/// the default bottom-up allocation.
virtual std::optional<int64_t> allocate(RegPool &pool, const LiveRange &range,
llvm::ArrayRef<LiveRange> allRanges,
int64_t maxPressure) = 0;
};
/// Allocate long-lived multi-register VGPR ranges from the top of the
/// expected register usage and short-lived ranges from the bottom. This
/// separates interleaved buffer_load (prefetch) and ds_read (consumed)
/// destinations into contiguous regions, reducing fragmentation.
class BidirectionalStrategy : public AllocationStrategy {
public:
explicit BidirectionalStrategy(int64_t thresholdPct = 75)
: thresholdPct(thresholdPct) {}
std::optional<int64_t> allocate(RegPool &pool, const LiveRange &range,
llvm::ArrayRef<LiveRange> allRanges,
int64_t maxPressure) override;
private:
int64_t thresholdPct;
};
//===----------------------------------------------------------------------===//
// Linear Scan Register Allocator (Pure SSA)
//===----------------------------------------------------------------------===//
/// Linear scan register allocator for pure SSA IR
class LinearScanRegAlloc {
public:
LinearScanRegAlloc(int64_t maxVGPRs, int64_t maxSGPRs, int64_t maxAGPRs,
const llvm::DenseSet<int64_t> &reservedVGPRs,
const llvm::DenseSet<int64_t> &reservedSGPRs,
const llvm::DenseSet<int64_t> &reservedAGPRs)
: maxVGPRs(maxVGPRs), maxSGPRs(maxSGPRs), maxAGPRs(maxAGPRs),
reservedVGPRs(reservedVGPRs), reservedSGPRs(reservedSGPRs),
reservedAGPRs(reservedAGPRs) {}
/// Precolor a Value to a specific physical register (for ABI args)
void precolorValue(mlir::Value value, int64_t physIdx) {
precoloredValues[value] = physIdx;
}
/// Add a tied operand constraint: result must get same physical reg as
/// operand Used for MFMA accumulator tying where result overwrites the
/// accumulator
void addTiedOperand(mlir::Value result, mlir::Value operand) {
tiedOperands[result] = operand;
}
/// Set the allocation strategy used for VGPR ranges. When null (or not
/// set), all ranges use bottom-up allocation.
void setVGPRStrategy(std::unique_ptr<AllocationStrategy> strategy) {
vgprStrategy = std::move(strategy);
}
/// Result bundle returned by allocate().
struct AllocResult {
PhysicalMapping mapping;
AllocationStats stats;
llvm::SmallVector<SpillRecord> spills;
};
/// Run allocation on a kernel program.
/// Returns the physical mapping, statistics, and any cross-class spill
/// records, or failure if allocation fails.
mlir::FailureOr<AllocResult> allocate(ProgramOp program);
private:
/// Process active ranges, expiring those that end before currentPoint
void expireRanges(
llvm::SmallVector<std::tuple<int64_t, LiveRange, int64_t>> &active,
int64_t currentPoint, RegPool &pool, AllocationStats &stats);
int64_t maxVGPRs;
int64_t maxSGPRs;
int64_t maxAGPRs;
llvm::DenseSet<int64_t> reservedVGPRs;
llvm::DenseSet<int64_t> reservedSGPRs;
llvm::DenseSet<int64_t> reservedAGPRs;
llvm::DenseMap<mlir::Value, int64_t> precoloredValues;
llvm::DenseMap<mlir::Value, mlir::Value> tiedOperands; // result -> operand
std::unique_ptr<AllocationStrategy> vgprStrategy;
};
} // namespace waveasm
#endif // WaveASM_TRANSFORMS_REGALLOC_H