From 652ae2382ecba88ebbfe12351624ea04c541e035 Mon Sep 17 00:00:00 2001 From: ssjia Date: Tue, 3 Feb 2026 09:36:05 -0800 Subject: [PATCH] [ET-VK] Refactor Context::flush() and make ComputeGraph destructor exception-safe Refactored Context::flush() by extracting two new public functions: - wait_for_queue(): blocks until the GPU queue is idle - clear_resources(): clears command/descriptor pools and cleanup lists This allows callers to use these operations independently. ComputeGraph's destructor now uses these functions directly and wraps them in try/catch blocks to ensure it never throws exceptions, following C++ best practices for destructors. Authored with Claude. Differential Revision: [D92171362](https://our.internmc.facebook.com/intern/diff/D92171362/) ghstack-source-id: 337901647 Pull Request resolved: https://github.com/pytorch/executorch/pull/17158 --- backends/vulkan/runtime/api/Context.cpp | 10 ++++++++-- backends/vulkan/runtime/api/Context.h | 4 ++++ .../vulkan/runtime/graph/ComputeGraph.cpp | 19 ++++++++++++++----- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/backends/vulkan/runtime/api/Context.cpp b/backends/vulkan/runtime/api/Context.cpp index 326391424df..80aef97fc04 100644 --- a/backends/vulkan/runtime/api/Context.cpp +++ b/backends/vulkan/runtime/api/Context.cpp @@ -226,13 +226,14 @@ void Context::submit_cmd_to_gpu(VkFence fence_handle, const bool final_use) { } } -void Context::flush() { +void Context::wait_for_queue() { VK_CHECK(vkQueueWaitIdle(queue().handle)); +} +void Context::clear_resources() { command_pool_.flush(); descriptor_pool_.flush(); - // If there is an existing command buffer, invalidate it if (cmd_) { cmd_.invalidate(); } @@ -243,6 +244,11 @@ void Context::flush() { images_to_clear_.clear(); } +void Context::flush() { + wait_for_queue(); + clear_resources(); +} + bool available() { return context(); } diff --git a/backends/vulkan/runtime/api/Context.h b/backends/vulkan/runtime/api/Context.h index 5764cb6a894..49003435e3b 100644 --- a/backends/vulkan/runtime/api/Context.h +++ b/backends/vulkan/runtime/api/Context.h @@ -232,6 +232,10 @@ class Context final { return cmd_; } + void wait_for_queue(); + + void clear_resources(); + void flush(); #if defined(VK_KHR_pipeline_executable_properties) && \ diff --git a/backends/vulkan/runtime/graph/ComputeGraph.cpp b/backends/vulkan/runtime/graph/ComputeGraph.cpp index 3a5aabb1c7d..bb2df30a174 100644 --- a/backends/vulkan/runtime/graph/ComputeGraph.cpp +++ b/backends/vulkan/runtime/graph/ComputeGraph.cpp @@ -183,13 +183,22 @@ ComputeGraph::ComputeGraph(GraphConfig config) } ComputeGraph::~ComputeGraph() { - values_.clear(); + // Wait for all currently executing commands to complete before cleaning up. + // If wait_for_queue() throws an exception, still proceed with cleanup. + try { + context_->wait_for_queue(); + } catch (...) { + } - prepack_nodes_.clear(); - execute_nodes_.clear(); - clear_deferred_cmds(); + // Wrap in try/catch to ensure that destructor does not throw + try { + values_.clear(); - context_->flush(); + prepack_nodes_.clear(); + execute_nodes_.clear(); + clear_deferred_cmds(); + } catch (...) { + } } std::vector ComputeGraph::extract_int_or_symint_list(