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