-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathCoalesce.cpp
More file actions
284 lines (257 loc) · 11.2 KB
/
Coalesce.cpp
File metadata and controls
284 lines (257 loc) · 11.2 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
#include "mlir/Analysis/SliceAnalysis.h"
#include "triton/Analysis/AxisInfo.h"
#include "triton/Analysis/Utility.h"
#include "triton/Dialect/TritonGPU/IR/Dialect.h"
#include "triton/Dialect/TritonGPU/Transforms/Passes.h"
#include "llvm/Support/Debug.h"
#include <iterator>
#include <numeric>
using namespace mlir;
using namespace mlir::triton;
#define GEN_PASS_CLASSES
#include "triton/Dialect/TritonGPU/Transforms/Passes.h.inc"
template <class T> SmallVector<unsigned, 4> argSort(const T &arr) {
SmallVector<unsigned, 4> ret(arr.size());
std::iota(ret.begin(), ret.end(), 0);
std::stable_sort(ret.begin(), ret.end(),
[&](unsigned x, unsigned y) { return arr[x] > arr[y]; });
return ret;
}
unsigned getElementBitWidth(const Value &val) {
auto valType = val.getType();
if (valType.isa<PointerType>())
valType = valType.cast<PointerType>().getPointeeType();
auto tensorType = valType.cast<RankedTensorType>();
auto typeForMem =
tensorType.getElementType().isa<PointerType>()
? tensorType.getElementType().cast<PointerType>().getPointeeType()
: tensorType.getElementType();
return typeForMem.getIntOrFloatBitWidth();
}
static Value getMemAccessPtr(Operation *op) {
if (auto ld = dyn_cast<triton::LoadOp>(op))
return ld.getPtr();
if (auto atomic = dyn_cast<triton::AtomicRMWOp>(op))
return atomic.getPtr();
if (auto atomic = dyn_cast<triton::AtomicCASOp>(op))
return atomic.getPtr();
if (auto insert = dyn_cast<triton::gpu::InsertSliceAsyncOp>(op))
return insert.getSrc();
if (auto store = dyn_cast<triton::StoreOp>(op))
return store.getPtr();
return nullptr;
}
struct CoalescePass : public TritonGPUCoalesceBase<CoalescePass> {
void
setCoalescedEncoding(ModuleAxisInfoAnalysis &axisInfoAnalysis, Operation *op,
int numWarps, int threadsPerWarp,
llvm::MapVector<Operation *, Attribute> &layoutMap) {
Value ptr = getMemAccessPtr(op);
auto refType = ptr.getType();
if (refType.isa<PointerType>())
refType = refType.cast<PointerType>().getPointeeType();
auto refTensorType = refType.cast<RankedTensorType>();
// TODO(Keren): integrate it into AxisInfoAnalysis
// Get axis info
auto queryAxisInfo = [&](const Value &val) -> AxisInfo {
auto valType = val.getType();
// Tensor pointer
// TODO(Chenggang): encoding for tensor pointers is meaningless, remove
// these later while merging into the GitHub main
if (auto ptrType = valType.dyn_cast<PointerType>()) {
auto tensorTy = ptrType.getPointeeType().dyn_cast<RankedTensorType>();
assert(tensorTy);
auto makeTensorPtr = getMakeTensorPtrOp(val);
auto order = makeTensorPtr.getOrder();
auto tileShape = triton::gpu::getShapePerCTA(tensorTy);
size_t rank = order.size();
auto elemSizeInBytes =
tensorTy.getElementType().getIntOrFloatBitWidth() / 8;
SmallVector<int64_t> contiguity(rank, 1);
SmallVector<int64_t> divisibility(rank, 1);
SmallVector<int64_t> constancy(rank, 1);
// The contiguity in `order[0]` is `tileShape[order[0]]`
// The divisibility in `order[0]` is 16
// TODO[goostavz]: confirm the legality of it
contiguity[order[0]] = tileShape[order[0]];
divisibility[order[0]] = 16 * 8 / elemSizeInBytes;
return AxisInfo(contiguity, divisibility, constancy);
}
// Normal cases
assert(valType.isa<RankedTensorType>());
return *axisInfoAnalysis.getAxisInfo(val);
};
// Get the contiguity order of `ptr`
SmallVector<unsigned> order;
if (auto ptrType = ptr.getType().dyn_cast<PointerType>()) {
// Tensor pointer
auto makeTensorPtr = getMakeTensorPtrOp(ptr);
std::copy(makeTensorPtr.getOrder().begin(),
makeTensorPtr.getOrder().end(), std::back_inserter(order));
} else {
// Normal cases
order = argSort(queryAxisInfo(ptr).getContiguity());
}
auto matchesShape = [&refTensorType](const Value &val) {
if (val.getType() == refTensorType) {
return true;
}
auto rttType = val.getType().dyn_cast<RankedTensorType>();
if (!rttType) {
return false;
}
return rttType.getShape() == refTensorType.getShape();
};
// The desired divisibility is the maximum divisibility
// among all dependent pointers who have the same order as
// `ptr`.
// We only do it for normal tensors of pointers, not tensor pointers.
llvm::SmallSetVector<Operation *, 32> memAccessesSameOrder;
memAccessesSameOrder.insert(op);
if (refType.isa<RankedTensorType>() && ptr.getDefiningOp()) {
for (Operation *use : mlir::multiRootGetSlice(op)) {
Value val = getMemAccessPtr(use);
if (!val)
continue;
if (!matchesShape(val))
continue;
auto currOrder =
argSort(axisInfoAnalysis.getAxisInfo(val)->getContiguity());
if (order == currOrder) {
memAccessesSameOrder.insert(use);
}
}
}
auto shapePerCTA = triton::gpu::getShapePerCTA(refTensorType);
int numElems = product<int64_t>(shapePerCTA);
int numThreads = numWarps * threadsPerWarp;
int numElemsPerThread = std::max(numElems / numThreads, 1);
// For tensor of pointers, the element to access is the pointee type;
// while for tensor pointer type (`refType` is directly the final shape),
// the element to access is itself.
auto typeForMem = refTensorType.getElementType().isa<PointerType>()
? refTensorType.getElementType()
.cast<PointerType>()
.getPointeeType()
: refTensorType.getElementType();
auto getNumElementPerThread = [&](Operation *op) {
Value val = getMemAccessPtr(op);
auto valInfo = queryAxisInfo(val);
unsigned elemNumBits = getElementBitWidth(val);
unsigned elemNumBytes = std::max(elemNumBits / 8, 1u);
unsigned maxMultipleBytes = valInfo.getDivisibility(order[0]);
unsigned maxMultiple = std::max(maxMultipleBytes / elemNumBytes, 1u);
unsigned maxContig =
std::min(valInfo.getContiguity(order[0]), shapePerCTA[order[0]]);
unsigned alignment = std::min(maxMultiple, maxContig);
unsigned currPerThread = std::min(alignment, 128 / elemNumBits);
return currPerThread;
};
unsigned perThread = getNumElementPerThread(op);
for (Operation *op : memAccessesSameOrder) {
unsigned currPerThread = getNumElementPerThread(op);
perThread = std::max(perThread, currPerThread);
}
perThread = std::min<int>(perThread, numElemsPerThread);
if (!dyn_cast<triton::LoadOp>(op)) {
// For ops that can result in a global memory write, we should enforce
// that each thread handles at most 128 bits, which is the widest
// available vectorized store op; otherwise, the store will have "gaps"
// in the memory write at the warp level, resulting in worse performance.
// For loads, we can expect that the gaps won't matter due to the L1
// cache.
unsigned elemNumBits = getElementBitWidth(ptr);
perThread = std::min<int>(perThread, getNumElementPerThread(op));
}
SmallVector<unsigned, 4> sizePerThread(refTensorType.getRank(), 1);
sizePerThread[order[0]] = perThread;
auto CTALayout = triton::gpu::getCTALayout(refTensorType.getEncoding());
layoutMap[op] = triton::gpu::BlockedEncodingAttr::get(
&getContext(), refTensorType.getShape(), sizePerThread, order, numWarps,
threadsPerWarp, CTALayout);
}
static Type getNewType(Type type, Attribute encoding) {
RankedTensorType tensorType = type.cast<RankedTensorType>();
return RankedTensorType::get(tensorType.getShape(),
tensorType.getElementType(), encoding);
}
void coalesceOp(Attribute encoding, Operation *op) {
OpBuilder builder(op);
// Convert operands
// For load/store with tensor pointers, we don't have to change the
// operands' type, we do this by changing the outputs' type of
// `make_tensor_ptr`
SmallVector<Value, 4> newArgs;
for (auto operand : op->getOperands()) {
auto tensorType = operand.getType().dyn_cast<RankedTensorType>();
if (tensorType &&
!tensorType.getEncoding().isa<triton::gpu::SharedEncodingAttr>()) {
Type newType = getNewType(tensorType, encoding);
newArgs.push_back(builder.create<triton::gpu::ConvertLayoutOp>(
op->getLoc(), newType, operand));
} else {
newArgs.push_back(operand);
}
}
// Convert output types
SmallVector<Type, 4> newTypes;
for (auto t : op->getResultTypes()) {
bool isAsync = isa<triton::gpu::InsertSliceAsyncOp>(op);
newTypes.push_back(isAsync ? t : getNewType(t, encoding));
}
// Construct new op with the new encoding
Operation *newOp =
builder.create(op->getLoc(), op->getName().getIdentifier(), newArgs,
newTypes, op->getAttrs());
// Cast the results back to the original layout
for (size_t i = 0; i < op->getNumResults(); i++) {
Value newResult = newOp->getResult(i);
if (newTypes[i] != op->getResultTypes()[i]) {
newResult = builder.create<triton::gpu::ConvertLayoutOp>(
op->getLoc(), op->getResult(i).getType(), newResult);
}
op->getResult(i).replaceAllUsesWith(newResult);
}
op->erase();
}
void runOnOperation() override {
// Run axis info analysis
ModuleOp moduleOp = getOperation();
ModuleAxisInfoAnalysis axisInfoAnalysis(moduleOp);
// For each i/o operation, we determine what layout
// the pointers should have for best memory coalescing
llvm::MapVector<Operation *, Attribute> layoutMap;
moduleOp.walk([&](Operation *curr) {
Value ptr = getMemAccessPtr(curr);
if (!ptr)
return;
// We only convert `tensor<tt.ptr<>>` or `tt.ptr<tensor<>>` load/store
bool isPtrTensor = false, isTensorPointer = false;
if (auto tensorType = ptr.getType().dyn_cast<RankedTensorType>())
isPtrTensor = tensorType.getElementType().isa<PointerType>();
if (auto ptrType = ptr.getType().dyn_cast<PointerType>())
isTensorPointer = ptrType.getPointeeType().isa<RankedTensorType>();
if (!isPtrTensor && !isTensorPointer)
return;
auto mod = curr->getParentOfType<ModuleOp>();
int numWarps = triton::gpu::TritonGPUDialect::getNumWarps(mod);
int threadsPerWarp =
triton::gpu::TritonGPUDialect::getThreadsPerWarp(mod);
setCoalescedEncoding(axisInfoAnalysis, curr, numWarps, threadsPerWarp,
layoutMap);
});
// For each memory op that has a layout L1:
// 1. Create a coalesced memory layout L2 of the pointer operands
// 2. Convert all operands from layout L1 to layout L2
// 3. Create a new memory op that consumes these operands and
// produces a tensor with layout L2
// 4. Convert the output of this new memory op back to L1
// 5. Replace all the uses of the original memory op by the new one
for (auto &kv : layoutMap) {
coalesceOp(kv.second, kv.first);
}
}
};
std::unique_ptr<Pass> mlir::triton::gpu::createCoalescePass() {
return std::make_unique<CoalescePass>();
}