1 //===- Bufferize.h - Bufferization Utilities --------------------*- 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 // We use the term "bufferize" to mean conversion from tensor types to
10 // memref types.
11 //
12 // Generally speaking, for each op that operates on tensor types, the
13 // `BufferizableOpInterface` needs to be implemented. This file contains the
14 // bufferization driver that is responsible for bufferizing the ops in the right
15 // order, etc.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
20 #define MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
21 
22 #include "mlir/Transforms/DialectConversion.h"
23 
24 namespace mlir {
25 namespace bufferization {
26 
27 class AnalysisState;
28 struct BufferizationOptions;
29 class OpFilter;
30 
31 /// A helper type converter class that automatically populates the relevant
32 /// materializations and type conversions for bufferization.
33 class BufferizeTypeConverter : public TypeConverter {
34 public:
35   BufferizeTypeConverter();
36 };
37 
38 /// Marks ops used by bufferization for type conversion materializations as
39 /// "legal" in the given ConversionTarget.
40 ///
41 /// This function should be called by all bufferization passes using
42 /// BufferizeTypeConverter so that materializations work properly. One exception
43 /// is bufferization passes doing "full" conversions, where it can be desirable
44 /// for even the materializations to remain illegal so that they are eliminated,
45 /// such as via the patterns in
46 /// populateEliminateBufferizeMaterializationsPatterns.
47 void populateBufferizeMaterializationLegality(ConversionTarget &target);
48 
49 /// Populate patterns to eliminate bufferize materializations.
50 ///
51 /// In particular, these are the tensor_load/buffer_cast ops.
52 void populateEliminateBufferizeMaterializationsPatterns(
53     BufferizeTypeConverter &typeConverter, RewritePatternSet &patterns);
54 
55 /// Bufferize `op` and its nested ops that implement `BufferizableOpInterface`.
56 /// If `copyBeforeWrite`, buffers are duplicated and copied before any tensor
57 /// use that bufferizes to a memory write.
58 ///
59 /// Note: In the general case, it unsafe to run with `copyBeforeWrite = false`
60 /// because read-after-write conflicts may materialize during bufferization.
61 /// `copyBeforeWrite = false` is safe only if the input IR is guaranteed to
62 /// *not* require any out-of-place bufferization.
63 ///
64 /// Note: This function bufferizes ops without utilizing analysis results. It
65 /// can be used to implement partial bufferization passes.
66 LogicalResult bufferizeOp(Operation *op, const BufferizationOptions &options,
67                           bool copyBeforeWrite = true,
68                           const OpFilter *opFilter = nullptr);
69 
70 BufferizationOptions getPartialBufferizationOptions();
71 
72 } // namespace bufferization
73 } // namespace mlir
74 
75 #endif // MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
76