Skip to content

Leaked buffers for unused inputs #2455

Closed

Description

@antiagainst

At the moment IREE runtime is leaking buffers allocated for unused inputs. A repro case:

func @main(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> attributes {iree.module.export} {
  return %arg1 : tensor<4xf32>
}
$ iree-translate -iree-mlir-to-vm-bytecode-module --iree-hal-target-backends=vulkan-spirv ../unused-input.mlir > unused-input.vmfb 

$ iree-run-module --input_file=unused-input-vmla.vmfb --entry_function=main --inputs="4xf32,4xf32" --driver=vulkan

F ../third_party/vulkan_memory_allocator/src/vk_mem_alloc.h:11175] Check failed: m_pMetadata->IsEmpty() && "Some allocations were not freed before destruction of this memory block!"

This is made obvious by Vulkan's VMA allocator. But it's not specific to Vulkan HAL backend; the code in the above does not perform any computation at all. It should also be revealed via ASAN for other backends.

Activity

  1. benvanik commented on Jul 9, 2020

    @benvanik
    Collaborator

    Oo this may be a much simpler repro than #2252 (which I spent some time digging through but was too complex to isolate). I'll take a look.

  2. added
    bug 🐞
    runtime
    on Jul 9, 2020
  3. self-assigned this
    on Jul 9, 2020
  4. added this to the milestone on Jul 9, 2020
  5. antiagainst commented on Jul 9, 2020

    @antiagainst
    MemberAuthor

    Also attaching the vm IR for convenience:

      vm.module @module {
        vm.func @main(%arg0: !vm.ref<!hal.buffer>, %arg1: !vm.ref<!hal.buffer>) -> !vm.ref<!hal.buffer> {
          vm.return %arg1 : !vm.ref<!hal.buffer>
        }
        vm.export @main as("main$raw")
        vm.func @main$sync(%arg0: !vm.ref<!hal.buffer_view>, %arg1: !vm.ref<!hal.buffer_view>) -> !vm.ref<!hal.buffer_view> attributes {iree.reflection = {f = "I11!B3!d4B3!d4R6!B3!d4", fv = "1"}} {
          %c4 = vm.const.i32 4 : i32
          %c50331680 = vm.const.i32 50331680 : i32
          %ref = vm.call @hal.buffer_view.buffer(%arg1) : (!vm.ref<!hal.buffer_view>) -> !vm.ref<!hal.buffer>
          %ref_0 = vm.call.variadic @hal.buffer_view.create(%ref, [%c4], %c50331680) : (!vm.ref<!hal.buffer>, i32..., i32) -> !vm.ref<!hal.buffer_view>
          vm.return %ref_0 : !vm.ref<!hal.buffer_view>
        }
        vm.export @main$sync as("main")
        vm.import @hal.buffer_view.create(%buffer : !vm.ref<!hal.buffer>, %shape : i32..., %element_type : i32) -> !vm.ref<!hal.buffer_view> attributes {nosideeffects, sym_visibility = "private"}
        vm.import @hal.buffer_view.buffer(%buffer_view : !vm.ref<!hal.buffer_view>) -> !vm.ref<!hal.buffer> attributes {nosideeffects, sym_visibility = "private"}
      }
  6. antiagainst commented on Jul 9, 2020

    @antiagainst
    MemberAuthor

    I tried to poke around. And here is what I found so far: we get an initial refcount = 1 for these inputs when parsing inputs and build variant list. Then we +1 to the refcount when entering the function in vm. And then when calling into hal.buffer_view.buffer the refcount for %arg was decreased by 1 in a non-trivial (in the sense of code layering) way: it's via DispatchFunctor::ApplyFn's heavy templating. But %arg0's refcount remains as 2 until the program exit and then it gets decreased to 1 by variant list free. So it still lingers on..

  7. antiagainst commented on Jul 9, 2020

    @antiagainst
    MemberAuthor

    I feel we might missing refcount clean up when leaving a vm function? But I'm not very efficient when parsing through the vm layers. ;)

  8. benvanik commented on Jul 9, 2020

    @benvanik
    Collaborator

    I suspect it's staying alive in the stack somehow. For the buffer that is correctly freed the ApplyFn is doing a move (transferring ownership) and then release when the ApplyFn returns, hence the 2->1. I have some nice debug logging for the C++ ref_ptr type https://github.com/google/iree/blob/4e4f587b254c7e12aa3ceddb121a7cd54d4d1f83/iree/base/ref_ptr.h#L29 that I need to add in for the vm type.

  9. antiagainst commented on Jul 9, 2020

    @antiagainst
    MemberAuthor

    But should ApplyFn use move semantics? The %arg1 view can potentially be used by another call following the existing vm.call @hal.buffer_view.buffer? I feel it should not change refcount at all and let the vm function exit to decrease? At the moment because vm.call @hal.buffer_view.buffer only takes in %arg1 so %arg0 is entirely left where with its original refcount = 2 after entering @main$sync.

    (But OTOH, I doubt we haven't hit the case that multiple uses of the same input view and don't know how that managed to not blow up thus far. ;-P)

  10. benvanik commented on Jul 9, 2020

    @benvanik
    Collaborator

    whether to use a move or not is determined by the compiler:
    https://github.com/google/iree/blob/4c0a53e955c44480e2387a9d756a33f66830a394/iree/vm/module_abi_packing.h#L209-L214
    the register allocator knows if a value is the last use and will add the MOVE bit - just like a C++ programmer would add std::move of a shared_ptr vs copying it and increasing the ref count.

  11. antiagainst commented on Jul 9, 2020

    @antiagainst
    MemberAuthor

    Ooooh, of course, compilers to the rescue. :) :) :) Very cool stuff! :D :D :D Eager to chat more.

  12. benvanik commented on Jul 9, 2020

    @benvanik
    Collaborator

    I think I found the issue. For some reason I added a "leak this ref" line:
    https://github.com/google/iree/blob/b39149801a2c0dafaab23c06137db64962242745/iree/vm/module_abi_packing.h#L424

    This would overwrite results from cross-module calls (like HAL calls) in cases where one or more registers were unused (or last-used) by the cross-module call. Will do a bit more verification and try #2252 as it should fix that too!

  13. added a commit that references this issue on Jul 9, 2020
  14. added a commit that references this issue on Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐞runtime

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions