annotate mlir/docs/DialectConversion.md @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 # Dialect Conversion
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 This document describes a framework in MLIR in which to perform operation
anatofuz
parents:
diff changeset
4 conversions between, and within dialects. This framework allows for transforming
anatofuz
parents:
diff changeset
5 illegal operations to those supported by a provided conversion target, via a set
anatofuz
parents:
diff changeset
6 of pattern-based operation rewriting patterns.
anatofuz
parents:
diff changeset
7
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
8 The dialect conversion framework consists of the following components:
150
anatofuz
parents:
diff changeset
9
anatofuz
parents:
diff changeset
10 * A [Conversion Target](#conversion-target)
anatofuz
parents:
diff changeset
11 * A set of [Rewrite Patterns](#rewrite-pattern-specification)
anatofuz
parents:
diff changeset
12 * A [Type Converter](#type-conversion) (Optional)
anatofuz
parents:
diff changeset
13
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
14 [TOC]
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
15
150
anatofuz
parents:
diff changeset
16 ## Modes of Conversion
anatofuz
parents:
diff changeset
17
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
18 When applying a conversion to a set of operations, there are several different
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
19 conversion modes that may be selected from:
150
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 * Partial Conversion
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 - A partial conversion will legalize as many operations to the target as
anatofuz
parents:
diff changeset
24 possible, but will allow pre-existing operations that were not
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
25 explicitly marked as "illegal" to remain unconverted. This allows for
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
26 partially lowering parts of the input in the presence of unknown
150
anatofuz
parents:
diff changeset
27 operations.
anatofuz
parents:
diff changeset
28 - A partial conversion can be applied via `applyPartialConversion`.
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 * Full Conversion
anatofuz
parents:
diff changeset
31
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
32 - A full conversion legalizes all input operations, and is only successful
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
33 if all operations are properly legalized to the given conversion target.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
34 This ensures that only known operations will exist after the conversion
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
35 process.
150
anatofuz
parents:
diff changeset
36 - A full conversion can be applied via `applyFullConversion`.
anatofuz
parents:
diff changeset
37
anatofuz
parents:
diff changeset
38 * Analysis Conversion
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 - An analysis conversion will analyze which operations are legalizable to
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
41 the given conversion target if a conversion were to be applied. This is
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
42 done by performing a 'partial' conversion and recording which operations
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
43 would have been successfully converted if successful. Note that no
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
44 rewrites, or transformations, are actually applied to the input
150
anatofuz
parents:
diff changeset
45 operations.
anatofuz
parents:
diff changeset
46 - An analysis conversion can be applied via `applyAnalysisConversion`.
anatofuz
parents:
diff changeset
47
223
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
48 In all cases, the framework walks the operations in preorder, examining an op
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
49 before the ops in any regions it has.
5f17cb93ff66 LLVM13 (2021/7/18)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 221
diff changeset
50
150
anatofuz
parents:
diff changeset
51 ## Conversion Target
anatofuz
parents:
diff changeset
52
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
53 The conversion target is a formal definition of what is considered to be legal
150
anatofuz
parents:
diff changeset
54 during the conversion process. The final operations generated by the conversion
anatofuz
parents:
diff changeset
55 framework must be marked as legal on the `ConversionTarget` for the rewrite to
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
56 be a success. Depending on the conversion mode, existing operations need not
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
57 always be legal. Operations and dialects may be marked with any of the provided
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
58 legality actions below:
150
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 * Legal
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 - This action signals that every instance of a given operation is legal,
anatofuz
parents:
diff changeset
63 i.e. any combination of attributes, operands, types, etc. are valid.
anatofuz
parents:
diff changeset
64
anatofuz
parents:
diff changeset
65 * Dynamic
anatofuz
parents:
diff changeset
66
anatofuz
parents:
diff changeset
67 - This action signals that only some instances of a given operation are
anatofuz
parents:
diff changeset
68 legal. This allows for defining fine-tune constraints, e.g. saying that
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
69 `arith.addi` is only legal when operating on 32-bit integers.
150
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 * Illegal
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 - This action signals that no instance of a given operation is legal.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
74 Operations marked as "illegal" must always be converted for the
150
anatofuz
parents:
diff changeset
75 conversion to be successful. This action also allows for selectively
anatofuz
parents:
diff changeset
76 marking specific operations as illegal in an otherwise legal dialect.
anatofuz
parents:
diff changeset
77
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
78 Operations and dialects that are neither explicitly marked legal nor illegal are
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
79 separate from the above ("unknown" operations) and are treated differently, for
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
80 example, for the purposes of partial conversion as mentioned above.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
81
150
anatofuz
parents:
diff changeset
82 An example conversion target is shown below:
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 ```c++
anatofuz
parents:
diff changeset
85 struct MyTarget : public ConversionTarget {
anatofuz
parents:
diff changeset
86 MyTarget(MLIRContext &ctx) : ConversionTarget(ctx) {
anatofuz
parents:
diff changeset
87 //--------------------------------------------------------------------------
anatofuz
parents:
diff changeset
88 // Marking an operation as Legal:
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 /// Mark all operations within the LLVM dialect are legal.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
91 addLegalDialect<LLVMDialect>();
150
anatofuz
parents:
diff changeset
92
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
93 /// Mark `arith.constant` op is always legal on this target.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
94 addLegalOp<arith::ConstantOp>();
150
anatofuz
parents:
diff changeset
95
anatofuz
parents:
diff changeset
96 //--------------------------------------------------------------------------
anatofuz
parents:
diff changeset
97 // Marking an operation as dynamically legal.
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 /// Mark all operations within Affine dialect have dynamic legality
anatofuz
parents:
diff changeset
100 /// constraints.
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
101 addDynamicallyLegalDialect<affine::AffineDialect>(
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
102 [](Operation *op) { ... });
150
anatofuz
parents:
diff changeset
103
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
104 /// Mark `func.return` as dynamically legal, but provide a specific legality
150
anatofuz
parents:
diff changeset
105 /// callback.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
106 addDynamicallyLegalOp<func::ReturnOp>([](func::ReturnOp op) { ... });
150
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 /// Treat unknown operations, i.e. those without a legalization action
anatofuz
parents:
diff changeset
109 /// directly set, as dynamically legal.
anatofuz
parents:
diff changeset
110 markUnknownOpDynamicallyLegal([](Operation *op) { ... });
anatofuz
parents:
diff changeset
111
anatofuz
parents:
diff changeset
112 //--------------------------------------------------------------------------
anatofuz
parents:
diff changeset
113 // Marking an operation as illegal.
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 /// All operations within the GPU dialect are illegal.
anatofuz
parents:
diff changeset
116 addIllegalDialect<GPUDialect>();
anatofuz
parents:
diff changeset
117
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
118 /// Mark `cf.br` and `cf.cond_br` as illegal.
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
119 addIllegalOp<cf::BranchOp, cf::CondBranchOp>();
150
anatofuz
parents:
diff changeset
120 }
anatofuz
parents:
diff changeset
121
anatofuz
parents:
diff changeset
122 /// Implement the default legalization handler to handle operations marked as
anatofuz
parents:
diff changeset
123 /// dynamically legal that were not provided with an explicit handler.
anatofuz
parents:
diff changeset
124 bool isDynamicallyLegal(Operation *op) override { ... }
anatofuz
parents:
diff changeset
125 };
anatofuz
parents:
diff changeset
126 ```
anatofuz
parents:
diff changeset
127
anatofuz
parents:
diff changeset
128 ### Recursive Legality
anatofuz
parents:
diff changeset
129
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
130 In some cases, it may be desirable to mark entire regions as legal. This
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
131 provides an additional granularity of context to the concept of "legal". If an
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
132 operation is marked recursively legal, either statically or dynamically, then
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
133 all of the operations nested within are also considered legal even if they would
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
134 otherwise be considered "illegal". An operation can be marked via
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
135 `markOpRecursivelyLegal<>`:
150
anatofuz
parents:
diff changeset
136
anatofuz
parents:
diff changeset
137 ```c++
anatofuz
parents:
diff changeset
138 ConversionTarget &target = ...;
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 /// The operation must first be marked as `Legal` or `Dynamic`.
anatofuz
parents:
diff changeset
141 target.addLegalOp<MyOp>(...);
anatofuz
parents:
diff changeset
142 target.addDynamicallyLegalOp<MySecondOp>(...);
anatofuz
parents:
diff changeset
143
anatofuz
parents:
diff changeset
144 /// Mark the operation as always recursively legal.
anatofuz
parents:
diff changeset
145 target.markOpRecursivelyLegal<MyOp>();
anatofuz
parents:
diff changeset
146 /// Mark optionally with a callback to allow selective marking.
anatofuz
parents:
diff changeset
147 target.markOpRecursivelyLegal<MyOp, MySecondOp>([](Operation *op) { ... });
anatofuz
parents:
diff changeset
148 /// Mark optionally with a callback to allow selective marking.
anatofuz
parents:
diff changeset
149 target.markOpRecursivelyLegal<MyOp>([](MyOp op) { ... });
anatofuz
parents:
diff changeset
150 ```
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 ## Rewrite Pattern Specification
anatofuz
parents:
diff changeset
153
anatofuz
parents:
diff changeset
154 After the conversion target has been defined, a set of legalization patterns
anatofuz
parents:
diff changeset
155 must be provided to transform illegal operations into legal ones. The patterns
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
156 supplied here have the same structure and restrictions as those described in the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
157 main [Pattern](PatternRewriter.md) documentation. The patterns provided do not
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
158 need to generate operations that are directly legal on the target. The framework
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
159 will automatically build a graph of conversions to convert non-legal operations
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160 into a set of legal ones.
150
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 As an example, say you define a target that supports one operation: `foo.add`.
anatofuz
parents:
diff changeset
163 When providing the following patterns: [`bar.add` -> `baz.add`, `baz.add` ->
anatofuz
parents:
diff changeset
164 `foo.add`], the framework will automatically detect that it can legalize
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165 `bar.add` -> `foo.add` even though a direct conversion does not exist. This
150
anatofuz
parents:
diff changeset
166 means that you don’t have to define a direct legalization pattern for `bar.add`
anatofuz
parents:
diff changeset
167 -> `foo.add`.
anatofuz
parents:
diff changeset
168
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
169 ### Conversion Patterns
150
anatofuz
parents:
diff changeset
170
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
171 Along with the general `RewritePattern` classes, the conversion framework
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
172 provides a special type of rewrite pattern that can be used when a pattern
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
173 relies on interacting with constructs specific to the conversion process, the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
174 `ConversionPattern`. For example, the conversion process does not necessarily
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
175 update operations in-place and instead creates a mapping of events such as
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
176 replacements and erasures, and only applies them when the entire conversion
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
177 process is successful. Certain classes of patterns rely on using the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
178 updated/remapped operands of an operation, such as when the types of results
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
179 defined by an operation have changed. The general Rewrite Patterns can no longer
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
180 be used in these situations, as the types of the operands of the operation being
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
181 matched will not correspond with those expected by the user. This pattern
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
182 provides, as an additional argument to the `matchAndRewrite` and `rewrite`
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
183 methods, the list of operands that the operation should use after conversion. If
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
184 an operand was the result of a non-converted operation, for example if it was
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
185 already legal, the original operand is used. This means that the operands
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
186 provided always have a 1-1 non-null correspondence with the operands on the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
187 operation. The original operands of the operation are still intact and may be
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
188 inspected as normal. These patterns also utilize a special `PatternRewriter`,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
189 `ConversionPatternRewriter`, that provides special hooks for use with the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
190 conversion infrastructure.
150
anatofuz
parents:
diff changeset
191
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
192 ```c++
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
193 struct MyConversionPattern : public ConversionPattern {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
194 /// The `matchAndRewrite` hooks on ConversionPatterns take an additional
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
195 /// `operands` parameter, containing the remapped operands of the original
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
196 /// operation.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
197 virtual LogicalResult
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
198 matchAndRewrite(Operation *op, ArrayRef<Value> operands,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
199 ConversionPatternRewriter &rewriter) const;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
200 };
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
201 ```
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
202
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
203 #### Type Safety
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
204
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
205 The types of the remapped operands provided to a conversion pattern must be of a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
206 type expected by the pattern. The expected types of a pattern are determined by
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
207 a provided [TypeConverter](#type-converter). If no type converter is provided,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
208 the types of the remapped operands are expected to match the types of the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
209 original operands. If a type converter is provided, the types of the remapped
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
210 operands are expected to be legal as determined by the converter. If the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
211 remapped operand types are not of an expected type, and a materialization to the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
212 expected type could not be performed, the pattern fails application before the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
213 `matchAndRewrite` hook is invoked. This ensures that patterns do not have to
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
214 explicitly ensure type safety, or sanitize the types of the incoming remapped
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
215 operands. More information on type conversion is detailed in the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
216 [dedicated section](#type-conversion) below.
150
anatofuz
parents:
diff changeset
217
anatofuz
parents:
diff changeset
218 ## Type Conversion
anatofuz
parents:
diff changeset
219
anatofuz
parents:
diff changeset
220 It is sometimes necessary as part of a conversion to convert the set types of
anatofuz
parents:
diff changeset
221 being operated on. In these cases, a `TypeConverter` object may be defined that
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
222 details how types should be converted when interfacing with a pattern. A
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
223 `TypeConverter` may be used to convert the signatures of block arguments and
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
224 regions, to define the expected inputs types of the pattern, and to reconcile
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
225 type differences in general.
150
anatofuz
parents:
diff changeset
226
anatofuz
parents:
diff changeset
227 ### Type Converter
anatofuz
parents:
diff changeset
228
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
229 The `TypeConverter` contains several hooks for detailing how to convert types,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
230 and how to materialize conversions between types in various situations. The two
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
231 main aspects of the `TypeConverter` are conversion and materialization.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
232
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
233 A `conversion` describes how a given illegal source `Type` should be converted
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
234 to N target types. If the source type is already "legal", it should convert to
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
235 itself. Type conversions are specified via the `addConversion` method described
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
236 below.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
237
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
238 A `materialization` describes how a set of values should be converted to a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
239 single value of a desired type. An important distinction with a `conversion` is
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
240 that a `materialization` can produce IR, whereas a `conversion` cannot. These
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
241 materializations are used by the conversion framework to ensure type safety
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
242 during the conversion process. There are several types of materializations
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
243 depending on the situation.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
244
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
245 * Argument Materialization
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
246
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
247 - An argument materialization is used when converting the type of a block
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
248 argument during a [signature conversion](#region-signature-conversion).
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
249
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
250 * Source Materialization
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
251
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
252 - A source materialization converts from a value with a "legal" target
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
253 type, back to a specific source type. This is used when an operation is
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
254 "legal" during the conversion process, but contains a use of an illegal
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
255 type. This may happen during a conversion where some operations are
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
256 converted to those with different resultant types, but still retain
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
257 users of the original type system.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
258 - This materialization is used in the following situations:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
259 * When a block argument has been converted to a different type, but
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
260 the original argument still has users that will remain live after
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
261 the conversion process has finished.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
262 * When the result type of an operation has been converted to a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
263 different type, but the original result still has users that will
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
264 remain live after the conversion process is finished.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
265
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
266 * Target Materialization
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
267
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
268 - A target materialization converts from a value with an "illegal" source
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
269 type, to a value of a "legal" type. This is used when a pattern expects
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
270 the remapped operands to be of a certain set of types, but the original
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
271 input operands have not been converted. This may happen during a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
272 conversion where some operations are converted to those with different
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
273 resultant types, but still retain uses of the original type system.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
274 - This materialization is used in the following situations:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
275 * When the remapped operands of a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
276 [conversion pattern](#conversion-patterns) are not legal for the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
277 type conversion provided by the pattern.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
278
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
279 If a converted value is used by an operation that isn't converted, it needs a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
280 conversion back to the `source` type, hence source materialization; if an
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
281 unconverted value is used by an operation that is being converted, it needs
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
282 conversion to the `target` type, hence target materialization.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
283
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
284 As noted above, the conversion process guarantees that the type contract of the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
285 IR is preserved during the conversion. This means that the types of value uses
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
286 will not implicitly change during the conversion process. When the type of a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
287 value definition, either block argument or operation result, is being changed,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
288 the users of that definition must also be updated during the conversion process.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
289 If they aren't, a type conversion must be materialized to ensure that a value of
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
290 the expected type is still present within the IR. If a target materialization is
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
291 required, but cannot be performed, the pattern application fails. If a source
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
292 materialization is required, but cannot be performed, the entire conversion
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
293 process fails.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
294
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
295 Several of the available hooks are detailed below:
150
anatofuz
parents:
diff changeset
296
anatofuz
parents:
diff changeset
297 ```c++
anatofuz
parents:
diff changeset
298 class TypeConverter {
anatofuz
parents:
diff changeset
299 public:
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
300 /// Register a conversion function. A conversion function defines how a given
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
301 /// source type should be converted. A conversion function must be convertible
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
302 /// to any of the following forms(where `T` is a class derived from `Type`:
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
303 /// * Optional<Type>(T)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
304 /// - This form represents a 1-1 type conversion. It should return nullptr
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
305 /// or `std::nullopt` to signify failure. If `std::nullopt` is returned, the
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
306 /// converter is allowed to try another conversion function to perform
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
307 /// the conversion.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
308 /// * Optional<LogicalResult>(T, SmallVectorImpl<Type> &)
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
309 /// - This form represents a 1-N type conversion. It should return
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
310 /// `failure` or `std::nullopt` to signify a failed conversion. If the new
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
311 /// set of types is empty, the type is removed and any usages of the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
312 /// existing value are expected to be removed during conversion. If
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
313 /// `std::nullopt` is returned, the converter is allowed to try another
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
314 /// conversion function to perform the conversion.
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
315 /// * Optional<LogicalResult>(T, SmallVectorImpl<Type> &, ArrayRef<Type>)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
316 /// - This form represents a 1-N type conversion supporting recursive
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
317 /// types. The first two arguments and the return value are the same as
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
318 /// for the regular 1-N form. The third argument is contains is the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
319 /// "call stack" of the recursive conversion: it contains the list of
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
320 /// types currently being converted, with the current type being the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
321 /// last one. If it is present more than once in the list, the
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
322 /// conversion concerns a recursive type.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
323 /// Note: When attempting to convert a type, e.g. via 'convertType', the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
324 /// mostly recently added conversions will be invoked first.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
325 template <typename FnT,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
326 typename T = typename llvm::function_traits<FnT>::template arg_t<0>>
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
327 void addConversion(FnT &&callback) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
328 registerConversion(wrapCallback<T>(std::forward<FnT>(callback)));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
329 }
150
anatofuz
parents:
diff changeset
330
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
331 /// Register a materialization function, which must be convertible to the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
332 /// following form:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
333 /// `Optional<Value> (OpBuilder &, T, ValueRange, Location)`,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
334 /// where `T` is any subclass of `Type`.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
335 /// This function is responsible for creating an operation, using the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
336 /// OpBuilder and Location provided, that "converts" a range of values into a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
337 /// single value of the given type `T`. It must return a Value of the
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
338 /// converted type on success, an `std::nullopt` if it failed but other
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
339 /// materialization can be attempted, and `nullptr` on unrecoverable failure.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
340 /// It will only be called for (sub)types of `T`.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
341 ///
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
342 /// This method registers a materialization that will be called when
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
343 /// converting an illegal block argument type, to a legal type.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
344 template <typename FnT,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
345 typename T = typename llvm::function_traits<FnT>::template arg_t<1>>
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
346 void addArgumentMaterialization(FnT &&callback) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
347 argumentMaterializations.emplace_back(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
348 wrapMaterialization<T>(std::forward<FnT>(callback)));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
349 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
350 /// This method registers a materialization that will be called when
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
351 /// converting a legal type to an illegal source type. This is used when
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
352 /// conversions to an illegal type must persist beyond the main conversion.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
353 template <typename FnT,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
354 typename T = typename llvm::function_traits<FnT>::template arg_t<1>>
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
355 void addSourceMaterialization(FnT &&callback) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
356 sourceMaterializations.emplace_back(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
357 wrapMaterialization<T>(std::forward<FnT>(callback)));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
358 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
359 /// This method registers a materialization that will be called when
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
360 /// converting type from an illegal, or source, type to a legal type.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
361 template <typename FnT,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
362 typename T = typename llvm::function_traits<FnT>::template arg_t<1>>
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
363 void addTargetMaterialization(FnT &&callback) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
364 targetMaterializations.emplace_back(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
365 wrapMaterialization<T>(std::forward<FnT>(callback)));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
366 }
150
anatofuz
parents:
diff changeset
367 };
anatofuz
parents:
diff changeset
368 ```
anatofuz
parents:
diff changeset
369
anatofuz
parents:
diff changeset
370 ### Region Signature Conversion
anatofuz
parents:
diff changeset
371
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
372 From the perspective of type conversion, the types of block arguments are a bit
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
373 special. Throughout the conversion process, blocks may move between regions of
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
374 different operations. Given this, the conversion of the types for blocks must be
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
375 done explicitly via a conversion pattern. To convert the types of block
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
376 arguments within a Region, a custom hook on the `ConversionPatternRewriter` must
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
377 be invoked; `convertRegionTypes`. This hook uses a provided type converter to
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
378 apply type conversions to all blocks within a given region, and all blocks that
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
379 move into that region. As noted above, the conversions performed by this method
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
380 use the argument materialization hook on the `TypeConverter`. This hook also
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
381 takes an optional `TypeConverter::SignatureConversion` parameter that applies a
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
382 custom conversion to the entry block of the region. The types of the entry block
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
383 arguments are often tied semantically to details on the operation, e.g. func::FuncOp,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
384 AffineForOp, etc. To convert the signature of just the region entry block, and
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
385 not any other blocks within the region, the `applySignatureConversion` hook may
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
386 be used instead. A signature conversion, `TypeConverter::SignatureConversion`,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
387 can be built programmatically:
150
anatofuz
parents:
diff changeset
388
anatofuz
parents:
diff changeset
389 ```c++
anatofuz
parents:
diff changeset
390 class SignatureConversion {
anatofuz
parents:
diff changeset
391 public:
anatofuz
parents:
diff changeset
392 /// Remap an input of the original signature with a new set of types. The
anatofuz
parents:
diff changeset
393 /// new types are appended to the new signature conversion.
anatofuz
parents:
diff changeset
394 void addInputs(unsigned origInputNo, ArrayRef<Type> types);
anatofuz
parents:
diff changeset
395
anatofuz
parents:
diff changeset
396 /// Append new input types to the signature conversion, this should only be
anatofuz
parents:
diff changeset
397 /// used if the new types are not intended to remap an existing input.
anatofuz
parents:
diff changeset
398 void addInputs(ArrayRef<Type> types);
anatofuz
parents:
diff changeset
399
anatofuz
parents:
diff changeset
400 /// Remap an input of the original signature with a range of types in the
anatofuz
parents:
diff changeset
401 /// new signature.
anatofuz
parents:
diff changeset
402 void remapInput(unsigned origInputNo, unsigned newInputNo,
anatofuz
parents:
diff changeset
403 unsigned newInputCount = 1);
anatofuz
parents:
diff changeset
404
anatofuz
parents:
diff changeset
405 /// Remap an input of the original signature to another `replacement`
anatofuz
parents:
diff changeset
406 /// value. This drops the original argument.
anatofuz
parents:
diff changeset
407 void remapInput(unsigned origInputNo, Value replacement);
anatofuz
parents:
diff changeset
408 };
anatofuz
parents:
diff changeset
409 ```
anatofuz
parents:
diff changeset
410
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
411 The `TypeConverter` provides several default utilities for signature conversion
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
412 and legality checking:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
413 `convertSignatureArgs`/`convertBlockSignature`/`isLegal(Region *|Type)`.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
414
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
415 ## Debugging
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
416
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
417 To debug the execution of the dialect conversion framework,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
418 `-debug-only=dialect-conversion` may be used. This command line flag activates
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
419 LLVM's debug logging infrastructure solely for the conversion framework. The
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
420 output is formatted as a tree structure, mirroring the structure of the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
421 conversion process. This output contains all of the actions performed by the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
422 rewriter, how generated operations get legalized, and why they fail.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
423
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
424 Example output is shown below:
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
425
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
426 ```
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
427 //===-------------------------------------------===//
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
428 Legalizing operation : 'func.return'(0x608000002e20) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
429 "func.return"() : () -> ()
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
430
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
431 * Fold {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
432 } -> FAILURE : unable to fold
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
433
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
434 * Pattern : 'func.return -> ()' {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
435 ** Insert : 'spirv.Return'(0x6070000453e0)
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
436 ** Replace : 'func.return'(0x608000002e20)
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
437
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
438 //===-------------------------------------------===//
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
439 Legalizing operation : 'spirv.Return'(0x6070000453e0) {
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
440 "spirv.Return"() : () -> ()
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
441
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
442 } -> SUCCESS : operation marked legal by the target
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
443 //===-------------------------------------------===//
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
444 } -> SUCCESS : pattern applied successfully
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
445 } -> SUCCESS
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
446 //===-------------------------------------------===//
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
447 ```
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
448
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
449 This output is describing the legalization of an `func.return` operation. We
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
450 first try to legalize by folding the operation, but that is unsuccessful for
236
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
451 `func.return`. From there, a pattern is applied that replaces the `func.return`
c4bab56944e8 LLVM 16
kono
parents: 223
diff changeset
452 with a `spirv.Return`. The newly generated `spirv.Return` is then processed for
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
453 legalization, but is found to already legal as per the target.