Skip to content

Commit f6d68bd

Browse files
committed
Don't use inheritence for L0 V2 event handles
In the future, we are going to require that handle objects have no vtable, so this change combines "native" and "pooled" event into one class. A variant is used to determine whether the event is native or pooled. For consistency, parameter order in constructors have been changed to always start with the context.
1 parent 6026786 commit f6d68bd

File tree

4 files changed

+62
-64
lines changed

4 files changed

+62
-64
lines changed

source/adapters/level_zero/v2/event.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,17 @@ uint64_t *event_profiling_data_t::eventEndTimestampAddr() {
8787
return &recordEventEndTimestamp;
8888
}
8989

90-
ur_event_handle_t_::ur_event_handle_t_(ur_context_handle_t hContext,
91-
ze_event_handle_t hZeEvent,
92-
v2::event_flags_t flags)
93-
: hContext(hContext), hZeEvent(hZeEvent), flags(flags),
94-
profilingData(hZeEvent) {}
90+
ur_event_handle_t_::ur_event_handle_t_(
91+
ur_context_handle_t hContext, ur_event_handle_t_::event_variant hZeEvent,
92+
v2::event_flags_t flags, v2::event_pool *pool)
93+
: hContext(hContext), event_pool(pool), hZeEvent(std::move(hZeEvent)),
94+
flags(flags), profilingData(getZeEvent()) {}
9595

9696
void ur_event_handle_t_::resetQueueAndCommand(ur_queue_handle_t hQueue,
9797
ur_command_t commandType) {
9898
this->hQueue = hQueue;
9999
this->commandType = commandType;
100-
profilingData = event_profiling_data_t(hZeEvent);
100+
profilingData = event_profiling_data_t(getZeEvent());
101101
}
102102

103103
void ur_event_handle_t_::recordStartTimestamp() {
@@ -123,13 +123,16 @@ void ur_event_handle_t_::reset() {
123123
// consider make an abstraction for regular/counter based
124124
// events if there's more of this type of conditions
125125
if (!(flags & v2::EVENT_FLAGS_COUNTER)) {
126-
zeEventHostReset(hZeEvent);
126+
zeEventHostReset(getZeEvent());
127127
}
128128
}
129129

130130
ze_event_handle_t ur_event_handle_t_::getZeEvent() const {
131-
assert(hZeEvent);
132-
return hZeEvent;
131+
if (event_pool) {
132+
return std::get<v2::raii::cache_borrowed_event>(hZeEvent).get();
133+
} else {
134+
return std::get<v2::raii::ze_event_handle_t>(hZeEvent).get();
135+
}
133136
}
134137

135138
ur_result_t ur_event_handle_t_::retain() {
@@ -138,7 +141,7 @@ ur_result_t ur_event_handle_t_::retain() {
138141
}
139142

140143
ur_result_t ur_event_handle_t_::releaseDeferred() {
141-
assert(zeEventQueryStatus(hZeEvent) == ZE_RESULT_SUCCESS);
144+
assert(zeEventQueryStatus(getZeEvent()) == ZE_RESULT_SUCCESS);
142145
assert(RefCount.load() == 0);
143146

144147
return this->forceRelease();
@@ -176,7 +179,7 @@ bool ur_event_handle_t_::isProfilingEnabled() const {
176179

177180
std::pair<uint64_t *, ze_event_handle_t>
178181
ur_event_handle_t_::getEventEndTimestampAndHandle() {
179-
return {profilingData.eventEndTimestampAddr(), hZeEvent};
182+
return {profilingData.eventEndTimestampAddr(), getZeEvent()};
180183
}
181184

182185
ur_queue_handle_t ur_event_handle_t_::getQueue() const { return hQueue; }
@@ -185,29 +188,33 @@ ur_context_handle_t ur_event_handle_t_::getContext() const { return hContext; }
185188

186189
ur_command_t ur_event_handle_t_::getCommandType() const { return commandType; }
187190

188-
ur_pooled_event_t::ur_pooled_event_t(
191+
ur_event_handle_t_::ur_event_handle_t_(
189192
ur_context_handle_t hContext,
190193
v2::raii::cache_borrowed_event eventAllocation, v2::event_pool *pool)
191-
: ur_event_handle_t_(hContext, eventAllocation.get(), pool->getFlags()),
192-
zeEvent(std::move(eventAllocation)), pool(pool) {}
193-
194-
ur_result_t ur_pooled_event_t::forceRelease() {
195-
pool->free(this);
196-
return UR_RESULT_SUCCESS;
197-
}
194+
: ur_event_handle_t_(hContext, std::move(eventAllocation), pool->getFlags(),
195+
pool) {}
198196

199-
ur_native_event_t::ur_native_event_t(
200-
ur_native_handle_t hNativeEvent, ur_context_handle_t hContext,
197+
ur_event_handle_t_::ur_event_handle_t_(
198+
ur_context_handle_t hContext, ur_native_handle_t hNativeEvent,
201199
const ur_event_native_properties_t *pProperties)
202200
: ur_event_handle_t_(
203201
hContext,
204-
reinterpret_cast<ze_event_handle_t>(hNativeEvent), v2::EVENT_FLAGS_PROFILING_ENABLED /* TODO: this follows legacy adapter logic, we could check this with zeEventGetPool */),
205-
zeEvent(reinterpret_cast<ze_event_handle_t>(hNativeEvent),
206-
pProperties ? pProperties->isNativeHandleOwned : false) {}
207-
208-
ur_result_t ur_native_event_t::forceRelease() {
209-
zeEvent.release();
210-
delete this;
202+
v2::raii::ze_event_handle_t{
203+
reinterpret_cast<ze_event_handle_t>(hNativeEvent),
204+
pProperties ? pProperties->isNativeHandleOwned : false},
205+
v2::EVENT_FLAGS_PROFILING_ENABLED /* TODO: this follows legacy adapter
206+
logic, we could check this with
207+
zeEventGetPool */
208+
,
209+
nullptr) {}
210+
211+
ur_result_t ur_event_handle_t_::forceRelease() {
212+
if (event_pool) {
213+
event_pool->free(this);
214+
} else {
215+
std::get<v2::raii::ze_event_handle_t>(hZeEvent).release();
216+
delete this;
217+
}
211218
return UR_RESULT_SUCCESS;
212219
}
213220

@@ -389,7 +396,7 @@ urEventCreateWithNativeHandle(ur_native_handle_t hNativeEvent,
389396
*phEvent = hContext->nativeEventsPool.allocate();
390397
ZE2UR_CALL(zeEventHostSignal, ((*phEvent)->getZeEvent()));
391398
} else {
392-
*phEvent = new ur_native_event_t(hNativeEvent, hContext, pProperties);
399+
*phEvent = new ur_event_handle_t_(hContext, hNativeEvent, pProperties);
393400
}
394401
return UR_RESULT_SUCCESS;
395402
} catch (...) {

source/adapters/level_zero/v2/event.hpp

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,24 @@ struct event_profiling_data_t {
4747

4848
struct ur_event_handle_t_ : _ur_object {
4949
public:
50-
ur_event_handle_t_(ur_context_handle_t hContext, ze_event_handle_t hZeEvent,
51-
v2::event_flags_t flags);
50+
// cache_borrowed_event is used for pooled events, whilst ze_event_handle_t is
51+
// used for native events
52+
using event_variant =
53+
std::variant<v2::raii::cache_borrowed_event, v2::raii::ze_event_handle_t>;
54+
55+
ur_event_handle_t_(ur_context_handle_t hContext,
56+
v2::raii::cache_borrowed_event eventAllocation,
57+
v2::event_pool *pool);
58+
59+
ur_event_handle_t_(ur_context_handle_t hContext,
60+
ur_native_handle_t hNativeEvent,
61+
const ur_event_native_properties_t *pProperties);
5262

5363
// Set the queue and command that this event is associated with
5464
void resetQueueAndCommand(ur_queue_handle_t hQueue, ur_command_t commandType);
5565

5666
// releases event immediately
57-
virtual ur_result_t forceRelease() = 0;
58-
virtual ~ur_event_handle_t_() = default;
67+
ur_result_t forceRelease();
5968

6069
void reset();
6170
ze_event_handle_t getZeEvent() const;
@@ -97,11 +106,16 @@ struct ur_event_handle_t_ : _ur_object {
97106
uint64_t getEventStartTimestmap() const;
98107
uint64_t getEventEndTimestamp();
99108

109+
private:
110+
ur_event_handle_t_(ur_context_handle_t hContext, event_variant hZeEvent,
111+
v2::event_flags_t flags, v2::event_pool *pool);
112+
100113
protected:
101114
ur_context_handle_t hContext;
102115

103-
// non-owning handle to the L0 event
104-
const ze_event_handle_t hZeEvent;
116+
// Pool is used if and only if this is a pooled event
117+
v2::event_pool *event_pool = nullptr;
118+
event_variant hZeEvent;
105119

106120
// queue and commandType that this event is associated with, set by enqueue
107121
// commands
@@ -111,26 +125,3 @@ struct ur_event_handle_t_ : _ur_object {
111125
v2::event_flags_t flags;
112126
event_profiling_data_t profilingData;
113127
};
114-
115-
struct ur_pooled_event_t : ur_event_handle_t_ {
116-
ur_pooled_event_t(ur_context_handle_t hContext,
117-
v2::raii::cache_borrowed_event eventAllocation,
118-
v2::event_pool *pool);
119-
120-
ur_result_t forceRelease() override;
121-
122-
private:
123-
v2::raii::cache_borrowed_event zeEvent;
124-
v2::event_pool *pool;
125-
};
126-
127-
struct ur_native_event_t : ur_event_handle_t_ {
128-
ur_native_event_t(ur_native_handle_t hNativeEvent,
129-
ur_context_handle_t hContext,
130-
const ur_event_native_properties_t *pProperties);
131-
132-
ur_result_t forceRelease() override;
133-
134-
private:
135-
v2::raii::ze_event_handle_t zeEvent;
136-
};

source/adapters/level_zero/v2/event_pool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace v2 {
1717

1818
static constexpr size_t EVENTS_BURST = 64;
1919

20-
ur_pooled_event_t *event_pool::allocate() {
20+
ur_event_handle_t event_pool::allocate() {
2121
TRACK_SCOPE_LATENCY("event_pool::allocate");
2222

2323
std::unique_lock<std::mutex> lock(*mutex);
@@ -42,7 +42,7 @@ ur_pooled_event_t *event_pool::allocate() {
4242
return event;
4343
}
4444

45-
void event_pool::free(ur_pooled_event_t *event) {
45+
void event_pool::free(ur_event_handle_t event) {
4646
TRACK_SCOPE_LATENCY("event_pool::free");
4747

4848
std::unique_lock<std::mutex> lock(*mutex);

source/adapters/level_zero/v2/event_pool.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ class event_pool {
4141
event_pool &operator=(const event_pool &) = delete;
4242

4343
// Allocate an event from the pool. Thread safe.
44-
ur_pooled_event_t *allocate();
44+
ur_event_handle_t allocate();
4545

4646
// Free an event back to the pool. Thread safe.
47-
void free(ur_pooled_event_t *event);
47+
void free(ur_event_handle_t event);
4848

4949
event_provider *getProvider() const;
5050
event_flags_t getFlags() const;
@@ -53,8 +53,8 @@ class event_pool {
5353
ur_context_handle_t hContext;
5454
std::unique_ptr<event_provider> provider;
5555

56-
std::deque<ur_pooled_event_t> events;
57-
std::vector<ur_pooled_event_t *> freelist;
56+
std::deque<ur_event_handle_t_> events;
57+
std::vector<ur_event_handle_t> freelist;
5858

5959
std::unique_ptr<std::mutex> mutex;
6060
};

0 commit comments

Comments
 (0)