From 1c0617f97bae8d4b0007d544c5416ca6ab1e68e3 Mon Sep 17 00:00:00 2001 From: Martijn Courteaux Date: Wed, 27 Nov 2024 15:59:33 +0100 Subject: [PATCH] Buildbot test to see if we can properly unload vulkan using atexit(). super hacky Proof-of-concept. --- src/runtime/vulkan.cpp | 19 ++++--------------- src/runtime/vulkan_interface.h | 4 ++++ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/runtime/vulkan.cpp b/src/runtime/vulkan.cpp index cb79950f7421..019ba27d54ef 100644 --- a/src/runtime/vulkan.cpp +++ b/src/runtime/vulkan.cpp @@ -1397,21 +1397,6 @@ WEAK __attribute__((constructor)) void register_vulkan_allocation_pool() { halide_register_device_allocation_pool(&vulkan_allocation_pool); } -WEAK __attribute__((destructor)) void halide_vulkan_cleanup() { - // FIXME: When the VK_LAYER_KHRONOS_validation is intercepting calls to the API, it will - // cause a segfault if it's invoked inside the destructor since it uses it's own global - // state to track object usage which is no longer valid once this call is invoked. - // Calling this destructor with the validator hooks in place will cause an uncaught - // exception for an uninitialized mutex lock. We can avoid the crash on exit by \ - // bypassing the device release call and leak (!!!!) - // ISSUE: https://github.com/halide/Halide/issues/8290 - const char *layer_names = vk_get_layer_names_internal(nullptr); - bool has_validation_layer = strstr(layer_names, "VK_LAYER_KHRONOS_validation"); - if (!has_validation_layer) { - halide_vulkan_device_release(nullptr); - } -} - // -------------------------------------------------------------------------- } // namespace @@ -1472,3 +1457,7 @@ WEAK halide_device_interface_t vulkan_device_interface = { } // namespace Internal } // namespace Runtime } // namespace Halide + +WEAK void halide_vulkan_cleanup() { + halide_vulkan_device_release(nullptr); +} diff --git a/src/runtime/vulkan_interface.h b/src/runtime/vulkan_interface.h index d7ff99c860ea..302b85bf5faf 100644 --- a/src/runtime/vulkan_interface.h +++ b/src/runtime/vulkan_interface.h @@ -30,6 +30,9 @@ // Vulkan API Definition // -------------------------------------------------------------------------- +extern "C" WEAK void atexit(void (*fn)()); +WEAK void halide_vulkan_cleanup(); + namespace Halide { namespace Runtime { namespace Internal { @@ -66,6 +69,7 @@ extern "C" WEAK void *halide_vulkan_get_symbol(void *user_context, const char *n lib_vulkan = halide_load_library(lib_name); if (lib_vulkan) { debug(user_context) << " Loaded Vulkan loader library: " << lib_name << "\n"; + atexit(halide_vulkan_cleanup); break; } else { debug(user_context) << " Missing Vulkan loader library: " << lib_name << "\n";