Files
clang-p2996/mlir/lib/Dialect/Shape/Transforms/RemoveShapeConstraints.cpp
River Riddle b6eb26fd0e [mlir][NFC] Move around the code related to PatternRewriting to improve layering
There are several pieces of pattern rewriting infra in IR/ that really shouldn't be there. This revision moves those pieces to a better location such that they are easier to evolve in the future(e.g. with PDL). More concretely this revision does the following:

* Create a Transforms/GreedyPatternRewriteDriver.h and move the apply*andFold methods there.
The definitions for these methods are already in Transforms/ so it doesn't make sense for the declarations to be in IR.

* Create a new lib/Rewrite library and move PatternApplicator there.
This new library will be focused on applying rewrites, and will also include compiling rewrites with PDL.

Differential Revision: https://reviews.llvm.org/D89103
2020-10-26 18:01:06 -07:00

66 lines
2.0 KiB
C++

//===-- 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"
#include "mlir/Transforms/GreedyPatternRewriteDriver.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>();
}