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 
14 #include "mlir/IR/PatternMatch.h"
15 #include "mlir/Pass/Pass.h"
17 
18 namespace mlir {
19 #define GEN_PASS_DEF_CONVERTSHAPECONSTRAINTSPASS
20 #include "mlir/Conversion/Passes.h.inc"
21 } // namespace mlir
22 
23 using namespace mlir;
24 
25 namespace {
26 #include "ShapeToStandard.cpp.inc"
27 } // namespace
28 
29 namespace {
30 class ConvertCstrRequireOp : public OpRewritePattern<shape::CstrRequireOp> {
31 public:
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 
49 namespace {
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`.
54 class ConvertShapeConstraints
55  : public impl::ConvertShapeConstraintsPassBase<ConvertShapeConstraints> {
56  void runOnOperation() override {
57  auto *func = getOperation();
58  auto *context = &getContext();
59 
60  RewritePatternSet patterns(context);
62 
63  if (failed(applyPatternsGreedily(func, std::move(patterns))))
64  return signalPassFailure();
65  }
66 };
67 } // namespace
static MLIRContext * getContext(OpFoldResult val)
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:783
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Definition: PatternMatch.h:519
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition: Remarks.h:491
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...
Definition: PatternMatch.h:314
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...
Definition: PatternMatch.h:319