|
| 1 | +//===-- BufferizableOpInterface.td - Compreh. Bufferize ----*- 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_LINALG_TRANSFORMS_BUFFERIZABLEOPINTERFACE |
| 10 | +#define MLIR_DIALECT_LINALG_TRANSFORMS_BUFFERIZABLEOPINTERFACE |
| 11 | + |
| 12 | +include "mlir/IR/OpBase.td" |
| 13 | + |
| 14 | +def BufferizableOpInterface : OpInterface<"BufferizableOpInterface"> { |
| 15 | + let description = [{ |
| 16 | + An op interface for Comprehensive Bufferization. Ops that implement this |
| 17 | + interface can be bufferized using Comprehensive Bufferization. |
| 18 | + }]; |
| 19 | + let cppNamespace = "::mlir::linalg"; |
| 20 | + let methods = [ |
| 21 | + InterfaceMethod< |
| 22 | + /*desc=*/[{ |
| 23 | + Return `true` if the given OpOperand bufferizes to a memory read. This |
| 24 | + method will never be called on OpOperands that do not have a tensor |
| 25 | + type. |
| 26 | + |
| 27 | + Note: It is always safe to consider an OpOperand as a memory read, |
| 28 | + even if it does actually not read; however, this can introduce |
| 29 | + unnecessary out-of-place bufferization decisions. The analysis of |
| 30 | + Comprehensive Bufferize considers OpOperands of unknown ops (that do |
| 31 | + not implement this interface) as reading OpOperands. |
| 32 | + }], |
| 33 | + /*retType=*/"bool", |
| 34 | + /*methodName=*/"bufferizesToMemoryRead", |
| 35 | + /*args=*/(ins "OpOperand &":$opOperand), |
| 36 | + /*methodBody=*/"", |
| 37 | + /*defaultImplementation=*/[{ |
| 38 | + // Does not have to be implemented for ops without tensor OpOperands. |
| 39 | + llvm_unreachable("bufferizesToMemoryRead not implemented"); |
| 40 | + }] |
| 41 | + >, |
| 42 | + InterfaceMethod< |
| 43 | + /*desc=*/[{ |
| 44 | + Return `true` if the given OpOperand bufferizes to a memory write. |
| 45 | + This method will never be called on OpOperands that do not have a |
| 46 | + tensor type. |
| 47 | + |
| 48 | + Note: It is always safe to consider an OpOperand as a memory write, |
| 49 | + even if it does actually not write; however, this can introduce |
| 50 | + unnecessary out-of-place bufferization decisions. The analysis of |
| 51 | + Comprehensive Bufferize considers OpOperands of unknown ops (that do |
| 52 | + not implement this interface) as writing OpOperands. |
| 53 | + }], |
| 54 | + /*retType=*/"bool", |
| 55 | + /*methodName=*/"bufferizesToMemoryWrite", |
| 56 | + /*args=*/(ins "OpOperand &":$opOperand), |
| 57 | + /*methodBody=*/"", |
| 58 | + /*defaultImplementation=*/[{ |
| 59 | + // Does not have to be implemented for ops without tensor OpOperands. |
| 60 | + llvm_unreachable("bufferizesToMemoryWrite not implemented"); |
| 61 | + }] |
| 62 | + >, |
| 63 | + // TODO: Simplify this interface by removing `bufferizesToAliasOnly` and |
| 64 | + // `getInplaceableOpResult`. Instead, always use `getAliasingOpResult`. If |
| 65 | + // `getAliasingOpResult` returns a non-null value, we know that an alias |
| 66 | + // is created. If `bufferizesToMemoryRead` and `bufferizesToMemoryWrite` |
| 67 | + // return `false`, we know that the operands "bufferizes to alias only". |
| 68 | + InterfaceMethod< |
| 69 | + /*desc=*/[{ |
| 70 | + Return `true` if the given OpOperand creates an alias but does neither |
| 71 | + read nor write. This implies that `bufferizesToMemoryRead` and |
| 72 | + `bufferizesToMemoryWrite` must return `false`. This method will never |
| 73 | + be called on OpOperands that do not have a tensor type. |
| 74 | + |
| 75 | + Examples of such ops are `tensor.extract_slice` and `tensor.cast`. |
| 76 | + }], |
| 77 | + /*retType=*/"bool", |
| 78 | + /*methodName=*/"bufferizesToAliasOnly", |
| 79 | + /*args=*/(ins "OpOperand &":$opOperand), |
| 80 | + /*methodBody=*/"", |
| 81 | + /*defaultImplementation=*/[{ |
| 82 | + // For better debugging, run `bufferizesToMemoryWrite`, which fires an |
| 83 | + // assertion when called on an op that should not have tensor |
| 84 | + // OpOperands. |
| 85 | + (void) cast<BufferizableOpInterface>($_op.getOperation()) |
| 86 | + .bufferizesToMemoryWrite(opOperand); |
| 87 | + // Return `false` by default, as most ops are not "alias only". |
| 88 | + return false; |
| 89 | + }] |
| 90 | + >, |
| 91 | + InterfaceMethod< |
| 92 | + /*desc=*/[{ |
| 93 | + Return the OpResult that can bufferize in-place with a given |
| 94 | + OpOperand. Return a null value if the OpOperand cannot bufferize |
| 95 | + in-place. This method will never be called on OpOperands that do not |
| 96 | + have a tensor type. |
| 97 | + }], |
| 98 | + /*retType=*/"OpResult", |
| 99 | + /*methodName=*/"getInplaceableOpResult", |
| 100 | + /*args=*/(ins "OpOperand &":$opOperand), |
| 101 | + /*methodBody=*/"", |
| 102 | + /*defaultImplementation=*/[{ |
| 103 | + // Does not have to be implemented for ops without tensor OpOperands. |
| 104 | + llvm_unreachable("getInplaceableOpResult not implemented"); |
| 105 | + }] |
| 106 | + >, |
| 107 | + InterfaceMethod< |
| 108 | + /*desc=*/[{ |
| 109 | + Return the OpResult that aliases with a given OpOperand when |
| 110 | + bufferized in-place. This is a superset of `getInplaceableOpResult`. |
| 111 | + This method will never be called on OpOperands that do not have a |
| 112 | + tensor type. |
| 113 | + }], |
| 114 | + /*retType=*/"OpResult", |
| 115 | + /*methodName=*/"getAliasingOpResult", |
| 116 | + /*args=*/(ins "OpOperand &":$opOperand), |
| 117 | + /*methodBody=*/"", |
| 118 | + /*defaultImplementation=*/[{ |
| 119 | + return cast<BufferizableOpInterface>($_op.getOperation()) |
| 120 | + .getInplaceableOpResult(opOperand); |
| 121 | + }] |
| 122 | + >, |
| 123 | + InterfaceMethod< |
| 124 | + /*desc=*/[{ |
| 125 | + Return the OpOperands that alias with a given OpResult when |
| 126 | + bufferized in-place. This method will never be called on OpResults |
| 127 | + that do not have a tensor type. |
| 128 | + |
| 129 | + Note: This method can return multiple OpOperands, indicating that the |
| 130 | + given OpResult may at runtime alias with any of the OpOperands. This |
| 131 | + is useful for branches and for ops such as `std.select`. |
| 132 | + }], |
| 133 | + /*retType=*/"SmallVector<OpOperand *>", |
| 134 | + /*methodName=*/"getAliasingOpOperand", |
| 135 | + /*args=*/(ins "OpResult":$opResult), |
| 136 | + /*methodBody=*/"", |
| 137 | + /*defaultImplementation=*/[{ |
| 138 | + // Does not have to be implemented for ops without tensor OpResults. |
| 139 | + llvm_unreachable("getInplaceableOpResult not implemented"); |
| 140 | + }] |
| 141 | + >, |
| 142 | + InterfaceMethod< |
| 143 | + /*desc=*/[{ |
| 144 | + Return the buffer relation between the given OpOperand and its |
| 145 | + aliasing OpResult when bufferized in-place. Most OpOperands have an |
| 146 | + "equivalence" relation. |
| 147 | + |
| 148 | + TODO: Support other relations such as "OpOperand is included in |
| 149 | + OpResult". |
| 150 | + }], |
| 151 | + /*retType=*/"BufferRelation", |
| 152 | + /*methodName=*/"bufferRelation", |
| 153 | + /*args=*/(ins "OpOperand &":$opOperand), |
| 154 | + /*methodBody=*/"", |
| 155 | + /*defaultImplementation=*/[{ |
| 156 | + // Does not have to be implemented for ops without tensor OpOperands. |
| 157 | + llvm_unreachable("bufferRelation not implemented"); |
| 158 | + }] |
| 159 | + >, |
| 160 | + // TODO: Simplify method signature: Pass an OpBuilder and a |
| 161 | + // BufferizationState object. |
| 162 | + InterfaceMethod< |
| 163 | + /*desc=*/[{ |
| 164 | + Bufferize this op, i.e., rewrite it into a memref-based equivalent. |
| 165 | + `bvm` maps tensor values to memref values and this method should map |
| 166 | + tensor results to memref results after creating/modifying ops. |
| 167 | + }], |
| 168 | + /*retType=*/"LogicalResult", |
| 169 | + /*methodName=*/"bufferize", |
| 170 | + /*args=*/(ins "OpBuilder &":$b, |
| 171 | + "BlockAndValueMapping &":$bvm, |
| 172 | + "BufferizationAliasInfo &":$aliasInfo, |
| 173 | + "AllocationCallbacks &":$allocationFn), |
| 174 | + /*methodBody=*/"", |
| 175 | + /*defaultImplementation=*/[{ |
| 176 | + llvm_unreachable("bufferize not implemented"); |
| 177 | + return failure(); |
| 178 | + }] |
| 179 | + >, |
| 180 | + ]; |
| 181 | +} |
| 182 | + |
| 183 | +#endif // MLIR_DIALECT_LINALG_TRANSFORMS_BUFFERIZABLEOPINTERFACE |
0 commit comments