Skip to content

Commit 45d38a2

Browse files
committed
Remove legacy error checking Pt 3
1 parent 1221006 commit 45d38a2

File tree

10 files changed

+74
-72
lines changed

10 files changed

+74
-72
lines changed

offload/plugins-nextgen/level_zero/include/L0Context.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class L0ContextTy {
7171

7272
/// Create context, initialize event pool and extension functions
7373
L0ContextTy(LevelZeroPluginTy &Plugin, ze_driver_handle_t zeDriver,
74-
int32_t DriverId);
74+
int32_t DriverId)
75+
: Plugin(Plugin), zeDriver(zeDriver) {}
7576

7677
L0ContextTy(const L0ContextTy &) = delete;
7778
L0ContextTy(L0ContextTy &&) = delete;
@@ -81,15 +82,8 @@ class L0ContextTy {
8182
/// Release resources
8283
~L0ContextTy() {}
8384

84-
Error deinit() {
85-
EventPool.deinit();
86-
auto Err = HostMemAllocator.deinit();
87-
if (Err)
88-
return Err;
89-
if (zeContext)
90-
CALL_ZE_RET_ERROR(zeContextDestroy, zeContext);
91-
return Error::success();
92-
}
85+
Error init();
86+
Error deinit();
9387

9488
auto &getPlugin() const { return Plugin; }
9589

offload/plugins-nextgen/level_zero/include/L0Defs.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,6 @@ using namespace error;
4242
/// Generic L0 handle type
4343
using ZeHandleTy = void *;
4444

45-
template <typename... ArgsTy>
46-
static Error Plugin::check(int32_t Code, const char *ErrFmt, ArgsTy... Args) {
47-
48-
if (Code == OFFLOAD_SUCCESS)
49-
return Plugin::success();
50-
const char *Desc = "Unknown error";
51-
return createStringError<ArgsTy..., const char *>(inconvertibleErrorCode(),
52-
ErrFmt, Args..., Desc);
53-
}
54-
5545
} // namespace llvm::omp::target::plugin
5646

5747
#endif // OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_LEVEL_ZERO_L0DEFS_H

offload/plugins-nextgen/level_zero/include/L0Device.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,6 @@ class L0DeviceTy final : public GenericDeviceTy {
267267
/// Get copy command queue group ordinal. Returns Ordinal-NumQueues pair
268268
std::pair<uint32_t, uint32_t> findCopyOrdinal(bool LinkCopy = false);
269269

270-
Error internalInit();
271-
272270
public:
273271
L0DeviceTy(GenericPluginTy &Plugin, int32_t DeviceId, int32_t NumDevices,
274272
ze_device_handle_t zeDevice, L0ContextTy &DriverInfo,
@@ -284,11 +282,6 @@ class L0DeviceTy final : public GenericDeviceTy {
284282
MemoryProperties.pNext = nullptr;
285283
CacheProperties.stype = ZE_STRUCTURE_TYPE_DEVICE_CACHE_PROPERTIES;
286284
CacheProperties.pNext = nullptr;
287-
288-
auto Err = internalInit();
289-
if (Err)
290-
FATAL_MESSAGE(DeviceId, "Couldn't initialize device: %s\n",
291-
toString(std::move(Err)).c_str());
292285
}
293286

294287
static L0DeviceTy &makeL0Device(GenericDeviceTy &Device) {
@@ -507,11 +500,13 @@ class L0DeviceTy final : public GenericDeviceTy {
507500
}
508501

509502
/// Return an event from the driver associated to this device
510-
ze_event_handle_t getEvent() { return l0Context.getEventPool().getEvent(); }
503+
Expected<ze_event_handle_t> getEvent() {
504+
return l0Context.getEventPool().getEvent();
505+
}
511506

512507
/// Release event to the pool associated to this device
513-
void releaseEvent(ze_event_handle_t Event) {
514-
l0Context.getEventPool().releaseEvent(Event, *this);
508+
Error releaseEvent(ze_event_handle_t Event) {
509+
return l0Context.getEventPool().releaseEvent(Event, *this);
515510
}
516511

517512
StagingBufferTy &getStagingBuffer() { return l0Context.getStagingBuffer(); }

offload/plugins-nextgen/level_zero/include/L0Memory.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,25 +464,27 @@ class EventPoolTy {
464464

465465
public:
466466
/// Initialize context, flags, and mutex
467-
void init(ze_context_handle_t ContextIn, uint32_t FlagsIn) {
467+
Error init(ze_context_handle_t ContextIn, uint32_t FlagsIn) {
468468
Context = ContextIn;
469469
Flags = FlagsIn;
470470
Mtx.reset(new std::mutex);
471+
return Plugin::success();
471472
}
472473

473474
/// Destroys L0 resources
474-
void deinit() {
475+
Error deinit() {
475476
for (auto E : Events)
476-
CALL_ZE_RET_VOID(zeEventDestroy, E);
477+
CALL_ZE_RET_ERROR(zeEventDestroy, E);
477478
for (auto P : Pools)
478-
CALL_ZE_RET_VOID(zeEventPoolDestroy, P);
479+
CALL_ZE_RET_ERROR(zeEventPoolDestroy, P);
480+
return Plugin::success();
479481
}
480482

481483
/// Get a free event from the pool
482-
ze_event_handle_t getEvent();
484+
Expected<ze_event_handle_t> getEvent();
483485

484486
/// Return an event to the pool
485-
void releaseEvent(ze_event_handle_t Event, L0DeviceTy &Device);
487+
Error releaseEvent(ze_event_handle_t Event, L0DeviceTy &Device);
486488
};
487489

488490
/// Staging buffer

offload/plugins-nextgen/level_zero/include/L0Plugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class LevelZeroPluginTy final : public GenericPluginTy {
8181

8282
/// Find L0 devices and initialize device properties.
8383
/// Returns number of devices reported to omptarget.
84-
int32_t findDevices();
84+
Expected<int32_t> findDevices();
8585

8686
L0DeviceTy &getDeviceFromId(int32_t DeviceId) const {
8787
assert("Invalid device ID" && DeviceId >= 0 &&

offload/plugins-nextgen/level_zero/src/L0Context.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@
1515

1616
namespace llvm::omp::target::plugin {
1717

18-
L0ContextTy::L0ContextTy(LevelZeroPluginTy &Plugin, ze_driver_handle_t zeDriver,
19-
int32_t /*DriverId*/)
20-
: Plugin(Plugin), zeDriver(zeDriver) {
21-
CALL_ZE_RET_VOID(zeDriverGetApiVersion, zeDriver, &APIVersion);
18+
Error L0ContextTy::init() {
19+
CALL_ZE_RET_ERROR(zeDriverGetApiVersion, zeDriver, &APIVersion);
2220
DP("Driver API version is %" PRIx32 "\n", APIVersion);
2321

2422
ze_context_desc_t Desc{ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};
25-
CALL_ZE_RET_VOID(zeContextCreate, zeDriver, &Desc, &zeContext);
26-
27-
EventPool.init(zeContext, 0);
23+
CALL_ZE_RET_ERROR(zeContextCreate, zeDriver, &Desc, &zeContext);
24+
if (auto Err = EventPool.init(zeContext, 0))
25+
return Err;
2826
HostMemAllocator.initHostPool(*this, Plugin.getOptions());
27+
return Plugin::success();
28+
}
29+
30+
Error L0ContextTy::deinit() {
31+
if (auto Err = EventPool.deinit())
32+
return Err;
33+
if (auto Err = HostMemAllocator.deinit())
34+
return Err;
35+
if (zeContext)
36+
CALL_ZE_RET_ERROR(zeContextDestroy, zeContext);
37+
return Plugin::success();
2938
}
3039

3140
StagingBufferTy &L0ContextTy::getStagingBuffer() {

offload/plugins-nextgen/level_zero/src/L0Device.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void L0DeviceTy::reportDeviceInfo() const {
174174
DP("-- Max clock frequency (MHz) : %" PRIu32 "\n", getClockRate());
175175
}
176176

177-
Error L0DeviceTy::internalInit() {
177+
Error L0DeviceTy::initImpl(GenericPluginTy &Plugin) {
178178
const auto &Options = getPlugin().getOptions();
179179

180180
uint32_t Count = 1;
@@ -219,10 +219,6 @@ Error L0DeviceTy::internalInit() {
219219
return Plugin::success();
220220
}
221221

222-
Error L0DeviceTy::initImpl(GenericPluginTy &Plugin) {
223-
return Plugin::success();
224-
}
225-
226222
Error L0DeviceTy::deinitImpl() {
227223
for (auto &PGM : Programs)
228224
if (auto Err = PGM.deinit())
@@ -293,7 +289,8 @@ Error L0DeviceTy::synchronizeImpl(__tgt_async_info &AsyncInfo,
293289
CALL_ZE_RET_ERROR(zeEventHostSynchronize, KE, UINT64_MAX);
294290
}
295291
for (auto &Event : WaitEvents) {
296-
releaseEvent(Event);
292+
if (auto Err = releaseEvent(Event))
293+
return Err;
297294
}
298295
} else { // Async
299296
// Wait for all events. We should wait and reset events in reverse order
@@ -308,7 +305,8 @@ Error L0DeviceTy::synchronizeImpl(__tgt_async_info &AsyncInfo,
308305
if (*Itr == AsyncQueue->KernelEvent)
309306
WaitDone = true;
310307
}
311-
releaseEvent(*Itr);
308+
if (auto Err = releaseEvent(*Itr))
309+
return Err;
312310
}
313311
}
314312
}
@@ -507,13 +505,13 @@ Error L0DeviceTy::dataExchangeImpl(const void *SrcPtr, GenericDeviceTy &DstDev,
507505
const bool UseCopyEngine = getZeDevice() != L0DstDev.getZeDevice();
508506

509507
if (asyncEnabled() && AsyncInfoWrapper.hasQueue()) {
510-
if (enqueueMemCopyAsync(DstPtr, SrcPtr, Size,
511-
(__tgt_async_info *)AsyncInfoWrapper))
512-
return Plugin::error(ErrorCode::UNKNOWN, "dataExchangeImpl failed");
508+
if (auto Err = enqueueMemCopyAsync(DstPtr, SrcPtr, Size,
509+
(__tgt_async_info *)AsyncInfoWrapper))
510+
return Err;
513511
} else {
514-
if (enqueueMemCopy(DstPtr, SrcPtr, Size,
515-
/* AsyncInfo */ nullptr, UseCopyEngine))
516-
return Plugin::error(ErrorCode::UNKNOWN, "dataExchangeImpl failed");
512+
if (auto Err = enqueueMemCopy(DstPtr, SrcPtr, Size,
513+
/* AsyncInfo */ nullptr, UseCopyEngine))
514+
return Err;
517515
}
518516
return Plugin::success();
519517
}
@@ -708,7 +706,10 @@ Error L0DeviceTy::enqueueMemCopyAsync(void *Dst, const void *Src, size_t Size,
708706
bool CopyTo) {
709707
const bool Ordered =
710708
(getPlugin().getOptions().CommandMode == CommandModeTy::AsyncOrdered);
711-
ze_event_handle_t SignalEvent = getEvent();
709+
auto EventOrErr = getEvent();
710+
if (!EventOrErr)
711+
return EventOrErr.takeError();
712+
ze_event_handle_t SignalEvent = *EventOrErr;
712713
size_t NumWaitEvents = 0;
713714
ze_event_handle_t *WaitEvents = nullptr;
714715
AsyncQueueTy *AsyncQueue = reinterpret_cast<AsyncQueueTy *>(AsyncInfo->Queue);
@@ -734,7 +735,10 @@ Error L0DeviceTy::enqueueMemFill(void *Ptr, const void *Pattern,
734735
size_t PatternSize, size_t Size) {
735736
if (useImmForCopy()) {
736737
const auto CmdList = getImmCopyCmdList();
737-
auto Event = getEvent();
738+
auto EventOrErr = getEvent();
739+
if (!EventOrErr)
740+
return EventOrErr.takeError();
741+
ze_event_handle_t Event = *EventOrErr;
738742
CALL_ZE_RET_ERROR(zeCommandListAppendMemoryFill, CmdList, Ptr, Pattern,
739743
PatternSize, Size, Event, 0, nullptr);
740744
CALL_ZE_RET_ERROR(zeEventHostSynchronize, Event, UINT64_MAX);

offload/plugins-nextgen/level_zero/src/L0Kernel.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,10 @@ Error L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
557557
if (UseImmCmdList) {
558558
INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId,
559559
"Using immediate command list for kernel submission.\n");
560-
auto Event = Device.getEvent();
560+
auto EventOrError = Device.getEvent();
561+
if (!EventOrError)
562+
return EventOrError.takeError();
563+
ze_event_handle_t Event = *EventOrError;
561564
size_t NumWaitEvents = 0;
562565
ze_event_handle_t *WaitEvents = nullptr;
563566
if (IsAsync && !AsyncQueue->WaitEvents.empty()) {
@@ -584,7 +587,8 @@ Error L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
584587
AsyncQueue->KernelEvent = Event;
585588
} else {
586589
CALL_ZE_RET_ERROR(zeEventHostSynchronize, Event, UINT64_MAX);
587-
Device.releaseEvent(Event);
590+
if (auto Err = Device.releaseEvent(Event))
591+
return Err;
588592
}
589593
} else {
590594
ze_event_handle_t Event = nullptr;
@@ -603,7 +607,8 @@ Error L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
603607
CALL_ZE_RET_ERROR(zeCommandQueueSynchronize, CmdQueue, UINT64_MAX);
604608
CALL_ZE_RET_ERROR(zeCommandListReset, CmdList);
605609
if (Event) {
606-
Device.releaseEvent(Event);
610+
if (auto Err = Device.releaseEvent(Event))
611+
return Err;
607612
}
608613
}
609614

offload/plugins-nextgen/level_zero/src/L0Memory.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ Expected<void *> MemAllocatorTy::allocL0(size_t Size, size_t Align,
647647
return Mem;
648648
}
649649

650-
ze_event_handle_t EventPoolTy::getEvent() {
650+
Expected<ze_event_handle_t> EventPoolTy::getEvent() {
651651
std::lock_guard<std::mutex> Lock(*Mtx);
652652

653653
if (Events.empty()) {
@@ -656,7 +656,7 @@ ze_event_handle_t EventPoolTy::getEvent() {
656656
Desc.flags = ZE_EVENT_POOL_FLAG_HOST_VISIBLE | Flags;
657657
Desc.count = PoolSize;
658658
ze_event_pool_handle_t Pool;
659-
CALL_ZE_RET_NULL(zeEventPoolCreate, Context, &Desc, 0, nullptr, &Pool);
659+
CALL_ZE_RET_ERROR(zeEventPoolCreate, Context, &Desc, 0, nullptr, &Pool);
660660
Pools.push_back(Pool);
661661

662662
// Create events
@@ -666,7 +666,7 @@ ze_event_handle_t EventPoolTy::getEvent() {
666666
for (uint32_t I = 0; I < PoolSize; I++) {
667667
EventDesc.index = I;
668668
ze_event_handle_t Event;
669-
CALL_ZE_RET_NULL(zeEventCreate, Pool, &EventDesc, &Event);
669+
CALL_ZE_RET_ERROR(zeEventCreate, Pool, &EventDesc, &Event);
670670
Events.push_back(Event);
671671
}
672672
}
@@ -678,10 +678,11 @@ ze_event_handle_t EventPoolTy::getEvent() {
678678
}
679679

680680
/// Return an event to the pool
681-
void EventPoolTy::releaseEvent(ze_event_handle_t Event, L0DeviceTy &Device) {
681+
Error EventPoolTy::releaseEvent(ze_event_handle_t Event, L0DeviceTy &Device) {
682682
std::lock_guard<std::mutex> Lock(*Mtx);
683-
CALL_ZE_RET_VOID(zeEventHostReset, Event);
683+
CALL_ZE_RET_ERROR(zeEventHostReset, Event);
684684
Events.push_back(Event);
685+
return Plugin::success();
685686
}
686687

687688
} // namespace llvm::omp::target::plugin

offload/plugins-nextgen/level_zero/src/L0Plugin.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ using namespace error;
2727
// Common data across all possible plugin instantiations
2828
L0OptionsTy LevelZeroPluginTy::Options;
2929

30-
int32_t LevelZeroPluginTy::findDevices() {
31-
CALL_ZE_RET_ZERO(zeInit, ZE_INIT_FLAG_GPU_ONLY);
30+
Expected<int32_t> LevelZeroPluginTy::findDevices() {
31+
CALL_ZE_RET_ERROR(zeInit, ZE_INIT_FLAG_GPU_ONLY);
3232
uint32_t NumDrivers = 0;
33-
CALL_ZE_RET_ZERO(zeDriverGet, &NumDrivers, nullptr);
33+
CALL_ZE_RET_ERROR(zeDriverGet, &NumDrivers, nullptr);
3434
if (NumDrivers == 0) {
3535
DP("Cannot find any drivers.\n");
3636
return 0;
@@ -39,7 +39,7 @@ int32_t LevelZeroPluginTy::findDevices() {
3939
// We expect multiple drivers on Windows to support different device types,
4040
// so we need to maintain multiple drivers and contexts in general.
4141
llvm::SmallVector<ze_driver_handle_t> FoundDrivers(NumDrivers);
42-
CALL_ZE_RET_ZERO(zeDriverGet, &NumDrivers, FoundDrivers.data());
42+
CALL_ZE_RET_ERROR(zeDriverGet, &NumDrivers, FoundDrivers.data());
4343

4444
struct RootInfoTy {
4545
uint32_t OrderId;
@@ -62,8 +62,10 @@ int32_t LevelZeroPluginTy::findDevices() {
6262
// We have a driver that supports at least one device
6363
ContextList.emplace_back(*this, Driver, DriverId);
6464
auto &DrvInfo = ContextList.back();
65+
if (auto Err = DrvInfo.init())
66+
return std::move(Err);
6567
llvm::SmallVector<ze_device_handle_t> FoundDevices(DeviceCount);
66-
CALL_ZE_RET_ZERO(zeDeviceGet, Driver, &DeviceCount, FoundDevices.data());
68+
CALL_ZE_RET_ERROR(zeDeviceGet, Driver, &DeviceCount, FoundDevices.data());
6769

6870
for (auto &zeDevice : FoundDevices)
6971
RootDevices.push_back(

0 commit comments

Comments
 (0)