Skip to content

[mlir][irdl] Introduce names in IRDL value lists #123525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 44 additions & 34 deletions mlir/include/mlir/Dialect/IRDL/IR/IRDLOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ include "IRDLInterfaces.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/IR/SymbolInterfaces.td"
include "mlir/IR/BuiltinAttributes.td"

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

Example:

Expand All @@ -143,17 +144,19 @@ def IRDL_ParametersOp : IRDL_Op<"parameters",
%0 = irdl.is i32
%1 = irdl.is i64
%2 = irdl.any_of(%0, %1)
irdl.parameters(%2)
irdl.parameters(elem: %2)
}
}
```

The above program defines a type `complex` inside the dialect `cmath`. The
type has a single parameter that should be either `i32` or `i64`.
type has a single parameter `elem` that should be either `i32` or `i64`.
}];

let arguments = (ins Variadic<IRDL_AttributeType>:$args);
let assemblyFormat = " `(` $args `)` attr-dict ";
let arguments = (ins Variadic<IRDL_AttributeType>:$args,
StrArrayAttr:$names);
let assemblyFormat = " `` custom<NamedValueList>($args, $names) attr-dict ";
let hasVerifier = true;
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -198,16 +201,17 @@ def IRDL_OperationOp : IRDL_Op<"operation",
let regions = (region SizedRegion<1>:$body);
let assemblyFormat =
"$sym_name attr-dict-with-keyword custom<SingleBlockRegion>($body)";
let hasRegionVerifier = true;
}

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

In the following example, `irdl.operands` defines the operands of the
`norm` operation:
`mul` operation:

```mlir
irdl.dialect @cmath {
Expand All @@ -217,8 +221,8 @@ def IRDL_OperandsOp : IRDL_Op<"operands", [HasParent<"OperationOp">]> {
irdl.operation @mul {
%0 = irdl.any
%1 = irdl.parametric @cmath::@complex<%0>
irdl.results(%1)
irdl.operands(%1, %1)
irdl.results(res: %1)
irdl.operands(lhs: %1, rhs: %1)
}
}
```
Expand All @@ -228,43 +232,45 @@ def IRDL_OperandsOp : IRDL_Op<"operands", [HasParent<"OperationOp">]> {

The operands can also be marked as variadic or optional:
```mlir
irdl.operands(%0, single %1, optional %2, variadic %3)
irdl.operands(foo: %0, bar: single %1, baz: optional %2, qux: variadic %3)
```

Here, %0 and %1 are required single operands, %2 is an optional operand,
and %3 is a variadic operand.
Here, foo and bar are required single operands, baz is an optional operand,
and qux is a variadic operand.

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

let arguments = (ins Variadic<IRDL_AttributeType>:$args,
VariadicityArrayAttr:$variadicity);
StrArrayAttr:$names,
VariadicityArrayAttr:$variadicity);
let assemblyFormat =
"`` custom<ValuesWithVariadicity>($args, $variadicity) attr-dict";
" `` custom<NamedValueListWithVariadicity>($args, $names, $variadicity) attr-dict";
let hasVerifier = true;
}

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

In the following example, `irdl.results` defines the results of the
`norm` operation:
`get_values` operation:

```mlir
irdl.dialect @cmath {

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

/// Returns the real and imaginary parts of a complex number.
irdl.operation @get_values {
%0 = irdl.any
%1 = irdl.parametric @cmath::@complex<%0>
irdl.results(%0, %0)
irdl.operands(%1)
irdl.results(re: %0, im: %0)
irdl.operands(complex: %1)
}
}
```
Expand All @@ -274,21 +280,22 @@ def IRDL_ResultsOp : IRDL_Op<"results", [HasParent<"OperationOp">]> {

The results can also be marked as variadic or optional:
```mlir
irdl.results(%0, single %1, optional %2, variadic %3)
irdl.results(foo: %0, bar: single %1, baz: optional %2, qux: variadic %3)
```

Here, %0 and %1 are required single results, %2 is an optional result,
and %3 is a variadic result.
Here, foo and bar are required single results, baz is an optional result,
and qux is a variadic result.

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

let arguments = (ins Variadic<IRDL_AttributeType>:$args,
StrArrayAttr:$names,
VariadicityArrayAttr:$variadicity);
let assemblyFormat =
" `` custom<ValuesWithVariadicity>($args, $variadicity) attr-dict";
" `` custom<NamedValueListWithVariadicity>($args, $names, $variadicity) attr-dict";
let hasVerifier = true;
}

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

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

irdl.regions(%r0, %r1, %r2, %r3)
irdl.regions(foo: %r0, bar: %r1, baz: %r2, qux: %r3)
}
}
```

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

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

Example:

Expand All @@ -401,18 +410,19 @@ def IRDL_RegionsOp : IRDL_Op<"regions", [HasParent<"OperationOp">]> {
%r1 = irdl.region with size 3
%0 = irdl.any
%r2 = irdl.region(%0)
irdl.regions(%r1, %r2)
irdl.regions(foo: %r1, bar: %r2)
}
}
```

In the snippet above the operation is constrained to have two regions.
The first region should contain three blocks.
The second region should have one region with one argument.
The first region (`foo`) should contain three blocks.
The second region (`bar`) should have one region with one argument.
}];

let arguments = (ins Variadic<IRDL_RegionType>:$args);
let assemblyFormat = " `(` $args `)` attr-dict ";
let arguments = (ins Variadic<IRDL_RegionType>:$args, StrArrayAttr:$names);
let assemblyFormat = " `` custom<NamedValueList>($args, $names) attr-dict ";
let hasVerifier = true;
}

//===----------------------------------------------------------------------===//
Expand Down
Loading
Loading