Skip to content

Commit 88c3287

Browse files
authored
Merge pull request #2051 from igchor/ze_handle
[L0 v2] Use ze_handle_wrappers to hold ze_handles
2 parents 76ef4d5 + 35b8ef5 commit 88c3287

File tree

8 files changed

+38
-43
lines changed

8 files changed

+38
-43
lines changed

source/adapters/level_zero/v2/command_list_cache.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ inline size_t command_list_descriptor_hash_t::operator()(
4343
command_list_cache_t::command_list_cache_t(ze_context_handle_t ZeContext)
4444
: ZeContext{ZeContext} {}
4545

46-
raii::ze_command_list_t
46+
raii::ze_command_list_handle_t
4747
command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) {
4848
if (auto ImmCmdDesc =
4949
std::get_if<immediate_command_list_descriptor_t>(&desc)) {
@@ -61,7 +61,7 @@ command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) {
6161
ZE2UR_CALL_THROWS(
6262
zeCommandListCreateImmediate,
6363
(ZeContext, ImmCmdDesc->ZeDevice, &QueueDesc, &ZeCommandList));
64-
return raii::ze_command_list_t(ZeCommandList, &zeCommandListDestroy);
64+
return raii::ze_command_list_handle_t(ZeCommandList);
6565
} else {
6666
auto RegCmdDesc = std::get<regular_command_list_descriptor_t>(desc);
6767
ZeStruct<ze_command_list_desc_t> CmdListDesc;
@@ -72,7 +72,7 @@ command_list_cache_t::createCommandList(const command_list_descriptor_t &desc) {
7272
ze_command_list_handle_t ZeCommandList;
7373
ZE2UR_CALL_THROWS(zeCommandListCreate, (ZeContext, RegCmdDesc.ZeDevice,
7474
&CmdListDesc, &ZeCommandList));
75-
return raii::ze_command_list_t(ZeCommandList, &zeCommandListDestroy);
75+
return raii::ze_command_list_handle_t(ZeCommandList);
7676
}
7777
}
7878

@@ -94,8 +94,7 @@ command_list_cache_t::getImmediateCommandList(
9494
auto CommandList = getCommandList(Desc).release();
9595
return raii::cache_borrowed_command_list_t(
9696
CommandList, [Cache = this, Desc](ze_command_list_handle_t CmdList) {
97-
Cache->addCommandList(
98-
Desc, raii::ze_command_list_t(CmdList, &zeCommandListDestroy));
97+
Cache->addCommandList(Desc, raii::ze_command_list_handle_t(CmdList));
9998
});
10099
}
101100

@@ -113,12 +112,11 @@ command_list_cache_t::getRegularCommandList(ze_device_handle_t ZeDevice,
113112

114113
return raii::cache_borrowed_command_list_t(
115114
CommandList, [Cache = this, Desc](ze_command_list_handle_t CmdList) {
116-
Cache->addCommandList(
117-
Desc, raii::ze_command_list_t(CmdList, &zeCommandListDestroy));
115+
Cache->addCommandList(Desc, raii::ze_command_list_handle_t(CmdList));
118116
});
119117
}
120118

121-
raii::ze_command_list_t
119+
raii::ze_command_list_handle_t
122120
command_list_cache_t::getCommandList(const command_list_descriptor_t &desc) {
123121
std::unique_lock<ur_mutex> Lock(ZeCommandListCacheMutex);
124122
auto it = ZeCommandListCache.find(desc);
@@ -129,7 +127,8 @@ command_list_cache_t::getCommandList(const command_list_descriptor_t &desc) {
129127

130128
assert(!it->second.empty());
131129

132-
raii::ze_command_list_t CommandListHandle = std::move(it->second.top());
130+
raii::ze_command_list_handle_t CommandListHandle =
131+
std::move(it->second.top());
133132
it->second.pop();
134133

135134
if (it->second.empty())
@@ -138,8 +137,9 @@ command_list_cache_t::getCommandList(const command_list_descriptor_t &desc) {
138137
return CommandListHandle;
139138
}
140139

141-
void command_list_cache_t::addCommandList(const command_list_descriptor_t &desc,
142-
raii::ze_command_list_t cmdList) {
140+
void command_list_cache_t::addCommandList(
141+
const command_list_descriptor_t &desc,
142+
raii::ze_command_list_handle_t cmdList) {
143143
// TODO: add a limit?
144144
std::unique_lock<ur_mutex> Lock(ZeCommandListCacheMutex);
145145
auto [it, _] = ZeCommandListCache.try_emplace(desc);

source/adapters/level_zero/v2/command_list_cache.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
#include <ur_ddi.h>
1818
#include <ze_api.h>
1919

20-
#include "../common.hpp"
20+
#include "common.hpp"
2121

2222
namespace v2 {
2323
namespace raii {
24-
using ze_command_list_t = std::unique_ptr<::_ze_command_list_handle_t,
25-
decltype(&zeCommandListDestroy)>;
2624
using cache_borrowed_command_list_t =
2725
std::unique_ptr<::_ze_command_list_handle_t,
28-
std::function<void(ze_command_list_handle_t)>>;
26+
std::function<void(::ze_command_list_handle_t)>>;
2927
} // namespace raii
3028

3129
struct immediate_command_list_descriptor_t {
@@ -72,15 +70,16 @@ struct command_list_cache_t {
7270
private:
7371
ze_context_handle_t ZeContext;
7472
std::unordered_map<command_list_descriptor_t,
75-
std::stack<raii::ze_command_list_t>,
73+
std::stack<raii::ze_command_list_handle_t>,
7674
command_list_descriptor_hash_t>
7775
ZeCommandListCache;
7876
ur_mutex ZeCommandListCacheMutex;
7977

80-
raii::ze_command_list_t getCommandList(const command_list_descriptor_t &desc);
78+
raii::ze_command_list_handle_t
79+
getCommandList(const command_list_descriptor_t &desc);
8180
void addCommandList(const command_list_descriptor_t &desc,
82-
raii::ze_command_list_t cmdList);
83-
raii::ze_command_list_t
81+
raii::ze_command_list_handle_t cmdList);
82+
raii::ze_command_list_handle_t
8483
createCommandList(const command_list_descriptor_t &desc);
8584
};
8685
} // namespace v2

source/adapters/level_zero/v2/common.hpp

Lines changed: 8 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

@@ -94,5 +96,11 @@ using ze_event_handle_t =
9496
using ze_event_pool_handle_t =
9597
ze_handle_wrapper<::ze_event_pool_handle_t, zeEventPoolDestroy>;
9698

99+
using ze_context_handle_t =
100+
ze_handle_wrapper<::ze_context_handle_t, zeContextDestroy>;
101+
102+
using ze_command_list_handle_t =
103+
ze_handle_wrapper<::ze_command_list_handle_t, zeCommandListDestroy>;
104+
97105
} // namespace raii
98106
} // namespace v2

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)