[Mlir-commits] [mlir] [MLIR][Transforms] Fix Mem2Reg removal order to respect dominance (PR #68687)
Christian Ulmann
llvmlistbot at llvm.org
Tue Oct 10 04:42:54 PDT 2023
https://github.com/Dinistro updated https://github.com/llvm/llvm-project/pull/68687
>From 3739511149027060a1841ae71e3b7d0b2a3bb023 Mon Sep 17 00:00:00 2001
From: Christian Ulmann <christian.ulmann at nextsilicon.com>
Date: Tue, 10 Oct 2023 10:50:02 +0000
Subject: [PATCH 1/2] [MLIR][Transforms] Fix Mem2Reg removal order to respect
dominance
This commit fixes a bug in the Mem2Reg operation erasure order.
Replacing the topological order with a dominance based order ensures
that no operation is removed before all its uses have been replaced.
Additionally, the reliance on the `DenseMap` key order was eliminated by
switching to a `MapVector`, that gives a deterministic iteration order.
Example:
```
%ptr = alloca ...
...
%val0 = %load %ptr ... // LOAD0
store %val0 %ptr ...
%val1 = load %ptr ... // LOAD1
````
When promoting the slot backing %ptr, it can happen that the LOAD0
was cleaned before LOAD1. This results in all uses of LOAD0 being
replaced by its reaching definition, before LOAD1's result is replaced
by LOAD0's result. The subsequent erasure of LOAD0 can thus not
succeed, as it has remaining usages.
---
mlir/lib/Transforms/Mem2Reg.cpp | 28 +++++++++++++++------------
mlir/test/Dialect/LLVMIR/mem2reg.mlir | 13 +++++++++++++
2 files changed, 29 insertions(+), 12 deletions(-)
diff --git a/mlir/lib/Transforms/Mem2Reg.cpp b/mlir/lib/Transforms/Mem2Reg.cpp
index 65de25dd2f32663..0591d0541290d74 100644
--- a/mlir/lib/Transforms/Mem2Reg.cpp
+++ b/mlir/lib/Transforms/Mem2Reg.cpp
@@ -106,7 +106,7 @@ struct MemorySlotPromotionInfo {
/// its uses, it is because the defining ops of the blocking uses requested
/// it. The defining ops therefore must also have blocking uses or be the
/// starting point of the bloccking uses.
- DenseMap<Operation *, SmallPtrSet<OpOperand *, 4>> userToBlockingUses;
+ llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>> userToBlockingUses;
};
/// Computes information for basic slot promotion. This will check that direct
@@ -129,8 +129,9 @@ class MemorySlotPromotionAnalyzer {
/// uses (typically, removing its users because it will delete itself to
/// resolve its own blocking uses). This will fail if one of the transitive
/// users cannot remove a requested use, and should prevent promotion.
- LogicalResult computeBlockingUses(
- DenseMap<Operation *, SmallPtrSet<OpOperand *, 4>> &userToBlockingUses);
+ LogicalResult
+ computeBlockingUses(llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>>
+ &userToBlockingUses);
/// Computes in which blocks the value stored in the slot is actually used,
/// meaning blocks leading to a load. This method uses `definingBlocks`, the
@@ -233,7 +234,8 @@ Value MemorySlotPromoter::getLazyDefaultValue() {
}
LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
- DenseMap<Operation *, SmallPtrSet<OpOperand *, 4>> &userToBlockingUses) {
+ llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>>
+ &userToBlockingUses) {
// The promotion of an operation may require the promotion of further
// operations (typically, removing operations that use an operation that must
// delete itself). We thus need to start from the use of the slot pointer and
@@ -243,7 +245,7 @@ LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
// use it.
for (OpOperand &use : slot.ptr.getUses()) {
SmallPtrSet<OpOperand *, 4> &blockingUses =
- userToBlockingUses.getOrInsertDefault(use.getOwner());
+ userToBlockingUses[use.getOwner()];
blockingUses.insert(&use);
}
@@ -281,7 +283,7 @@ LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
assert(llvm::is_contained(user->getResults(), blockingUse->get()));
SmallPtrSetImpl<OpOperand *> &newUserBlockingUseSet =
- userToBlockingUses.getOrInsertDefault(blockingUse->getOwner());
+ userToBlockingUses[blockingUse->getOwner()];
newUserBlockingUseSet.insert(blockingUse);
}
}
@@ -516,14 +518,16 @@ void MemorySlotPromoter::computeReachingDefInRegion(Region *region,
}
void MemorySlotPromoter::removeBlockingUses() {
- llvm::SetVector<Operation *> usersToRemoveUses;
- for (auto &user : llvm::make_first_range(info.userToBlockingUses))
- usersToRemoveUses.insert(user);
- SetVector<Operation *> sortedUsersToRemoveUses =
- mlir::topologicalSort(usersToRemoveUses);
+ llvm::SmallVector<Operation *> usersToRemoveUses(
+ llvm::make_first_range(info.userToBlockingUses));
+ // The uses need to be traversed in *reverse dominance* order to ensure that
+ // transitive replacements are performed correctly.
+ llvm::sort(usersToRemoveUses, [&](Operation *a, Operation *b) {
+ return dominance.properlyDominates(b, a);
+ });
llvm::SmallVector<Operation *> toErase;
- for (Operation *toPromote : llvm::reverse(sortedUsersToRemoveUses)) {
+ for (Operation *toPromote : usersToRemoveUses) {
if (auto toPromoteMemOp = dyn_cast<PromotableMemOpInterface>(toPromote)) {
Value reachingDef = reachingDefs.lookup(toPromoteMemOp);
// If no reaching definition is known, this use is outside the reach of
diff --git a/mlir/test/Dialect/LLVMIR/mem2reg.mlir b/mlir/test/Dialect/LLVMIR/mem2reg.mlir
index 30ba459d07a49f3..ced9afffdc043d4 100644
--- a/mlir/test/Dialect/LLVMIR/mem2reg.mlir
+++ b/mlir/test/Dialect/LLVMIR/mem2reg.mlir
@@ -683,3 +683,16 @@ llvm.func @no_inner_alloca_promotion(%arg: i64) -> i64 {
// CHECK: llvm.return %[[RES]] : i64
llvm.return %2 : i64
}
+
+// -----
+
+// CHECK-LABEL: @transitive_reaching_def
+llvm.func @transitive_reaching_def() -> !llvm.ptr {
+ %0 = llvm.mlir.constant(1 : i32) : i32
+ // CHECK-NOT: alloca
+ %3 = llvm.alloca %0 x !llvm.ptr {alignment = 8 : i64} : (i32) -> !llvm.ptr
+ %6 = llvm.load %3 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
+ llvm.store %6, %3 {alignment = 8 : i64} : !llvm.ptr, !llvm.ptr
+ %7 = llvm.load %3 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
+ llvm.return %7 : !llvm.ptr
+}
>From 2d7401d9e905f69be476c1f846aa94068213893e Mon Sep 17 00:00:00 2001
From: Christian Ulmann <christian.ulmann at nextsilicon.com>
Date: Tue, 10 Oct 2023 11:42:39 +0000
Subject: [PATCH 2/2] address review comments
---
mlir/lib/Transforms/Mem2Reg.cpp | 16 ++++++++--------
mlir/test/Dialect/LLVMIR/mem2reg.mlir | 10 +++++-----
2 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/mlir/lib/Transforms/Mem2Reg.cpp b/mlir/lib/Transforms/Mem2Reg.cpp
index 0591d0541290d74..62246a87b291ac8 100644
--- a/mlir/lib/Transforms/Mem2Reg.cpp
+++ b/mlir/lib/Transforms/Mem2Reg.cpp
@@ -96,6 +96,9 @@ using namespace mlir;
namespace {
+using BlockingUsesMap =
+ llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>>;
+
/// Information computed during promotion analysis used to perform actual
/// promotion.
struct MemorySlotPromotionInfo {
@@ -106,7 +109,7 @@ struct MemorySlotPromotionInfo {
/// its uses, it is because the defining ops of the blocking uses requested
/// it. The defining ops therefore must also have blocking uses or be the
/// starting point of the bloccking uses.
- llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>> userToBlockingUses;
+ BlockingUsesMap userToBlockingUses;
};
/// Computes information for basic slot promotion. This will check that direct
@@ -129,9 +132,7 @@ class MemorySlotPromotionAnalyzer {
/// uses (typically, removing its users because it will delete itself to
/// resolve its own blocking uses). This will fail if one of the transitive
/// users cannot remove a requested use, and should prevent promotion.
- LogicalResult
- computeBlockingUses(llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>>
- &userToBlockingUses);
+ LogicalResult computeBlockingUses(BlockingUsesMap &userToBlockingUses);
/// Computes in which blocks the value stored in the slot is actually used,
/// meaning blocks leading to a load. This method uses `definingBlocks`, the
@@ -234,8 +235,7 @@ Value MemorySlotPromoter::getLazyDefaultValue() {
}
LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
- llvm::MapVector<Operation *, SmallPtrSet<OpOperand *, 4>>
- &userToBlockingUses) {
+ BlockingUsesMap &userToBlockingUses) {
// The promotion of an operation may require the promotion of further
// operations (typically, removing operations that use an operation that must
// delete itself). We thus need to start from the use of the slot pointer and
@@ -522,8 +522,8 @@ void MemorySlotPromoter::removeBlockingUses() {
llvm::make_first_range(info.userToBlockingUses));
// The uses need to be traversed in *reverse dominance* order to ensure that
// transitive replacements are performed correctly.
- llvm::sort(usersToRemoveUses, [&](Operation *a, Operation *b) {
- return dominance.properlyDominates(b, a);
+ llvm::sort(usersToRemoveUses, [&](Operation *lhs, Operation *rhs) {
+ return dominance.properlyDominates(rhs, lhs);
});
llvm::SmallVector<Operation *> toErase;
diff --git a/mlir/test/Dialect/LLVMIR/mem2reg.mlir b/mlir/test/Dialect/LLVMIR/mem2reg.mlir
index ced9afffdc043d4..32e3fed7e5485df 100644
--- a/mlir/test/Dialect/LLVMIR/mem2reg.mlir
+++ b/mlir/test/Dialect/LLVMIR/mem2reg.mlir
@@ -690,9 +690,9 @@ llvm.func @no_inner_alloca_promotion(%arg: i64) -> i64 {
llvm.func @transitive_reaching_def() -> !llvm.ptr {
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK-NOT: alloca
- %3 = llvm.alloca %0 x !llvm.ptr {alignment = 8 : i64} : (i32) -> !llvm.ptr
- %6 = llvm.load %3 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
- llvm.store %6, %3 {alignment = 8 : i64} : !llvm.ptr, !llvm.ptr
- %7 = llvm.load %3 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
- llvm.return %7 : !llvm.ptr
+ %1 = llvm.alloca %0 x !llvm.ptr {alignment = 8 : i64} : (i32) -> !llvm.ptr
+ %2 = llvm.load %1 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
+ llvm.store %2, %1 {alignment = 8 : i64} : !llvm.ptr, !llvm.ptr
+ %3 = llvm.load %1 {alignment = 8 : i64} : !llvm.ptr -> !llvm.ptr
+ llvm.return %3 : !llvm.ptr
}
More information about the Mlir-commits
mailing list