21 #include "llvm/ADT/SetOperations.h"
22 #include "llvm/ADT/SmallString.h"
35 Block *placementBlock,
44 if (startOperation->
getBlock() != placementBlock) {
47 startOperation = opInPlacementBlock ? opInPlacementBlock
51 return startOperation;
59 void BufferPlacementAllocs::build(
Operation *op) {
60 op->
walk([&](MemoryEffectOpInterface opInterface) {
63 opInterface.getEffects(effects);
67 effects, std::back_inserter(allocateResultEffects),
70 return isa<MemoryEffects::Allocate>(it.
getEffect()) && value &&
77 if (allocateResultEffects.size() != 1)
80 Value allocValue = allocateResultEffects[0].getValue();
86 allocs.push_back(std::make_tuple(allocValue, *dealloc));
97 : aliases(op), allocs(op), liveness(op) {}
106 if (isa<LoopLikeOpInterface>(op))
111 RegionBranchOpInterface regionInterface;
112 if (!(regionInterface = dyn_cast<RegionBranchOpInterface>(op)))
118 std::function<bool(
Region *)> recurse = [&](
Region *current) {
122 if (!visitedRegions.insert(current).second)
126 regionInterface.getSuccessorRegions(current->getRegionNumber(), successors);
128 if (recurse(regionEntry.getSuccessor()))
135 regionInterface.getSuccessorRegions(std::nullopt, successorRegions);
137 if (recurse(regionEntry.getSuccessor()))
139 visitedRegions.clear();
151 auto type = constantOp.getType().cast<RankedTensorType>();
152 auto moduleOp = constantOp->getParentOfType<ModuleOp>();
159 auto globalOp = dyn_cast<memref::GlobalOp>(&op);
162 if (!globalOp.getInitialValue().has_value())
164 uint64_t opAlignment = globalOp.getAlignment().value_or(0);
165 Attribute initialValue = globalOp.getInitialValue().value();
166 if (opAlignment == alignment && initialValue == constantOp.getValue())
172 OpBuilder globalBuilder(moduleOp.getContext());
177 llvm::raw_svector_ostream os(buf);
178 interleave(type.getShape(), os,
"x");
179 os <<
"x" << type.getElementType();
182 IntegerAttr memrefAlignment =
183 alignment > 0 ? IntegerAttr::get(globalBuilder.
getI64Type(), alignment)
187 auto global = globalBuilder.
create<memref::GlobalOp>(
188 constantOp.getLoc(), (Twine(
"__constant_") + os.str()).str(),
190 typeConverter.
convertType(type).cast<MemRefType>(),
191 constantOp.getValue().cast<ElementsAttr>(),
194 symbolTable.
insert(global);
197 global->moveBefore(&moduleOp.front());
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 provides support for representing a failure result, or a valid value of type T.
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 class helps build Operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
This is a value defined by a result of an operation.
Operation is the basic unit of execution within MLIR.
Block * getBlock()
Returns the operation block that contains this operation.
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
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),...
This class represents a successor of a region.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
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 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.
LogicalResult convertType(Type t, SmallVectorImpl< Type > &results)
Convert the given type.
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.
A helper type converter class that automatically populates the relevant materializations and type con...
FailureOr< memref::GlobalOp > getGlobalFor(arith::ConstantOp constantOp, uint64_t alignment)
std::optional< Operation * > findDealloc(Value allocValue)
Finds a single dealloc operation for the given allocated value.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.