MLIR: lib/Dialect/Bufferization/Transforms/BufferUtils.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
14
23 #include "llvm/ADT/SetOperations.h"
24 #include "llvm/ADT/SmallString.h"
25 #include
26
27 using namespace mlir;
29
30
31
32
33
34
35
37 Block *placementBlock,
39
40
43
44
45
46 if (startOperation->getBlock() != placementBlock) {
49 startOperation = opInPlacementBlock ? opInPlacementBlock
51 }
52
53 return startOperation;
54 }
55
56
57
59
60
61 void BufferPlacementAllocs::build(Operation *op) {
62 op->walk([&](MemoryEffectOpInterface opInterface) {
63
65 opInterface.getEffects(effects);
66
68 llvm::copy_if(
69 effects, std::back_inserter(allocateResultEffects),
72 return isaMemoryEffects::Allocate(it.getEffect()) && value &&
73 isa(value) &&
76 });
77
78
79 if (allocateResultEffects.size() != 1)
80 return;
81
82 Value allocValue = allocateResultEffects[0].getValue();
83
85
86 if (!dealloc)
87 return;
88 allocs.push_back(std::make_tuple(allocValue, *dealloc));
89 });
90 }
91
92
93
94
95
96
99 : aliases(op), allocs(op), liveness(op) {}
100
101
102
103
104
105 FailureOrmemref::GlobalOp
108 uint64_t alignment, Attribute memorySpace) {
109 auto type = cast(constantOp.getType());
110 auto moduleOp = constantOp->getParentOfType();
111 if (!moduleOp)
112 return failure();
113
114
115
117 auto globalOp = dyn_castmemref::GlobalOp(&op);
118 if (!globalOp)
119 continue;
120 if (!globalOp.getInitialValue().has_value())
121 continue;
122 uint64_t opAlignment = globalOp.getAlignment().value_or(0);
123 Attribute initialValue = globalOp.getInitialValue().value();
124 if (opAlignment == alignment && initialValue == constantOp.getValue())
125 return globalOp;
126 }
127
128
129
130 OpBuilder globalBuilder(moduleOp.getContext());
132
133
135 llvm::raw_svector_ostream os(buf);
136 interleave(type.getShape(), os, "x");
137 os << "x" << type.getElementType();
138
139
140 IntegerAttr memrefAlignment =
142 : IntegerAttr();
143
144
145 auto memrefType =
147 if (memorySpace)
149 auto global = globalBuilder.creatememref::GlobalOp(
150 constantOp.getLoc(), (Twine("__constant_") + os.str()).str(),
151 globalBuilder.getStringAttr("private"),
152 memrefType,
153 cast(constantOp.getValue()),
154 true,
155 memrefAlignment);
156 symbolTable.insert(global);
157
158
159 global->moveBefore(&moduleOp.front());
160 return global;
161 }
162
165 SymbolTable &symbolTable = state.getSymbolTables().getSymbolTable(
167
168 symbolTable.remove(op);
169 }
170
172 SymbolTable &symbolTable = state.getSymbolTables().getSymbolTable(
174
175 symbolTable.insert(op);
176 }
177 }
Attributes are known-constant values of operations.
Block represents an ordered list of Operations.
Operation * findAncestorOpInBlock(Operation &op)
Returns 'op' if 'op' lies in this block, or otherwise finds the ancestor operation of 'op' that lies ...
Operation * getTerminator()
Get the terminator operation of this block.
StringAttr getStringAttr(const Twine &bytes)
This class represents liveness information on block level.
Operation * getStartOperation(Value value) const
Gets the start operation for the given value.
Represents an analysis for computing liveness information from a given top-level operation.
const LivenessBlockInfo * getLiveness(Block *block) const
Gets liveness info (if any) for the block.
This is a builder type that keeps local references to arguments.
Builder & setMemorySpace(Attribute newMemorySpace)
This class helps build Operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
A trait used to provide symbol table functionalities to a region operation.
Operation is the basic unit of execution within MLIR.
std::enable_if_t< llvm::function_traits< std::decay_t< FnT > >::num_args==1, RetT > walk(FnT &&callback)
Walk the operation by calling the callback for each nested operation (including this one),...
Block * getBlock()
Returns the operation block that contains this operation.
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Operation * getParentWithTrait()
Returns the closest surrounding parent operation with trait Trait.
iterator_range< OpIterator > getOps()
This class represents a specific instance of an effect.
Resource * getResource() const
Return the resource that the effect applies to.
EffectT * getEffect() const
Return the effect being applied.
Value getValue() const
Return the value the effect is applied on, or nullptr if there isn't a known value being affected.
static AutomaticAllocationScopeResource * get()
Returns a unique instance for the given effect class.
This class represents a collection of SymbolTables.
virtual SymbolTable & getSymbolTable(Operation *op)
Lookup, or create, a symbol table for an operation.
This class allows for representing and managing the symbol table used by operations with the 'SymbolT...
StringAttr insert(Operation *symbol, Block::iterator insertPt={})
Insert a new symbol into the table, and rename it as necessary to avoid collisions.
void remove(Operation *op)
Remove the given symbol from the table, without deleting it.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
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.
BufferPlacementTransformationBase(Operation *op)
Constructs a new operation base using the given root operation.
BufferizationState provides information about the state of the IR during the bufferization process.
BaseMemRefType getMemRefTypeWithStaticIdentityLayout(TensorType tensorType, Attribute memorySpace=nullptr)
Return a MemRef type with a static identity layout (i.e., no layout map).
void insertSymbol(Operation *op, BufferizationState &state)
FailureOr< memref::GlobalOp > getGlobalFor(arith::ConstantOp constantOp, SymbolTableCollection &symbolTables, uint64_t alignment, Attribute memorySpace={})
void removeSymbol(Operation *op, BufferizationState &state)
std::optional< Operation * > findDealloc(Value allocValue)
Finds a single dealloc operation for the given allocated value.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...