Skip to content

Commit cccc7e5

Browse files
committed
[MLIR] Don't remove memref allocation if stored into another allocation
A canonicalization accidentally will remove a memref allocation if it is only stored into. However, this is incorrect if the allocation is the value being stored, not the allocation being stored into. Differential Revision: https://reviews.llvm.org/D104947
1 parent 75cacc6 commit cccc7e5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ struct SimplifyDeadAlloc : public OpRewritePattern<T> {
174174

175175
LogicalResult matchAndRewrite(T alloc,
176176
PatternRewriter &rewriter) const override {
177-
if (llvm::any_of(alloc->getUsers(), [](Operation *op) {
178-
return !isa<StoreOp, DeallocOp>(op);
177+
if (llvm::any_of(alloc->getUsers(), [&](Operation *op) {
178+
if (auto storeOp = dyn_cast<StoreOp>(op))
179+
return storeOp.value() == alloc;
180+
return !isa<DeallocOp>(op);
179181
}))
180182
return failure();
181183

mlir/test/Dialect/MemRef/canonicalize.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,13 @@ func @alloc_const_fold_with_symbols2() -> memref<?xi32, #map0> {
420420
%0 = memref.alloc(%c1)[%c1, %c1] : memref<?xi32, #map0>
421421
return %0 : memref<?xi32, #map0>
422422
}
423+
424+
// -----
425+
// CHECK-LABEL: func @allocator
426+
// CHECK: %[[alloc:.+]] = memref.alloc
427+
// CHECK: memref.store %[[alloc:.+]], %arg0
428+
func @allocator(%arg0 : memref<memref<?xi32>>, %arg1 : index) {
429+
%0 = memref.alloc(%arg1) : memref<?xi32>
430+
memref.store %0, %arg0[] : memref<memref<?xi32>>
431+
return
432+
}

0 commit comments

Comments
 (0)