Skip to content

Commit 9599d39

Browse files
[L0] Refactoring of boolean event parameters
CI in LLVM/SYCL: intel/llvm#14536 Signed-off-by: Winston Zhang <[email protected]>
1 parent 6298474 commit 9599d39

File tree

5 files changed

+87
-89
lines changed

5 files changed

+87
-89
lines changed

source/adapters/level_zero/common.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ enum {
240240
2, // blocking UR calls, where supported (usually in enqueue commands)
241241
};
242242

243+
typedef uint32_t ur_event_flags_t;
244+
typedef enum ur_event_flag_t {
245+
USING_IMM_CMDLIST = UR_BIT(0),
246+
HOST_VISIBLE = UR_BIT(1),
247+
COUNTER_BASED = UR_BIT(2),
248+
ENABLE_PROFILER = UR_BIT(3),
249+
MULTIDEVICE = UR_BIT(4),
250+
/// @cond
251+
UR_EVENT_FLAG_FORCE_UINT32 = 0x7FFFFFFF
252+
/// @endcond
253+
254+
} ur_event_flag_t;
255+
243256
static const uint32_t UrL0Serialize = [] {
244257
const char *ZeSerializeMode = std::getenv("ZE_SERIALIZE");
245258
const char *UrL0SerializeMode = std::getenv("UR_L0_SERIALIZE");

source/adapters/level_zero/context.cpp

+31-19
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,8 @@ static const uint32_t MaxNumEventsPerPool = [] {
473473
}();
474474

475475
ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
476-
ze_event_pool_handle_t &Pool, size_t &Index, bool HostVisible,
477-
bool ProfilingEnabled, ur_device_handle_t Device,
478-
bool CounterBasedEventEnabled, bool UsingImmCmdList) {
476+
ze_event_pool_handle_t &Pool, size_t &Index, ur_event_flags_t Flags,
477+
ur_device_handle_t Device) {
479478
// Lock while updating event pool machinery.
480479
std::scoped_lock<ur_mutex> Lock(ZeEventPoolCacheMutex);
481480

@@ -485,8 +484,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
485484
ZeDevice = Device->ZeDevice;
486485
}
487486
std::list<ze_event_pool_handle_t> *ZePoolCache =
488-
getZeEventPoolCache(HostVisible, ProfilingEnabled,
489-
CounterBasedEventEnabled, UsingImmCmdList, ZeDevice);
487+
getZeEventPoolCache(Flags, ZeDevice);
490488

491489
if (!ZePoolCache->empty()) {
492490
if (NumEventsAvailableInEventPool[ZePoolCache->front()] == 0) {
@@ -517,14 +515,14 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
517515
ZeEventPoolDesc.count = MaxNumEventsPerPool;
518516
ZeEventPoolDesc.flags = 0;
519517
ZeEventPoolDesc.pNext = nullptr;
520-
if (HostVisible)
518+
if (Flags & HOST_VISIBLE)
521519
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_HOST_VISIBLE;
522-
if (ProfilingEnabled)
520+
if (Flags & ENABLE_PROFILER)
523521
ZeEventPoolDesc.flags |= ZE_EVENT_POOL_FLAG_KERNEL_TIMESTAMP;
524522
logger::debug("ze_event_pool_desc_t flags set to: {}",
525523
ZeEventPoolDesc.flags);
526-
if (CounterBasedEventEnabled) {
527-
if (UsingImmCmdList) {
524+
if (Flags & COUNTER_BASED) {
525+
if (Flags & USING_IMM_CMDLIST) {
528526
counterBasedExt.flags = ZE_EVENT_POOL_COUNTER_BASED_EXP_FLAG_IMMEDIATE;
529527
} else {
530528
counterBasedExt.flags =
@@ -558,19 +556,21 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
558556
return UR_RESULT_SUCCESS;
559557
}
560558

561-
ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
562-
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
563-
bool CounterBasedEventEnabled) {
559+
ur_event_handle_t
560+
ur_context_handle_t_::getEventFromContextCache(ur_event_flags_t Flags,
561+
ur_device_handle_t Device) {
564562
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
565-
auto Cache = getEventCache(HostVisible, WithProfiling, Device);
563+
auto Cache = getEventCache(Flags, Device);
566564
if (Cache->empty())
567565
return nullptr;
568566

569567
auto It = Cache->begin();
570568
ur_event_handle_t Event = *It;
571-
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
569+
if (Event->CounterBasedEventsEnabled !=
570+
static_cast<bool>(Flags & COUNTER_BASED)) {
572571
return nullptr;
573572
}
573+
574574
Cache->erase(It);
575575
// We have to reset event before using it.
576576
Event->reset();
@@ -584,9 +584,13 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
584584
if (!Event->IsMultiDevice && Event->UrQueue) {
585585
Device = Event->UrQueue->Device;
586586
}
587+
ur_event_flags_t Flags = 0;
588+
if (Event->isHostVisible())
589+
Flags |= HOST_VISIBLE;
590+
if (Event->isProfilingEnabled())
591+
Flags |= ENABLE_PROFILER;
587592

588-
auto Cache = getEventCache(Event->isHostVisible(),
589-
Event->isProfilingEnabled(), Device);
593+
auto Cache = getEventCache(Flags, Device);
590594
Cache->emplace_back(Event);
591595
}
592596

@@ -609,9 +613,17 @@ ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
609613
ZeDevice = Event->UrQueue->Device->ZeDevice;
610614
}
611615

612-
std::list<ze_event_pool_handle_t> *ZePoolCache = getZeEventPoolCache(
613-
Event->isHostVisible(), Event->isProfilingEnabled(),
614-
Event->CounterBasedEventsEnabled, UsingImmediateCommandlists, ZeDevice);
616+
ur_event_flags_t Flags = 0;
617+
if (Event->isHostVisible())
618+
Flags |= HOST_VISIBLE;
619+
if (Event->isProfilingEnabled())
620+
Flags |= ENABLE_PROFILER;
621+
if (Event->CounterBasedEventsEnabled)
622+
Flags |= COUNTER_BASED;
623+
if (UsingImmediateCommandlists)
624+
Flags |= USING_IMM_CMDLIST;
625+
std::list<ze_event_pool_handle_t> *ZePoolCache =
626+
getZeEventPoolCache(Flags, ZeDevice);
615627

616628
// Put the empty pool to the cache of the pools.
617629
if (NumEventsUnreleasedInEventPool[Event->ZeEventPool] == 0)

source/adapters/level_zero/context.hpp

+24-60
Original file line numberDiff line numberDiff line change
@@ -201,75 +201,38 @@ struct ur_context_handle_t_ : _ur_object {
201201
// slot for a host-visible event. The ProfilingEnabled tells is we need a
202202
// slot for an event with profiling capabilities.
203203
ur_result_t getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &, size_t &,
204-
bool HostVisible,
205-
bool ProfilingEnabled,
206-
ur_device_handle_t Device,
207-
bool CounterBasedEventEnabled,
208-
bool UsingImmCmdList);
204+
ur_event_flags_t Flags,
205+
ur_device_handle_t Device);
209206

210207
// Get ur_event_handle_t from cache.
211-
ur_event_handle_t getEventFromContextCache(bool HostVisible,
212-
bool WithProfiling,
213-
ur_device_handle_t Device,
214-
bool CounterBasedEventEnabled);
208+
ur_event_handle_t getEventFromContextCache(ur_event_flags_t Flags,
209+
ur_device_handle_t Device);
215210

216211
// Add ur_event_handle_t to cache.
217212
void addEventToContextCache(ur_event_handle_t);
218213

219-
enum EventPoolCacheType {
220-
HostVisibleCacheType,
221-
HostInvisibleCacheType,
222-
HostVisibleCounterBasedRegularCacheType,
223-
HostInvisibleCounterBasedRegularCacheType,
224-
HostVisibleCounterBasedImmediateCacheType,
225-
HostInvisibleCounterBasedImmediateCacheType
226-
};
227-
228214
std::list<ze_event_pool_handle_t> *
229-
getZeEventPoolCache(bool HostVisible, bool WithProfiling,
230-
bool CounterBasedEventEnabled, bool UsingImmediateCmdList,
231-
ze_device_handle_t ZeDevice) {
232-
EventPoolCacheType CacheType;
215+
getZeEventPoolCache(ur_event_flags_t Flags, ze_device_handle_t ZeDevice) {
216+
bool WithProfiling = Flags & ENABLE_PROFILER;
217+
Flags &= ~ENABLE_PROFILER;
218+
Flags &= ~MULTIDEVICE;
219+
int index = static_cast<int>(Flags);
220+
(Flags & COUNTER_BASED) ? index /= 2 : (index - 2);
233221

234-
calculateCacheIndex(HostVisible, CounterBasedEventEnabled,
235-
UsingImmediateCmdList, CacheType);
236222
if (ZeDevice) {
237223
auto ZeEventPoolCacheMap =
238-
WithProfiling ? &ZeEventPoolCacheDeviceMap[CacheType * 2]
239-
: &ZeEventPoolCacheDeviceMap[CacheType * 2 + 1];
224+
WithProfiling ? &ZeEventPoolCacheDeviceMap[index * 2]
225+
: &ZeEventPoolCacheDeviceMap[index * 2 + 1];
240226
if (ZeEventPoolCacheMap->find(ZeDevice) == ZeEventPoolCacheMap->end()) {
241227
ZeEventPoolCache.emplace_back();
242228
ZeEventPoolCacheMap->insert(
243229
std::make_pair(ZeDevice, ZeEventPoolCache.size() - 1));
244230
}
245231
return &ZeEventPoolCache[(*ZeEventPoolCacheMap)[ZeDevice]];
246232
} else {
247-
return WithProfiling ? &ZeEventPoolCache[CacheType * 2]
248-
: &ZeEventPoolCache[CacheType * 2 + 1];
249-
}
250-
}
251-
252-
ur_result_t calculateCacheIndex(bool HostVisible,
253-
bool CounterBasedEventEnabled,
254-
bool UsingImmediateCmdList,
255-
EventPoolCacheType &CacheType) {
256-
if (CounterBasedEventEnabled && HostVisible && !UsingImmediateCmdList) {
257-
CacheType = HostVisibleCounterBasedRegularCacheType;
258-
} else if (CounterBasedEventEnabled && !HostVisible &&
259-
!UsingImmediateCmdList) {
260-
CacheType = HostInvisibleCounterBasedRegularCacheType;
261-
} else if (CounterBasedEventEnabled && HostVisible &&
262-
UsingImmediateCmdList) {
263-
CacheType = HostVisibleCounterBasedImmediateCacheType;
264-
} else if (CounterBasedEventEnabled && !HostVisible &&
265-
UsingImmediateCmdList) {
266-
CacheType = HostInvisibleCounterBasedImmediateCacheType;
267-
} else if (!CounterBasedEventEnabled && HostVisible) {
268-
CacheType = HostVisibleCacheType;
269-
} else {
270-
CacheType = HostInvisibleCacheType;
233+
return WithProfiling ? &ZeEventPoolCache[index * 2]
234+
: &ZeEventPoolCache[index * 2 + 1];
271235
}
272-
return UR_RESULT_SUCCESS;
273236
}
274237

275238
// Decrement number of events living in the pool upon event destroy
@@ -311,33 +274,34 @@ struct ur_context_handle_t_ : _ur_object {
311274

312275
private:
313276
// Get the cache of events for a provided scope and profiling mode.
314-
auto getEventCache(bool HostVisible, bool WithProfiling,
315-
ur_device_handle_t Device) {
316-
if (HostVisible) {
277+
auto getEventCache(ur_event_flags_t Flags, ur_device_handle_t Device) {
278+
if (Flags & HOST_VISIBLE) {
317279
if (Device) {
318-
auto EventCachesMap =
319-
WithProfiling ? &EventCachesDeviceMap[0] : &EventCachesDeviceMap[1];
280+
auto EventCachesMap = (Flags & ENABLE_PROFILER)
281+
? &EventCachesDeviceMap[0]
282+
: &EventCachesDeviceMap[1];
320283
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
321284
EventCaches.emplace_back();
322285
EventCachesMap->insert(
323286
std::make_pair(Device, EventCaches.size() - 1));
324287
}
325288
return &EventCaches[(*EventCachesMap)[Device]];
326289
} else {
327-
return WithProfiling ? &EventCaches[0] : &EventCaches[1];
290+
return (Flags & ENABLE_PROFILER) ? &EventCaches[0] : &EventCaches[1];
328291
}
329292
} else {
330293
if (Device) {
331-
auto EventCachesMap =
332-
WithProfiling ? &EventCachesDeviceMap[2] : &EventCachesDeviceMap[3];
294+
auto EventCachesMap = (Flags & ENABLE_PROFILER)
295+
? &EventCachesDeviceMap[2]
296+
: &EventCachesDeviceMap[3];
333297
if (EventCachesMap->find(Device) == EventCachesMap->end()) {
334298
EventCaches.emplace_back();
335299
EventCachesMap->insert(
336300
std::make_pair(Device, EventCaches.size() - 1));
337301
}
338302
return &EventCaches[(*EventCachesMap)[Device]];
339303
} else {
340-
return WithProfiling ? &EventCaches[2] : &EventCaches[3];
304+
return (Flags & ENABLE_PROFILER) ? &EventCaches[2] : &EventCaches[3];
341305
}
342306
}
343307
}

source/adapters/level_zero/event.cpp

+18-10
Original file line numberDiff line numberDiff line change
@@ -1249,15 +1249,24 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12491249
bool ProfilingEnabled =
12501250
ForceDisableProfiling ? false : (!Queue || Queue->isProfilingEnabled());
12511251
bool UsingImmediateCommandlists = !Queue || Queue->UsingImmCmdLists;
1252+
ur_event_flags_t Flags = 0;
1253+
if (ProfilingEnabled)
1254+
Flags |= ENABLE_PROFILER;
1255+
if (UsingImmediateCommandlists)
1256+
Flags |= USING_IMM_CMDLIST;
1257+
if (HostVisible)
1258+
Flags |= HOST_VISIBLE;
1259+
if (IsMultiDevice)
1260+
Flags |= MULTIDEVICE;
1261+
if (CounterBasedEventEnabled)
1262+
Flags |= COUNTER_BASED;
12521263

12531264
ur_device_handle_t Device = nullptr;
1254-
1255-
if (!IsMultiDevice && Queue) {
1265+
if (!(Flags & MULTIDEVICE) && Queue) {
12561266
Device = Queue->Device;
12571267
}
12581268

1259-
if (auto CachedEvent = Context->getEventFromContextCache(
1260-
HostVisible, ProfilingEnabled, Device, CounterBasedEventEnabled)) {
1269+
if (auto CachedEvent = Context->getEventFromContextCache(Flags, Device)) {
12611270
*RetEvent = CachedEvent;
12621271
return UR_RESULT_SUCCESS;
12631272
}
@@ -1267,16 +1276,15 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
12671276

12681277
size_t Index = 0;
12691278

1270-
if (auto Res = Context->getFreeSlotInExistingOrNewPool(
1271-
ZeEventPool, Index, HostVisible, ProfilingEnabled, Device,
1272-
CounterBasedEventEnabled, UsingImmediateCommandlists))
1279+
if (auto Res = Context->getFreeSlotInExistingOrNewPool(ZeEventPool, Index,
1280+
Flags, Device))
12731281
return Res;
12741282

12751283
ZeStruct<ze_event_desc_t> ZeEventDesc;
12761284
ZeEventDesc.index = Index;
12771285
ZeEventDesc.wait = 0;
12781286

1279-
if (HostVisible || CounterBasedEventEnabled) {
1287+
if ((Flags & USING_IMM_CMDLIST) || (Flags & COUNTER_BASED)) {
12801288
ZeEventDesc.signal = ZE_EVENT_SCOPE_FLAG_HOST;
12811289
} else {
12821290
//
@@ -1301,8 +1309,8 @@ ur_result_t EventCreate(ur_context_handle_t Context, ur_queue_handle_t Queue,
13011309
} catch (...) {
13021310
return UR_RESULT_ERROR_UNKNOWN;
13031311
}
1304-
(*RetEvent)->CounterBasedEventsEnabled = CounterBasedEventEnabled;
1305-
if (HostVisible)
1312+
(*RetEvent)->CounterBasedEventsEnabled = (Flags & COUNTER_BASED);
1313+
if (Flags & HOST_VISIBLE)
13061314
(*RetEvent)->HostVisibleEvent =
13071315
reinterpret_cast<ur_event_handle_t>(*RetEvent);
13081316

source/adapters/level_zero/event.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ struct ur_event_handle_t_ : _ur_object {
132132
OwnNativeHandle = OwnZeEvent;
133133
}
134134

135+
ur_event_flags_t flags;
135136
// Level Zero event handle.
136137
ze_event_handle_t ZeEvent;
137138

0 commit comments

Comments
 (0)