15#include "llvm/Support/DebugLog.h"
18#define GEN_PASS_DEF_SROA
19#include "mlir/Transforms/Passes.h.inc"
22#define DEBUG_TYPE "sroa"
31struct MemorySlotDestructuringInfo {
47static std::optional<MemorySlotDestructuringInfo>
50 assert(isa<DestructurableTypeInterface>(slot.
valueType));
55 MemorySlotDestructuringInfo info;
59 auto scheduleAsBlockingUse = [&](
OpOperand &use) {
61 info.userToBlockingUses[use.getOwner()];
62 blockingUses.insert(&use);
68 dyn_cast<DestructurableAccessorOpInterface>(use.getOwner())) {
69 if (accessor.canRewire(slot, info.usedIndices, usedSafelyWorklist,
71 info.accessors.push_back(accessor);
78 scheduleAsBlockingUse(use);
82 while (!usedSafelyWorklist.empty()) {
83 MemorySlot mustBeUsedSafely = usedSafelyWorklist.pop_back_val();
85 if (!visited.insert(&subslotUse).second)
87 Operation *subslotUser = subslotUse.getOwner();
89 if (
auto memOp = dyn_cast<SafeMemorySlotAccessOpInterface>(subslotUser))
90 if (succeeded(memOp.ensureOnlySafeAccesses(
91 mustBeUsedSafely, usedSafelyWorklist, dataLayout)))
96 scheduleAsBlockingUse(subslotUse);
100 if (info.userToBlockingUses.empty())
107 auto it = info.userToBlockingUses.find(user);
108 if (it == info.userToBlockingUses.end())
112 auto promotable = dyn_cast<PromotableOpInterface>(user);
122 if (!promotable.canUsesBeRemoved(blockingUses, newBlockingUses, dataLayout))
126 for (
OpOperand *blockingUse : newBlockingUses) {
127 assert(llvm::is_contained(user->getResults(), blockingUse->get()));
130 info.userToBlockingUses[blockingUse->getOwner()];
131 newUserBlockingUseSet.insert(blockingUse);
143 DestructurableAllocationOpInterface allocator,
OpBuilder &builder,
144 const DataLayout &dataLayout, MemorySlotDestructuringInfo &info,
151 allocator.destructure(slot, info.usedIndices, builder, newAllocators);
161 usersToRewire.insert_range(llvm::make_first_range(info.userToBlockingUses));
162 usersToRewire.insert_range(info.accessors);
166 bool needsTopologicalSort =
167 !info.userToBlockingUses.empty() ||
168 llvm::any_of(info.accessors, [](DestructurableAccessorOpInterface op) {
169 return op->getNumRegions() > 0;
171 if (needsTopologicalSort)
175 for (
Operation *toRewire : llvm::reverse(usersToRewire)) {
177 if (
auto accessor = dyn_cast<DestructurableAccessorOpInterface>(toRewire)) {
178 if (accessor.rewire(slot, subslots, builder, dataLayout) ==
180 toErase.push_back(accessor);
184 auto promotable = cast<PromotableOpInterface>(toRewire);
185 if (promotable.removeBlockingUses(info.userToBlockingUses[promotable],
187 toErase.push_back(promotable);
193 assert(slot.
ptr.
use_empty() &&
"after destructuring, the original slot "
194 "pointer should no longer be used");
196 LDBG() <<
"Destructured memory slot: " << slot.
ptr;
201 std::optional<DestructurableAllocationOpInterface> newAllocator =
202 allocator.handleDestructuringComplete(slot, builder);
205 newAllocators.push_back(*newAllocator);
212 bool destructuredAny =
false;
216 newWorkList.reserve(allocators.size());
220 bool changesInThisRound =
false;
222 for (DestructurableAllocationOpInterface allocator : workList) {
223 bool destructuredAnySlot =
false;
225 std::optional<MemorySlotDestructuringInfo> info =
231 newWorkList, statistics);
232 destructuredAnySlot =
true;
238 if (!destructuredAnySlot)
239 newWorkList.push_back(allocator);
240 changesInThisRound |= destructuredAnySlot;
243 if (!changesInThisRound)
245 destructuredAny |= changesInThisRound;
249 workList.swap(newWorkList);
253 return success(destructuredAny);
261 void runOnOperation()
override {
264 SROAStatistics statistics{&destructuredAmount, &slotsWithMemoryBenefit,
265 &maxSubelementAmount};
267 auto &dataLayoutAnalysis = getAnalysis<DataLayoutAnalysis>();
268 const DataLayout &dataLayout = dataLayoutAnalysis.getAtOrAbove(scopeOp);
269 bool changed =
false;
271 for (Region ®ion : scopeOp->
getRegions()) {
272 if (region.getBlocks().empty())
275 OpBuilder builder(®ion.front(), region.front().begin());
277 SmallVector<DestructurableAllocationOpInterface> allocators;
279 region.walk([&](DestructurableAllocationOpInterface allocator) {
280 allocators.emplace_back(allocator);
289 markAllAnalysesPreserved();
static void destructureSlot(DestructurableMemorySlot &slot, DestructurableAllocationOpInterface allocator, OpBuilder &builder, const DataLayout &dataLayout, MemorySlotDestructuringInfo &info, SmallVectorImpl< DestructurableAllocationOpInterface > &newAllocators, const SROAStatistics &statistics)
Performs the destructuring of a destructible slot given associated destructuring information.
static std::optional< MemorySlotDestructuringInfo > computeDestructuringInfo(DestructurableMemorySlot &slot, const DataLayout &dataLayout)
Computes information for slot destructuring.
The main mechanism for performing data layout queries.
RAII guard to reset the insertion point of the builder when destroyed.
This class helps build Operations.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
void setInsertionPointAfter(Operation *op)
Sets the insertion point to the node after the specified operation, which will cause subsequent inser...
This class represents an operand of an operation.
Operation is the basic unit of execution within MLIR.
MutableArrayRef< Region > getRegions()
Returns the regions held by this operation.
bool use_empty() const
Returns true if this value has no uses.
use_range getUses() const
Returns a range of all uses, which is useful for iterating over all uses.
Block * getParentBlock()
Return the Block in which this Value is defined.
Include the generated interface declarations.
llvm::SetVector< T, Vector, Set, N > SetVector
@ Delete
Delete the operation after promotion.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
LogicalResult tryToDestructureMemorySlots(ArrayRef< DestructurableAllocationOpInterface > allocators, OpBuilder &builder, const DataLayout &dataLayout, SROAStatistics statistics={})
Attempts to destructure the slots of destructurable allocators.
SetVector< Operation * > topologicalSort(const SetVector< Operation * > &toSort)
Sorts all operations in toSort topologically while also considering region semantics.
void getForwardSlice(Operation *op, SetVector< Operation * > *forwardSlice, const ForwardSliceOptions &options={})
Fills forwardSlice with the computed forward slice (i.e.
Memory slot attached with information about its destructuring procedure.
DenseMap< Attribute, Type > subelementTypes
Maps an index within the memory slot to the corresponding subelement type.
Represents a slot in memory.
Type valueType
Type of the value contained in the slot.
Value ptr
Pointer to the memory slot, used by operations to refer to it.
Statistics collected while applying SROA.
llvm::Statistic * maxSubelementAmount
Maximal number of sub-elements a successfully destructured slot initially had.
llvm::Statistic * slotsWithMemoryBenefit
Total amount of memory slots in which the destructured size was smaller than the total size after eli...
llvm::Statistic * destructuredAmount
Total amount of memory slots destructured.