Skip to content

Commit b1752dd

Browse files
[mlir][sparse] Fix memory leaks (part 4) (#85729)
This commit fixes memory leaks in sparse tensor integration tests by adding `bufferization.dealloc_tensor` ops. Note: Buffer deallocation will be automated in the future with the ownership-based buffer deallocation pass, making `dealloc_tensor` obsolete (only codegen path, not when using the runtime library). This commit fixes the remaining memory leaks in the MLIR test suite. `check-mlir` now passes when built with ASAN.
1 parent ff60a84 commit b1752dd

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

mlir/test/Integration/Dialect/SparseTensor/CPU/reshape_dot.mlir

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
#COO_3D = #sparse_tensor.encoding<{ map = (d0, d1, d2) -> (d0 : compressed(nonunique), d1 : singleton(nonunique), d2 : singleton), posWidth = 32, crdWidth = 32 }>
3636

3737
module {
38-
func.func private @printMemref3dF32(%ptr : tensor<?x?x?xf32>) attributes { llvm.emit_c_interface }
39-
func.func private @printMemref2dF32(%ptr : tensor<?x?xf32>) attributes { llvm.emit_c_interface }
38+
func.func private @printMemref3dF32(%ptr : tensor<?x?x?xf32> {bufferization.access = "read"}) attributes { llvm.emit_c_interface }
39+
func.func private @printMemref2dF32(%ptr : tensor<?x?xf32> {bufferization.access = "read"}) attributes { llvm.emit_c_interface }
4040

4141
func.func @test_sparse_rhs(%arg0: tensor<5x6xf32>, %arg1: tensor<6x2x3xf32, #COO_3D>) -> tensor<?x?x?xf32> {
4242
%collapsed = tensor.collapse_shape %arg1 [[0], [1, 2]] : tensor<6x2x3xf32, #COO_3D> into tensor<6x6xf32, #COO_2D>
@@ -46,6 +46,11 @@ module {
4646
%2 = linalg.matmul ins(%arg0, %collapsed : tensor<5x6xf32>, tensor<6x6xf32, #COO_2D>) outs(%1 : tensor<5x6xf32>) -> tensor<5x6xf32>
4747
%expanded = tensor.expand_shape %2 [[0], [1, 2]] : tensor<5x6xf32> into tensor<5x2x3xf32>
4848
%ret1 = tensor.cast %expanded : tensor<5x2x3xf32> to tensor<?x?x?xf32>
49+
50+
// Note: tensor.collapse_shape is a metadata-only operation on dense tensors
51+
// but requires reallocation on sparse tensors.
52+
bufferization.dealloc_tensor %collapsed : tensor<6x6xf32, #COO_2D>
53+
4954
return %ret1 : tensor<?x?x?xf32>
5055
}
5156

@@ -57,6 +62,11 @@ module {
5762
%2 = linalg.matmul ins(%arg0, %collapsed : tensor<5x6xf32, #COO_2D>, tensor<6x6xf32, #COO_2D>) outs(%1 : tensor<5x6xf32>) -> tensor<5x6xf32>
5863
%expanded = tensor.expand_shape %2 [[0], [1, 2]] : tensor<5x6xf32> into tensor<5x2x3xf32>
5964
%ret1 = tensor.cast %expanded : tensor<5x2x3xf32> to tensor<?x?x?xf32>
65+
66+
// Note: tensor.collapse_shape is a metadata-only operation on dense tensors
67+
// but requires reallocation on sparse tensors.
68+
bufferization.dealloc_tensor %collapsed : tensor<6x6xf32, #COO_2D>
69+
6070
return %ret1 : tensor<?x?x?xf32>
6171
}
6272

@@ -80,6 +90,11 @@ module {
8090
%2 = linalg.matmul ins(%arg0, %collapsed : tensor<5x6xf32, #COO_2D>, tensor<6x6xf32, #COO_2D>) outs(%1 : tensor<5x6xf32>) -> tensor<5x6xf32>
8191
%expanded = tensor.expand_shape %2 [[0], [1, 2]] : tensor<5x6xf32> into tensor<5x2x3xf32>
8292
%ret1 = tensor.cast %expanded : tensor<5x2x3xf32> to tensor<?x?x?xf32>
93+
94+
// Note: tensor.collapse_shape is a metadata-only operation on dense tensors
95+
// but requires reallocation on sparse tensors.
96+
bufferization.dealloc_tensor %collapsed : tensor<6x6xf32, #COO_2D>
97+
8398
return %ret1 : tensor<?x?x?xf32>
8499
}
85100

@@ -192,6 +207,7 @@ module {
192207
bufferization.dealloc_tensor %so1 : tensor<?x?x?xf32>
193208
bufferization.dealloc_tensor %so2 : tensor<?x?x?xf32>
194209
bufferization.dealloc_tensor %so3 : tensor<?x?x?xf32>
210+
195211
return
196212
}
197213
}

mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_block_matmul.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ module {
161161
call @dump_dense_f64(%s24) : (tensor<4x4xf64>) -> ()
162162
call @dump_dense_f64(%scsr) : (tensor<4x4xf64>) -> ()
163163

164+
bufferization.dealloc_tensor %a : tensor<4x8xf64, #BSR>
165+
bufferization.dealloc_tensor %b : tensor<4x8xf64, #NV_24>
166+
bufferization.dealloc_tensor %c : tensor<4x8xf64, #CSR>
167+
bufferization.dealloc_tensor %d : tensor<4x4xf64>
168+
bufferization.dealloc_tensor %s : tensor<4x4xf64>
169+
bufferization.dealloc_tensor %s24 : tensor<4x4xf64>
170+
bufferization.dealloc_tensor %scsr : tensor<4x4xf64>
171+
164172
return
165173
}
166174
}

mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_pack.mlir

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,31 @@ module {
279279
%si = tensor.extract %li[] : tensor<i64>
280280
vector.print %si : i64
281281

282+
// TODO: This check is no longer needed once the codegen path uses the
283+
// buffer deallocation pass. "dealloc_tensor" turn into a no-op in the
284+
// codegen path.
285+
%has_runtime = sparse_tensor.has_runtime_library
286+
scf.if %has_runtime {
287+
// sparse_tensor.assemble copies buffers when running with the runtime
288+
// library. Deallocations are needed not needed when running in codgen
289+
// mode.
290+
bufferization.dealloc_tensor %s4 : tensor<10x10xf64, #SortedCOO>
291+
bufferization.dealloc_tensor %s5 : tensor<10x10xf64, #SortedCOOI32>
292+
bufferization.dealloc_tensor %csr : tensor<2x2xf64, #CSR>
293+
bufferization.dealloc_tensor %bs : tensor<2x10x10xf64, #BCOO>
294+
}
295+
296+
bufferization.dealloc_tensor %li : tensor<i64>
297+
bufferization.dealloc_tensor %od : tensor<3xf64>
298+
bufferization.dealloc_tensor %op : tensor<2xi32>
299+
bufferization.dealloc_tensor %oi : tensor<3x2xi32>
300+
bufferization.dealloc_tensor %d_csr : tensor<4xf64>
301+
bufferization.dealloc_tensor %p_csr : tensor<3xi32>
302+
bufferization.dealloc_tensor %i_csr : tensor<3xi32>
303+
bufferization.dealloc_tensor %bod : tensor<6xf64>
304+
bufferization.dealloc_tensor %bop : tensor<4xindex>
305+
bufferization.dealloc_tensor %boi : tensor<6x2xindex>
306+
282307
return
283308
}
284309
}

0 commit comments

Comments
 (0)