RemoveShapeConstraints.cpp
1.99 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
//===-- RemoveShapeConstraints.cpp - Remove Shape Cstr and Assuming Ops ---===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "PassDetail.h"
#include "mlir/Dialect/Shape/IR/Shape.h"
#include "mlir/Dialect/Shape/Transforms/Passes.h"
#include "mlir/Transforms/DialectConversion.h"
using namespace mlir;
namespace {
/// Removal patterns.
class RemoveCstrBroadcastableOp
: public OpRewritePattern<shape::CstrBroadcastableOp> {
public:
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(shape::CstrBroadcastableOp op,
PatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<shape::ConstWitnessOp>(op.getOperation(), true);
return success();
}
};
class RemoveCstrEqOp : public OpRewritePattern<shape::CstrEqOp> {
public:
using OpRewritePattern::OpRewritePattern;
LogicalResult matchAndRewrite(shape::CstrEqOp op,
PatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<shape::ConstWitnessOp>(op.getOperation(), true);
return success();
}
};
/// Removal pass.
class RemoveShapeConstraintsPass
: public RemoveShapeConstraintsBase<RemoveShapeConstraintsPass> {
void runOnFunction() override {
MLIRContext &ctx = getContext();
OwningRewritePatternList patterns;
populateRemoveShapeConstraintsPatterns(patterns, &ctx);
applyPatternsAndFoldGreedily(getFunction(), patterns);
}
};
} // namespace
void mlir::populateRemoveShapeConstraintsPatterns(
OwningRewritePatternList &patterns, MLIRContext *ctx) {
patterns.insert<RemoveCstrBroadcastableOp, RemoveCstrEqOp>(ctx);
}
std::unique_ptr<FunctionPass> mlir::createRemoveShapeConstraintsPass() {
return std::make_unique<RemoveShapeConstraintsPass>();
}