Skip to content

Commit

Permalink
[MLIR] Don't remove memref allocation if stored into another allocation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wsmoses committed Jun 28, 2021
1 parent 75cacc6 commit cccc7e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ struct SimplifyDeadAlloc : public OpRewritePattern<T> {

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

Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Dialect/MemRef/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,13 @@ func @alloc_const_fold_with_symbols2() -> memref<?xi32, #map0> {
%0 = memref.alloc(%c1)[%c1, %c1] : memref<?xi32, #map0>
return %0 : memref<?xi32, #map0>
}

// -----
// CHECK-LABEL: func @allocator
// CHECK: %[[alloc:.+]] = memref.alloc
// CHECK: memref.store %[[alloc:.+]], %arg0
func @allocator(%arg0 : memref<memref<?xi32>>, %arg1 : index) {
%0 = memref.alloc(%arg1) : memref<?xi32>
memref.store %0, %arg0[] : memref<memref<?xi32>>
return
}

0 comments on commit cccc7e5

Please sign in to comment.