|
| 1 | +#ifndef DIALECTINLINERINTERFACE |
| 2 | +#define DIALECTINLINERINTERFACE |
| 3 | + |
| 4 | +include "mlir/IR/Interfaces.td" |
| 5 | + |
| 6 | +def DialectInlinerInterface : DialectInterface<"DialectInlinerInterface"> { |
| 7 | + let description = [{ |
| 8 | + This is the interface that must be implemented by the dialects of operations |
| 9 | + to be inlined. This interface should only handle the operations of the |
| 10 | + given dialect. |
| 11 | + }]; |
| 12 | + |
| 13 | + |
| 14 | + let cppNamespace = "::mlir"; |
| 15 | + |
| 16 | + let methods = [ |
| 17 | + InterfaceMethod< |
| 18 | + /*desc=*/ [{ |
| 19 | + Returns true if the given operation 'callable', that implements the |
| 20 | + 'CallableOpInterface', can be inlined into the position given call |
| 21 | + operation 'call', that is registered to the current dialect and implements |
| 22 | + the `CallOpInterface`. 'wouldBeCloned' is set to true if the region of the |
| 23 | + given 'callable' is set to be cloned during the inlining process, or false |
| 24 | + if the region is set to be moved in-place(i.e. no duplicates would be |
| 25 | + created). |
| 26 | + }], |
| 27 | + /*returnType=*/ "bool", |
| 28 | + /*methodName=*/ "isLegalToInline", |
| 29 | + /*args=*/ (ins "::mlir::Operation *":$call, "::mlir::Operation *":$callable, "bool":$wouldBeCloned), |
| 30 | + /*methodBody=*/ [{ |
| 31 | + return false; |
| 32 | + }] |
| 33 | + >, |
| 34 | + InterfaceMethod< |
| 35 | + /*desc=*/ [{ |
| 36 | + Returns true if the given region 'src' can be inlined into the region |
| 37 | + 'dest' that is attached to an operation registered to the current dialect. |
| 38 | + 'wouldBeCloned' is set to true if the given 'src' region is set to be |
| 39 | + cloned during the inlining process, or false if the region is set to be |
| 40 | + moved in-place(i.e. no duplicates would be created). 'valueMapping' |
| 41 | + contains any remapped values from within the 'src' region. This can be |
| 42 | + used to examine what values will replace entry arguments into the 'src' |
| 43 | + region for example. |
| 44 | + }], |
| 45 | + /*returnType=*/ "bool", |
| 46 | + /*methodName=*/ "isLegalToInline", |
| 47 | + /*args=*/ (ins "::mlir::Region *":$dest, "::mlir::Region *":$src, "bool":$wouldBeCloned, |
| 48 | + "::mlir::IRMapping &":$valueMapping), |
| 49 | + /*methodBody=*/ [{ |
| 50 | + return false; |
| 51 | + }] |
| 52 | + >, |
| 53 | + InterfaceMethod< |
| 54 | + /*desc=*/ [{ |
| 55 | + Returns true if the given region 'src' can be inlined into the region |
| 56 | + 'dest' that is attached to an operation registered to the current dialect. |
| 57 | + 'wouldBeCloned' is set to true if the given 'src' region is set to be |
| 58 | + cloned during the inlining process, or false if the region is set to be |
| 59 | + moved in-place(i.e. no duplicates would be created). 'valueMapping' |
| 60 | + contains any remapped values from within the 'src' region. This can be |
| 61 | + used to examine what values will replace entry arguments into the 'src' |
| 62 | + region for example. |
| 63 | + }], |
| 64 | + /*returnType=*/ "bool", |
| 65 | + /*methodName=*/ "isLegalToInline", |
| 66 | + /*args=*/ (ins "::mlir::Operation *":$op, "::mlir::Region *":$dest, "bool":$wouldBeCloned, |
| 67 | + "::mlir::IRMapping &":$valueMapping), |
| 68 | + /*methodBody=*/ [{ |
| 69 | + return false; |
| 70 | + }] |
| 71 | + >, |
| 72 | + InterfaceMethod< |
| 73 | + /*desc=*/ [{ |
| 74 | + This hook is invoked on an operation that contains regions. It should |
| 75 | + return true if the analyzer should recurse within the regions of this |
| 76 | + operation when computing legality and cost, false otherwise. The default |
| 77 | + implementation returns true. |
| 78 | + }], |
| 79 | + /*returnType=*/ "bool", |
| 80 | + /*methodName=*/ "shouldAnalyzeRecursively", |
| 81 | + /*args=*/ (ins "::mlir::Operation *":$op), |
| 82 | + /*methodBody=*/ [{ |
| 83 | + return true; |
| 84 | + }] |
| 85 | + >, |
| 86 | + InterfaceMethod< |
| 87 | + /*desc=*/ [{ |
| 88 | + Handle the given inlined terminator by replacing it with a new operation |
| 89 | + as necessary. This overload is called when the inlined region has more |
| 90 | + than one block. The 'newDest' block represents the new final branching |
| 91 | + destination of blocks within this region, i.e. operations that release |
| 92 | + control to the parent operation will likely now branch to this block. |
| 93 | + Its block arguments correspond to any values that need to be replaced by |
| 94 | + terminators within the inlined region. |
| 95 | + }], |
| 96 | + /*returnType=*/ "void", |
| 97 | + /*methodName=*/ "handleTerminator", |
| 98 | + /*args=*/ (ins "::mlir::Operation *":$op, "::mlir::Block *":$newDest), |
| 99 | + /*methodBody=*/ [{ |
| 100 | + llvm_unreachable("must implement handleTerminator in the case of multiple " |
| 101 | + "inlined blocks"); |
| 102 | + }] |
| 103 | + >, |
| 104 | + InterfaceMethod< |
| 105 | + /*desc=*/ [{ |
| 106 | + Handle the given inlined terminator by replacing it with a new operation |
| 107 | + as necessary. This overload is called when the inlined region only |
| 108 | + contains one block. 'valuesToReplace' contains the previously returned |
| 109 | + values of the call site before inlining. These values must be replaced by |
| 110 | + this callback if they had any users (for example for traditional function |
| 111 | + calls, these are directly replaced with the operands of the `return` |
| 112 | + operation). The given 'op' will be removed by the caller, after this |
| 113 | + function has been called. |
| 114 | + }], |
| 115 | + /*returnType=*/ "void", |
| 116 | + /*methodName=*/ "handleTerminator", |
| 117 | + /*args=*/ (ins "::mlir::Operation *":$op, "::mlir::ValueRange":$valuesToReplace), |
| 118 | + /*methodBody=*/ [{ |
| 119 | + llvm_unreachable( |
| 120 | + "must implement handleTerminator in the case of one inlined block"); |
| 121 | + }] |
| 122 | + >, |
| 123 | + InterfaceMethod< |
| 124 | + /*desc=*/ [{ |
| 125 | + Attempt to materialize a conversion for a type mismatch between a call |
| 126 | + from this dialect, and a callable region. This method should generate an |
| 127 | + operation that takes 'input' as the only operand, and produces a single |
| 128 | + result of 'resultType'. If a conversion can not be generated, nullptr |
| 129 | + should be returned. For example, this hook may be invoked in the following |
| 130 | + scenarios: |
| 131 | + func @foo(i32) -> i32 { ... } |
| 132 | + |
| 133 | + // Mismatched input operand |
| 134 | + ... = foo.call @foo(%input : i16) -> i32 |
| 135 | + |
| 136 | + // Mismatched result type. |
| 137 | + ... = foo.call @foo(%input : i32) -> i16 |
| 138 | + |
| 139 | + NOTE: This hook may be invoked before the 'isLegal' checks above. |
| 140 | + }], |
| 141 | + /*returnType=*/ "::mlir::Operation *", |
| 142 | + /*methodName=*/ "materializeCallConversion", |
| 143 | + /*args=*/ (ins "::mlir::OpBuilder &":$builder, "::mlir::Value":$input, |
| 144 | + "::mlir::Type":$resultType, "::mlir::Location":$conversionLoc), |
| 145 | + /*methodBody=*/ [{ |
| 146 | + return nullptr; |
| 147 | + }] |
| 148 | + >, |
| 149 | + InterfaceMethod< |
| 150 | + /*desc=*/ [{ |
| 151 | + Hook to transform the call arguments before using them to replace the |
| 152 | + callee arguments. Returns a value of the same type or the `argument` |
| 153 | + itself if nothing changed. The `argumentAttrs` dictionary is non-null even |
| 154 | + if no attribute is present. The hook is called after converting the |
| 155 | + callsite argument types using the materializeCallConversion callback, and |
| 156 | + right before inlining the callee region. Any operations created using the |
| 157 | + provided `builder` are inserted right before the inlined callee region. An |
| 158 | + example use case is the insertion of copies for by value arguments. |
| 159 | + }], |
| 160 | + /*returnType=*/ "::mlir::Value", |
| 161 | + /*methodName=*/ "handleArgument", |
| 162 | + /*args=*/ (ins "::mlir::OpBuilder &":$builder, "::mlir::Operation *":$call, |
| 163 | + "::mlir::Operation *":$callable, "::mlir::Value":$argument, |
| 164 | + "::mlir::DictionaryAttr":$argumentAttrs), |
| 165 | + /*methodBody=*/ [{ |
| 166 | + return argument; |
| 167 | + }] |
| 168 | + >, |
| 169 | + InterfaceMethod< |
| 170 | + /*desc=*/ [{ |
| 171 | + Hook to transform the callee results before using them to replace the call |
| 172 | + results. Returns a value of the same type or the `result` itself if |
| 173 | + nothing changed. The `resultAttrs` dictionary is non-null even if no |
| 174 | + attribute is present. The hook is called right before handling |
| 175 | + terminators, and obtains the callee result before converting its type |
| 176 | + using the `materializeCallConversion` callback. Any operations created |
| 177 | + using the provided `builder` are inserted right after the inlined callee |
| 178 | + region. An example use case is the insertion of copies for by value |
| 179 | + results. NOTE: This hook is invoked after inlining the `callable` region. |
| 180 | + }], |
| 181 | + /*returnType=*/ "::mlir::Value", |
| 182 | + /*methodName=*/ "handleResult", |
| 183 | + /*args=*/ (ins "::mlir::OpBuilder &":$builder, "::mlir::Operation *":$call, |
| 184 | + "::mlir::Operation *":$callable, "::mlir::Value":$result, |
| 185 | + "::mlir::DictionaryAttr":$resultAttrs), |
| 186 | + /*methodBody=*/ [{ |
| 187 | + return result; |
| 188 | + }] |
| 189 | + >, |
| 190 | + InterfaceMethod< |
| 191 | + /*desc=*/ [{ |
| 192 | + Process a set of blocks that have been inlined for a call. This callback |
| 193 | + is invoked before inlined terminator operations have been processed. |
| 194 | + }], |
| 195 | + /*returnType=*/ "void", |
| 196 | + /*methodName=*/ "processInlinedCallBlocks", |
| 197 | + /*args=*/ (ins "::mlir::Operation *":$call, |
| 198 | + "::mlir::iterator_range<::mlir::Region::iterator>":$inlinedBlocks), |
| 199 | + /*methodBody=*/ [{}] |
| 200 | + >, |
| 201 | + InterfaceMethod< |
| 202 | + /*desc=*/ [{ |
| 203 | + Returns true if the inliner can assume a fast path of not creating a new |
| 204 | + block, if there is only one block. |
| 205 | + }], |
| 206 | + /*returnType=*/ "bool", |
| 207 | + /*methodName=*/ "allowSingleBlockOptimization", |
| 208 | + /*args=*/ (ins "::mlir::iterator_range<::mlir::Region::iterator>":$inlinedBlocks), |
| 209 | + /*methodBody=*/ [{ |
| 210 | + return true; |
| 211 | + }] |
| 212 | + > |
| 213 | + ]; |
| 214 | +} |
| 215 | + |
| 216 | + |
| 217 | +#endif |
0 commit comments