diff --git a/mlir/lib/Dialect/Affine/Utils/Utils.cpp b/mlir/lib/Dialect/Affine/Utils/Utils.cpp --- a/mlir/lib/Dialect/Affine/Utils/Utils.cpp +++ b/mlir/lib/Dialect/Affine/Utils/Utils.cpp @@ -1029,6 +1029,15 @@ // A list of memref's that are potentially dead / could be eliminated. SmallPtrSet memrefsToErase; + // Walk all store's and perform unused store elimination + f.walk([&](AffineWriteOpInterface storeOp) { + findUnusedStore(storeOp, opsToErase, postDomInfo); + }); + // Erase all store op's which don't impact the program + for (auto *op : opsToErase) + op->erase(); + opsToErase.clear(); + // Walk all load's and perform store to load forwarding. f.walk([&](AffineReadOpInterface loadOp) { if (failed( @@ -1042,14 +1051,6 @@ op->erase(); opsToErase.clear(); - // Walk all store's and perform unused store elimination - f.walk([&](AffineWriteOpInterface storeOp) { - findUnusedStore(storeOp, opsToErase, postDomInfo); - }); - // Erase all store op's which don't impact the program - for (auto *op : opsToErase) - op->erase(); - // Check if the store fwd'ed memrefs are now left with only stores and // deallocs and can thus be completely deleted. Note: the canonicalize pass // should be able to do this as well, but we'll do it here since we collected diff --git a/mlir/test/Dialect/Affine/scalrep.mlir b/mlir/test/Dialect/Affine/scalrep.mlir --- a/mlir/test/Dialect/Affine/scalrep.mlir +++ b/mlir/test/Dialect/Affine/scalrep.mlir @@ -846,3 +846,25 @@ // CHECK-NEXT: } // CHECK-NEXT: return } + +// CHECK-LABEL: func @redundant_store_op() -> memref<15xi1> { +func.func @redundant_store_op() -> memref<15xi1> { + %c1 = arith.constant 1 : index + %alloc = memref.alloc() : memref<15xi1> + %true = arith.constant true + affine.store %true, %alloc[%c1] : memref<15xi1> + affine.store %true, %alloc[%c1] : memref<15xi1> + %c2 = arith.constant 2 : index + affine.if affine_set<(d0, d1) : ((d0 + 1) mod 8 >= 0, d0 * -8 >= 0)>(%c1, %c1){ + %load1 = affine.load %alloc[%c1] : memref<15xi1> + } + %m1 = memref.alloc() : memref<15xi1> + return %m1 : memref<15xi1> +// CHECK: %[[C1:.*]] = arith.constant 1 : index +// CHECK-NEXT: %true = arith.constant true +// CHECK-NEXT: %[[C2:.*]] = arith.constant 2 : index +// CHECK-NEXT: affine.if #set(%[[C1]], %[[C1]]) { +// CHECK-NEXT: } +// CHECK-NEXT: %[[MEM:.*]] = memref.alloc() : memref<15xi1> +// CHECK-NEXT: return %[[MEM]] : memref<15xi1> +}