1//===- TestLinalgDropUnitDims.cpp - Test Linalg drop unit dims -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a pass for testing the transformation to drop unit
10// extent dimensions from `linalg.generic` operations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/Func/IR/FuncOps.h"
15#include "mlir/Dialect/Linalg/IR/Linalg.h"
16#include "mlir/Dialect/Linalg/Transforms/Transforms.h"
17#include "mlir/Pass/Pass.h"
18#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
19
20using namespace mlir;
21
22namespace {
23
24LogicalResult dropOutermostUnitDims(RewriterBase &rewriter,
25 linalg::GenericOp genericOp) {
26 linalg::ControlDropUnitDims options;
27 options.controlFn = [](Operation *op) { return SmallVector<unsigned>{0}; };
28 FailureOr<linalg::DropUnitDimsResult> result =
29 linalg::dropUnitDims(rewriter, genericOp, options);
30 if (failed(Result: result)) {
31 return failure();
32 }
33 rewriter.replaceOp(op: genericOp, newValues: result->replacements);
34 return success();
35}
36
37LogicalResult dropOutermostUnitDimsWithEncoding(RewriterBase &rewriter,
38 linalg::GenericOp genericOp) {
39 linalg::ControlDropUnitDims options;
40 linalg::ControlDropUnitDims::CollapseFnTy defaultCollapseFn =
41 options.collapseFn;
42 linalg::ControlDropUnitDims::ExpandFnTy defaultExpandFn = options.expandFn;
43 options.controlFn = [](Operation *op) { return SmallVector<unsigned>{0}; };
44 options.collapseFn =
45 [=](RewriterBase &rewriter, Location loc, Value operand,
46 ArrayRef<int64_t> targetShape,
47 ArrayRef<ReassociationIndices> reassociation,
48 const linalg::ControlDropUnitDims &control) -> FailureOr<Value> {
49 if (auto tensorType = dyn_cast<RankedTensorType>(Val: operand.getType())) {
50 if (tensorType.getEncoding()) {
51 assert(control.rankReductionStrategy ==
52 linalg::ControlDropUnitDims::RankReductionStrategy::
53 ReassociativeReshape &&
54 "unexpected rank reduction strategy");
55 auto targetType = RankedTensorType::get(
56 shape: targetShape, elementType: tensorType.getElementType(), encoding: tensorType.getEncoding());
57 return tensor::CollapseShapeOp::create(builder&: rewriter, location: loc, resultType: targetType,
58 src: operand, reassociation)
59 .getResult();
60 }
61 }
62 return defaultCollapseFn(rewriter, loc, operand, targetShape, reassociation,
63 control);
64 };
65 options.expandFn =
66 [=](RewriterBase &rewriter, Location loc, Value result, Value origDest,
67 ArrayRef<ReassociationIndices> reassociation,
68 const linalg::ControlDropUnitDims &control) -> FailureOr<Value> {
69 if (auto tensorType = dyn_cast<RankedTensorType>(Val: origDest.getType())) {
70 if (tensorType.getEncoding()) {
71 assert(control.rankReductionStrategy ==
72 linalg::ControlDropUnitDims::RankReductionStrategy::
73 ReassociativeReshape &&
74 "unexpected rank reduction strategy");
75 return tensor::ExpandShapeOp::create(builder&: rewriter, location: loc, resultType: tensorType, src: result,
76 reassociation)
77 .getResult();
78 }
79 }
80 return defaultExpandFn(rewriter, loc, result, origDest, reassociation,
81 control);
82 };
83
84 FailureOr<linalg::DropUnitDimsResult> result =
85 linalg::dropUnitDims(rewriter, genericOp, options);
86 if (failed(Result: result)) {
87 return failure();
88 }
89 rewriter.replaceOp(op: genericOp, newValues: result->replacements);
90 return success();
91}
92
93struct TestLinalgDropUnitDims
94 : public PassWrapper<TestLinalgDropUnitDims, OperationPass<func::FuncOp>> {
95
96 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLinalgDropUnitDims)
97
98 TestLinalgDropUnitDims() = default;
99 TestLinalgDropUnitDims(const TestLinalgDropUnitDims &pass)
100 : PassWrapper(pass) {}
101
102 Option<bool> collapseEncoded{
103 *this, "collapse-encoded",
104 llvm::cl::desc("Collapse and expand tensors with encodings"),
105 llvm::cl::init(Val: false)};
106
107 void getDependentDialects(DialectRegistry &registry) const override {
108 registry.insert<linalg::LinalgDialect>();
109 }
110
111 StringRef getArgument() const final { return "test-linalg-drop-unit-dims"; }
112
113 StringRef getDescriptions() const {
114 return "Test transformation to drop unit-extent dims from Linalg "
115 "operations";
116 }
117
118 void runOnOperation() override {
119 MLIRContext *context = &this->getContext();
120 func::FuncOp funcOp = this->getOperation();
121 IRRewriter rewriter(context);
122 SmallVector<linalg::GenericOp> genericOps;
123 funcOp.walk(
124 callback: [&](linalg::GenericOp genericOp) { genericOps.push_back(Elt: genericOp); });
125
126 for (auto genericOp : genericOps) {
127 rewriter.setInsertionPoint(genericOp);
128 if (collapseEncoded) {
129 (void)dropOutermostUnitDimsWithEncoding(rewriter, genericOp);
130 continue;
131 }
132 (void)dropOutermostUnitDims(rewriter, genericOp);
133 }
134 }
135};
136} // namespace
137
138namespace mlir {
139namespace test {
140void registerTestLinalgDropUnitDims() {
141 PassRegistration<TestLinalgDropUnitDims>();
142}
143} // namespace test
144} // namespace mlir
145