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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
//===- TestTensorTransforms.cpp - Test Tensor transformation patterns -----===//
//
// Part of the LLVM Project, 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
//
//===----------------------------------------------------------------------===//
//
// This file implements logic for testing Tensor transformations.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Dialect/Tensor/Transforms/TransformUtils.h"
#include "mlir/Dialect/Tensor/Transforms/Transforms.h"
#include "mlir/Dialect/Transform/IR/TransformOps.h"
#include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
using namespace mlir;
namespace {
struct TestTensorTransforms
: public PassWrapper<TestTensorTransforms, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestTensorTransforms)
TestTensorTransforms() = default;
TestTensorTransforms(const TestTensorTransforms &pass) : PassWrapper(pass) {}
void getDependentDialects(DialectRegistry ®istry) const override {
registry.insert<arith::ArithDialect, scf::SCFDialect, linalg::LinalgDialect,
transform::TransformDialect>();
}
StringRef getArgument() const final {
return "test-tensor-transform-patterns";
}
StringRef getDescription() const final {
return "Test Tensor transformation patterns by applying them greedily.";
}
void runOnOperation() override;
Option<bool> testFoldConstantExtractSlice{
*this, "test-fold-constant-extract-slice",
llvm::cl::desc("Test folding arith.constant and tensor.extract_slice"),
llvm::cl::init(false)};
Option<bool> testFoldConsecutiveInsertExtractSlice{
*this, "test-fold-consecutive-insert-extract-slice",
llvm::cl::desc(
"Test folding consecutive tensor.insert_slice/tensor.extract_slice"),
llvm::cl::init(false)};
Option<bool> testRewriteExtractSliceWithTiledCollapseShape{
*this, "test-rewrite-extract-slice-from-collapse-shape",
llvm::cl::desc("Test swapping tensor.extract_slice of a collapse_shape "
"with loop nest"),
llvm::cl::init(false)};
Option<bool> testDropRedundantInsertSliceRankExpansion{
*this, "test-drop-redundant-insert-slice-rank-expansion",
llvm::cl::desc("Test dropping redundant insert_slice rank expansions"),
llvm::cl::init(false)};
Option<bool> testReassociativeReshapeFolding{
*this, "test-reassociative-reshape-folding",
llvm::cl::desc("Test folding of expand_shape/collapse_shape"),
llvm::cl::init(false)};
Option<bool> testBubbleUpExpandShapePatterns{
*this, "test-expand-shape-bubbling",
llvm::cl::desc("Test folding of expand_shape/collapse_shape"),
llvm::cl::init(false)};
Option<bool> testFoldExtractFromCollapseShape{
*this, "test-fold-extract-from-collapse-shape",
llvm::cl::desc("Test folding of extract from collapse_shape"),
llvm::cl::init(false)};
Option<bool> useForeach{
*this, "use-foreach",
llvm::cl::desc(
"Use the scf.forall operation when generating loop nests for "
"the extract_slice of collapse_shape pattern"),
llvm::cl::init(false)};
Option<bool> testTrackingListener{
*this, "test-tracking-listener",
llvm::cl::desc("Test tensor TrackingListener for the transform dialect"),
llvm::cl::init(false)};
};
} // namespace
static void applyReassociativeReshapeFoldingPatterns(Operation *rootOp) {
RewritePatternSet patterns(rootOp->getContext());
tensor::populateReassociativeReshapeFoldingPatterns(patterns);
(void)applyPatternsGreedily(rootOp, std::move(patterns));
}
static void applyBubbleUpExpandShapePatterns(Operation *rootOp) {
RewritePatternSet patterns(rootOp->getContext());
tensor::populateBubbleUpExpandShapePatterns(patterns);
(void)applyPatternsGreedily(rootOp, std::move(patterns));
}
static void applyFoldConstantExtractSlicePatterns(Operation *rootOp) {
RewritePatternSet patterns(rootOp->getContext());
tensor::ControlConstantExtractSliceFusionFn controlFn =
[](tensor::ExtractSliceOp op) {
if (!op.getSource().hasOneUse())
return false;
auto resultType = cast<ShapedType>(op.getResult().getType());
constexpr int64_t kConstantFoldingMaxNumElements = 1024;
return resultType.getNumElements() <= kConstantFoldingMaxNumElements;
};
tensor::populateFoldConstantExtractSlicePatterns(patterns, controlFn);
(void)applyPatternsGreedily(rootOp, std::move(patterns));
}
static void applyFoldConsecutiveInsertExtractSlicePatterns(Operation *rootOp) {
RewritePatternSet patterns(rootOp->getContext());
tensor::populateMergeConsecutiveInsertExtractSlicePatterns(patterns);
(void)applyPatternsGreedily(rootOp, std::move(patterns));
}
static void
applyDropRedundantInsertSliceRankExpansionPatterns(Operation *rootOp) {
RewritePatternSet patterns(rootOp->getContext());
tensor::populateDropRedundantInsertSliceRankExpansionPatterns(patterns);
(void)applyPatternsGreedily(rootOp, std::move(patterns));
}
static void applyFoldExtractFromCollapseShapePatterns(Operation *rootOp) {
RewritePatternSet patterns(rootOp->getContext());
tensor::populateFoldCollapseExtractPatterns(patterns);
(void)applyPatternsGreedily(rootOp, std::move(patterns));
}
namespace {
/// Base pattern to rewrite a `tensor.collapse_shape -> tensor.extract_slice`.
/// The `tensor.extract_slice` is replaced by a loop or gather operation that
/// stitches together the desired tile from slices of the source of the collapse
/// shape op.
struct RewriteExtractSliceFromCollapseShapeBase
: public OpRewritePattern<tensor::ExtractSliceOp> {
RewriteExtractSliceFromCollapseShapeBase(MLIRContext *context)
: mlir::OpRewritePattern<tensor::ExtractSliceOp>(context) {}
/// Emit a loop or gather operation that uses `helper` to take each point in
/// the parallel iteration space bounds, extract a slice from the source
/// tensor and insert it into `dest`. For examples, see below for `scf.for`
/// and `scf.foreach`.
virtual LogicalResult
emitReplacement(tensor::ExtractSliceOp op, Value dest,
tensor::ExtractSliceFromCollapseHelper &helper,
PatternRewriter &rewriter) const = 0;
LogicalResult matchAndRewrite(tensor::ExtractSliceOp op,
PatternRewriter &rewriter) const override {
auto collapseOp = op.getSource().getDefiningOp<tensor::CollapseShapeOp>();
if (!collapseOp)
return rewriter.notifyMatchFailure(
op, "producer is not a tensor.collapse_shape op");
// Try to simplify the collapse shape using a rank-reducing slice, if
// possible.
FailureOr<Operation *> simplifiedCollapseShapeResult =
tensor::simplifyCollapseShapeWithRankReducingExtractSlice(collapseOp,
rewriter);
if (succeeded(simplifiedCollapseShapeResult)) {
auto newCollapseOp =
dyn_cast<tensor::CollapseShapeOp>(*simplifiedCollapseShapeResult);
// The collapse shape op might have been simplified away, so we can just
// return.
if (!newCollapseOp)
return success();
collapseOp = newCollapseOp;
}
// Materialize the output shape values of the slice operation.
ReifiedRankedShapedTypeDims reifiedShapes;
if (failed(reifyResultShapes(rewriter, op, reifiedShapes)))
return rewriter.notifyMatchFailure(op, "failed to reify result shapes");
// Create the destination tensor using the above values.
Type elementType = op.getSourceType().getElementType();
SmallVector<OpFoldResult> outputShape = reifiedShapes[0];
Value dest = tensor::EmptyOp::create(rewriter, op->getLoc(), outputShape,
elementType);
// Calculate the parameters for the tile loop nest.
FailureOr<tensor::ExtractSliceFromCollapseHelper> params =
tensor::ExtractSliceFromCollapseHelper::create(rewriter, collapseOp,
op);
if (failed(params))
return rewriter.notifyMatchFailure(
op, "could not calculate tiling parameters");
return emitReplacement(op, dest, *params, rewriter);
}
};
struct RewriteExtractSliceFromCollapseShapeUsingScfFor
: public RewriteExtractSliceFromCollapseShapeBase {
RewriteExtractSliceFromCollapseShapeUsingScfFor(MLIRContext *context)
: RewriteExtractSliceFromCollapseShapeBase(context) {}
LogicalResult emitReplacement(tensor::ExtractSliceOp op, Value dest,
tensor::ExtractSliceFromCollapseHelper &helper,
PatternRewriter &rewriter) const override {
Location loc = op.getLoc();
const unsigned numTiledDims = helper.getIterationSpaceSizes().size();
auto zero = arith::ConstantIndexOp::create(rewriter, loc, 0);
auto one = arith::ConstantIndexOp::create(rewriter, loc, 1);
SmallVector<Value> lbs(numTiledDims, zero);
SmallVector<Value> steps(numTiledDims, one);
scf::LoopNest nest = scf::buildLoopNest(
rewriter, loc, lbs, helper.getIterationSpaceSizes(), steps, dest,
[&](OpBuilder &nestedBuilder, Location loc, ValueRange outputIvs,
ValueRange iterArgs) -> scf::ValueVector {
auto [tile, insertParams] =
helper.emitLoopNestBody(nestedBuilder, loc, outputIvs);
// Insert the slice into the destination.
return {tensor::InsertSliceOp::create(nestedBuilder, loc, tile,
iterArgs[0], insertParams)};
});
rewriter.replaceOp(op, nest.results);
return success();
}
};
struct RewriteExtractSliceFromCollapseShapeUsingScfForeach
: public RewriteExtractSliceFromCollapseShapeBase {
RewriteExtractSliceFromCollapseShapeUsingScfForeach(MLIRContext *context)
: RewriteExtractSliceFromCollapseShapeBase(context) {}
LogicalResult emitReplacement(tensor::ExtractSliceOp op, Value dest,
tensor::ExtractSliceFromCollapseHelper &helper,
PatternRewriter &rewriter) const override {
Location loc = op.getLoc();
auto forallOp = scf::ForallOp::create(
rewriter, loc,
/*numThreads=*/getAsOpFoldResult(helper.getIterationSpaceSizes()),
/*outputs=*/dest,
/*mapping=*/std::nullopt,
[&](OpBuilder &nestedBuilder, Location loc, ValueRange regionArgs) {
unsigned numThreadIdRegionArgs =
helper.getIterationSpaceSizes().size();
unsigned numOutputRegionArgs =
regionArgs.size() - numThreadIdRegionArgs;
ValueRange outputIvs = regionArgs.take_front(numThreadIdRegionArgs);
ValueRange outputArgs = regionArgs.take_back(numOutputRegionArgs);
assert(outputArgs.size() == 1 &&
"there should only be one output region argument");
auto [tile, insertParams] =
helper.emitLoopNestBody(nestedBuilder, loc, outputIvs);
// Insert the slice into the destination.
auto term = scf::InParallelOp::create(nestedBuilder, loc);
nestedBuilder.setInsertionPointToStart(term.getBody());
tensor::ParallelInsertSliceOp::create(nestedBuilder, loc, tile,
outputArgs[0], insertParams);
});
rewriter.replaceOp(op, forallOp->getResult(0));
return success();
}
};
} // namespace
static LogicalResult
applyRewriteExtractFromCollapseShapePatterns(Operation *rootOp,
bool useForeach) {
RewritePatternSet patterns(rootOp->getContext());
if (useForeach)
patterns.add<RewriteExtractSliceFromCollapseShapeUsingScfForeach>(
rootOp->getContext());
else
patterns.add<RewriteExtractSliceFromCollapseShapeUsingScfFor>(
rootOp->getContext());
return applyPatternsGreedily(rootOp, std::move(patterns));
}
namespace {
class DummyTrackingListener : public transform::TrackingListener {
public:
using transform::TrackingListener::TrackingListener;
// Expose `findReplacementOp` as a public function, so that it can be tested.
Operation *getReplacementOp(Operation *op, ValueRange newValues) const {
Operation *replacementOp;
if (!findReplacementOp(replacementOp, op, newValues).succeeded())
return nullptr;
return replacementOp;
}
};
} // namespace
static LogicalResult testTrackingListenerReplacements(Operation *rootOp) {
// Find replaced op.
Operation *replaced = nullptr;
WalkResult status = rootOp->walk([&](Operation *op) {
if (op->hasAttr("replaced")) {
if (replaced) {
op->emitError("only one 'replaced' op is allowed per test case");
replaced->emitRemark("other 'replaced' op");
return WalkResult::interrupt();
}
replaced = op;
}
return WalkResult::advance();
});
if (status.wasInterrupted())
return failure();
if (!replaced) {
rootOp->emitError("could not find 'replaced' op");
return failure();
}
// Find replacements.
SmallVector<Value> replacements(replaced->getNumResults(), Value());
status = rootOp->walk([&](Operation *op) {
for (int64_t i = 0; i < replaced->getNumResults(); ++i) {
if (auto attr = op->getAttrOfType<IntegerAttr>("replacement_" +
std::to_string(i))) {
if (replacements[i]) {
op->emitError("only one 'replacement_" + std::to_string(i) +
"' is allowed per test case");
replacements[i].getDefiningOp()->emitRemark("other 'replacement_" +
std::to_string(i) + "'");
return WalkResult::interrupt();
}
replacements[i] = op->getResult(attr.getInt());
}
}
return WalkResult::advance();
});
if (status.wasInterrupted())
return failure();
if (!llvm::all_of(replacements,
[](Value v) { return static_cast<bool>(v); })) {
replaced->emitError("insufficient replacement values");
return failure();
}
// Find the replacement op (if any) and emit a remark/error.
transform::TransformState transformState =
transform::detail::makeTransformStateForTesting(/*region=*/nullptr,
/*payloadRoot=*/nullptr);
MLIRContext *context = rootOp->getContext();
OpBuilder builder(context);
OwningOpRef<transform::NamedSequenceOp> transformOp =
transform::NamedSequenceOp::create(
builder, rootOp->getLoc(),
/*sym_name=*/"test_sequence",
/*function_type=*/
TypeAttr::get(FunctionType::get(context, TypeRange{}, TypeRange{})),
/*sym_visibility*/ StringAttr::get(context, "public"),
/*arg_attrs=*/ArrayAttr::get(context, ArrayRef<Attribute>()),
/*res_attrs=*/ArrayAttr::get(context, ArrayRef<Attribute>()));
DummyTrackingListener listener(transformState, transformOp.get());
Operation *replacement = listener.getReplacementOp(replaced, replacements);
if (!replacement) {
replaced->emitError("listener could not find replacement op");
return failure();
}
replacement->emitRemark("replacement found");
return success();
}
void TestTensorTransforms::runOnOperation() {
Operation *rootOp = getOperation();
if (testFoldConstantExtractSlice)
applyFoldConstantExtractSlicePatterns(rootOp);
if (testFoldConsecutiveInsertExtractSlice)
applyFoldConsecutiveInsertExtractSlicePatterns(rootOp);
if (testDropRedundantInsertSliceRankExpansion)
applyDropRedundantInsertSliceRankExpansionPatterns(rootOp);
if (testReassociativeReshapeFolding)
applyReassociativeReshapeFoldingPatterns(rootOp);
if (testBubbleUpExpandShapePatterns)
applyBubbleUpExpandShapePatterns(rootOp);
if (testRewriteExtractSliceWithTiledCollapseShape) {
if (failed(
applyRewriteExtractFromCollapseShapePatterns(rootOp, useForeach)))
return signalPassFailure();
}
if (testFoldExtractFromCollapseShape)
applyFoldExtractFromCollapseShapePatterns(rootOp);
if (testTrackingListener)
if (failed(testTrackingListenerReplacements(rootOp)))
return signalPassFailure();
}
namespace mlir {
namespace test {
void registerTestTensorTransforms() {
PassRegistration<TestTensorTransforms>();
}
} // namespace test
} // namespace mlir
|