MLIR  19.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 
33 /// A simple analysis that detects allocation operations.
35 public:
36  /// Represents a tuple of allocValue and deallocOperation.
37  using AllocEntry = std::tuple<Value, Operation *>;
38 
39  /// Represents a list containing all alloc entries.
41 
42  /// Get the start operation to place the given alloc value within the
43  /// specified placement block.
44  static Operation *getStartOperation(Value allocValue, Block *placementBlock,
45  const Liveness &liveness);
46 
47 public:
48  /// Initializes the internal list by discovering all supported allocation
49  /// nodes.
51 
52  /// Returns the begin iterator to iterate over all allocations.
53  AllocEntryList::const_iterator begin() const { return allocs.begin(); }
54 
55  /// Returns the end iterator that can be used in combination with begin.
56  AllocEntryList::const_iterator end() const { return allocs.end(); }
57 
58  /// Returns the begin iterator to iterate over all allocations.
59  AllocEntryList::iterator begin() { return allocs.begin(); }
60 
61  /// Returns the end iterator that can be used in combination with begin.
62  AllocEntryList::iterator end() { return allocs.end(); }
63 
64  /// Registers a new allocation entry.
65  void registerAlloc(const AllocEntry &entry) { allocs.push_back(entry); }
66 
67 private:
68  /// Searches for and registers all supported allocation entries.
69  void build(Operation *op);
70 
71 private:
72  /// Maps allocation nodes to their associated blocks.
73  AllocEntryList allocs;
74 };
75 
76 /// Finds a common dominator for the given value while taking the positions
77 /// of the values in the value set into account. It supports dominator and
78 /// post-dominator analyses via template arguments. If no common dominator
79 /// can be found, this function will return "nullptr".
80 template <typename DominatorT>
83  const DominatorT &doms) {
84  // Store blocks in a set before querying `DominanceInfo` to filter out
85  // duplicate blocks (for performance reasons).
87  // Start with the current block the value is defined in.
88  blocks.insert(value.getParentBlock());
89  for (Value childValue : values) {
90  for (Operation *user : childValue.getUsers()) {
91  // Find an appropriate dominator block that takes the current use into
92  // account.
93  blocks.insert(user->getBlock());
94  }
95  // Take values without any users into account.
96  blocks.insert(childValue.getParentBlock());
97  }
98  return doms.findNearestCommonDominator(blocks);
99 }
100 
101 /// The base class for all BufferPlacement transformations.
103 public:
105 
106  /// Constructs a new operation base using the given root operation.
108 
109 protected:
110  /// Alias information that can be updated during the insertion of copies.
112 
113  /// Stores all internally managed allocations.
115 
116  /// The underlying liveness analysis to compute fine grained information
117  /// about alloc and dealloc positions.
119 };
120 
121 // Create a global op for the given tensor-valued constant in the program.
122 // Globals are created lazily at the top of the enclosing ModuleOp with pretty
123 // names. Duplicates are avoided.
124 FailureOr<memref::GlobalOp> getGlobalFor(arith::ConstantOp constantOp,
125  uint64_t alignment,
126  Attribute memorySpace = {});
127 
128 } // namespace bufferization
129 } // namespace mlir
130 
131 #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:31
A straight-forward alias analysis which ensures that all dependencies of all values will be determine...
SmallPtrSet< Value, 16 > ValueSetT
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
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:869
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:34
void registerAlloc(const AllocEntry &entry)
Registers a new allocation entry.
Definition: BufferUtils.h:65
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:34
BufferPlacementAllocs(Operation *op)
Initializes the internal list by discovering all supported allocation nodes.
Definition: BufferUtils.cpp:56
AllocEntryList::iterator end()
Returns the end iterator that can be used in combination with begin.
Definition: BufferUtils.h:62
AllocEntryList::const_iterator end() const
Returns the end iterator that can be used in combination with begin.
Definition: BufferUtils.h:56
AllocEntryList::iterator begin()
Returns the begin iterator to iterate over all allocations.
Definition: BufferUtils.h:59
std::tuple< Value, Operation * > AllocEntry
Represents a tuple of allocValue and deallocOperation.
Definition: BufferUtils.h:37
AllocEntryList::const_iterator begin() const
Returns the begin iterator to iterate over all allocations.
Definition: BufferUtils.h:53
SmallVector< AllocEntry, 8 > AllocEntryList
Represents a list containing all alloc entries.
Definition: BufferUtils.h:40
The base class for all BufferPlacement transformations.
Definition: BufferUtils.h:102
BufferViewFlowAnalysis aliases
Alias information that can be updated during the insertion of copies.
Definition: BufferUtils.h:111
BufferPlacementAllocs allocs
Stores all internally managed allocations.
Definition: BufferUtils.h:114
BufferPlacementTransformationBase(Operation *op)
Constructs a new operation base using the given root operation.
Definition: BufferUtils.cpp:95
Liveness liveness
The underlying liveness analysis to compute fine grained information about alloc and dealloc position...
Definition: BufferUtils.h:118
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:81
FailureOr< memref::GlobalOp > getGlobalFor(arith::ConstantOp constantOp, uint64_t alignment, Attribute memorySpace={})
Include the generated interface declarations.