Skip to content

Commit 41da45c

Browse files
Lallapalloozamiguelcsx
authored andcommitted
[MLIR][MemRef] Add alloca support for erase_dead_alloc_and_stores (llvm#142131)
Previously, `erase_dead_alloc_and_stores` didn't support `memref.alloca`. This patch introduces support for it. --------- Signed-off-by: Vitalii Shutov <[email protected]>
1 parent 9235926 commit 41da45c

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

mlir/include/mlir/Dialect/MemRef/TransformOps/MemRefTransformOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def MemRefEraseDeadAllocAndStoresOp
245245
]> {
246246
let description = [{
247247
This applies memory optimization on memref. In particular it does store to
248-
load forwarding, dead store elimination and dead alloc elimination.
248+
load forwarding, dead store elimination and dead alloc/alloca elimination.
249249

250250
#### Return modes
251251

mlir/lib/Dialect/MemRef/Utils/MemRefUtils.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ static bool resultIsNotRead(Operation *op, std::vector<Operation *> &uses) {
156156

157157
void eraseDeadAllocAndStores(RewriterBase &rewriter, Operation *parentOp) {
158158
std::vector<Operation *> opToErase;
159-
parentOp->walk([&](memref::AllocOp op) {
159+
parentOp->walk([&](Operation *op) {
160160
std::vector<Operation *> candidates;
161-
if (resultIsNotRead(op, candidates)) {
161+
if (isa<memref::AllocOp, memref::AllocaOp>(op) &&
162+
resultIsNotRead(op, candidates)) {
162163
llvm::append_range(opToErase, candidates);
163-
opToErase.push_back(op.getOperation());
164+
opToErase.push_back(op);
164165
}
165166
});
167+
166168
for (Operation *op : opToErase)
167169
rewriter.eraseOp(op);
168170
}

mlir/test/Dialect/MemRef/transform-ops.mlir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,30 @@ module attributes {transform.with_named_sequence} {
327327
}
328328
}
329329

330+
// -----
331+
332+
// CHECK-LABEL: func.func @dead_alloca
333+
func.func @dead_alloca() {
334+
// CHECK-NOT: %{{.+}} = memref.alloca
335+
%0 = memref.alloca() : memref<8x64xf32, 3>
336+
%1 = memref.subview %0[0, 0] [8, 4] [1, 1] : memref<8x64xf32, 3> to
337+
memref<8x4xf32, affine_map<(d0, d1) -> (d0 * 64 + d1)>, 3>
338+
%c0 = arith.constant 0 : index
339+
%cst_0 = arith.constant dense<0.000000e+00> : vector<1x4xf32>
340+
vector.transfer_write %cst_0, %1[%c0, %c0] {in_bounds = [true, true]} :
341+
vector<1x4xf32>, memref<8x4xf32, affine_map<(d0, d1) -> (d0 * 64 + d1)>, 3>
342+
return
343+
}
344+
345+
module attributes {transform.with_named_sequence} {
346+
transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
347+
%0 = transform.structured.match ops{["func.func"]} in %arg1 : (!transform.any_op) -> !transform.any_op
348+
transform.memref.erase_dead_alloc_and_stores %0 : (!transform.any_op) -> ()
349+
transform.yield
350+
}
351+
}
352+
353+
330354
// -----
331355

332356
// CHECK-LABEL: @store_to_load

0 commit comments

Comments
 (0)