Skip to content

Commit 69d3ba3

Browse files
authored
[mlir][irdl] Introduce names in IRDL value lists (llvm#123525)
In order to meaningfully generate getters and setters from IRDL, it makes sense to embed the names of operands, results, etc. in the IR definition. This PR introduces this feature. Names are constrained similarly to TableGen names.
1 parent 04034f0 commit 69d3ba3

File tree

16 files changed

+528
-164
lines changed

16 files changed

+528
-164
lines changed

mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ include "IRDLInterfaces.td"
2020
include "mlir/Interfaces/SideEffectInterfaces.td"
2121
include "mlir/Interfaces/InferTypeOpInterface.td"
2222
include "mlir/IR/SymbolInterfaces.td"
23+
include "mlir/IR/BuiltinAttributes.td"
2324

2425
class IRDL_Op<string mnemonic, list<Trait> traits = []>
2526
: Op<IRDL_Dialect, mnemonic, traits>;
@@ -133,7 +134,7 @@ def IRDL_ParametersOp : IRDL_Op<"parameters",
133134
"Define the constraints on parameters of a type/attribute definition";
134135
let description = [{
135136
`irdl.parameters` defines the constraints on parameters of a type or
136-
attribute definition.
137+
attribute definition. Each parameter is named after an identifier.
137138

138139
Example:
139140

@@ -143,17 +144,19 @@ def IRDL_ParametersOp : IRDL_Op<"parameters",
143144
%0 = irdl.is i32
144145
%1 = irdl.is i64
145146
%2 = irdl.any_of(%0, %1)
146-
irdl.parameters(%2)
147+
irdl.parameters(elem: %2)
147148
}
148149
}
149150
```
150151

151152
The above program defines a type `complex` inside the dialect `cmath`. The
152-
type has a single parameter that should be either `i32` or `i64`.
153+
type has a single parameter `elem` that should be either `i32` or `i64`.
153154
}];
154155

155-
let arguments = (ins Variadic<IRDL_AttributeType>:$args);
156-
let assemblyFormat = " `(` $args `)` attr-dict ";
156+
let arguments = (ins Variadic<IRDL_AttributeType>:$args,
157+
StrArrayAttr:$names);
158+
let assemblyFormat = " `` custom<NamedValueList>($args, $names) attr-dict ";
159+
let hasVerifier = true;
157160
}
158161

159162
//===----------------------------------------------------------------------===//
@@ -198,16 +201,17 @@ def IRDL_OperationOp : IRDL_Op<"operation",
198201
let regions = (region SizedRegion<1>:$body);
199202
let assemblyFormat =
200203
"$sym_name attr-dict-with-keyword custom<SingleBlockRegion>($body)";
204+
let hasRegionVerifier = true;
201205
}
202206

203207
def IRDL_OperandsOp : IRDL_Op<"operands", [HasParent<"OperationOp">]> {
204208
let summary = "Define the operands of an operation";
205209
let description = [{
206210
`irdl.operands` define the operands of the `irdl.operation` parent operation
207-
definition.
211+
definition. Each operand is named after an identifier.
208212

209213
In the following example, `irdl.operands` defines the operands of the
210-
`norm` operation:
214+
`mul` operation:
211215

212216
```mlir
213217
irdl.dialect @cmath {
@@ -217,8 +221,8 @@ def IRDL_OperandsOp : IRDL_Op<"operands", [HasParent<"OperationOp">]> {
217221
irdl.operation @mul {
218222
%0 = irdl.any
219223
%1 = irdl.parametric @cmath::@complex<%0>
220-
irdl.results(%1)
221-
irdl.operands(%1, %1)
224+
irdl.results(res: %1)
225+
irdl.operands(lhs: %1, rhs: %1)
222226
}
223227
}
224228
```
@@ -228,43 +232,45 @@ def IRDL_OperandsOp : IRDL_Op<"operands", [HasParent<"OperationOp">]> {
228232

229233
The operands can also be marked as variadic or optional:
230234
```mlir
231-
irdl.operands(%0, single %1, optional %2, variadic %3)
235+
irdl.operands(foo: %0, bar: single %1, baz: optional %2, qux: variadic %3)
232236
```
233237

234-
Here, %0 and %1 are required single operands, %2 is an optional operand,
235-
and %3 is a variadic operand.
238+
Here, foo and bar are required single operands, baz is an optional operand,
239+
and qux is a variadic operand.
236240

237241
When more than one operand is marked as optional or variadic, the operation
238242
will expect a 'operandSegmentSizes' attribute that defines the number of
239243
operands in each segment.
240244
}];
241245

242246
let arguments = (ins Variadic<IRDL_AttributeType>:$args,
243-
VariadicityArrayAttr:$variadicity);
247+
StrArrayAttr:$names,
248+
VariadicityArrayAttr:$variadicity);
244249
let assemblyFormat =
245-
"`` custom<ValuesWithVariadicity>($args, $variadicity) attr-dict";
250+
" `` custom<NamedValueListWithVariadicity>($args, $names, $variadicity) attr-dict";
246251
let hasVerifier = true;
247252
}
248253

249254
def IRDL_ResultsOp : IRDL_Op<"results", [HasParent<"OperationOp">]> {
250255
let summary = "Define the results of an operation";
251256
let description = [{
252257
`irdl.results` define the results of the `irdl.operation` parent operation
253-
definition.
258+
definition. Each result is named after an identifier.
254259

255260
In the following example, `irdl.results` defines the results of the
256-
`norm` operation:
261+
`get_values` operation:
257262

258263
```mlir
259264
irdl.dialect @cmath {
260265

261266
irdl.type @complex { /* ... */ }
262267

268+
/// Returns the real and imaginary parts of a complex number.
263269
irdl.operation @get_values {
264270
%0 = irdl.any
265271
%1 = irdl.parametric @cmath::@complex<%0>
266-
irdl.results(%0, %0)
267-
irdl.operands(%1)
272+
irdl.results(re: %0, im: %0)
273+
irdl.operands(complex: %1)
268274
}
269275
}
270276
```
@@ -274,21 +280,22 @@ def IRDL_ResultsOp : IRDL_Op<"results", [HasParent<"OperationOp">]> {
274280

275281
The results can also be marked as variadic or optional:
276282
```mlir
277-
irdl.results(%0, single %1, optional %2, variadic %3)
283+
irdl.results(foo: %0, bar: single %1, baz: optional %2, qux: variadic %3)
278284
```
279285

280-
Here, %0 and %1 are required single results, %2 is an optional result,
281-
and %3 is a variadic result.
286+
Here, foo and bar are required single results, baz is an optional result,
287+
and qux is a variadic result.
282288

283289
When more than one result is marked as optional or variadic, the operation
284290
will expect a 'resultSegmentSizes' attribute that defines the number of
285291
results in each segment.
286292
}];
287293

288294
let arguments = (ins Variadic<IRDL_AttributeType>:$args,
295+
StrArrayAttr:$names,
289296
VariadicityArrayAttr:$variadicity);
290297
let assemblyFormat =
291-
" `` custom<ValuesWithVariadicity>($args, $variadicity) attr-dict";
298+
" `` custom<NamedValueListWithVariadicity>($args, $names, $variadicity) attr-dict";
292299
let hasVerifier = true;
293300
}
294301

@@ -335,7 +342,8 @@ def IRDL_RegionOp : IRDL_Op<"region",
335342
let summary = "Define a region of an operation";
336343
let description = [{
337344
The irdl.region construct defines a set of characteristics
338-
that a region of an operation should satify.
345+
that a region of an operation should satify. Each region is named after
346+
an identifier.
339347

340348
These characteristics include constraints for the entry block arguments
341349
of the region and the total number of blocks it contains.
@@ -360,19 +368,19 @@ def IRDL_RegionOp : IRDL_Op<"region",
360368
%r2 = irdl.region(%v0, %v1)
361369
%r3 = irdl.region with size 3
362370

363-
irdl.regions(%r0, %r1, %r2, %r3)
371+
irdl.regions(foo: %r0, bar: %r1, baz: %r2, qux: %r3)
364372
}
365373
}
366374
```
367375

368376
The above snippet demonstrates an operation named `@op_with_regions`,
369377
which is constrained to have four regions.
370378

371-
* Region `%r0` doesn't have any constraints on the arguments
379+
* Region `foo` doesn't have any constraints on the arguments
372380
or the number of blocks.
373-
* Region `%r1` should have an empty set of arguments.
374-
* Region `%r2` should have two arguments of types `i32` and `i64`.
375-
* Region `%r3` should contain exactly three blocks.
381+
* Region `bar` should have an empty set of arguments.
382+
* Region `baz` should have two arguments of types `i32` and `i64`.
383+
* Region `qux` should contain exactly three blocks.
376384
}];
377385
let arguments = (ins Variadic<IRDL_AttributeType>:$entryBlockArgs,
378386
OptionalAttr<I32Attr>:$numberOfBlocks,
@@ -391,7 +399,8 @@ def IRDL_RegionsOp : IRDL_Op<"regions", [HasParent<"OperationOp">]> {
391399
let summary = "Define the regions of an operation";
392400
let description = [{
393401
`irdl.regions` defines the regions of an operation by accepting
394-
values produced by `irdl.region` operation as arguments.
402+
values produced by `irdl.region` operation as arguments. Each
403+
region has an identifier as name.
395404

396405
Example:
397406

@@ -401,18 +410,19 @@ def IRDL_RegionsOp : IRDL_Op<"regions", [HasParent<"OperationOp">]> {
401410
%r1 = irdl.region with size 3
402411
%0 = irdl.any
403412
%r2 = irdl.region(%0)
404-
irdl.regions(%r1, %r2)
413+
irdl.regions(foo: %r1, bar: %r2)
405414
}
406415
}
407416
```
408417

409418
In the snippet above the operation is constrained to have two regions.
410-
The first region should contain three blocks.
411-
The second region should have one region with one argument.
419+
The first region (`foo`) should contain three blocks.
420+
The second region (`bar`) should have one region with one argument.
412421
}];
413422

414-
let arguments = (ins Variadic<IRDL_RegionType>:$args);
415-
let assemblyFormat = " `(` $args `)` attr-dict ";
423+
let arguments = (ins Variadic<IRDL_RegionType>:$args, StrArrayAttr:$names);
424+
let assemblyFormat = " `` custom<NamedValueList>($args, $names) attr-dict ";
425+
let hasVerifier = true;
416426
}
417427

418428
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)