-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathCoalesce.cpp
195 lines (170 loc) · 7.42 KB
/
Coalesce.cpp
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
#include <iterator>
#include <numeric>
#include "mlir/Analysis/SliceAnalysis.h"
#include "mlir/Support/LLVM.h"
#include "triton/Analysis/AxisInfo.h"
#include "triton/Dialect/Triton/IR/Utility.h"
#include "triton/Dialect/TritonGPU/IR/Dialect.h"
#include "triton/Dialect/TritonGPU/Transforms/Passes.h"
#include "triton/Dialect/TritonGPU/Transforms/Utility.h"
#include "triton/Tools/StrUtil.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "tritongpu-coalesce"
#define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
#define LDBG(X) LLVM_DEBUG(DBGS() << X << "\n")
namespace mlir {
namespace triton {
namespace gpu {
#define GEN_PASS_DEF_TRITONGPUCOALESCE
#include "triton/Dialect/TritonGPU/Transforms/Passes.h.inc"
struct CoalescePass : public impl::TritonGPUCoalesceBase<CoalescePass> {
void
setCoalescedEncoding(ModuleAxisInfoAnalysis &axisInfoAnalysis, Operation *op,
int numWarps, int threadsPerWarp,
llvm::MapVector<Operation *, Attribute> &layoutMap) {
Value ptr = getMemAccessPtr(op);
auto refTensorType = cast<RankedTensorType>(ptr.getType());
LDBG("Considering op: " << *op);
LLVM_DEBUG({
DBGS() << "axis info of pointer: ";
axisInfoAnalysis.getAxisInfo(ptr)->print(llvm::dbgs());
llvm::dbgs() << "\n";
});
auto contiguity = axisInfoAnalysis.getAxisInfo(ptr)->getContiguity();
SmallVector<unsigned> order = argSort(contiguity);
LDBG("order=[" << triton::join(order, ", ") << "]");
auto matchesShape = [&refTensorType](const Value &val) {
auto rttType = dyn_cast<RankedTensorType>(val.getType());
return rttType && rttType.getShape() == refTensorType.getShape();
};
// The desired divisibility is the maximum divisibility among all dependent
// pointers which have the same shape and order as `ptr`.
llvm::SmallSetVector<Operation *, 32> memAccessesSameOrder;
memAccessesSameOrder.insert(op);
if (ptr.getDefiningOp()) {
for (Operation *use : mlir::multiRootGetSlice(op)) {
Value val = getMemAccessPtr(use);
if (!val || !matchesShape(val) || memAccessesSameOrder.contains(use))
continue;
auto currOrder =
argSort(axisInfoAnalysis.getAxisInfo(val)->getContiguity());
if (order == currOrder) {
LDBG("multi-root-slice: insert to memAccessesSameOrder " << *use);
memAccessesSameOrder.insert(use);
}
}
}
auto shapePerCTA = triton::gpu::getShapePerCTA(refTensorType);
LDBG("shapePerCTA=[" << triton::join(shapePerCTA, ", ") << "]");
int numElems = product<int64_t>(shapePerCTA);
int numThreads = numWarps * threadsPerWarp;
unsigned perThread = getNumElementsPerThread(op, order, axisInfoAnalysis);
LDBG("perThread for op: " << perThread);
for (Operation *opSameOrder : memAccessesSameOrder) {
if (opSameOrder == op)
continue;
unsigned currPerThread =
getNumElementsPerThread(opSameOrder, order, axisInfoAnalysis);
LDBG("perThread for opSameOrder: " << currPerThread);
perThread = std::max(perThread, currPerThread);
}
perThread = std::min<int>(perThread, std::max(numElems / numThreads, 1));
LDBG("perThread: " << perThread);
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.
perThread = std::min<int>(
perThread, getNumElementsPerThread(op, order, axisInfoAnalysis));
}
SmallVector<unsigned> 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 = cast<RankedTensorType>(type);
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 = dyn_cast<RankedTensorType>(operand.getType());
if (tensorType &&
!isa<triton::gpu::SharedEncodingTrait>(tensorType.getEncoding())) {
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::AsyncCopyGlobalToLocalOp>(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;
int threadsPerWarp = TritonGPUDialect::getThreadsPerWarp(moduleOp);
moduleOp.walk([&](Operation *curr) {
Value ptr = getMemAccessPtr(curr);
if (!ptr)
return;
// We only convert `tensor<tt.ptr<>>` load/store
bool isPtrTensor = false;
if (auto tensorType = dyn_cast<RankedTensorType>(ptr.getType()))
isPtrTensor = isa<PointerType>(tensorType.getElementType());
if (!isPtrTensor)
return;
int numWarps = lookupNumWarps(curr);
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);
}
}
};
} // namespace gpu
} // namespace triton
} // namespace mlir