Skip to content

Commit

Permalink
[mlir][sparse] Fix memory leaks (part 2) (llvm#81979)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
matthias-springer authored Feb 17, 2024
1 parent 47c65cf commit ccc20b4
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ module {
%dv = sparse_tensor.convert %arg0 : tensor<?xf64, #SparseVector> to tensor<?xf64>
%3 = vector.transfer_read %dv[%c0], %d0: tensor<?xf64>, vector<32xf64>
vector.print %3 : vector<32xf64>
bufferization.dealloc_tensor %dv : tensor<?xf64>
return
}

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

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

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

bufferization.dealloc_tensor %c : tensor<4x4xf64>
return
}

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

bufferization.dealloc_tensor %c : tensor<4x4xi8>
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func.func @entry() {
bufferization.dealloc_tensor %out2D_nhwc : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %out2D_nhwc_CCCD : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %out2D_nhwc_CCCC : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %dense_ret :tensor<?x?x?x?xf32>

bufferization.dealloc_tensor %in2D_nhwc_CCCC : tensor<?x?x?x?xf32, #CCCC>
bufferization.dealloc_tensor %in2D_nhwc_CCCD : tensor<?x?x?x?xf32, #CDCD>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,9 @@ func.func @entry() {
bufferization.dealloc_tensor %CDCD_ret : tensor<?x?x?x?xf32, #CDCD>
bufferization.dealloc_tensor %DCCD_ret : tensor<?x?x?x?xf32, #DCCD>

bufferization.dealloc_tensor %1 : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %2 : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %3 : tensor<?x?x?x?xf32>

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,11 @@ func.func @entry() {
bufferization.dealloc_tensor %CDC_ret : tensor<?x?x?xf32, #CDC>
bufferization.dealloc_tensor %DDC_ret : tensor<?x?x?xf32, #DDC>
bufferization.dealloc_tensor %DCC_ret : tensor<?x?x?xf32, #DCC>

bufferization.dealloc_tensor %1 : tensor<?x?x?xf32>
bufferization.dealloc_tensor %2 : tensor<?x?x?xf32>
bufferization.dealloc_tensor %3 : tensor<?x?x?xf32>
bufferization.dealloc_tensor %4 : tensor<?x?x?xf32>

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,8 @@ func.func @entry() {
bufferization.dealloc_tensor %CCCCC_ret : tensor<?x?x?x?x?xf32, #CCCCC>
bufferization.dealloc_tensor %CDCDC_ret : tensor<?x?x?x?x?xf32, #CDCDC>

bufferization.dealloc_tensor %1 : tensor<?x?x?x?x?xf32>
bufferization.dealloc_tensor %2 : tensor<?x?x?x?x?xf32>

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ module {
bufferization.dealloc_tensor %s1 : tensor<2x3x4xf64, #Tensor1>
bufferization.dealloc_tensor %s2 : tensor<2x3x4xf64, #Tensor2>
bufferization.dealloc_tensor %s3 : tensor<2x3x4xf64, #Tensor3>
bufferization.dealloc_tensor %d1 : tensor<2x3x4xf32>
bufferization.dealloc_tensor %d2 : tensor<2x3x4xf32>
bufferization.dealloc_tensor %d3 : tensor<2x3x4xf32>

return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,10 @@ func.func @entry() {
bufferization.dealloc_tensor %filter2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>
bufferization.dealloc_tensor %in2D_nhwc_CCCC : tensor<?x?x?x?xf32, #CCCC>
bufferization.dealloc_tensor %in2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>

bufferization.dealloc_tensor %dense_ret : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %CCCC_ret : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %CDCC_ret : tensor<?x?x?x?xf32>

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ module {
%du = tensor.cast %r : tensor<4x4xf64> to tensor<*xf64>
call @printMemrefF64(%du) : (tensor<*xf64>) -> ()

// Releases resources (we do not need to deallocate slices).
// Releases resources.
bufferization.dealloc_tensor %c2 : tensor<4x4xf64>
bufferization.dealloc_tensor %c3 : tensor<4x4xf64>
bufferization.dealloc_tensor %c4 : tensor<4x4xf64>
bufferization.dealloc_tensor %c4_coo : tensor<4x4xf64>
bufferization.dealloc_tensor %c4_dyn : tensor<4x4xf64>
bufferization.dealloc_tensor %d : tensor<4x4xf64>
bufferization.dealloc_tensor %b1 : tensor<8x4xf64, #CSR>
bufferization.dealloc_tensor %t1 : tensor<8x8xf64, #CSR>
bufferization.dealloc_tensor %b1_coo : tensor<8x4xf64, #COO>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,10 @@ func.func @entry() {
bufferization.dealloc_tensor %filter2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>
bufferization.dealloc_tensor %in2D_nhwc_CCCC : tensor<?x?x?x?xf32, #CCCC>
bufferization.dealloc_tensor %in2D_nhwc_CDCC : tensor<?x?x?x?xf32, #CDCC>

bufferization.dealloc_tensor %dense_ret : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %CCCC_ret : tensor<?x?x?x?xf32>
bufferization.dealloc_tensor %CDCC_ret : tensor<?x?x?x?xf32>

return
}

0 comments on commit ccc20b4

Please sign in to comment.