15 #include "llvm/Support/DebugLog.h"
18 #define GEN_PASS_DEF_SROA
19 #include "mlir/Transforms/Passes.h.inc"
22 #define DEBUG_TYPE "sroa"
31 struct MemorySlotDestructuringInfo {
47 static std::optional<MemorySlotDestructuringInfo>
50 assert(isa<DestructurableTypeInterface>(slot.
elemType));
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);
104 auto it = info.userToBlockingUses.find(user);
105 if (it == info.userToBlockingUses.end())
109 auto promotable = dyn_cast<PromotableOpInterface>(user);
119 if (!promotable.canUsesBeRemoved(blockingUses, newBlockingUses, dataLayout))
123 for (
OpOperand *blockingUse : newBlockingUses) {
124 assert(llvm::is_contained(user->getResults(), blockingUse->get()));
127 info.userToBlockingUses[blockingUse->getOwner()];
128 newUserBlockingUseSet.insert(blockingUse);
140 DestructurableAllocationOpInterface allocator,
OpBuilder &builder,
141 const DataLayout &dataLayout, MemorySlotDestructuringInfo &info,
148 allocator.destructure(slot, info.usedIndices, builder, newAllocators);
158 usersToRewire.insert_range(llvm::make_first_range(info.userToBlockingUses));
159 usersToRewire.insert_range(info.accessors);
163 for (
Operation *toRewire : llvm::reverse(usersToRewire)) {
165 if (
auto accessor = dyn_cast<DestructurableAccessorOpInterface>(toRewire)) {
166 if (accessor.rewire(slot, subslots, builder, dataLayout) ==
168 toErase.push_back(accessor);
172 auto promotable = cast<PromotableOpInterface>(toRewire);
173 if (promotable.removeBlockingUses(info.userToBlockingUses[promotable],
175 toErase.push_back(promotable);
181 assert(slot.
ptr.
use_empty() &&
"after destructuring, the original slot "
182 "pointer should no longer be used");
184 LDBG() <<
"Destructured memory slot: " << slot.
ptr;
189 std::optional<DestructurableAllocationOpInterface> newAllocator =
190 allocator.handleDestructuringComplete(slot, builder);
193 newAllocators.push_back(*newAllocator);
200 bool destructuredAny =
false;
204 newWorkList.reserve(allocators.size());
208 bool changesInThisRound =
false;
210 for (DestructurableAllocationOpInterface allocator : workList) {
211 bool destructuredAnySlot =
false;
213 std::optional<MemorySlotDestructuringInfo> info =
219 newWorkList, statistics);
220 destructuredAnySlot =
true;
226 if (!destructuredAnySlot)
227 newWorkList.push_back(allocator);
228 changesInThisRound |= destructuredAnySlot;
231 if (!changesInThisRound)
233 destructuredAny |= changesInThisRound;
237 workList.swap(newWorkList);
241 return success(destructuredAny);
246 struct SROA :
public impl::SROABase<SROA> {
247 using impl::SROABase<SROA>::SROABase;
249 void runOnOperation()
override {
252 SROAStatistics statistics{&destructuredAmount, &slotsWithMemoryBenefit,
253 &maxSubelementAmount};
255 auto &dataLayoutAnalysis = getAnalysis<DataLayoutAnalysis>();
256 const DataLayout &dataLayout = dataLayoutAnalysis.getAtOrAbove(scopeOp);
260 if (region.getBlocks().empty())
263 OpBuilder builder(®ion.front(), region.front().begin());
267 region.walk([&](DestructurableAllocationOpInterface allocator) {
268 allocators.emplace_back(allocator);
277 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.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
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.
const FrozenRewritePatternSet GreedyRewriteConfig bool * changed
@ Delete
Delete the operation after promotion.
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.
Value ptr
Pointer to the memory slot, used by operations to refer to it.
Type elemType
Type of the value contained in the slot.
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.