Skip to content

Commit 172de6c

Browse files
committed
[L0 v2] Use ze_handle_wraper instead of custom raii handle
in command_list_cache
1 parent 7875d58 commit 172de6c

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
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_api.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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ using ze_event_pool_handle_t =
163163
using ze_context_handle_t =
164164
ze_handle_wrapper<::ze_context_handle_t, zeContextDestroy>;
165165

166+
using ze_command_list_handle_t =
167+
ze_handle_wrapper<::ze_command_list_handle_t, zeCommandListDestroy>;
168+
166169
using ur_queue_shared_handle_t =
167170
ur_shared_handle<ur_queue_handle_t, urQueueRetain, urQueueRelease>;
168171

0 commit comments

Comments
 (0)