-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTileAndFuse.cpp
More file actions
193 lines (163 loc) · 6.1 KB
/
TileAndFuse.cpp
File metadata and controls
193 lines (163 loc) · 6.1 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
#include <queue>
#include "Passes.h"
#include "Tutorial.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/MemRef/Transforms/Transforms.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/SCF/Transforms/Patterns.h"
#include "mlir/Dialect/SCF/Transforms/TileUsingInterface.h"
#include "mlir/Dialect/SCF/Transforms/Transforms.h"
#include "mlir/Dialect/Tensor/Transforms/Transforms.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
namespace mlir::tutorial {
#define GEN_PASS_DEF_TUTORIALTILEANDFUSE
#include "Passes.h.inc"
namespace {
class TutorialTileAndFuse final
: public impl::TutorialTileAndFuseBase<TutorialTileAndFuse> {
using TutorialTileAndFuseBase::TutorialTileAndFuseBase;
void runOnOperation() override;
};
static FailureOr<TilingInterface> getLoweringConfigOp(func::FuncOp funcOp) {
TilingInterface tilingOp;
funcOp.walk([&](Operation* op) {
if (auto tileOp = dyn_cast<TilingInterface>(op)) {
if (tileOp->hasAttr("lowering_config")) {
tilingOp = tileOp;
return WalkResult::interrupt();
}
}
return WalkResult::advance();
});
if (tilingOp) {
return tilingOp;
}
return failure();
}
static SmallVector<OpFoldResult> getTilingSizes(DictionaryAttr loweringConfig,
tutorial::TilingLevel level) {
switch (level) {
case tutorial::TilingLevel::Parallel: {
if (auto parallel = loweringConfig.getAs<ArrayAttr>("parallel")) {
return llvm::map_to_vector(
parallel.getAsRange<IntegerAttr>(),
[](IntegerAttr x) { return OpFoldResult(x); });
}
break;
}
case tutorial::TilingLevel::Reduction: {
if (auto reduction = loweringConfig.getAs<ArrayAttr>("reduction")) {
return llvm::map_to_vector(
reduction.getAsRange<IntegerAttr>(),
[](IntegerAttr x) { return OpFoldResult(x); });
}
break;
}
}
return SmallVector<OpFoldResult>{};
}
bool isDestinationSlice(tensor::ExtractSliceOp extractSliceOp) {
auto blockArg = dyn_cast<BlockArgument>(extractSliceOp.getSource());
if (blockArg && isa<scf::ForOp>(blockArg.getOwner()->getParentOp())) {
return true;
}
return false;
}
} // namespace
void TutorialTileAndFuse::runOnOperation() {
MLIRContext* context = &getContext();
func::FuncOp funcOp = getOperation();
IRRewriter rewriter(funcOp);
FailureOr<TilingInterface> maybeTilingOp = getLoweringConfigOp(funcOp);
if (failed(maybeTilingOp)) {
return;
}
TilingInterface tilingOp = maybeTilingOp.value();
SmallVector<OpFoldResult> tileSizes = getTilingSizes(
tilingOp->getAttrOfType<DictionaryAttr>("lowering_config"), tilingLevel);
auto zero = rewriter.getIndexAttr(0);
int64_t numLoops = tilingOp.getLoopIteratorTypes().size();
tileSizes.resize(numLoops, zero);
scf::SCFTilingOptions tilingOptions;
tilingOptions.setTileSizes(tileSizes);
if (tilingLevel == tutorial::TilingLevel::Parallel) {
tilingOptions.setLoopType(scf::SCFTilingOptions::LoopType::ForallOp);
} else {
tilingOptions.setLoopType(scf::SCFTilingOptions::LoopType::ForOp);
}
struct SliceListener : public RewriterBase::Listener {
void notifyOperationInserted(Operation* op,
OpBuilder::InsertPoint) override {
if (isa<tensor::ExtractSliceOp, tensor::InsertSliceOp,
tensor::ParallelInsertSliceOp>(op)) {
candidates.push_back(op);
}
}
void notifyOperationReplaced(Operation* op, ValueRange) override {
removeOp(op);
};
void notifyOperationErased(Operation* op) override { removeOp(op); };
void removeOp(Operation* op) {
auto it = llvm::find(candidates, op);
if (it != candidates.end()) {
candidates.erase(it);
}
}
std::deque<Operation*> candidates;
};
SliceListener listener;
rewriter.setListener(&listener);
FailureOr<scf::SCFTilingResult> tiledResults =
scf::tileUsingSCF(rewriter, tilingOp, tilingOptions);
if (failed(tiledResults)) {
return signalPassFailure();
}
rewriter.replaceOp(tilingOp, tiledResults->mergeResult.replacements);
MutableArrayRef<LoopLikeOpInterface> loops = tiledResults->loops;
std::deque<Operation*>& candidates = listener.candidates;
while (!candidates.empty()) {
Operation* candidate = candidates.front();
candidates.pop_front();
if (auto producerSlice = dyn_cast<tensor::ExtractSliceOp>(candidate)) {
if (candidate->getUsers().empty()) {
continue;
}
// Do not tile destination slices for reduction tiling.
if (tilingLevel == tutorial::TilingLevel::Reduction &&
isDestinationSlice(producerSlice)) {
continue;
}
std::optional<scf::SCFFuseProducerOfSliceResult> fusedResult =
scf::tileAndFuseProducerOfSlice(rewriter, producerSlice, loops);
}
if (tilingLevel == tutorial::TilingLevel::Reduction) {
// Do not do consumer fusion for reduction tiling.
continue;
}
if (isa<tensor::InsertSliceOp, tensor::ParallelInsertSliceOp>(candidate)) {
FailureOr<scf::SCFFuseConsumerOfSliceResult> fusedResult =
scf::tileAndFuseConsumerOfSlice(rewriter, candidate, loops);
if (succeeded(fusedResult)) {
rewriter.replaceOp(fusedResult->origConsumerOperand->getOwner(),
fusedResult->tiledOps.front());
}
}
}
// Cleanup.
RewritePatternSet patterns =
linalg::getLinalgTilingCanonicalizationPatterns(context);
scf::populateSCFForLoopCanonicalizationPatterns(patterns);
tensor::populateFoldTensorEmptyPatterns(patterns);
memref::populateResolveRankedShapedTypeResultDimsPatterns(patterns);
// Pull in tensor dialect canonicalization patterns to fold tensor.cast
// into producers when possible.
context->getLoadedDialect<tensor::TensorDialect>()
->getCanonicalizationPatterns(patterns);
if (failed(applyPatternsGreedily(funcOp, std::move(patterns)))) {
return signalPassFailure();
}
}
} // namespace mlir::tutorial