Skip to content

Commit 1be9a80

Browse files
authored
[mlir][affine] Fix the crash due to the simultaneous replacement store (#90829)
`AffineScalarReplacement` should forward the memref store op to load op only if the store op reaches the load. But it now checks the reachability only if these ops are in the same block, which causes the crash reported in #76309. We need to check the reachability even if they are both in the same block, which rescues the case where consecutive store operations are written before the load op.
1 parent e0a93d3 commit 1be9a80

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

mlir/lib/Dialect/Affine/Utils/Utils.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,7 @@ static void forwardStoreToLoad(
860860
// 3. The store must reach the load. Access function equivalence only
861861
// guarantees this for accesses in the same block. The load could be in a
862862
// nested block that is unreachable.
863-
if (storeOp->getBlock() != loadOp->getBlock() &&
864-
!mustReachAtInnermost(srcAccess, destAccess))
863+
if (!mustReachAtInnermost(srcAccess, destAccess))
865864
continue;
866865

867866
// 4. Ensure there is no intermediate operation which could replace the

mlir/test/Dialect/Affine/scalrep.mlir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// CHECK-DAG: [[$MAP2:#map[0-9]*]] = affine_map<(d0, d1) -> (d1)>
66
// CHECK-DAG: [[$MAP3:#map[0-9]*]] = affine_map<(d0, d1) -> (d0 - 1)>
77
// CHECK-DAG: [[$MAP4:#map[0-9]*]] = affine_map<(d0) -> (d0 + 1)>
8+
// CHECK-DAG: [[$IDENT:#map[0-9]*]] = affine_map<(d0) -> (d0)>
89

910
// CHECK-LABEL: func @simple_store_load() {
1011
func.func @simple_store_load() {
@@ -931,3 +932,23 @@ func.func @cross_block() {
931932
%69 = affine.load %alloc_99[%c10] : memref<13xi1>
932933
return
933934
}
935+
936+
#map1 = affine_map<(d0) -> (d0)>
937+
938+
// CHECK-LABEL: func @consecutive_store
939+
func.func @consecutive_store() {
940+
// CHECK: %[[CST:.*]] = arith.constant
941+
%tmp = arith.constant 1.1 : f16
942+
// CHECK: %[[ALLOC:.*]] = memref.alloc
943+
%alloc_66 = memref.alloc() : memref<f16, 1>
944+
affine.for %arg2 = 4 to 6 {
945+
affine.for %arg3 = #map1(%arg2) to #map1(%arg2) step 4 {
946+
// CHECK: affine.store %[[CST]], %[[ALLOC]][]
947+
affine.store %tmp, %alloc_66[] : memref<f16, 1>
948+
// CHECK-NOT: affine.store %[[CST]], %[[ALLOC]][]
949+
affine.store %tmp, %alloc_66[] : memref<f16, 1>
950+
%270 = affine.load %alloc_66[] : memref<f16, 1>
951+
}
952+
}
953+
return
954+
}

0 commit comments

Comments
 (0)