MLIR 22.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
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
26namespace mlir {
27namespace memref {
28class GlobalOp;
29} // namespace memref
30
31namespace bufferization {
32class BufferizationState;
33
34/// A simple analysis that detects allocation operations.
36public:
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
48public:
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
68private:
69 /// Searches for and registers all supported allocation entries.
70 void build(Operation *op);
71
72private:
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".
81template <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.
104public:
106
107 /// Constructs a new operation base using the given root operation.
109
110protected:
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.
125FailureOr<memref::GlobalOp> getGlobalFor(arith::ConstantOp constantOp,
126 SymbolTableCollection &symbolTables,
127 uint64_t alignment,
128 Attribute memorySpace = {});
129
130void removeSymbol(Operation *op, BufferizationState &state);
131
132void 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.
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:46
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.
BufferPlacementAllocs(Operation *op)
Initializes the internal list by discovering all supported allocation nodes.
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
BufferViewFlowAnalysis aliases
Alias information that can be updated during the insertion of copies.
BufferPlacementAllocs allocs
Stores all internally managed allocations.
BufferPlacementTransformationBase(Operation *op)
Constructs a new operation base using the given root operation.
Liveness liveness
The underlying liveness analysis to compute fine grained information about alloc and dealloc position...
void insertSymbol(Operation *op, BufferizationState &state)
FailureOr< memref::GlobalOp > getGlobalFor(arith::ConstantOp constantOp, SymbolTableCollection &symbolTables, uint64_t alignment, Attribute memorySpace={})
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
void removeSymbol(Operation *op, BufferizationState &state)
Include the generated interface declarations.