Skip to content

Commit ccc20b4

Browse files
[mlir][sparse] Fix memory leaks (part 2) (llvm#81979)
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).
1 parent 47c65cf commit ccc20b4

9 files changed

+39
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ module {
380380
%dv = sparse_tensor.convert %arg0 : tensor<?xf64, #SparseVector> to tensor<?xf64>
381381
%3 = vector.transfer_read %dv[%c0], %d0: tensor<?xf64>, vector<32xf64>
382382
vector.print %3 : vector<32xf64>
383+
bufferization.dealloc_tensor %dv : tensor<?xf64>
383384
return
384385
}
385386

@@ -394,6 +395,7 @@ module {
394395
%dv = sparse_tensor.convert %arg0 : tensor<?xi32, #SparseVector> to tensor<?xi32>
395396
%3 = vector.transfer_read %dv[%c0], %d0: tensor<?xi32>, vector<32xi32>
396397
vector.print %3 : vector<32xi32>
398+
bufferization.dealloc_tensor %dv : tensor<?xi32>
397399
return
398400
}
399401

@@ -403,6 +405,7 @@ module {
403405
%dm = sparse_tensor.convert %arg0 : tensor<?x?xf64, #DCSR> to tensor<?x?xf64>
404406
%1 = vector.transfer_read %dm[%c0, %c0], %d0: tensor<?x?xf64>, vector<4x8xf64>
405407
vector.print %1 : vector<4x8xf64>
408+
bufferization.dealloc_tensor %dm : tensor<?x?xf64>
406409
return
407410
}
408411

@@ -418,6 +421,7 @@ module {
418421
%2 = vector.transfer_read %1[%c0], %du: memref<?xf64>, vector<16xf64>
419422
vector.print %2 : vector<16xf64>
420423

424+
bufferization.dealloc_tensor %c : tensor<4x4xf64>
421425
return
422426
}
423427

@@ -433,6 +437,7 @@ module {
433437
%2 = vector.transfer_read %1[%c0], %du: memref<?xi8>, vector<16xi8>
434438
vector.print %2 : vector<16xi8>
435439

440+
bufferization.dealloc_tensor %c : tensor<4x4xi8>
436441
return
437442
}
438443

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func.func @entry() {
207207
bufferization.dealloc_tensor %out2D_nhwc : tensor<?x?x?x?xf32>
208208
bufferization.dealloc_tensor %out2D_nhwc_CCCD : tensor<?x?x?x?xf32>
209209
bufferization.dealloc_tensor %out2D_nhwc_CCCC : tensor<?x?x?x?xf32>
210+
bufferization.dealloc_tensor %dense_ret :tensor<?x?x?x?xf32>
210211

211212
bufferization.dealloc_tensor %in2D_nhwc_CCCC : tensor<?x?x?x?xf32, #CCCC>
212213
bufferization.dealloc_tensor %in2D_nhwc_CCCD : tensor<?x?x?x?xf32, #CDCD>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,5 +227,9 @@ func.func @entry() {
227227
bufferization.dealloc_tensor %CDCD_ret : tensor<?x?x?x?xf32, #CDCD>
228228
bufferization.dealloc_tensor %DCCD_ret : tensor<?x?x?x?xf32, #DCCD>
229229

230+
bufferization.dealloc_tensor %1 : tensor<?x?x?x?xf32>
231+
bufferization.dealloc_tensor %2 : tensor<?x?x?x?xf32>
232+
bufferization.dealloc_tensor %3 : tensor<?x?x?x?xf32>
233+
230234
return
231235
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,11 @@ func.func @entry() {
348348
bufferization.dealloc_tensor %CDC_ret : tensor<?x?x?xf32, #CDC>
349349
bufferization.dealloc_tensor %DDC_ret : tensor<?x?x?xf32, #DDC>
350350
bufferization.dealloc_tensor %DCC_ret : tensor<?x?x?xf32, #DCC>
351+
352+
bufferization.dealloc_tensor %1 : tensor<?x?x?xf32>
353+
bufferization.dealloc_tensor %2 : tensor<?x?x?xf32>
354+
bufferization.dealloc_tensor %3 : tensor<?x?x?xf32>
355+
bufferization.dealloc_tensor %4 : tensor<?x?x?xf32>
356+
351357
return
352358
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,5 +249,8 @@ func.func @entry() {
249249
bufferization.dealloc_tensor %CCCCC_ret : tensor<?x?x?x?x?xf32, #CCCCC>
250250
bufferization.dealloc_tensor %CDCDC_ret : tensor<?x?x?x?x?xf32, #CDCDC>
251251

252+
bufferization.dealloc_tensor %1 : tensor<?x?x?x?x?xf32>
253+
bufferization.dealloc_tensor %2 : tensor<?x?x?x?x?xf32>
254+
252255
return
253256
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ module {
106106
bufferization.dealloc_tensor %s1 : tensor<2x3x4xf64, #Tensor1>
107107
bufferization.dealloc_tensor %s2 : tensor<2x3x4xf64, #Tensor2>
108108
bufferization.dealloc_tensor %s3 : tensor<2x3x4xf64, #Tensor3>
109+
bufferization.dealloc_tensor %d1 : tensor<2x3x4xf32>
110+
bufferization.dealloc_tensor %d2 : tensor<2x3x4xf32>
111+
bufferization.dealloc_tensor %d3 : tensor<2x3x4xf32>
109112

110113
return
111114
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,10 @@ func.func @entry() {
144144
bufferization.dealloc_tensor %filter2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>
145145
bufferization.dealloc_tensor %in2D_nhwc_CCCC : tensor<?x?x?x?xf32, #CCCC>
146146
bufferization.dealloc_tensor %in2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>
147+
148+
bufferization.dealloc_tensor %dense_ret : tensor<?x?x?x?xf32>
149+
bufferization.dealloc_tensor %CCCC_ret : tensor<?x?x?x?xf32>
150+
bufferization.dealloc_tensor %CDCC_ret : tensor<?x?x?x?xf32>
151+
147152
return
148153
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,13 @@ module {
264264
%du = tensor.cast %r : tensor<4x4xf64> to tensor<*xf64>
265265
call @printMemrefF64(%du) : (tensor<*xf64>) -> ()
266266

267-
// Releases resources (we do not need to deallocate slices).
267+
// Releases resources.
268+
bufferization.dealloc_tensor %c2 : tensor<4x4xf64>
269+
bufferization.dealloc_tensor %c3 : tensor<4x4xf64>
270+
bufferization.dealloc_tensor %c4 : tensor<4x4xf64>
271+
bufferization.dealloc_tensor %c4_coo : tensor<4x4xf64>
272+
bufferization.dealloc_tensor %c4_dyn : tensor<4x4xf64>
273+
bufferization.dealloc_tensor %d : tensor<4x4xf64>
268274
bufferization.dealloc_tensor %b1 : tensor<8x4xf64, #CSR>
269275
bufferization.dealloc_tensor %t1 : tensor<8x8xf64, #CSR>
270276
bufferization.dealloc_tensor %b1_coo : tensor<8x4xf64, #COO>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,10 @@ func.func @entry() {
143143
bufferization.dealloc_tensor %filter2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>
144144
bufferization.dealloc_tensor %in2D_nhwc_CCCC : tensor<?x?x?x?xf32, #CCCC>
145145
bufferization.dealloc_tensor %in2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>
146+
147+
bufferization.dealloc_tensor %dense_ret : tensor<?x?x?x?xf32>
148+
bufferization.dealloc_tensor %CCCC_ret : tensor<?x?x?x?xf32>
149+
bufferization.dealloc_tensor %CDCC_ret : tensor<?x?x?x?xf32>
150+
146151
return
147152
}

0 commit comments

Comments
 (0)