Skip to content

Commit 7875d58

Browse files
committed
[L0 v2] Use ze_handle_wrapper to hold ze context
This was the last place where we stored ze handle directly. Now, we always use ze_handle_wrapper for managing ownership which makes the code more robust.
1 parent 4ec71b8 commit 7875d58

File tree

6 files changed

+17
-24
lines changed

6 files changed

+17
-24
lines changed

source/adapters/level_zero/v2/common.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct ze_handle_wrapper {
5454
try {
5555
reset();
5656
} catch (...) {
57+
// TODO: add appropriate logging or pass the error
58+
// to the caller (make the dtor noexcept(false) or use tls?)
5759
}
5860
}
5961

@@ -158,6 +160,9 @@ using ze_event_handle_t =
158160
using ze_event_pool_handle_t =
159161
ze_handle_wrapper<::ze_event_pool_handle_t, zeEventPoolDestroy>;
160162

163+
using ze_context_handle_t =
164+
ze_handle_wrapper<::ze_context_handle_t, zeContextDestroy>;
165+
161166
using ur_queue_shared_handle_t =
162167
ur_shared_handle<ur_queue_handle_t, urQueueRetain, urQueueRelease>;
163168

source/adapters/level_zero/v2/context.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ ur_context_handle_t_::ur_context_handle_t_(ze_context_handle_t hContext,
1717
uint32_t numDevices,
1818
const ur_device_handle_t *phDevices,
1919
bool ownZeContext)
20-
: hContext(hContext), hDevices(phDevices, phDevices + numDevices),
21-
commandListCache(hContext),
20+
: hContext(hContext, ownZeContext),
21+
hDevices(phDevices, phDevices + numDevices), commandListCache(hContext),
2222
eventPoolCache(phDevices[0]->Platform->getNumDevices(),
2323
[context = this,
2424
platform = phDevices[0]->Platform](DeviceId deviceId) {
@@ -27,19 +27,7 @@ ur_context_handle_t_::ur_context_handle_t_(ze_context_handle_t hContext,
2727
return std::make_unique<v2::provider_normal>(
2828
context, device, v2::EVENT_COUNTER,
2929
v2::QUEUE_IMMEDIATE);
30-
}) {
31-
std::ignore = ownZeContext;
32-
}
33-
34-
ur_context_handle_t_::~ur_context_handle_t_() noexcept(false) {
35-
// ur_context_handle_t_ is only created/destroyed through urContextCreate
36-
// and urContextRelease so it's safe to throw here
37-
ZE2UR_CALL_THROWS(zeContextDestroy, (hContext));
38-
}
39-
40-
ze_context_handle_t ur_context_handle_t_::getZeHandle() const {
41-
return hContext;
42-
}
30+
}) {}
4331

4432
ur_result_t ur_context_handle_t_::retain() {
4533
RefCount.increment();

source/adapters/level_zero/v2/context.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
#include <ur_api.h>
1414

1515
#include "command_list_cache.hpp"
16+
#include "common.hpp"
1617
#include "event_pool_cache.hpp"
1718

1819
struct ur_context_handle_t_ : _ur_object {
1920
ur_context_handle_t_(ze_context_handle_t hContext, uint32_t numDevices,
2021
const ur_device_handle_t *phDevices, bool ownZeContext);
21-
~ur_context_handle_t_() noexcept(false);
2222

2323
ur_result_t retain();
2424
ur_result_t release();
2525

26-
ze_context_handle_t getZeHandle() const;
26+
inline ze_context_handle_t getZeHandle() const { return hContext.get(); }
2727
ur_platform_handle_t getPlatform() const;
2828
const std::vector<ur_device_handle_t> &getDevices() const;
2929

3030
// Checks if Device is covered by this context.
3131
// For that the Device or its root devices need to be in the context.
3232
bool isValidDevice(ur_device_handle_t Device) const;
3333

34-
const ze_context_handle_t hContext;
34+
const v2::raii::ze_context_handle_t hContext;
3535
const std::vector<ur_device_handle_t> hDevices;
3636
v2::command_list_cache_t commandListCache;
3737
v2::event_pool_cache eventPoolCache;

source/adapters/level_zero/v2/event_provider_counter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ provider_counter::provider_counter(ur_platform_handle_t platform,
2727
ZE2UR_CALL_THROWS(zeDriverGetExtensionFunctionAddress,
2828
(platform->ZeDriver, "zexCounterBasedEventCreate",
2929
(void **)&this->eventCreateFunc));
30-
ZE2UR_CALL_THROWS(
31-
zelLoaderTranslateHandle,
32-
(ZEL_HANDLE_CONTEXT, context->hContext, (void **)&translatedContext));
30+
ZE2UR_CALL_THROWS(zelLoaderTranslateHandle,
31+
(ZEL_HANDLE_CONTEXT, context->getZeHandle(),
32+
(void **)&translatedContext));
3333
ZE2UR_CALL_THROWS(
3434
zelLoaderTranslateHandle,
3535
(ZEL_HANDLE_DEVICE, device->ZeDevice, (void **)&translatedDevice));

source/adapters/level_zero/v2/event_provider_normal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ provider_pool::provider_pool(ur_context_handle_t context,
4343
}
4444

4545
ZE2UR_CALL_THROWS(zeEventPoolCreate,
46-
(context->hContext, &desc, 1,
46+
(context->getZeHandle(), &desc, 1,
4747
const_cast<ze_device_handle_t *>(&device->ZeDevice),
4848
pool.ptr()));
4949

test/adapters/level_zero/v2/command_list_cache_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct CommandListCacheTest : public uur::urContextTest {};
2323
UUR_INSTANTIATE_DEVICE_TEST_SUITE_P(CommandListCacheTest);
2424

2525
TEST_P(CommandListCacheTest, CanStoreAndRetriveImmediateAndRegularCmdLists) {
26-
v2::command_list_cache_t cache(context->hContext);
26+
v2::command_list_cache_t cache(context->getZeHandle());
2727

2828
bool IsInOrder = false;
2929
uint32_t Ordinal = 0;
@@ -75,7 +75,7 @@ TEST_P(CommandListCacheTest, CanStoreAndRetriveImmediateAndRegularCmdLists) {
7575
}
7676

7777
TEST_P(CommandListCacheTest, ImmediateCommandListsHaveProperAttributes) {
78-
v2::command_list_cache_t cache(context->hContext);
78+
v2::command_list_cache_t cache(context->getZeHandle());
7979

8080
uint32_t numQueueGroups = 0;
8181
ASSERT_EQ(zeDeviceGetCommandQueueGroupProperties(device->ZeDevice,

0 commit comments

Comments
 (0)