-
Notifications
You must be signed in to change notification settings - Fork 10.8k
/
Bufferize.h
86 lines (74 loc) · 3.48 KB
/
Bufferize.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===- Bufferize.h - Bufferization Utilities --------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// We use the term "bufferize" to mean conversion from tensor types to
// memref types.
//
// Generally speaking, for each op that operates on tensor types, the
// `BufferizableOpInterface` needs to be implemented. This file contains the
// bufferization driver that is responsible for bufferizing the ops in the right
// order, etc.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
#define MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H
#include "mlir/Transforms/DialectConversion.h"
namespace mlir {
namespace bufferization {
class AnalysisState;
struct BufferizationOptions;
class OpFilter;
/// Bufferization statistics for debugging. These can be printed after running
/// the OneShotBufferizePass with `-mlir-pass-statistics`. See the pass
/// definition for more details.
struct BufferizationStatistics {
int64_t numBufferAlloc = 0;
int64_t numBufferDealloc = 0;
int64_t numTensorInPlace = 0;
int64_t numTensorOutOfPlace = 0;
};
/// A helper type converter class that automatically populates the relevant
/// materializations and type conversions for bufferization.
class BufferizeTypeConverter : public TypeConverter {
public:
BufferizeTypeConverter();
};
/// Marks ops used by bufferization for type conversion materializations as
/// "legal" in the given ConversionTarget.
///
/// This function should be called by all bufferization passes using
/// BufferizeTypeConverter so that materializations work properly. One exception
/// is bufferization passes doing "full" conversions, where it can be desirable
/// for even the materializations to remain illegal so that they are eliminated,
/// such as via the patterns in
/// populateEliminateBufferizeMaterializationsPatterns.
void populateBufferizeMaterializationLegality(ConversionTarget &target);
/// Populate patterns to eliminate bufferize materializations.
///
/// In particular, these are the tensor_load/buffer_cast ops.
void populateEliminateBufferizeMaterializationsPatterns(
BufferizeTypeConverter &typeConverter, RewritePatternSet &patterns);
/// Bufferize `op` and its nested ops that implement `BufferizableOpInterface`.
/// If `copyBeforeWrite`, buffers are duplicated and copied before any tensor
/// use that bufferizes to a memory write.
///
/// Note: In the general case, it unsafe to run with `copyBeforeWrite = false`
/// because read-after-write conflicts may materialize during bufferization.
/// `copyBeforeWrite = false` is safe only if the input IR is guaranteed to
/// *not* require any out-of-place bufferization.
///
/// Note: This function bufferizes ops without utilizing analysis results. It
/// can be used to implement partial bufferization passes.
LogicalResult bufferizeOp(Operation *op, const BufferizationOptions &options,
bool copyBeforeWrite = true,
const OpFilter *opFilter = nullptr,
BufferizationStatistics *statistics = nullptr);
BufferizationOptions getPartialBufferizationOptions();
} // namespace bufferization
} // namespace mlir
#endif // MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERIZE_H