1//===- PDLExtensionOps.td - Transform dialect operations ---*- tablegen -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8 9#ifndef MLIR_DIALECT_TRANSFORM_PDLEXTENSION_PDLEXTENSIONOPS 10#define MLIR_DIALECT_TRANSFORM_PDLEXTENSION_PDLEXTENSIONOPS 11 12include "mlir/Dialect/Transform/IR/TransformDialect.td" 13include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.td" 14include "mlir/Interfaces/SideEffectInterfaces.td" 15include "mlir/IR/OpAsmInterface.td" 16include "mlir/IR/SymbolInterfaces.td" 17 18def PDLMatchOp : TransformDialectOp<"pdl_match", 19 [DeclareOpInterfaceMethods<TransformOpInterface>, 20 DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> { 21 let summary = "Finds ops that match the named PDL pattern"; 22 let description = [{ 23 Find Payload IR ops nested within the Payload IR op associated with the 24 operand that match the PDL pattern identified by its name. The pattern is 25 expected to be defined in the closest surrounding `WithPDLPatternsOp`. 26 27 Produces a Transform IR value associated with the list of Payload IR ops 28 that matched the pattern. The order of results in the list is that of the 29 Operation::walk, clients are advised not to rely on a specific order though. 30 If the operand is associated with multiple Payload IR ops, finds matching 31 ops nested within each of those and produces a single list containing all 32 of the matched ops. 33 34 The transformation is considered successful regardless of whether some 35 Payload IR ops actually matched the pattern and only fails if the pattern 36 could not be looked up or compiled. 37 }]; 38 39 let arguments = (ins 40 Arg<TransformHandleTypeInterface, "Payload IR scope to match within">:$root, 41 SymbolRefAttr:$pattern_name); 42 let results = (outs 43 Res<TransformHandleTypeInterface, "Handle to the matched Payload IR ops">:$matched); 44 45 let assemblyFormat = "$pattern_name `in` $root attr-dict `:` " 46 "functional-type(operands, results)"; 47} 48 49def WithPDLPatternsOp : TransformDialectOp<"with_pdl_patterns", 50 [DeclareOpInterfaceMethods<TransformOpInterface>, NoTerminator, 51 OpAsmOpInterface, PossibleTopLevelTransformOpTrait, 52 DeclareOpInterfaceMethods<MemoryEffectsOpInterface>, 53 SymbolTable]> { 54 let summary = "Contains PDL patterns available for use in transforms"; 55 let description = [{ 56 This op contains a set of named PDL patterns that are available for the 57 Transform dialect operations to be used for pattern matching. For example, 58 PDLMatchOp can be used to produce a Transform IR value associated with all 59 Payload IR operations that match the pattern as follows: 60 61 ```mlir 62 transform.with_pdl_patterns { 63 ^bb0(%arg0: !transform.any_op): 64 pdl.pattern @my_pattern : benefit(1) { 65 %0 = pdl.operation //... 66 // Regular PDL goes here. 67 pdl.rewrite %0 with "transform.dialect" 68 } 69 70 sequence %arg0 failures(propagate) { 71 ^bb0(%arg1: !transform.any_op): 72 %1 = pdl_match @my_pattern in %arg1 73 // Use %1 as handle 74 } 75 } 76 ``` 77 78 Note that the pattern is expected to finish with a `pdl.rewrite` terminator 79 that points to the custom rewriter named "transform.dialect". The rewriter 80 actually does nothing, but the transform application will keep track of the 81 operations that matched the pattern. 82 83 This op is expected to contain `pdl.pattern` operations and exactly one 84 another Transform dialect operation that gets executed with all patterns 85 available. This op is a possible top-level Transform IR op, the argument of 86 its entry block corresponds to either the root op of the payload IR or the 87 ops associated with its operand when provided. 88 }]; 89 90 let arguments = (ins 91 Arg<Optional<TransformHandleTypeInterface>, "Root operation of the Payload IR" 92 >:$root); 93 let regions = (region SizedRegion<1>:$body); 94 let assemblyFormat = "($root^ `:` type($root))? attr-dict-with-keyword regions"; 95 96 let hasVerifier = 1; 97 98 let extraClassDeclaration = [{ 99 /// Allow the dialect prefix to be omitted. 100 static StringRef getDefaultDialect() { return "transform"; } 101 }]; 102} 103 104#endif // MLIR_DIALECT_TRANSFORM_PDLEXTENSION_PDLEXTENSIONOPS 105