Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLIR] Double free with double --buffer-deallocation #86467

Open
Anonymous15592 opened this issue Mar 25, 2024 · 0 comments
Open

[MLIR] Double free with double --buffer-deallocation #86467

Anonymous15592 opened this issue Mar 25, 2024 · 0 comments
Labels

Comments

@Anonymous15592
Copy link

I tried to lower and execute the following mlir program:

func.func @func1() {
  %false = arith.constant false
  %52 = tensor.empty() : tensor<1x1x1xi32>
  %69 = scf.while (%arg0 = %52) : (tensor<1x1x1xi32>) -> tensor<1x1x1xi32> {
    %173 = tensor.empty() : tensor<1x1x1xi32>
    scf.condition(%false) %173 : tensor<1x1x1xi32>  // change %173 to %52, the bug disappears
  } do {
    ^bb0(%arg0: tensor<1x1x1xi32>): 
    %175 = tensor.empty() : tensor<1x1x1xi32>
    scf.yield %175 : tensor<1x1x1xi32>  // change %173 to (%52 or %arg0), the bug disappears
  }
  vector.print %false : i1
  return
}

with the passes:

--scf-bufferize \
--empty-tensor-to-alloc-tensor \
--bufferization-bufferize \
--buffer-deallocation \
--buffer-deallocation \
--convert-scf-to-cf \
--convert-func-to-llvm \
--convert-vector-to-llvm \
--convert-bufferization-to-memref \
--finalize-memref-to-llvm \
--convert-arith-to-llvm \
--reconcile-unrealized-casts \

And I ran the executable and finally got the following error:

free(): double free detected in tcache 2
Aborted (core dumped)

I observed that --buffer-deallocation generated double free for %2:

// mlir file after double --buffer-deallocation
module {
  func.func @func1() {
    %false = arith.constant false
    %alloc = memref.alloc() {alignment = 64 : i64} : memref<1x1x1xi32>
    %0 = bufferization.clone %alloc : memref<1x1x1xi32> to memref<1x1x1xi32>
    %1 = bufferization.clone %0 : memref<1x1x1xi32> to memref<1x1x1xi32>
    memref.dealloc %0 : memref<1x1x1xi32>
    memref.dealloc %alloc : memref<1x1x1xi32>
    %2 = scf.while (%arg0 = %1) : (memref<1x1x1xi32>) -> memref<1x1x1xi32> {
      %alloc_0 = memref.alloc() {alignment = 64 : i64} : memref<1x1x1xi32>
      memref.dealloc %arg0 : memref<1x1x1xi32>
      memref.dealloc %arg0 : memref<1x1x1xi32>
      %3 = bufferization.clone %alloc_0 : memref<1x1x1xi32> to memref<1x1x1xi32>
      %4 = bufferization.clone %3 : memref<1x1x1xi32> to memref<1x1x1xi32>
      memref.dealloc %3 : memref<1x1x1xi32>
      memref.dealloc %alloc_0 : memref<1x1x1xi32>
      scf.condition(%false) %4 : memref<1x1x1xi32>
    } do {
    ^bb0(%arg0: memref<1x1x1xi32>):
      %alloc_0 = memref.alloc() {alignment = 64 : i64} : memref<1x1x1xi32>
      memref.dealloc %arg0 : memref<1x1x1xi32>
      memref.dealloc %arg0 : memref<1x1x1xi32>
      %3 = bufferization.clone %alloc_0 : memref<1x1x1xi32> to memref<1x1x1xi32>
      %4 = bufferization.clone %3 : memref<1x1x1xi32> to memref<1x1x1xi32>
      memref.dealloc %3 : memref<1x1x1xi32>
      memref.dealloc %alloc_0 : memref<1x1x1xi32>
      scf.yield %4 : memref<1x1x1xi32>
    }
    memref.dealloc %2 : memref<1x1x1xi32>    // here
    memref.dealloc %2 : memref<1x1x1xi32>
    vector.print %false : i1
    return
  }
}

Then I noticed that using the --buffer-deallocation-simplification option might resolve the double free issue. However, upon trying it, I still got the same error. I'm unsure if there's a problem with how I'm using the passes or with the transformations in --buffer-deallocation and --buffer-deallocation-simplification.

@github-actions github-actions bot added the mlir label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant