CIRCT 22.0.0git
Loading...
Searching...
No Matches
ConversionPatternSet.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// Utilities to collect sets of conversion patterns.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_SUPPORT_CONVERSIONPATTERNSET_H
14#define CIRCT_SUPPORT_CONVERSIONPATTERNSET_H
15
16#include "circt/Support/LLVM.h"
17#include "mlir/IR/PatternMatch.h"
18
19namespace circt {
20
21/// Extension of `RewritePatternSet` that allows adding `matchAndRewrite`
22/// functions with op adaptors and `ConversionPatternRewriter` as patterns.
23class ConversionPatternSet : public RewritePatternSet {
24public:
25 const TypeConverter &typeConverter;
26
27 ConversionPatternSet(MLIRContext *context, const TypeConverter &typeConverter)
28 : RewritePatternSet(context), typeConverter(typeConverter) {}
29
30 // Expose the `add` implementations of `RewritePatternSet`.
31 using RewritePatternSet::add;
32
33 /// Add a `matchAndRewrite` function as a conversion pattern to the set.
34 template <class Op>
36 add(LogicalResult (*implFn)(Op, typename Op::Adaptor,
37 ConversionPatternRewriter &)) {
38
39 struct FnPattern final : public OpConversionPattern<Op> {
40 using OpConversionPattern<Op>::OpConversionPattern;
41 LogicalResult (*implFn)(Op, typename Op::Adaptor,
42 ConversionPatternRewriter &);
43
44 LogicalResult
45 matchAndRewrite(Op op, typename Op::Adaptor adaptor,
46 ConversionPatternRewriter &rewriter) const override {
47 return implFn(op, adaptor, rewriter);
48 }
49 };
50
51 auto pattern = std::make_unique<FnPattern>(typeConverter, getContext());
52 pattern->implFn = implFn;
53 add(std::move(pattern));
54 return *this;
55 }
56
57 /// Add a `matchAndRewrite` function as a conversion pattern to the set.
58 template <class Op>
59 ConversionPatternSet &add(LogicalResult (*implFn)(Op, typename Op::Adaptor,
60 ConversionPatternRewriter &,
61 const TypeConverter &)) {
62
63 struct FnPattern final : public OpConversionPattern<Op> {
64 using OpConversionPattern<Op>::OpConversionPattern;
65 LogicalResult (*implFn)(Op, typename Op::Adaptor,
66 ConversionPatternRewriter &,
67 const TypeConverter &);
68
69 LogicalResult
70 matchAndRewrite(Op op, typename Op::Adaptor adaptor,
71 ConversionPatternRewriter &rewriter) const override {
72 return implFn(op, adaptor, rewriter, *this->typeConverter);
73 }
74 };
75
76 auto pattern = std::make_unique<FnPattern>(typeConverter, getContext());
77 pattern->implFn = implFn;
78 add(std::move(pattern));
79 return *this;
80 }
81};
82
83} // namespace circt
84
85#endif // CIRCT_SUPPORT_CONVERSIONPATTERNSET_H
RewritePatternSet pattern
Extension of RewritePatternSet that allows adding matchAndRewrite functions with op adaptors and Conv...
ConversionPatternSet(MLIRContext *context, const TypeConverter &typeConverter)
ConversionPatternSet & add(LogicalResult(*implFn)(Op, typename Op::Adaptor, ConversionPatternRewriter &, const TypeConverter &))
Add a matchAndRewrite function as a conversion pattern to the set.
ConversionPatternSet & add(LogicalResult(*implFn)(Op, typename Op::Adaptor, ConversionPatternRewriter &))
Add a matchAndRewrite function as a conversion pattern to the set.
const TypeConverter & typeConverter
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.