Skip to content

Commit 660a78f

Browse files
committed
Revert "Reland: [MLIR][Transforms] Fix Mem2Reg removal order to respect dominance (#68767)"
This reverts commit 59fec73. This reland did not properly fix the partial order.
1 parent 4313351 commit 660a78f

File tree

2 files changed

+12
-34
lines changed

2 files changed

+12
-34
lines changed

mlir/lib/Transforms/Mem2Reg.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ using namespace mlir;
9696

9797
namespace {
9898

99-
using BlockingUsesMap =
100-
llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>>;
101-
10299
/// Information computed during promotion analysis used to perform actual
103100
/// promotion.
104101
struct MemorySlotPromotionInfo {
@@ -109,7 +106,7 @@ struct MemorySlotPromotionInfo {
109106
/// its uses, it is because the defining ops of the blocking uses requested
110107
/// it. The defining ops therefore must also have blocking uses or be the
111108
/// starting point of the bloccking uses.
112-
BlockingUsesMap userToBlockingUses;
109+
DenseMap<Operation *, SmallPtrSet<OpOperand *, 4>> userToBlockingUses;
113110
};
114111

115112
/// Computes information for basic slot promotion. This will check that direct
@@ -132,7 +129,8 @@ class MemorySlotPromotionAnalyzer {
132129
/// uses (typically, removing its users because it will delete itself to
133130
/// resolve its own blocking uses). This will fail if one of the transitive
134131
/// users cannot remove a requested use, and should prevent promotion.
135-
LogicalResult computeBlockingUses(BlockingUsesMap &userToBlockingUses);
132+
LogicalResult computeBlockingUses(
133+
DenseMap<Operation *, SmallPtrSet<OpOperand *, 4>> &userToBlockingUses);
136134

137135
/// Computes in which blocks the value stored in the slot is actually used,
138136
/// meaning blocks leading to a load. This method uses `definingBlocks`, the
@@ -235,7 +233,7 @@ Value MemorySlotPromoter::getLazyDefaultValue() {
235233
}
236234

237235
LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
238-
BlockingUsesMap &userToBlockingUses) {
236+
DenseMap<Operation *, SmallPtrSet<OpOperand *, 4>> &userToBlockingUses) {
239237
// The promotion of an operation may require the promotion of further
240238
// operations (typically, removing operations that use an operation that must
241239
// delete itself). We thus need to start from the use of the slot pointer and
@@ -245,7 +243,7 @@ LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
245243
// use it.
246244
for (OpOperand &use : slot.ptr.getUses()) {
247245
SmallPtrSet<OpOperand *, 4> &blockingUses =
248-
userToBlockingUses[use.getOwner()];
246+
userToBlockingUses.getOrInsertDefault(use.getOwner());
249247
blockingUses.insert(&use);
250248
}
251249

@@ -283,7 +281,7 @@ LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
283281
assert(llvm::is_contained(user->getResults(), blockingUse->get()));
284282

285283
SmallPtrSetImpl<OpOperand *> &newUserBlockingUseSet =
286-
userToBlockingUses[blockingUse->getOwner()];
284+
userToBlockingUses.getOrInsertDefault(blockingUse->getOwner());
287285
newUserBlockingUseSet.insert(blockingUse);
288286
}
289287
}
@@ -518,21 +516,14 @@ void MemorySlotPromoter::computeReachingDefInRegion(Region *region,
518516
}
519517

520518
void MemorySlotPromoter::removeBlockingUses() {
521-
llvm::SmallVector<Operation *> usersToRemoveUses(
522-
llvm::make_first_range(info.userToBlockingUses));
523-
524-
// The uses need to be traversed in *reverse dominance* order to ensure that
525-
// transitive replacements are performed correctly.
526-
// NOTE: The order can be non-deterministic, due to a pointer comparision, but
527-
// this has no effect on the result of the pattern. This is necessary to get a
528-
// strict weak order relation.
529-
llvm::sort(usersToRemoveUses, [&](Operation *lhs, Operation *rhs) {
530-
return dominance.properlyDominates(rhs, lhs) ||
531-
(!dominance.properlyDominates(lhs, rhs) && rhs < lhs);
532-
});
519+
llvm::SetVector<Operation *> usersToRemoveUses;
520+
for (auto &user : llvm::make_first_range(info.userToBlockingUses))
521+
usersToRemoveUses.insert(user);
522+
SetVector<Operation *> sortedUsersToRemoveUses =
523+
mlir::topologicalSort(usersToRemoveUses);
533524

534525
llvm::SmallVector<Operation *> toErase;
535-
for (Operation *toPromote : usersToRemoveUses) {
526+
for (Operation *toPromote : llvm::reverse(sortedUsersToRemoveUses)) {
536527
if (auto toPromoteMemOp = dyn_cast<PromotableMemOpInterface>(toPromote)) {
537528
Value reachingDef = reachingDefs.lookup(toPromoteMemOp);
538529
// If no reaching definition is known, this use is outside the reach of

mlir/test/Dialect/LLVMIR/mem2reg.mlir

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -683,16 +683,3 @@ llvm.func @no_inner_alloca_promotion(%arg: i64) -> i64 {
683683
// CHECK: llvm.return %[[RES]] : i64
684684
llvm.return %2 : i64
685685
}
686-
687-
// -----
688-
689-
// CHECK-LABEL: @transitive_reaching_def
690-
llvm.func @transitive_reaching_def() -> !llvm.ptr {
691-
%0 = llvm.mlir.constant(1 : i32) : i32
692-
// CHECK-NOT: alloca
693-
%1 = llvm.alloca %0 x !llvm.ptr {alignment = 8 : i64} : (i32) -> !llvm.ptr
694-
%2 = llvm.load %1 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
695-
llvm.store %2, %1 {alignment = 8 : i64} : !llvm.ptr, !llvm.ptr
696-
%3 = llvm.load %1 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
697-
llvm.return %3 : !llvm.ptr
698-
}

0 commit comments

Comments
 (0)