Skip to content

Fix calls to ze destructors in level-zero adapter v2 #2531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions source/adapters/level_zero/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ void zeParseError(ze_result_t ZeError, const char *&ErrorString);
#define ZE_CALL_NOCHECK(ZeName, ZeArgs) \
ZeCall().doCall(ZeName ZeArgs, #ZeName, #ZeArgs, false)

#define ZE_CALL_NOCHECK_NAME(ZeName, ZeArgs, callName) \
ZeCall().doCall(ZeName ZeArgs, callName, #ZeArgs, false)

// This wrapper around std::atomic is created to limit operations with reference
// counter and to make allowed operations more transparent in terms of
// thread-safety in the plugin. increment() and load() operations do not need a
Expand Down
36 changes: 25 additions & 11 deletions source/adapters/level_zero/v2/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@

#include "../common.hpp"
#include "logger/ur_logger.hpp"
namespace {
#define DECLARE_DESTROY_FUNCTION(name) \
template <typename ZeHandleT> ze_result_t name##_wrapped(ZeHandleT handle) { \
return ZE_CALL_NOCHECK_NAME(name, (handle), #name); \
}

#define HANDLE_WRAPPER_TYPE(handle, destroy) \
ze_handle_wrapper<handle, destroy##_wrapped<handle>>
} // namespace

namespace v2 {

DECLARE_DESTROY_FUNCTION(zeKernelDestroy);
DECLARE_DESTROY_FUNCTION(zeEventDestroy);
DECLARE_DESTROY_FUNCTION(zeEventPoolDestroy);
DECLARE_DESTROY_FUNCTION(zeContextDestroy);
DECLARE_DESTROY_FUNCTION(zeCommandListDestroy);
namespace raii {

template <typename ZeHandleT, ze_result_t (*destroy)(ZeHandleT)>
Expand Down Expand Up @@ -65,7 +79,7 @@ struct ze_handle_wrapper {
}

if (ownZeHandle) {
auto zeResult = ZE_CALL_NOCHECK(destroy, (handle));
auto zeResult = destroy(handle);
// Gracefully handle the case that L0 was already unloaded.
if (zeResult && zeResult != ZE_RESULT_ERROR_UNINITIALIZED)
throw ze2urResult(zeResult);
Expand All @@ -89,20 +103,20 @@ struct ze_handle_wrapper {
bool ownZeHandle;
};

using ze_kernel_handle_t =
ze_handle_wrapper<::ze_kernel_handle_t, zeKernelDestroy>;
using ze_kernel_handle_t = HANDLE_WRAPPER_TYPE(::ze_kernel_handle_t,
zeKernelDestroy);

using ze_event_handle_t =
ze_handle_wrapper<::ze_event_handle_t, zeEventDestroy>;
using ze_event_handle_t = HANDLE_WRAPPER_TYPE(::ze_event_handle_t,
zeEventDestroy);

using ze_event_pool_handle_t =
ze_handle_wrapper<::ze_event_pool_handle_t, zeEventPoolDestroy>;
using ze_event_pool_handle_t = HANDLE_WRAPPER_TYPE(::ze_event_pool_handle_t,
zeEventPoolDestroy);

using ze_context_handle_t =
ze_handle_wrapper<::ze_context_handle_t, zeContextDestroy>;
using ze_context_handle_t = HANDLE_WRAPPER_TYPE(::ze_context_handle_t,
zeContextDestroy);

using ze_command_list_handle_t =
ze_handle_wrapper<::ze_command_list_handle_t, zeCommandListDestroy>;
using ze_command_list_handle_t = HANDLE_WRAPPER_TYPE(::ze_command_list_handle_t,
zeCommandListDestroy);

} // namespace raii
} // namespace v2
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ ur_command_list_handler_t::ur_command_list_handler_t(
: commandList(hZeCommandList,
[ownZeHandle](ze_command_list_handle_t hZeCommandList) {
if (ownZeHandle) {
zeCommandListDestroy(hZeCommandList);
ZE_CALL_NOCHECK(zeCommandListDestroy, (hZeCommandList));
}
}) {}

Expand Down
Loading