| 1 | //===- Bufferization.h - Bufferization dialect ------------------*- C++ -*-===// |
| 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_BUFFERIZATION_IR_BUFFERIZATION_H_ |
| 10 | #define MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZATION_H_ |
| 11 | |
| 12 | #include "mlir/Dialect/Bufferization/IR/AllocationOpInterface.h" |
| 13 | #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h" |
| 14 | #include "mlir/Dialect/Bufferization/IR/BufferizationDialect.h" |
| 15 | |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // Bufferization Dialect Operations |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #define GET_OP_CLASSES |
| 21 | #include "mlir/Dialect/Bufferization/IR/BufferizationOps.h.inc" |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // Helper functions |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | namespace mlir { |
| 28 | namespace bufferization { |
| 29 | /// Populate `dynamicDims` with tensor::DimOp / memref::DimOp results for all |
| 30 | /// dynamic dimensions of the given shaped value. |
| 31 | void populateDynamicDimSizes(OpBuilder &b, Location loc, Value shapedValue, |
| 32 | SmallVector<Value> &dynamicDims); |
| 33 | |
| 34 | /// Try to cast the given ranked MemRef-typed value to the given ranked MemRef |
| 35 | /// type. Insert a reallocation + copy if it cannot be statically guaranteed |
| 36 | /// that a direct cast would be valid. |
| 37 | /// |
| 38 | /// E.g., when casting from a ranked MemRef type with dynamic layout to a ranked |
| 39 | /// MemRef type with static layout, it is not statically known whether the cast |
| 40 | /// will succeed or not. Such `memref.cast` ops may fail at runtime. This |
| 41 | /// function never generates such casts and conservatively inserts a copy. |
| 42 | /// |
| 43 | /// This function returns `failure()` in case of unsupported casts. E.g., casts |
| 44 | /// with differing element types or memory spaces. |
| 45 | FailureOr<Value> castOrReallocMemRefValue(OpBuilder &b, Value value, |
| 46 | MemRefType type, |
| 47 | const BufferizationOptions &options); |
| 48 | |
| 49 | /// Try to fold to_buffer(to_tensor(x)). If x's type and the result type of the |
| 50 | /// to_buffer op are different, a memref.cast is needed. |
| 51 | LogicalResult foldToBufferToTensorPair(RewriterBase &rewriter, |
| 52 | ToBufferOp toBuffer, |
| 53 | const BufferizationOptions &options); |
| 54 | |
| 55 | /// Add the canonicalization patterns for bufferization.dealloc to the given |
| 56 | /// pattern set to make them available to other passes (such as |
| 57 | /// BufferDeallocationSimplification). |
| 58 | void populateDeallocOpCanonicalizationPatterns(RewritePatternSet &patterns, |
| 59 | MLIRContext *context); |
| 60 | |
| 61 | } // namespace bufferization |
| 62 | } // namespace mlir |
| 63 | |
| 64 | #endif // MLIR_DIALECT_BUFFERIZATION_IR_BUFFERIZATION_H_ |
| 65 | |