1717#include < fstream>
1818#include < iomanip>
1919#include < list>
20+ #include < mutex>
2021#include < numeric>
2122#include < sstream>
2223#include < string>
@@ -408,7 +409,8 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
408409 public:
409410 VGFBackend () = default ;
410411
411- // Lazy Vulkan init — runs on first use, not in the constructor.
412+ // Lazy Vulkan init. All callers hold mutex_; no Vulkan work is done
413+ // from the process-global backend's constructor or destructor.
412414 void ensure_initialized () {
413415 if (is_initialized_) {
414416 return ;
@@ -430,6 +432,7 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
430432 if (result != VK_SUCCESS ) {
431433 ET_LOG (
432434 Error, " Failed to initialize the Vulkan device error 0x%08X" , result);
435+ release_basics_if_unused ();
433436 return ;
434437 }
435438
@@ -440,27 +443,36 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
440443 Error,
441444 " Failed to verify VKML extensions needed, error 0x%08X" ,
442445 result);
446+ release_basics_if_unused ();
443447 return ;
444448 }
445449
446450 is_initialized_ = true ;
447451 }
448452
449- ~VGFBackend () = default ;
453+ // Vulkan teardown belongs to destroy(), not static destruction: the
454+ // loader and layers may already have torn down their dispatch state.
455+ ~VGFBackend () override = default ;
450456
451457 bool is_available () const override {
458+ auto * self = const_cast <VGFBackend*>(this );
459+ const std::lock_guard<std::mutex> lock (mutex_);
452460 ET_LOG (Info, " Checking VGFBackend is available" );
453- const_cast <VGFBackend*>(this )->ensure_initialized ();
454- if (!is_initialized_) {
455- return false ;
456- }
457- return vkml_load_extensions (&vk_device) == VK_SUCCESS ;
461+ self->ensure_initialized ();
462+ const bool available =
463+ is_initialized_ && vkml_load_extensions (&vk_device) == VK_SUCCESS ;
464+ // An availability-only probe must not retain a device until exit.
465+ // Existing delegates, if any, keep their shared device alive.
466+ self->release_basics_if_unused ();
467+ return available;
458468 }
459469
460470 Result<DelegateHandle*> init (
461471 BackendInitContext& context,
462472 FreeableBuffer* processed,
463473 ArrayRef<CompileSpec> compile_specs) const override {
474+ auto * self = const_cast <VGFBackend*>(this );
475+ const std::lock_guard<std::mutex> lock (mutex_);
464476 ET_LOG (Info, " Entered VGF init" );
465477
466478#ifdef ET_EVENT_TRACER_ENABLED
@@ -478,7 +490,7 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
478490 /* delegate_debug_id=*/ -1 );
479491#endif
480492
481- const_cast <VGFBackend*>( this ) ->ensure_initialized ();
493+ self ->ensure_initialized ();
482494
483495#ifdef ET_EVENT_TRACER_ENABLED
484496 event_tracer_end_profiling_delegate (event_tracer, ensure_initialized_event);
@@ -506,6 +518,14 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
506518
507519 MemoryAllocator* allocator = context.get_runtime_allocator ();
508520 VgfRepr* repr = allocator->allocateInstance <VgfRepr>();
521+ if (repr == nullptr ) {
522+ #ifdef ET_EVENT_TRACER_ENABLED
523+ event_tracer_end_profiling_delegate (event_tracer, allocate_repr_event);
524+ event_tracer_end_profiling_delegate (event_tracer, init_total_event);
525+ #endif
526+ self->release_basics_if_unused ();
527+ return Error::MemoryAllocationFailed;
528+ }
509529 new (repr) VgfRepr (
510530 vk_instance,
511531 vk_physical_device,
@@ -543,20 +563,28 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
543563 event_tracer_end_profiling_delegate (event_tracer, init_total_event);
544564#endif
545565 ET_LOG (Error, " Failed to process VGF blob." );
566+ // The runtime never receives this handle, so it cannot destroy it.
567+ self->wait_for_device_idle ();
568+ repr->~VgfRepr ();
569+ self->release_basics_if_unused ();
546570 return Error::Internal;
547571 }
548572
549573#ifdef ET_EVENT_TRACER_ENABLED
550574 event_tracer_end_profiling_delegate (event_tracer, init_total_event);
551575#endif
552576
577+ ++self->live_delegates_ ;
553578 return repr;
554579 }
555580
556581 Error execute (
557582 BackendExecutionContext& context,
558583 DelegateHandle* handle,
559584 Span<EValue*> args) const override {
585+ // The queue and command pool are shared by all VGF delegates.
586+ // Serialize their host access with initialization and teardown.
587+ const std::lock_guard<std::mutex> lock (mutex_);
560588 VgfRepr* repr = static_cast <VgfRepr*>(handle);
561589 const size_t input_count = repr->model_input_count ;
562590 const size_t output_count = repr->model_output_count ;
@@ -783,11 +811,66 @@ class VGFBackend final : public ::executorch::runtime::BackendInterface {
783811 }
784812
785813 void destroy (DelegateHandle* handle) const override {
814+ if (handle == nullptr ) {
815+ return ;
816+ }
817+ auto * self = const_cast <VGFBackend*>(this );
818+ const std::lock_guard<std::mutex> lock (mutex_);
819+ ET_CHECK_MSG (live_delegates_ > 0 , " Unbalanced VGF delegate destruction" );
820+ // A failed execution may have submitted work without completing its
821+ // fence wait. Drain it before freeing any per-delegate resources.
822+ self->wait_for_device_idle ();
786823 VgfRepr* repr = static_cast <VgfRepr*>(handle);
787824 repr->~VgfRepr ();
825+ --self->live_delegates_ ;
826+ self->release_basics_if_unused ();
788827 }
789828
790829 private:
830+ // Call only with mutex_ held. A device-lost error still permits
831+ // destruction; record it rather than leaking the remaining objects.
832+ void wait_for_device_idle () {
833+ if (vk_device != VK_NULL_HANDLE ) {
834+ const VkResult result = vkDeviceWaitIdle (vk_device);
835+ if (result != VK_SUCCESS ) {
836+ ET_LOG (Error, " VGF teardown: vkDeviceWaitIdle failed: %d" , result);
837+ }
838+ ET_CHECK_MSG (
839+ result == VK_SUCCESS || result == VK_ERROR_DEVICE_LOST ,
840+ " Cannot free VGF resources while device completion is unknown" );
841+ }
842+ }
843+
844+ // Call only with mutex_ held, after destroying the last VgfRepr (or
845+ // when a probe/initialization failed without publishing a handle).
846+ // Any submitted work must have been drained before freeing the repr.
847+ void release_basics_if_unused () {
848+ if (live_delegates_ != 0 ) {
849+ return ;
850+ }
851+ if (vk_command_pool != VK_NULL_HANDLE ) {
852+ vkDestroyCommandPool (vk_device, vk_command_pool, nullptr );
853+ vk_command_pool = VK_NULL_HANDLE ;
854+ }
855+ if (vk_device != VK_NULL_HANDLE ) {
856+ vkDestroyDevice (vk_device, nullptr );
857+ vk_device = VK_NULL_HANDLE ;
858+ }
859+ if (vk_instance != VK_NULL_HANDLE ) {
860+ vkDestroyInstance (vk_instance, nullptr );
861+ vk_instance = VK_NULL_HANDLE ;
862+ }
863+ vk_physical_device = VK_NULL_HANDLE ;
864+ vk_queue = VK_NULL_HANDLE ;
865+ vk_queue_family_index = UINT32_MAX ;
866+ neural_statistics_config_ = {};
867+ neural_statistics_device_enabled_ = false ;
868+ is_initialized_ = false ;
869+ // Do not call volkFinalize(): the Vulkan backend shares the loader.
870+ }
871+
872+ mutable std::mutex mutex_;
873+ size_t live_delegates_ = 0 ;
791874 VkInstance vk_instance = VK_NULL_HANDLE ;
792875 VkPhysicalDevice vk_physical_device = VK_NULL_HANDLE ;
793876 VkDevice vk_device = VK_NULL_HANDLE ;
@@ -891,6 +974,8 @@ VkResult vkml_allocate_basics(
891974 result = vkCreateInstance (&instance_info, nullptr , instance);
892975 if (result != VK_SUCCESS ) {
893976 ET_LOG (Error, " Failed to create VkInstance" );
977+ // Failed creation leaves the output undefined; do not destroy it.
978+ *instance = VK_NULL_HANDLE ;
894979 return result;
895980 }
896981 volkLoadInstance (*instance);
@@ -1171,6 +1256,8 @@ VkResult vkml_allocate_basics(
11711256 result = vkCreateDevice (*physical_device, &dci, nullptr , device);
11721257 if (result != VK_SUCCESS ) {
11731258 ET_LOG (Error, " Failed to create VkDevice" );
1259+ // Failed creation leaves the output undefined; do not destroy it.
1260+ *device = VK_NULL_HANDLE ;
11741261 return result;
11751262 }
11761263 // Load the device with volk and populate function pointers
@@ -1187,6 +1274,8 @@ VkResult vkml_allocate_basics(
11871274 result = vkCreateCommandPool (*device, &poolInfo, nullptr , command_pool);
11881275 if (result != VK_SUCCESS ) {
11891276 ET_LOG (Error, " Failed to create VkCommandPool" );
1277+ // Failed creation leaves the output undefined; do not destroy it.
1278+ *command_pool = VK_NULL_HANDLE ;
11901279 return result;
11911280 }
11921281
0 commit comments