MLIR 22.0.0git
ConvertShapeConstraints.cpp
Go to the documentation of this file.
1//===- ConvertShapeConstraints.cpp - Conversion of shape constraints ------===//
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
10
15#include "mlir/Pass/Pass.h"
17
18namespace mlir {
19#define GEN_PASS_DEF_CONVERTSHAPECONSTRAINTSPASS
20#include "mlir/Conversion/Passes.h.inc"
21} // namespace mlir
22
23using namespace mlir;
24
25namespace {
26#include "ShapeToStandard.cpp.inc"
27} // namespace
28
29namespace {
30class ConvertCstrRequireOp : public OpRewritePattern<shape::CstrRequireOp> {
31public:
33 LogicalResult matchAndRewrite(shape::CstrRequireOp op,
34 PatternRewriter &rewriter) const override {
35 cf::AssertOp::create(rewriter, op.getLoc(), op.getPred(), op.getMsgAttr());
36 rewriter.replaceOpWithNewOp<shape::ConstWitnessOp>(op, true);
37 return success();
38 }
39};
40} // namespace
41
44 patterns.add<CstrBroadcastableToRequire>(patterns.getContext());
45 patterns.add<CstrEqToRequire>(patterns.getContext());
46 patterns.add<ConvertCstrRequireOp>(patterns.getContext());
47}
48
49namespace {
50// This pass eliminates shape constraints from the program, converting them to
51// eager (side-effecting) error handling code. After eager error handling code
52// is emitted, witnesses are satisfied, so they are replace with
53// `shape.const_witness true`.
54class ConvertShapeConstraints
55 : public impl::ConvertShapeConstraintsPassBase<ConvertShapeConstraints> {
56 void runOnOperation() override {
57 auto *func = getOperation();
58 auto *context = &getContext();
59
62
63 if (failed(applyPatternsGreedily(func, std::move(patterns))))
64 return signalPassFailure();
65 }
66};
67} // namespace
return success()
b getContext())
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Include the generated interface declarations.
LogicalResult applyPatternsGreedily(Region &region, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr)
Rewrite ops in the given region, which must be isolated from above, by repeatedly applying the highes...
const FrozenRewritePatternSet & patterns
void populateConvertShapeConstraintsConversionPatterns(RewritePatternSet &patterns)
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...