-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathPTOLowerFrontendPipeOpsPass.cpp
More file actions
330 lines (292 loc) · 10.8 KB
/
PTOLowerFrontendPipeOpsPass.cpp
File metadata and controls
330 lines (292 loc) · 10.8 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
// Copyright (c) 2026 Huawei Technologies Co., Ltd.
// This program is free software, you can redistribute it and/or modify it under the terms and conditions of
// CANN Open Software License Agreement Version 2.0 (the "License").
// Please refer to the License for details. You may not use this file except in compliance with the License.
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
// See LICENSE in the root of the software repository for the full text of the License.
// Please refer to the License for details. You may not use this file except in compliance with the License.
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
// See LICENSE in the root of the software repository for the full text of the License.
#include "PTO/IR/PTO.h"
#include "PTO/Transforms/Passes.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Dominance.h"
#include "mlir/Pass/Pass.h"
#include "mlir/IR/PatternMatch.h"
namespace mlir {
namespace pto {
namespace func = ::mlir::func;
#define GEN_PASS_DEF_PTOLOWERFRONTENDPIPEOPS
#include "PTO/Transforms/Passes.h.inc"
} // namespace pto
} // namespace mlir
using namespace mlir;
using namespace mlir::pto;
namespace {
struct FrontendPipeHandles {
Value c2vPipe;
Value v2cPipe;
Operation *anchorOp = nullptr;
};
template <typename InitOpT>
static FailureOr<FrontendPipeHandles> lowerFrontendInitOp(InitOpT initOp,
IRRewriter &rewriter) {
FrontendPipeHandles handles;
Location loc = initOp.getLoc();
MLIRContext *ctx = initOp.getContext();
auto pipeTy = PipeType::get(ctx);
PTOArch arch = getTargetArch(initOp.getOperation());
auto createPipe = [&](int8_t dirMask, int32_t slotNum,
Value localAddr) -> FailureOr<Value> {
auto dirAttr = rewriter.getI8IntegerAttr(dirMask);
auto slotSizeAttr = rewriter.getI32IntegerAttr(initOp.getSlotSize());
auto slotNumAttr = rewriter.getI32IntegerAttr(slotNum);
if (arch == PTOArch::A5) {
auto pipe = rewriter.create<InitializeL2LPipeOp>(
loc, pipeTy, dirAttr, slotSizeAttr, slotNumAttr, IntegerAttr{},
BoolAttr{},
localAddr, /*peer_local_addr=*/Value{});
return pipe.getPipe();
}
if (!initOp.getGmSlotBuffer()) {
initOp.emitOpError("requires 'gm_slot_buffer' when lowering to a2/a3");
return failure();
}
auto localSlotNumAttr = rewriter.getI32IntegerAttr(slotNum);
auto pipe = rewriter.create<InitializeL2G2LPipeOp>(
loc, pipeTy, dirAttr, slotSizeAttr, slotNumAttr, localSlotNumAttr,
IntegerAttr{}, BoolAttr{}, initOp.getGmSlotBuffer(), localAddr,
/*peer_local_addr=*/Value{});
return pipe.getPipe();
};
switch (initOp.getDirMask()) {
case 1: {
auto pipeOr =
createPipe(/*dirMask=*/1, /*slotNum=*/8, initOp.getC2vConsumerBuf());
if (failed(pipeOr))
return failure();
handles.c2vPipe = *pipeOr;
handles.anchorOp = handles.c2vPipe.getDefiningOp();
break;
}
case 2: {
auto pipeOr =
createPipe(/*dirMask=*/2, /*slotNum=*/8, initOp.getV2cConsumerBuf());
if (failed(pipeOr))
return failure();
handles.v2cPipe = *pipeOr;
handles.anchorOp = handles.v2cPipe.getDefiningOp();
break;
}
case 3: {
auto dirAttr = rewriter.getI8IntegerAttr(3);
auto slotSizeAttr = rewriter.getI32IntegerAttr(initOp.getSlotSize());
auto slotNumAttr = rewriter.getI32IntegerAttr(4);
Value c2vAddr = initOp.getC2vConsumerBuf();
Value v2cAddr = initOp.getV2cConsumerBuf();
if (arch == PTOArch::A5) {
auto pipe = rewriter.create<InitializeL2LPipeOp>(
loc, pipeTy, dirAttr, slotSizeAttr, slotNumAttr, IntegerAttr{},
BoolAttr{},
c2vAddr, v2cAddr);
handles.c2vPipe = pipe.getPipe();
handles.v2cPipe = pipe.getPipe();
handles.anchorOp = pipe.getOperation();
} else {
if (!initOp.getGmSlotBuffer()) {
initOp.emitOpError("requires 'gm_slot_buffer' when lowering to a2/a3");
return failure();
}
auto localSlotNumAttr = rewriter.getI32IntegerAttr(4);
auto pipe = rewriter.create<InitializeL2G2LPipeOp>(
loc, pipeTy, dirAttr, slotSizeAttr, slotNumAttr, localSlotNumAttr,
IntegerAttr{}, BoolAttr{}, initOp.getGmSlotBuffer(), c2vAddr,
v2cAddr);
handles.c2vPipe = pipe.getPipe();
handles.v2cPipe = pipe.getPipe();
handles.anchorOp = pipe.getOperation();
}
break;
}
default:
break;
}
return handles;
}
static FailureOr<FrontendPipeHandles> lowerInitIfPresent(func::FuncOp funcOp,
IRRewriter &rewriter) {
FrontendPipeHandles handles;
AicInitializePipeOp aicInit;
AivInitializePipeOp aivInit;
unsigned aicInitCount = 0;
unsigned aivInitCount = 0;
funcOp.walk([&](Operation *op) {
if (auto init = dyn_cast<AicInitializePipeOp>(op)) {
++aicInitCount;
if (!aicInit)
aicInit = init;
return WalkResult::advance();
}
if (auto init = dyn_cast<AivInitializePipeOp>(op)) {
++aivInitCount;
if (!aivInit)
aivInit = init;
return WalkResult::advance();
}
return WalkResult::advance();
});
if (aicInitCount > 1) {
funcOp.emitOpError("requires at most one pto.aic_initialize_pipe");
return failure();
}
if (aivInitCount > 1) {
funcOp.emitOpError("requires at most one pto.aiv_initialize_pipe");
return failure();
}
if (aicInit && aivInit) {
funcOp.emitOpError(
"cannot mix pto.aic_initialize_pipe and pto.aiv_initialize_pipe in one function");
return failure();
}
if (!aicInit && !aivInit)
return handles;
if (aicInit) {
rewriter.setInsertionPoint(aicInit);
auto loweredOr = lowerFrontendInitOp(aicInit, rewriter);
if (failed(loweredOr))
return failure();
handles = *loweredOr;
rewriter.eraseOp(aicInit);
} else {
rewriter.setInsertionPoint(aivInit);
auto loweredOr = lowerFrontendInitOp(aivInit, rewriter);
if (failed(loweredOr))
return failure();
handles = *loweredOr;
rewriter.eraseOp(aivInit);
}
return handles;
}
static bool hasFrontendPipeOps(func::FuncOp funcOp) {
bool found = false;
funcOp.walk([&](Operation *op) {
if (isa<AicInitializePipeOp, AivInitializePipeOp, TPushToAivOp, TPushToAicOp,
TPopFromAicOp, TPopFromAivOp, TFreeFromAicOp, TFreeFromAivOp>(op)) {
found = true;
return WalkResult::interrupt();
}
return WalkResult::advance();
});
return found;
}
static LogicalResult lowerFrontendDataOps(func::FuncOp funcOp,
const FrontendPipeHandles &handles,
IRRewriter &rewriter) {
DominanceInfo dom(funcOp);
SmallVector<Operation *> frontendOps;
funcOp.walk([&](Operation *op) {
if (isa<TPushToAivOp, TPushToAicOp, TPopFromAicOp, TPopFromAivOp,
TFreeFromAicOp, TFreeFromAivOp>(op))
frontendOps.push_back(op);
});
for (Operation *op : frontendOps) {
if (!handles.anchorOp) {
op->emitOpError("requires a frontend initialize_pipe op in the same function");
return failure();
}
if (!dom.dominates(handles.anchorOp, op)) {
op->emitOpError(
"requires a dominating frontend initialize_pipe op");
return failure();
}
rewriter.setInsertionPoint(op);
if (auto push = dyn_cast<TPushToAivOp>(op)) {
if (!handles.c2vPipe) {
op->emitOpError(
"requires the dominating initialize_pipe op to enable C2V");
return failure();
}
rewriter.replaceOpWithNewOp<TPushOp>(push, push.getTile(), handles.c2vPipe,
push.getSplitAttr());
continue;
}
if (auto push = dyn_cast<TPushToAicOp>(op)) {
if (!handles.v2cPipe) {
op->emitOpError(
"requires the dominating initialize_pipe op to enable V2C");
return failure();
}
rewriter.replaceOpWithNewOp<TPushOp>(push, push.getTile(), handles.v2cPipe,
push.getSplitAttr());
continue;
}
if (auto pop = dyn_cast<TPopFromAicOp>(op)) {
if (!handles.c2vPipe) {
op->emitOpError(
"requires the dominating initialize_pipe op to enable C2V");
return failure();
}
auto decl = rewriter.create<DeclareTileOp>(pop.getLoc(),
pop.getTile().getType());
rewriter.create<TPopOp>(pop.getLoc(), decl.getTile(), handles.c2vPipe,
pop.getSplitAttr());
rewriter.replaceOp(pop, decl.getTile());
continue;
}
if (auto pop = dyn_cast<TPopFromAivOp>(op)) {
if (!handles.v2cPipe) {
op->emitOpError(
"requires the dominating initialize_pipe op to enable V2C");
return failure();
}
auto decl = rewriter.create<DeclareTileOp>(pop.getLoc(),
pop.getTile().getType());
rewriter.create<TPopOp>(pop.getLoc(), decl.getTile(), handles.v2cPipe,
pop.getSplitAttr());
rewriter.replaceOp(pop, decl.getTile());
continue;
}
if (auto free = dyn_cast<TFreeFromAicOp>(op)) {
if (!handles.c2vPipe) {
op->emitOpError(
"requires the dominating initialize_pipe op to enable C2V");
return failure();
}
rewriter.replaceOpWithNewOp<TFreeOp>(free, handles.c2vPipe,
free.getSplitAttr());
continue;
}
auto free = cast<TFreeFromAivOp>(op);
if (!handles.v2cPipe) {
op->emitOpError(
"requires the dominating initialize_pipe op to enable V2C");
return failure();
}
rewriter.replaceOpWithNewOp<TFreeOp>(free, handles.v2cPipe,
free.getSplitAttr());
}
return success();
}
struct PTOLowerFrontendPipeOpsPass
: public mlir::pto::impl::PTOLowerFrontendPipeOpsBase<
PTOLowerFrontendPipeOpsPass> {
void runOnOperation() override {
func::FuncOp funcOp = getOperation();
if (!hasFrontendPipeOps(funcOp))
return;
IRRewriter rewriter(funcOp.getContext());
auto loweredOr = lowerInitIfPresent(funcOp, rewriter);
if (failed(loweredOr)) {
signalPassFailure();
return;
}
if (failed(lowerFrontendDataOps(funcOp, *loweredOr, rewriter)))
signalPassFailure();
}
};
} // namespace
std::unique_ptr<Pass> mlir::pto::createPTOLowerFrontendPipeOpsPass() {
return std::make_unique<PTOLowerFrontendPipeOpsPass>();
}