Skip to content

Commit 10d79a4

Browse files
authored
[ET-VK] Refactor Context::flush() and make ComputeGraph destructor exception-safe (#17191)
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/)
1 parent 3b819a2 commit 10d79a4

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

backends/vulkan/runtime/api/Context.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,14 @@ void Context::submit_cmd_to_gpu(VkFence fence_handle, const bool final_use) {
226226
}
227227
}
228228

229-
void Context::flush() {
229+
void Context::wait_for_queue() {
230230
VK_CHECK(vkQueueWaitIdle(queue().handle));
231+
}
231232

233+
void Context::clear_resources() {
232234
command_pool_.flush();
233235
descriptor_pool_.flush();
234236

235-
// If there is an existing command buffer, invalidate it
236237
if (cmd_) {
237238
cmd_.invalidate();
238239
}
@@ -243,6 +244,11 @@ void Context::flush() {
243244
images_to_clear_.clear();
244245
}
245246

247+
void Context::flush() {
248+
wait_for_queue();
249+
clear_resources();
250+
}
251+
246252
bool available() {
247253
return context();
248254
}

backends/vulkan/runtime/api/Context.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ class Context final {
232232
return cmd_;
233233
}
234234

235+
void wait_for_queue();
236+
237+
void clear_resources();
238+
235239
void flush();
236240

237241
#if defined(VK_KHR_pipeline_executable_properties) && \

backends/vulkan/runtime/graph/ComputeGraph.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,22 @@ ComputeGraph::ComputeGraph(GraphConfig config)
183183
}
184184

185185
ComputeGraph::~ComputeGraph() {
186-
values_.clear();
186+
// Wait for all currently executing commands to complete before cleaning up.
187+
// If wait_for_queue() throws an exception, still proceed with cleanup.
188+
try {
189+
context_->wait_for_queue();
190+
} catch (...) {
191+
}
187192

188-
prepack_nodes_.clear();
189-
execute_nodes_.clear();
190-
clear_deferred_cmds();
193+
// Wrap in try/catch to ensure that destructor does not throw
194+
try {
195+
values_.clear();
191196

192-
context_->flush();
197+
prepack_nodes_.clear();
198+
execute_nodes_.clear();
199+
clear_deferred_cmds();
200+
} catch (...) {
201+
}
193202
}
194203

195204
std::vector<int64_t> ComputeGraph::extract_int_or_symint_list(

0 commit comments

Comments
 (0)