Changeset View
Changeset View
Standalone View
Standalone View
mlir/include/mlir/Dialect/PDL/IR/PDLOps.td
Show All 23 Lines | |||||
class PDL_Op<string mnemonic, list<OpTrait> traits = []> | class PDL_Op<string mnemonic, list<OpTrait> traits = []> | ||||
: Op<PDL_Dialect, mnemonic, traits> { | : Op<PDL_Dialect, mnemonic, traits> { | ||||
let printer = [{ ::print(p, *this); }]; | let printer = [{ ::print(p, *this); }]; | ||||
let parser = [{ return ::parse$cppClass(parser, result); }]; | let parser = [{ return ::parse$cppClass(parser, result); }]; | ||||
let verifier = [{ return ::verify(*this); }]; | let verifier = [{ return ::verify(*this); }]; | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// pdl::ApplyConstraintOp | // pdl::ApplyNativeConstraintOp | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
def PDL_ApplyConstraintOp | def PDL_ApplyNativeConstraintOp | ||||
: PDL_Op<"apply_constraint", [HasParent<"pdl::PatternOp">]> { | : PDL_Op<"apply_native_constraint", [HasParent<"pdl::PatternOp">]> { | ||||
let summary = "Apply a generic constraint to a set of provided entities"; | let summary = "Apply a native constraint to a set of provided entities"; | ||||
let description = [{ | let description = [{ | ||||
`apply_constraint` operations apply a generic constraint, that has been | `pdl.apply_native_constraint` operations apply a native C++ constraint, that | ||||
registered externally with the consumer of PDL, to a given set of entities. | has been registered externally with the consumer of PDL, to a given set of | ||||
The constraint is permitted to accept any number of constant valued | entities. The constraint is permitted to accept any number of constant | ||||
parameters. | valued parameters. | ||||
Example: | Example: | ||||
```mlir | ```mlir | ||||
// Apply `myConstraint` to the entities defined by `input`, `attr`, and | // Apply `myConstraint` to the entities defined by `input`, `attr`, and | ||||
// `op`. `42`, `"abc"`, and `i32` are constant parameters passed to the | // `op`. `42`, `"abc"`, and `i32` are constant parameters passed to the | ||||
// constraint. | // constraint. | ||||
pdl.apply_constraint "myConstraint"[42, "abc", i32](%input, %attr, %op : !pdl.value, !pdl.attribute, !pdl.operation) | pdl.apply_native_constraint "myConstraint"[42, "abc", i32](%input, %attr, %op : !pdl.value, !pdl.attribute, !pdl.operation) | ||||
``` | ``` | ||||
}]; | }]; | ||||
let arguments = (ins StrAttr:$name, | let arguments = (ins StrAttr:$name, | ||||
Variadic<PDL_AnyType>:$args, | Variadic<PDL_AnyType>:$args, | ||||
OptionalAttr<ArrayAttr>:$constParams); | OptionalAttr<ArrayAttr>:$constParams); | ||||
let assemblyFormat = [{ | let assemblyFormat = [{ | ||||
$name ($constParams^)? `(` $args `:` type($args) `)` attr-dict | $name ($constParams^)? `(` $args `:` type($args) `)` attr-dict | ||||
}]; | }]; | ||||
let builders = [ | let builders = [ | ||||
OpBuilder<(ins "StringRef":$name, CArg<"ValueRange", "{}">:$args, | OpBuilder<(ins "StringRef":$name, CArg<"ValueRange", "{}">:$args, | ||||
CArg<"ArrayRef<Attribute>", "{}">:$params), [{ | CArg<"ArrayRef<Attribute>", "{}">:$params), [{ | ||||
build($_builder, $_state, $_builder.getStringAttr(name), args, | build($_builder, $_state, $_builder.getStringAttr(name), args, | ||||
params.empty() ? ArrayAttr() : $_builder.getArrayAttr(params)); | params.empty() ? ArrayAttr() : $_builder.getArrayAttr(params)); | ||||
}]>, | }]>, | ||||
]; | ]; | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// pdl::ApplyNativeRewriteOp | |||||
//===----------------------------------------------------------------------===// | |||||
def PDL_ApplyNativeRewriteOp | |||||
: PDL_Op<"apply_native_rewrite", [HasParent<"pdl::RewriteOp">]> { | |||||
let summary = "Apply a native rewrite method inside of pdl.rewrite region"; | |||||
let description = [{ | |||||
`pdl.apply_native_rewrite` operations apply a native C++ function, that has | |||||
been registered externally with the consumer of PDL, to perform a rewrite | |||||
and optionally return a number of values. The native function may accept any | |||||
number of arguments and constant attribute parameters. This operation is | |||||
used within a pdl.rewrite region to enable the interleaving of native | |||||
rewrite methods with other pdl constructs. | |||||
Example: | |||||
```mlir | |||||
// Apply a native rewrite method that returns an attribute. | |||||
%ret = pdl.apply_native_rewrite "myNativeFunc"[42, "gt"](%arg0, %arg1) : !pdl.attribute | |||||
jpienaar: Could you show the C++ side here too? | |||||
``` | |||||
```c++ | |||||
// The native rewrite as defined in C++: | |||||
static void myNativeFunc(ArrayRef<PDLValue> args, ArrayAttr constantParams, | |||||
PatternRewriter &rewriter, | |||||
PDLResultList &results) { | |||||
Value arg0 = args[0].cast<Value>(); | |||||
Value arg1 = args[1].cast<Value>(); | |||||
IntegerAttr param0 = constantParams[0].cast<IntegerAttr>(); | |||||
StringAttr param1 = constantParams[1].cast<StringAttr>(); | |||||
// Just push back the first param attribute. | |||||
results.push_back(param0); | |||||
} | |||||
void registerNativeRewrite(PDLPatternModule &pdlModule) { | |||||
pdlModule.registerRewriteFunction("myNativeFunc", myNativeFunc); | |||||
} | |||||
``` | |||||
}]; | |||||
let arguments = (ins StrAttr:$name, | |||||
Variadic<PDL_AnyType>:$args, | |||||
OptionalAttr<ArrayAttr>:$constParams); | |||||
let results = (outs Variadic<PDL_AnyType>:$results); | |||||
let assemblyFormat = [{ | |||||
$name ($constParams^)? (`(` $args^ `:` type($args) `)`)? `:` type($results) | |||||
attr-dict | |||||
}]; | |||||
} | |||||
//===----------------------------------------------------------------------===// | |||||
// pdl::AttributeOp | // pdl::AttributeOp | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
def PDL_AttributeOp : PDL_Op<"attribute"> { | def PDL_AttributeOp : PDL_Op<"attribute"> { | ||||
let summary = "Define an input attribute in a pattern"; | let summary = "Define an input attribute in a pattern"; | ||||
let description = [{ | let description = [{ | ||||
`pdl.attribute` operations capture named attribute edges into an operation. | `pdl.attribute` operations capture named attribute edges into an operation. | ||||
Instances of this operation define, and partially constrain, attributes of a | Instances of this operation define, and partially constrain, attributes of a | ||||
Show All 30 Lines | let builders = [ | ||||
}]>, | }]>, | ||||
OpBuilder<(ins "Attribute":$attr), [{ | OpBuilder<(ins "Attribute":$attr), [{ | ||||
build($_builder, $_state, $_builder.getType<AttributeType>(), Value(), attr); | build($_builder, $_state, $_builder.getType<AttributeType>(), Value(), attr); | ||||
}]>, | }]>, | ||||
]; | ]; | ||||
} | } | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
// pdl::CreateNativeOp | |||||
//===----------------------------------------------------------------------===// | |||||
def PDL_CreateNativeOp | |||||
: PDL_Op<"create_native", [HasParent<"pdl::RewriteOp">]> { | |||||
let summary = "Call a native creation method to construct an `Attribute`, " | |||||
"`Operation`, `Type`, or `Value`"; | |||||
let description = [{ | |||||
`pdl.create_native` operations invoke a native C++ function, that has been | |||||
registered externally with the consumer of PDL, to create an `Attribute`, | |||||
`Operation`, `Type`, or `Value`. The native function must produce a value | |||||
of the specified return type, and may accept any number of positional | |||||
arguments and constant attribute parameters. | |||||
Example: | |||||
```mlir | |||||
%ret = pdl.create_native "myNativeFunc"[42, "gt"](%arg0, %arg1) : !pdl.attribute | |||||
``` | |||||
}]; | |||||
let arguments = (ins StrAttr:$name, | |||||
Variadic<PDL_AnyType>:$args, | |||||
OptionalAttr<ArrayAttr>:$constParams); | |||||
let results = (outs PDL_AnyType:$result); | |||||
let assemblyFormat = [{ | |||||
$name ($constParams^)? (`(` $args^ `:` type($args) `)`)? `:` type($result) | |||||
attr-dict | |||||
}]; | |||||
let verifier = ?; | |||||
} | |||||
//===----------------------------------------------------------------------===// | |||||
// pdl::EraseOp | // pdl::EraseOp | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
def PDL_EraseOp : PDL_Op<"erase", [HasParent<"pdl::RewriteOp">]> { | def PDL_EraseOp : PDL_Op<"erase", [HasParent<"pdl::RewriteOp">]> { | ||||
let summary = "Mark an input operation as `erased`"; | let summary = "Mark an input operation as `erased`"; | ||||
let description = [{ | let description = [{ | ||||
`pdl.erase` operations are used within `pdl.rewrite` regions to specify that | `pdl.erase` operations are used within `pdl.rewrite` regions to specify that | ||||
an input operation should be marked as erased. The semantics of this | an input operation should be marked as erased. The semantics of this | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | let description = [{ | ||||
When used within a matching context, the name of the operation may be | When used within a matching context, the name of the operation may be | ||||
omitted. | omitted. | ||||
When used within a rewriting context, i.e. when defined within a | When used within a rewriting context, i.e. when defined within a | ||||
`pdl.rewrite`, all of the result types must be "inferable". This means that | `pdl.rewrite`, all of the result types must be "inferable". This means that | ||||
the type must be attributable to either a constant type value or the result | the type must be attributable to either a constant type value or the result | ||||
type of another entity, such as an attribute, the result of a | type of another entity, such as an attribute, the result of a | ||||
`createNative`, or the result type of another operation. If the result type | `apply_native_rewrite`, or the result type of another operation. If the | ||||
value does not meet any of these criteria, the operation must provide the | result type value does not meet any of these criteria, the operation must | ||||
`InferTypeOpInterface` to ensure that the result types can be inferred. | override the `InferTypeOpInterface` to ensure that the result types can be | ||||
inferred. | |||||
Example: | Example: | ||||
```mlir | ```mlir | ||||
// Define an instance of a `foo.op` operation. | // Define an instance of a `foo.op` operation. | ||||
%op = pdl.operation "foo.op"(%arg0, %arg1) {"attrA" = %attr0} -> %type, %type, %type, %type | %op = pdl.operation "foo.op"(%arg0, %arg1) {"attrA" = %attr0} -> %type, %type, %type, %type | ||||
``` | ``` | ||||
}]; | }]; | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | |||||
def PDL_RewriteOp : PDL_Op<"rewrite", [ | def PDL_RewriteOp : PDL_Op<"rewrite", [ | ||||
Terminator, HasParent<"pdl::PatternOp">, | Terminator, HasParent<"pdl::PatternOp">, | ||||
SingleBlockImplicitTerminator<"pdl::RewriteEndOp"> | SingleBlockImplicitTerminator<"pdl::RewriteEndOp"> | ||||
]> { | ]> { | ||||
let summary = "Specify the rewrite of a matched pattern"; | let summary = "Specify the rewrite of a matched pattern"; | ||||
let description = [{ | let description = [{ | ||||
`pdl.rewrite` operations terminate the region of a `pdl.pattern` and specify | `pdl.rewrite` operations terminate the region of a `pdl.pattern` and specify | ||||
the rewrite of a `pdl.pattern`, on the specified root operation. The | the main rewrite of a `pdl.pattern`, on the specified root operation. The | ||||
rewrite is specified either via a string name (`name`) to an external | rewrite is specified either via a string name (`name`) to an external | ||||
rewrite function, or via the region body. The rewrite region, if specified, | rewrite function, or via the region body. The rewrite region, if specified, | ||||
must contain a single block and terminate via the `pdl.rewrite_end` | must contain a single block and terminate via the `pdl.rewrite_end` | ||||
operation. If the rewrite is external, it also takes a set of constant | operation. If the rewrite is external, it also takes a set of constant | ||||
parameters and a set of additional positional values defined within the | parameters and a set of additional positional values defined within the | ||||
matcher as arguments. | matcher as arguments. If the rewrite is external, the root operation is | ||||
passed to the native function as the first argument. | |||||
Example: | Example: | ||||
```mlir | ```mlir | ||||
// Specify an external rewrite function: | // Specify an external rewrite function: | ||||
pdl.rewrite %root with "myExternalRewriter"(%value : !pdl.value) | pdl.rewrite %root with "myExternalRewriter"(%value : !pdl.value) | ||||
// Specify the rewrite inline using PDL: | // Specify the rewrite inline using PDL: | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |
Could you show the C++ side here too?