CIRCT 23.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 /// Extra arguments beyond the op, adaptor, and rewriter are deduced from the
35 /// function pointer signature, stored in the pattern, and forwarded on each
36 /// invocation.
37 template <class Op, typename... ExtraArgs>
38 ConversionPatternSet &add(LogicalResult (*implFn)(Op, typename Op::Adaptor,
39 ConversionPatternRewriter &,
40 ExtraArgs...),
41 llvm::type_identity_t<ExtraArgs>... args) {
42
43 struct FnPattern final : public OpConversionPattern<Op> {
44 LogicalResult (*implFn)(Op, typename Op::Adaptor,
45 ConversionPatternRewriter &, ExtraArgs...);
46 std::tuple<ExtraArgs...> extraArgs;
47
48 FnPattern(const TypeConverter &tc, MLIRContext *ctx,
49 LogicalResult (*implFn)(Op, typename Op::Adaptor,
50 ConversionPatternRewriter &,
51 ExtraArgs...),
52 ExtraArgs... args)
53 : OpConversionPattern<Op>(tc, ctx), implFn(implFn),
54 extraArgs(args...) {}
55
56 LogicalResult
57 matchAndRewrite(Op op, typename Op::Adaptor adaptor,
58 ConversionPatternRewriter &rewriter) const override {
59 return std::apply(
60 implFn, std::tuple_cat(std::tie(op, adaptor, rewriter), extraArgs));
61 }
62 };
63
64 add(std::make_unique<FnPattern>(typeConverter, getContext(), implFn,
65 args...));
66 return *this;
67 }
68
69 /// Add a `matchAndRewrite` function as a conversion pattern to the set. The
70 /// pattern's type converter is automatically forwarded to the function,
71 /// followed by any extra arguments.
72 template <class Op, typename... ExtraArgs>
73 ConversionPatternSet &add(LogicalResult (*implFn)(Op, typename Op::Adaptor,
74 ConversionPatternRewriter &,
75 const TypeConverter &,
76 ExtraArgs...),
77 llvm::type_identity_t<ExtraArgs>... args) {
78
79 struct FnPattern final : public OpConversionPattern<Op> {
80 LogicalResult (*implFn)(Op, typename Op::Adaptor,
81 ConversionPatternRewriter &,
82 const TypeConverter &, ExtraArgs...);
83 std::tuple<ExtraArgs...> extraArgs;
84
85 FnPattern(const TypeConverter &tc, MLIRContext *ctx,
86 LogicalResult (*implFn)(Op, typename Op::Adaptor,
87 ConversionPatternRewriter &,
88 const TypeConverter &, ExtraArgs...),
89 ExtraArgs... args)
90 : OpConversionPattern<Op>(tc, ctx), implFn(implFn),
91 extraArgs(args...) {}
92
93 LogicalResult
94 matchAndRewrite(Op op, typename Op::Adaptor adaptor,
95 ConversionPatternRewriter &rewriter) const override {
96 return std::apply(
97 implFn, std::tuple_cat(std::tie(op, adaptor, rewriter),
98 std::tie(*this->typeConverter), extraArgs));
99 }
100 };
101
102 add(std::make_unique<FnPattern>(typeConverter, getContext(), implFn,
103 args...));
104 return *this;
105 }
106};
107
108} // namespace circt
109
110#endif // CIRCT_SUPPORT_CONVERSIONPATTERNSET_H
static std::unique_ptr< Context > context
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 &, ExtraArgs...), llvm::type_identity_t< ExtraArgs >... args)
Add a matchAndRewrite function as a conversion pattern to the set.
const TypeConverter & typeConverter
ConversionPatternSet & add(LogicalResult(*implFn)(Op, typename Op::Adaptor, ConversionPatternRewriter &, const TypeConverter &, ExtraArgs...), llvm::type_identity_t< ExtraArgs >... args)
Add a matchAndRewrite function as a conversion pattern to the set.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.