MLIR  21.0.0git
BufferUtils.h
Go to the documentation of this file.
1 //===- BufferUtils.h - Buffer optimization 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 // This file provides utilities for passes optimizing code that has already
10 // been converted to buffers.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERUTILS_H
15 #define MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERUTILS_H
16 
17 #include "mlir/Analysis/Liveness.h"
20 #include "mlir/IR/Builders.h"
21 #include "mlir/IR/BuiltinOps.h"
22 #include "mlir/IR/Dominance.h"
23 #include "mlir/IR/Operation.h"
25 
26 namespace mlir {
27 namespace memref {
28 class GlobalOp;
29 } // namespace memref
30 
31 namespace bufferization {
32 class BufferizationState;
33 
34 /// A simple analysis that detects allocation operations.
36 public:
37  /// Represents a tuple of allocValue and deallocOperation.
38  using AllocEntry = std::tuple<Value, Operation *>;
39 
40  /// Represents a list containing all alloc entries.
42 
43  /// Get the start operation to place the given alloc value within the
44  /// specified placement block.
45  static Operation *getStartOperation(Value allocValue, Block *placementBlock,
46  const Liveness &liveness);
47 
48 public:
49  /// Initializes the internal list by discovering all supported allocation
50  /// nodes.
52 
53  /// Returns the begin iterator to iterate over all allocations.
54  AllocEntryList::const_iterator begin() const { return allocs.begin(); }
55 
56  /// Returns the end iterator that can be used in combination with begin.
57  AllocEntryList::const_iterator end() const { return allocs.end(); }
58 
59  /// Returns the begin iterator to iterate over all allocations.
60  AllocEntryList::iterator begin() { return allocs.begin(); }
61 
62  /// Returns the end iterator that can be used in combination with begin.
63  AllocEntryList::iterator end() { return allocs.end(); }
64 
65  /// Registers a new allocation entry.
66  void registerAlloc(const AllocEntry &entry) { allocs.push_back(entry); }
67 
68 private:
69  /// Searches for and registers all supported allocation entries.
70  void build(Operation *op);
71 
72 private:
73  /// Maps allocation nodes to their associated blocks.
74  AllocEntryList allocs;
75 };
76 
77 /// Finds a common dominator for the given value while taking the positions
78 /// of the values in the value set into account. It supports dominator and
79 /// post-dominator analyses via template arguments. If no common dominator
80 /// can be found, this function will return "nullptr".
81 template <typename DominatorT>
84  const DominatorT &doms) {
85  // Store blocks in a set before querying `DominanceInfo` to filter out
86  // duplicate blocks (for performance reasons).
88  // Start with the current block the value is defined in.
89  blocks.insert(value.getParentBlock());
90  for (Value childValue : values) {
91  for (Operation *user : childValue.getUsers()) {
92  // Find an appropriate dominator block that takes the current use into
93  // account.
94  blocks.insert(user->getBlock());
95  }
96  // Take values without any users into account.
97  blocks.insert(childValue.getParentBlock());
98  }
99  return doms.findNearestCommonDominator(blocks);
100 }
101 
102 /// The base class for all BufferPlacement transformations.
104 public:
106 
107  /// Constructs a new operation base using the given root operation.
109 
110 protected:
111  /// Alias information that can be updated during the insertion of copies.
113 
114  /// Stores all internally managed allocations.
116 
117  /// The underlying liveness analysis to compute fine grained information
118  /// about alloc and dealloc positions.
120 };
121 
122 // Create a global op for the given tensor-valued constant in the program.
123 // Globals are created lazily at the top of the enclosing ModuleOp with pretty
124 // names. Duplicates are avoided.
125 FailureOr<memref::GlobalOp> getGlobalFor(arith::ConstantOp constantOp,
126  SymbolTableCollection &symbolTables,
127  uint64_t alignment,
128  Attribute memorySpace = {});
129 
130 void removeSymbol(Operation *op, BufferizationState &state);
131 
132 void insertSymbol(Operation *op, BufferizationState &state);
133 
134 } // namespace bufferization
135 } // namespace mlir
136 
137 #endif // MLIR_DIALECT_BUFFERIZATION_TRANSFORMS_BUFFERUTILS_H
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Block represents an ordered list of Operations.
Definition: Block.h:33
A straight-forward alias analysis which ensures that all dependencies of all values will be determine...
SmallPtrSet< Value, 16 > ValueSetT
Represents an analysis for computing liveness information from a given top-level operation.
Definition: Liveness.h:47
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
user_range getUsers()
Returns a range of all users.
Definition: Operation.h:873
This class represents a collection of SymbolTables.
Definition: SymbolTable.h:283
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Block * getParentBlock()
Return the Block in which this Value is defined.
Definition: Value.cpp:48
A simple analysis that detects allocation operations.
Definition: BufferUtils.h:35
void registerAlloc(const AllocEntry &entry)
Registers a new allocation entry.
Definition: BufferUtils.h:66
static Operation * getStartOperation(Value allocValue, Block *placementBlock, const Liveness &liveness)
Get the start operation to place the given alloc value within the specified placement block.
Definition: BufferUtils.cpp:36
BufferPlacementAllocs(Operation *op)
Initializes the internal list by discovering all supported allocation nodes.
Definition: BufferUtils.cpp:58
AllocEntryList::iterator end()
Returns the end iterator that can be used in combination with begin.
Definition: BufferUtils.h:63
AllocEntryList::const_iterator end() const
Returns the end iterator that can be used in combination with begin.
Definition: BufferUtils.h:57
AllocEntryList::iterator begin()
Returns the begin iterator to iterate over all allocations.
Definition: BufferUtils.h:60
std::tuple< Value, Operation * > AllocEntry
Represents a tuple of allocValue and deallocOperation.
Definition: BufferUtils.h:38
AllocEntryList::const_iterator begin() const
Returns the begin iterator to iterate over all allocations.
Definition: BufferUtils.h:54
SmallVector< AllocEntry, 8 > AllocEntryList
Represents a list containing all alloc entries.
Definition: BufferUtils.h:41
The base class for all BufferPlacement transformations.
Definition: BufferUtils.h:103
BufferViewFlowAnalysis aliases
Alias information that can be updated during the insertion of copies.
Definition: BufferUtils.h:112
BufferPlacementAllocs allocs
Stores all internally managed allocations.
Definition: BufferUtils.h:115
BufferPlacementTransformationBase(Operation *op)
Constructs a new operation base using the given root operation.
Definition: BufferUtils.cpp:97
Liveness liveness
The underlying liveness analysis to compute fine grained information about alloc and dealloc position...
Definition: BufferUtils.h:119
void insertSymbol(Operation *op, BufferizationState &state)
Block * findCommonDominator(Value value, const BufferViewFlowAnalysis::ValueSetT &values, const DominatorT &doms)
Finds a common dominator for the given value while taking the positions of the values in the value se...
Definition: BufferUtils.h:82
FailureOr< memref::GlobalOp > getGlobalFor(arith::ConstantOp constantOp, SymbolTableCollection &symbolTables, uint64_t alignment, Attribute memorySpace={})
void removeSymbol(Operation *op, BufferizationState &state)
Include the generated interface declarations.