You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ 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.
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..
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)
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!
Activity
benvanik commentedon Jul 9, 2020
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.
antiagainst commentedon Jul 9, 2020
Also attaching the vm IR for convenience:
antiagainst commentedon Jul 9, 2020
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.bufferthe refcount for%argwas decreased by 1 in a non-trivial (in the sense of code layering) way: it's viaDispatchFunctor::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..antiagainst commentedon Jul 9, 2020
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. ;)
benvanik commentedon Jul 9, 2020
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.
antiagainst commentedon Jul 9, 2020
But should
ApplyFnuse move semantics? The%arg1view can potentially be used by another call following the existingvm.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 becausevm.call @hal.buffer_view.bufferonly takes in%arg1so%arg0is 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)
benvanik commentedon Jul 9, 2020
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.
antiagainst commentedon Jul 9, 2020
Ooooh, of course, compilers to the rescue. :) :) :) Very cool stuff! :D :D :D Eager to chat more.
benvanik commentedon Jul 9, 2020
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!
Fixing #2455 and #2252 (leak on particular ABI patterns + unused regi…
Fixing #2455 and #2252 (leak on particular ABI patterns + unused regi…