/
LinalgMatchOps.td
616 lines (503 loc) · 24.8 KB
/
LinalgMatchOps.td
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//===- LinalgMatchOps.td - Linalg transform matcher ops ----*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LINALG_MATCH_OPS
#define LINALG_MATCH_OPS
include "mlir/Dialect/Linalg/TransformOps/LinalgTransformEnums.td"
include "mlir/Dialect/Transform/Interfaces/MatchInterfaces.td"
include "mlir/Dialect/Transform/IR/TransformAttrs.td"
include "mlir/Dialect/Transform/IR/TransformDialect.td"
include "mlir/Dialect/Transform/IR/TransformTypes.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
//===----------------------------------------------------------------------===//
// Structured match op and predicates usable inside it.
//===----------------------------------------------------------------------===//
def MatchStructuredOp : Op<Transform_Dialect, "match.structured", [
MatchOpInterface,
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
SingleOpMatcher,
SingleBlockImplicitTerminator<"::mlir::transform::MatchStructuredYieldOp">]> {
let summary =
"Matches a structured (linalg) operation with additional conditions";
let description = [{
Checks if the payload operation associated with the operand handle is a
structured operation, that is, an operation that implements
`LinalgOpInterface`, and that all conditions listed in the body of this
operation are satisfied. Produces a silenceable failure if the payload
operation is not structured.
The transform operations nested in the body region are applied one by one.
If any of them produces a failure, silenceable or definite, the following
operations are not applied. If the failure propagation mode is "propagate",
silenceable failures are forwarded as the result of this operation. If it is
"suppress", they are ignored and this operation immediately succeeds.
Definite failures are always propagated immediately.
In case of success, the transform values produced by this operation are
associated with the same payload as the operands of the block terminator. If
any of the nested operations produced a silenceable failure, regardless of
the failure propagation mode, the transform values produced by this
operation that correspond to the already defined terminator operands are
associated with the same payload as the already defined terminator operands.
Other values produced by this operation are associated with empty payloads.
If the failure propagation mode is not specified, it is considered
"propagate" by default. The "suppress" mode can be used to specify optional
matches.
#### Return modes
This operation only reads all operand handles and produces all resulting
handles. It succeeds in "propagate" mode if the payload operation is a
structured operation and if all the nested operations succeed. It succeeds
in "suppress" mode as long as the operand handle is associated with exactly
one payload operation. It produces a definite failure when the handle is
not associated with exactly one payload operation.
}];
let arguments = (ins TransformHandleTypeInterface:$current,
OptionalAttr<FailurePropagationMode>:$failure_propagation_mode);
let results = (outs Variadic<Transform_AnyHandleOrParamType>:$outputs);
let regions = (region SizedRegion<1>:$body_region);
let assemblyFormat =
"(`failures` `(` $failure_propagation_mode^ `)`)?"
"$current `:` custom<SemiFunctionType>(type($current), type($outputs))"
"attr-dict-with-keyword regions";
let hasVerifier = 1;
let extraClassDeclaration = SingleOpMatcher.extraDeclaration # [{
::mlir::Value getOperandHandle() { return getCurrent(); }
}];
}
def StructuredPredicate : NativeOpTrait<"StructuredOpPredicateOpTrait"> {
let cppNamespace = "::mlir::transform";
string extraDescription = [{
This op can only appear immediately inside a `transform.match.structured`
op and apply to its first block argument because it assumes the payload
to have been already checked for being a single structured op.
}];
}
def MatchStructuredBodyOp : Op<Transform_Dialect, "match.structured.body", [
SingleOpMatcher,
StructuredPredicate,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary =
"Checks if the body of the structured op satisfies some criteria";
let description = !strconcat([{
Checks if the body of the structured payload op satisfies one of the
following mutually exclusive criteria specified by attributes:
* `reduction_position`: the body of the structured payload op implements
a reduction of the `n`-th operand (`n` is the value of the attribute)
using a single combiner operation;
* `passthrough`: the body of the structured payload op only forwards
inputs to the outputs (copy or broadcast).
* `elementwise`: the body of the structured payload op represents an
elementwise operation.
* `contraction`: the body of the structured payload op is a contraction
of the form `<red>(<elem>(bbarg0, bbarg1), bbarg2)` where `<elem>` and
`<red>` are binary operations whose names are specified in the attribute
and operands can be permuted and optionally forwarded through a chain of
unary side effect-free operations.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the operation body satisfies the specified criteria, produces a
silenceable failure otherwise. Produces a definite failure if the operand is
not associated with a single payload op.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle,
OptionalAttr<I64Attr>:$reduction_position,
UnitAttr:$passthrough,
UnitAttr:$elementwise,
OptionalAttr<StrArrayAttr>:$contraction);
let assemblyFormat = "$operand_handle attr-dict `:` type($operand_handle)";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration;
let hasVerifier = 1;
}
def MatchStructuredClassifyContractionDimsOp
: Op<Transform_Dialect, "match.structured.classify_contraction_dims", [
SingleOpMatcher,
StructuredPredicate,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary =
"Checks if an operation has contraction-like dimensions and returns them";
let description = !strconcat([{
Checks if the structured payload op has contraction-like dimensions as
follows:
C(batch, m, n) += A(batch, m, k) * B(batch, k, n)
That is:
- 'batch' are parallel dimensions used in inputs and result;
- 'm' are parallel dimensions used in the LHS and result;
- 'n' are parallel dimensions used in rhe RHS and result;
- 'k' are reduction dimensions present only in LHS and RHS.
Note that this doesn't check the operation in the body.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the operation has the contraction-like dimensions, produces a
silenceable failure otherwise.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle);
let results = (outs TransformParamTypeInterface:$batch,
TransformParamTypeInterface:$m,
TransformParamTypeInterface:$n,
TransformParamTypeInterface:$k);
let assemblyFormat =
"$operand_handle attr-dict `:` functional-type(operands, results)";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration;
}
def MatchStructuredClassifyConvolutionDimsOp
: Op<Transform_Dialect, "match.structured.classify_convolution_dims", [
SingleOpMatcher,
StructuredPredicate,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary =
"Checks if an operation has convolution-like dimensions and returns them";
let description = !strconcat([{
Checks if the structured payload op has convolution-like dimensions as
follows:
C(batch, depth, oi, oc) += A(batch, depth, oi, ic) * B(fl, depth, ic, oc)
That is:
- 'batch' are parallel dimensions used in the input and result;
- 'output_image' ('oi') are parallel dimensions used in the input and result;
- 'output_channel' ('oc') are parallel dimensions used in the filter and result;
- 'filter_loop' ('fl') are reduction dimensions representing the dimensions of the sliding window;
- 'input_channel' ('ic') are reduction dimensions present only in the input and filter.
- 'depth' ('ic') are parallel dimensions present in the input, filter, and output.
Additionally this will match stride and dilation information for the convolution:
- 'strides' are the static strides per convolution window dimension;
- 'dilations' are the static dilations per convolution window dimension.
Note that this doesn't check the operation in the body.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the operation has the convolution-like dimensions, produces a
silenceable failure otherwise.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle);
let results = (outs TransformParamTypeInterface:$batch,
TransformParamTypeInterface:$output_image,
TransformParamTypeInterface:$output_channel,
TransformParamTypeInterface:$filter_loop,
TransformParamTypeInterface:$input_channel,
TransformParamTypeInterface:$depth,
TransformParamTypeInterface:$strides,
TransformParamTypeInterface:$dilations);
let assemblyFormat =
"$operand_handle attr-dict `:` functional-type(operands, results)";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration;
}
class StructuredDimDescription<string kind> {
string description = !strconcat([{
The following }], kind ,[{ specifications are supported:
* `all`: all }], kind ,[{s are checked and captured;
* list of integers: the listed }], kind, [{s are checked and captured;
* `except(` list of integers `)`: all }], kind, [{s except the
specified ones are checked and captured.
Negative indexes are interpreted by counting values from the last one
(similarly to Python). For example, `-1` means the last }], kind, [{ and
`except(-1)` means all }], kind, [{s but the last. Indexes must be unique,
including after interpretation of negative ones.
Produces a silenceable failure in case of index overflow, including backward
counting.
}]);
}
def MatchStructuredDimOp : Op<Transform_Dialect, "match.structured.dim", [
StructuredPredicate,
SingleOpMatcher,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary =
"Checks if the dimensions of the structured op satisfy some criteria";
let description = !strconcat([{
Checks if the dimensions (loop ranges) of the structured payload op satisfy
the criteria specified as attributes. May capture the numeric value of the
dimension into a parameter that it returns.
}],
StructuredDimDescription<"dimension">.description,
[{
The following mutually exclusive conditions are available as unit
attributes:
* `parallel`: the dimension corresponds to a parallel loop;
* `reduction`: the dimension corresponds to a reduction loop.
If the result type is specified, associates the parameter with the (static)
values of dimensions in the same order as listed and preserving the natural
order for `all` and `except`. Specifically, if `-1, -2` are specified, the
parameter will be associated with the value of the second-to-last dimension
followed by the last dimension. If the dimension is dynamic, the parameter
will contain a negative value corresponding to kDynamic in C++.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the specified dimensions satisfy the specified criteria,
produces a silenceable failure otherwise. Produces a definite failure if
the operand is not associated with a single payload op.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle,
DenseI64ArrayAttr:$raw_dim_list,
UnitAttr:$is_inverted,
UnitAttr:$is_all,
UnitAttr:$parallel,
UnitAttr:$reduction);
let results = (outs Optional<TransformParamTypeInterface>:$result);
let assemblyFormat =
"$operand_handle `[`"
"custom<TransformMatchDims>($raw_dim_list, $is_inverted, $is_all)"
"`]` attr-dict `:` "
"custom<SemiFunctionType>(type($operand_handle), type($result))";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration # [{
::mlir::DiagnosedSilenceableFailure getDimensionsFor(
::mlir::linalg::LinalgOp op,
::llvm::SmallVectorImpl<int64_t> &dims);
}];
let hasVerifier = 1;
}
def MatchStructuredElementalBitwidthOp
: Op<Transform_Dialect, "match.structured.elemental_bitwidth", [
SingleValueMatcher,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary =
"Captures the bitwidth of the value's elemental type as a parameter";
let description = !strconcat([{
Produces a transform dialect parameter associated with the bitwidth of the
elemental type of the payload value passed as the operand.}],
StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the operand is associated with exactly one payload value of
`ShapedType`. Produces a silenceable failure otherwise.
}]);
let arguments = (ins TransformValueHandleTypeInterface:$operand_handle);
let results = (outs TransformParamTypeInterface:$result);
let assemblyFormat =
"$operand_handle attr-dict `:` functional-type(operands, results)";
let extraClassDeclaration = SingleValueMatcher.extraDeclaration;
}
class MatchStructuredOperandOp<string opname> : Op<Transform_Dialect, opname, [
StructuredPredicate,
SingleOpMatcher,
MatchOpInterface,
MemoryEffectsOpInterface]> {
// TODO: consider an attribute controlling whether to fail or succeed on
// out-of-bounds accesses.
let arguments = (ins TransformHandleTypeInterface:$operand_handle,
DenseI64ArrayAttr:$raw_position_list,
UnitAttr:$is_inverted,
UnitAttr:$is_all,
UnitAttr:$permutation,
UnitAttr:$projected_permutation);
// TODO: allow this to bind multiple inputs simultaneously after checking that
// `transform.foreach` works well in matches.
let results =
(outs Optional<AnyTypeOf<[TransformAnyHandle,Transform_AffineMapParamType]>>:$result);
let assemblyFormat =
"$operand_handle `[`"
"custom<TransformMatchDims>($raw_position_list, $is_inverted, $is_all)"
"`]` attr-dict "
"`:` custom<SemiFunctionType>(type($operand_handle), type($result))";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration # [{
::mlir::DiagnosedSilenceableFailure getPositionsFor(
::mlir::linalg::LinalgOp op,
::llvm::SmallVectorImpl<int64_t> &positions);
}];
let hasVerifier = 1;
}
def MatchStructuredInputOp : MatchStructuredOperandOp<"match.structured.input"> {
let summary =
"Captures input operand(s) of a structured operation";
let description = !strconcat([{
Produces a transform dialect value depending on the result type:
- If the result type is a value handle, it will be associated with the input
operand(s) of the payload operation associated with the operand handle.
- If the result type is an operation handle, it will be associated with the
operation defining the input operand(s) of the payload operation associated
with the operand handle.
- If the result type is an affine map parameter type, it will be associated
with the indexing map that corresponds to the input operand(s) of the
payload operation associated with the operand handle.
For example, given the following operation:
```mlir
%arg1 = some.op
linalg.matmul ins(%arg1, %arg2 : ...) outs(%arg3 : ...)
```
in case of a successful match for operand 0 this operation will return, for
each of the respective cases above:
- A handle to `%arg1` if the result is a value handle.
- A handle to `some.op` if the result is an operation handle.
- A parameter containing the LHS map of the matrix multiplication, i.e.
`affine_map<(d0, d1, d2) -> (d0, d2)>` if the result is an affine
map parameter.
The match succeeds if the conditions specified as attributes succeed.
}],
StructuredDimDescription<"input">.description,
[{
}],
StructuredPredicate.extraDescription,
[{
#### Return modes
Succeeds if all input indexes are in bounds, produces a silenceable failure
otherwise. Additionally, when the result is an operation handle, produces a
silenceable failure if the input specification defines more than one input
or if the operand is not an operation result.
}]);
}
def MatchStructuredInitOp : MatchStructuredOperandOp<"match.structured.init"> {
let summary =
"Captures init operand(s) of a structured operation";
let description = !strconcat([{
Produces a transform dialect value depending on the result type:
- If the result type is a value handle, it will be associated with the init
operand(s) of the payload operation associated with the operand handle.
- If the result type is an operation handle, it will be associated with the
operation defining the init operand(s) of the payload operation associated
with the operand handle.
- If the result type is an affine map parameter type, it will be associated
with the indexing map that corresponds to the init operand(s) of the
payload operation associated with the operand handle.
For example, given the following operation:
```mlir
%arg3 = linalg.fill
linalg.matmul ins(%arg1, %arg2 : ...) outs(%arg3 : ...)
```
in case of a successful match for init operand 0 this operation will return,
for each of the respective cases above:
- A handle to `%arg3` if the result is a value handle.
- A handle to `linalg.fill` if the result is an operation handle.
- A parameter containing the result map of the matrix multiplication, i.e.
`affine_map<(d0, d1, d2) -> (d0, d1)>` if the result is an affine
map parameter.
The match succeeds if the conditions specified as attributes succeed.
}],
StructuredDimDescription<"init">.description,
[{
}],
StructuredPredicate.extraDescription,
[{
#### Return modes
Succeeds if all init(outs) indexes are in bounds, produces a silenceable
failure otherwise. Additionally, when the result is an operation handle,
produces a silenceable failure if the init(outs) specification defines
more than one init(outs) or if the operand is not an operation result.
}]);
}
def MatchStructuredNumInputsOp
: Op<Transform_Dialect, "match.structured.num_inputs", [
StructuredPredicate,
SingleOpMatcher,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary = "Captures the number of input operands of a structured "
"operation as parameter";
let description = !strconcat([{
Produces a transform dialect parameter value associated with an integer
attribute containing the number of input operands of the payload operation
associated with the operand handle.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the operand is associated with exactly one structured payload
operation. Produces a silenceable failure otherwise.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle);
let results = (outs TransformParamTypeInterface:$result);
let assemblyFormat =
"$operand_handle attr-dict `:` functional-type(operands, results)";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration;
}
def MatchStructuredNumInitsOp
: Op<Transform_Dialect, "match.structured.num_inits", [
StructuredPredicate,
SingleOpMatcher,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary = "Captures the number of init(outs) operands of a structured"
"operation as parameter";
let description = !strconcat([{
Produces a transform dialect parameter value associated with an integer
attribute containing the number of init(outs) operands of the payload
operation associated with the operand handle.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the operand is associated with exactly one structured payload
operation. Produces a silenceable failure otherwise.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle);
let results = (outs TransformParamTypeInterface:$result);
let assemblyFormat =
"$operand_handle attr-dict `:` functional-type(operands, results)";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration;
}
def MatchStructuredRankOp : Op<Transform_Dialect, "match.structured.rank", [
StructuredPredicate,
SingleOpMatcher,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary = "Captures the rank of a structured operation as parameter";
let description = !strconcat([{
Produces a transform dialect parameter value associated with an integer
attribute containing the rank of the structured payload operation associated
with the operand handle.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the operand is associated with exactly one structured payload
operation. Produces a silenceable failure otherwise.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle);
let results = (outs TransformParamTypeInterface:$rank);
let assemblyFormat =
"$operand_handle attr-dict `:`"
"custom<SemiFunctionType>(type($operand_handle), type($rank))";
let extraClassDeclaration = SingleOpMatcher.extraDeclaration;
}
def MatchStructuredResultOp : Op<Transform_Dialect, "match.structured.result", [
StructuredPredicate,
SingleOpMatcher,
MatchOpInterface,
MemoryEffectsOpInterface]> {
let summary = "Captures the result of a structured payload operation in an "
"op or value handle";
let description = !strconcat([{
Produces a transform dialect value handle associated with the payload value
defined as a result of the payload operation associated with the operand
handle, or an operation handle to an operation using the produced result
with additional constraints specified by the attributes as follows.
* If `any` is specified, binds the resulting handle to any operation using
the result and succeeds.
* If `single` is specified, binds the resulting handle to the only
operation using the result or fails if there is more than one (or no)
such operation.
The number of the result is specified as `position` attribute. It may take
positive and negative values. Negative values are interpreted as counting
results from backwards, e.g., `-1` means the last result and `-2` means the
second-to-last result. In any case, the position must be in bounds for the
given payload operation. A silenceable failure is produced for out-of-bounds
positions.
}], StructuredPredicate.extraDescription, [{
#### Return modes
Succeeds if the position is in bounds and if the user operation could be
found when requested. Produces a silenceable failure otherwise.
}]);
let arguments = (ins TransformHandleTypeInterface:$operand_handle,
I64Attr:$position,
UnitAttr:$any,
UnitAttr:$single);
let results = (outs TransformAnyHandle:$result);
let assemblyFormat =
"$operand_handle `[` $position `]` (`any` $any^)? (`single` $single^)?"
"attr-dict `:` functional-type(operands, results)";
let hasVerifier = 1;
let extraClassDeclaration = SingleOpMatcher.extraDeclaration # [{
::mlir::DiagnosedSilenceableFailure
getPositionFor(::mlir::linalg::LinalgOp op, int64_t &position);
}];
}
def MatchStructuredYieldOp : Op<Transform_Dialect, "match.structured.yield", [
DeclareOpInterfaceMethods<MemoryEffectsOpInterface>,
Terminator]> {
let summary = "Terminator for transform.match.structured blocks";
let description = [{
Forwards the payload association from the operands to the results of the
parent op. Always succeeds.
}];
let builders = [
OpBuilder<(ins)>
];
let arguments = (ins Variadic<Transform_AnyHandleOrParamType>:$handles);
let assemblyFormat = "$handles attr-dict (`:` type($handles)^)?";
}
#endif // LINALG_MATCH_OPS