Skip to content

Commit 1221006

Browse files
committed
Remove legacy error checking Pt 2
1 parent 91f1033 commit 1221006

File tree

8 files changed

+103
-101
lines changed

8 files changed

+103
-101
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ class L0DeviceTy final : public GenericDeviceTy {
549549
return getDeviceMemAllocator();
550550
}
551551

552-
int32_t makeMemoryResident(void *Mem, size_t Size);
552+
Error makeMemoryResident(void *Mem, size_t Size);
553553

554554
// Generic device interface implementation
555555
Expected<DeviceImageTy *>

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ class L0KernelTy : public GenericKernelTy {
110110
KernelPropertiesTy Properties;
111111
auto &getProperties() { return Properties; }
112112

113-
int32_t runTargetTeamRegion(L0DeviceTy &Device, KernelArgsTy &KernelArgs,
114-
KernelLaunchParamsTy LaunchParams,
115-
__tgt_async_info *AsyncInfo) const;
113+
Error runTargetTeamRegion(L0DeviceTy &Device, KernelArgsTy &KernelArgs,
114+
KernelLaunchParamsTy LaunchParams,
115+
__tgt_async_info *AsyncInfo) const;
116116

117117
void decideKernelGroupArguments(L0DeviceTy &Device, uint32_t NumTeams,
118118
uint32_t ThreadLimit,
@@ -122,10 +122,12 @@ class L0KernelTy : public GenericKernelTy {
122122
bool HalfNumThreads,
123123
bool IsTeamsNDRange) const;
124124

125-
int32_t decideLoopKernelGroupArguments(
126-
L0DeviceTy &Device, uint32_t ThreadLimit, TgtNDRangeDescTy *LoopLevels,
127-
uint32_t *GroupSizes, ze_group_count_t &GroupCounts, bool HalfNumThreads,
128-
bool &AllowCooperative) const;
125+
Error decideLoopKernelGroupArguments(L0DeviceTy &Device, uint32_t ThreadLimit,
126+
TgtNDRangeDescTy *LoopLevels,
127+
uint32_t *GroupSizes,
128+
ze_group_count_t &GroupCounts,
129+
bool HalfNumThreads,
130+
bool &AllowCooperative) const;
129131

130132
Error buildKernel(L0ProgramTy &Program);
131133

@@ -160,10 +162,10 @@ class L0KernelTy : public GenericKernelTy {
160162

161163
ze_kernel_handle_t getZeKernel() const { return zeKernel; }
162164

163-
int32_t getGroupsShape(L0DeviceTy &Device, int32_t NumTeams,
164-
int32_t ThreadLimit, uint32_t *GroupSizes,
165-
ze_group_count_t &GroupCounts, void *LoopDesc,
166-
bool &AllowCooperative) const;
165+
Error getGroupsShape(L0DeviceTy &Device, int32_t NumTeams,
166+
int32_t ThreadLimit, uint32_t *GroupSizes,
167+
ze_group_count_t &GroupCounts, void *LoopDesc,
168+
bool &AllowCooperative) const;
167169
};
168170

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ class MemAllocatorTy {
329329
/// Allocate memory from L0 GPU RT. We use over-allocation workaround
330330
/// to support target pointer with offset, and positive "ActiveSize" is
331331
/// specified in such cases for correct debug logging.
332-
void *allocL0(size_t Size, size_t Align, int32_t Kind, size_t ActiveSize = 0);
332+
Expected<void *> allocL0(size_t Size, size_t Align, int32_t Kind,
333+
size_t ActiveSize = 0);
333334

334335
/// Allocate memory with the specified information from a memory pool
335336
Expected<void *> alloc(size_t Size, size_t Align, int32_t Kind,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class L0ProgramTy : public DeviceImageTy {
109109
/// \p Name and \p Size in the device environment for the current device.
110110
/// The lookup is first done via the device offload table. If it fails,
111111
/// then the lookup falls back to non-OpenMP specific lookup on the device.
112-
void *getOffloadVarDeviceAddr(const char *Name) const;
112+
Expected<void *> getOffloadVarDeviceAddr(const char *Name) const;
113113

114114
/// Returns the handle of a module that contains a given Kernel name
115115
ze_module_handle_t findModuleFromKernelName(const char *KernelName) const {

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ L0DeviceTy::loadBinaryImpl(std::unique_ptr<MemoryBuffer> &&TgtImage,
255255
auto &Program = addProgram(ImageId, std::move(TgtImage));
256256

257257
if (auto Err = Program.buildModules(CompilationOptions))
258-
return Err;
258+
return std::move(Err);
259259

260260
if (auto Err = Program.linkModules())
261-
return Err;
261+
return std::move(Err);
262262

263263
if (auto Err = Program.loadModuleKernels())
264-
return Err;
264+
return std::move(Err);
265265

266266
return &Program;
267267
}
@@ -787,17 +787,10 @@ Error L0DeviceTy::dataDelete(void *Ptr) {
787787
return Allocator.dealloc(Ptr);
788788
}
789789

790-
int32_t L0DeviceTy::makeMemoryResident(void *Mem, size_t Size) {
791-
ze_result_t RC;
792-
CALL_ZE(RC, zeContextMakeMemoryResident, getZeContext(), getZeDevice(), Mem,
793-
Size);
794-
if (RC != ZE_RESULT_SUCCESS) {
795-
DP("Could not make memory " DPxMOD " resident on Level Zero device " DPxMOD
796-
".\n",
797-
DPxPTR(Mem), DPxPTR(getZeDevice()));
798-
return OFFLOAD_FAIL;
799-
}
800-
return OFFLOAD_SUCCESS;
790+
Error L0DeviceTy::makeMemoryResident(void *Mem, size_t Size) {
791+
CALL_ZE_RET_ERROR(zeContextMakeMemoryResident, getZeContext(), getZeDevice(),
792+
Mem, Size);
793+
return Plugin::success();
801794
}
802795

803796
// Command queues related functions

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

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ Error L0KernelTy::launchImpl(GenericDeviceTy &GenericDevice,
2424
AsyncInfoWrapperTy &AsyncInfoWrapper) const {
2525

2626
auto &l0Device = L0DeviceTy::makeL0Device(GenericDevice);
27-
int32_t RC = runTargetTeamRegion(l0Device, KernelArgs,
28-
std::move(LaunchParams), AsyncInfoWrapper);
29-
if (RC == OFFLOAD_SUCCESS)
30-
return Plugin::success();
31-
return Plugin::error(error::ErrorCode::UNKNOWN,
32-
"Error in launch Kernel %s: %d", getName(), RC);
27+
return runTargetTeamRegion(l0Device, KernelArgs, std::move(LaunchParams),
28+
AsyncInfoWrapper);
3329
}
3430

3531
Error L0KernelTy::buildKernel(L0ProgramTy &Program) {
@@ -222,7 +218,7 @@ static uint64_t computeThreadsNeeded(const llvm::ArrayRef<size_t> TripCounts,
222218
return GroupCount[0] * ThreadsPerWG;
223219
}
224220

225-
int32_t L0KernelTy::decideLoopKernelGroupArguments(
221+
Error L0KernelTy::decideLoopKernelGroupArguments(
226222
L0DeviceTy &Device, uint32_t ThreadLimit, TgtNDRangeDescTy *LoopLevels,
227223
uint32_t *GroupSizes, ze_group_count_t &GroupCounts, bool HalfNumThreads,
228224
bool &AllowCooperative) const {
@@ -282,15 +278,15 @@ int32_t L0KernelTy::decideLoopKernelGroupArguments(
282278
INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId,
283279
"Invalid number of teams %zu due to large loop trip count\n",
284280
DistributeTripCount);
285-
return OFFLOAD_FAIL;
281+
return Plugin::success();
286282
}
287283
GRPCounts[DistributeDim] = DistributeTripCount;
288284
}
289285
AllowCooperative = false;
290286
GroupCounts.groupCountX = GRPCounts[0];
291287
GroupCounts.groupCountY = GRPCounts[1];
292288
GroupCounts.groupCountZ = GRPCounts[2];
293-
return OFFLOAD_SUCCESS;
289+
return Plugin::success();
294290
}
295291

296292
if (!MaxGroupSizeForced) {
@@ -355,9 +351,10 @@ int32_t L0KernelTy::decideLoopKernelGroupArguments(
355351
GRPSizes[I] = Trip;
356352
size_t Count = (Trip + GRPSizes[I] - 1) / GRPSizes[I];
357353
if (Count > UINT32_MAX) {
358-
INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId,
359-
"Invalid number of teams %zu due to large loop trip count\n", Count);
360-
return OFFLOAD_FAIL;
354+
return Plugin::error(ErrorCode::INVALID_ARGUMENT,
355+
"Invalid number of teams %zu due to large loop "
356+
"trip count\n",
357+
Count);
361358
}
362359
GRPCounts[I] = (uint32_t)Count;
363360
}
@@ -367,14 +364,13 @@ int32_t L0KernelTy::decideLoopKernelGroupArguments(
367364
GroupCounts.groupCountZ = GRPCounts[2];
368365
std::copy(GRPSizes, GRPSizes + 3, GroupSizes);
369366

370-
return OFFLOAD_SUCCESS;
367+
return Plugin::success();
371368
}
372369

373-
int32_t L0KernelTy::getGroupsShape(L0DeviceTy &Device, int32_t NumTeams,
374-
int32_t ThreadLimit, uint32_t *GroupSizes,
375-
ze_group_count_t &GroupCounts,
376-
void *LoopDesc,
377-
bool &AllowCooperative) const {
370+
Error L0KernelTy::getGroupsShape(L0DeviceTy &Device, int32_t NumTeams,
371+
int32_t ThreadLimit, uint32_t *GroupSizes,
372+
ze_group_count_t &GroupCounts, void *LoopDesc,
373+
bool &AllowCooperative) const {
378374

379375
const auto DeviceId = Device.getDeviceId();
380376
const auto &KernelPR = getProperties();
@@ -443,13 +439,13 @@ int32_t L0KernelTy::getGroupsShape(L0DeviceTy &Device, int32_t NumTeams,
443439
AllowCooperative = false;
444440
}
445441

446-
return OFFLOAD_SUCCESS;
442+
return Plugin::success();
447443
}
448444

449-
int32_t L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
450-
KernelArgsTy &KernelArgs,
451-
KernelLaunchParamsTy LaunchParams,
452-
__tgt_async_info *AsyncInfo) const {
445+
Error L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
446+
KernelArgsTy &KernelArgs,
447+
KernelLaunchParamsTy LaunchParams,
448+
__tgt_async_info *AsyncInfo) const {
453449
// Libomptarget can pass negative NumTeams and ThreadLimit now after
454450
// introducing __tgt_target_kernel. This happens only when we have valid
455451
// LoopDesc and the region is not a teams region.
@@ -500,13 +496,9 @@ int32_t L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
500496
GroupSizes, GroupCounts, AllowCooperative);
501497

502498
if (!GroupParamsReused) {
503-
auto RC = getGroupsShape(Device, NumTeams, ThreadLimit, GroupSizes,
504-
GroupCounts, LoopDesc, AllowCooperative);
505-
506-
if (RC != OFFLOAD_SUCCESS) {
507-
return RC;
508-
}
509-
499+
if (auto Err = getGroupsShape(Device, NumTeams, ThreadLimit, GroupSizes,
500+
GroupCounts, LoopDesc, AllowCooperative))
501+
return Err;
510502
KernelPR.cacheGroupParams(static_cast<TgtNDRangeDescTy *>(LoopDesc),
511503
NumTeams, ThreadLimit, GroupSizes, GroupCounts,
512504
AllowCooperative);
@@ -522,8 +514,8 @@ int32_t L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
522514
for (int32_t I = 0; I < NumArgs; I++) {
523515
{
524516
void *Arg = (static_cast<void **>(LaunchParams.Data))[I];
525-
CALL_ZE_RET_FAIL(zeKernelSetArgumentValue, zeKernel, I, sizeof(Arg),
526-
Arg == nullptr ? nullptr : &Arg);
517+
CALL_ZE_RET_ERROR(zeKernelSetArgumentValue, zeKernel, I, sizeof(Arg),
518+
Arg == nullptr ? nullptr : &Arg);
527519
INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId,
528520
"Kernel Pointer argument %" PRId32 " (value: " DPxMOD
529521
") was set successfully for device %s.\n",
@@ -540,14 +532,14 @@ int32_t L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
540532
if (PrevFlags != Flags) {
541533
// Combine with common access flags
542534
const auto FinalFlags = Device.getIndirectFlags() | Flags;
543-
CALL_ZE_RET_FAIL(zeKernelSetIndirectAccess, getZeKernel(), FinalFlags);
535+
CALL_ZE_RET_ERROR(zeKernelSetIndirectAccess, getZeKernel(), FinalFlags);
544536
DP("Setting indirect access flags " DPxMOD "\n", DPxPTR(FinalFlags));
545537
PrevFlags = Flags;
546538
}
547539

548540
if (!GroupParamsReused) {
549-
CALL_ZE_RET_FAIL(zeKernelSetGroupSize, zeKernel, GroupSizes[0],
550-
GroupSizes[1], GroupSizes[2]);
541+
CALL_ZE_RET_ERROR(zeKernelSetGroupSize, zeKernel, GroupSizes[0],
542+
GroupSizes[1], GroupSizes[2]);
551543
}
552544

553545
ze_command_list_handle_t CmdList = nullptr;
@@ -580,36 +572,36 @@ int32_t L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
580572
INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId,
581573
"Kernel depends on %zu data copying events.\n", NumWaitEvents);
582574
if (AllowCooperative)
583-
CALL_ZE_RET_FAIL(zeCommandListAppendLaunchCooperativeKernel, CmdList,
584-
zeKernel, &GroupCounts, Event, NumWaitEvents,
585-
WaitEvents);
575+
CALL_ZE_RET_ERROR(zeCommandListAppendLaunchCooperativeKernel, CmdList,
576+
zeKernel, &GroupCounts, Event, NumWaitEvents,
577+
WaitEvents);
586578
else
587-
CALL_ZE_RET_FAIL(zeCommandListAppendLaunchKernel, CmdList, zeKernel,
588-
&GroupCounts, Event, NumWaitEvents, WaitEvents);
579+
CALL_ZE_RET_ERROR(zeCommandListAppendLaunchKernel, CmdList, zeKernel,
580+
&GroupCounts, Event, NumWaitEvents, WaitEvents);
589581
KernelLock.unlock();
590582
if (IsAsync) {
591583
AsyncQueue->WaitEvents.push_back(Event);
592584
AsyncQueue->KernelEvent = Event;
593585
} else {
594-
CALL_ZE_RET_FAIL(zeEventHostSynchronize, Event, UINT64_MAX);
586+
CALL_ZE_RET_ERROR(zeEventHostSynchronize, Event, UINT64_MAX);
595587
Device.releaseEvent(Event);
596588
}
597589
} else {
598590
ze_event_handle_t Event = nullptr;
599591
if (AllowCooperative)
600-
CALL_ZE_RET_FAIL(zeCommandListAppendLaunchCooperativeKernel, CmdList,
601-
zeKernel, &GroupCounts, Event, 0, nullptr);
592+
CALL_ZE_RET_ERROR(zeCommandListAppendLaunchCooperativeKernel, CmdList,
593+
zeKernel, &GroupCounts, Event, 0, nullptr);
602594
else
603-
CALL_ZE_RET_FAIL(zeCommandListAppendLaunchKernel, CmdList, zeKernel,
604-
&GroupCounts, Event, 0, nullptr);
595+
CALL_ZE_RET_ERROR(zeCommandListAppendLaunchKernel, CmdList, zeKernel,
596+
&GroupCounts, Event, 0, nullptr);
605597
KernelLock.unlock();
606-
CALL_ZE_RET_FAIL(zeCommandListClose, CmdList);
607-
CALL_ZE_RET_FAIL_MTX(zeCommandQueueExecuteCommandLists, Device.getMutex(),
608-
CmdQueue, 1, &CmdList, nullptr);
598+
CALL_ZE_RET_ERROR(zeCommandListClose, CmdList);
599+
CALL_ZE_RET_ERROR_MTX(zeCommandQueueExecuteCommandLists, Device.getMutex(),
600+
CmdQueue, 1, &CmdList, nullptr);
609601
INFO(OMP_INFOTYPE_PLUGIN_KERNEL, DeviceId,
610602
"Submitted kernel " DPxMOD " to device %s\n", DPxPTR(zeKernel), IdStr);
611-
CALL_ZE_RET_FAIL(zeCommandQueueSynchronize, CmdQueue, UINT64_MAX);
612-
CALL_ZE_RET_FAIL(zeCommandListReset, CmdList);
603+
CALL_ZE_RET_ERROR(zeCommandQueueSynchronize, CmdQueue, UINT64_MAX);
604+
CALL_ZE_RET_ERROR(zeCommandListReset, CmdList);
613605
if (Event) {
614606
Device.releaseEvent(Event);
615607
}
@@ -619,7 +611,7 @@ int32_t L0KernelTy::runTargetTeamRegion(L0DeviceTy &l0Device,
619611
"Executed kernel entry " DPxMOD " on device %s\n", DPxPTR(zeKernel),
620612
IdStr);
621613

622-
return OFFLOAD_SUCCESS;
614+
return Plugin::success();
623615
}
624616

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

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ MemAllocatorTy::MemPoolTy::MemPoolTy(int32_t Kind, MemAllocatorTy *_Allocator,
8383

8484
// Check page size used for this allocation kind to decide minimum
8585
// allocation size when allocating from L0.
86-
void *Mem = Allocator->allocL0(8, 0, AllocKind);
86+
auto MemOrErr = Allocator->allocL0(8, 0, AllocKind);
87+
if (!MemOrErr)
88+
FATAL_MESSAGE0(0, "Failed to allocate memory pool\n");
89+
void *Mem = *MemOrErr;
8790
ze_memory_allocation_properties_t AP{
8891
ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES, nullptr,
8992
ZE_MEMORY_TYPE_UNKNOWN, 0, 0};
@@ -277,7 +280,14 @@ void *MemAllocatorTy::MemPoolTy::alloc(size_t Size, size_t &AllocSize) {
277280
// Bucket is empty or all blocks in the bucket are full
278281
const auto ChunkSize = BucketParams[BucketId].first;
279282
const auto BlockSize = BucketParams[BucketId].second;
280-
void *Base = Allocator->allocL0(BlockSize, 0, AllocKind);
283+
auto BaseOrErr = Allocator->allocL0(BlockSize, 0, AllocKind);
284+
if (!BaseOrErr) {
285+
consumeError(BaseOrErr.takeError());
286+
DP("Failed to allocate new block for %s pool\n",
287+
AllocKindToStr(AllocKind));
288+
return nullptr;
289+
}
290+
void *Base = *BaseOrErr;
281291

282292
if (ZeroInit) {
283293
auto Err = Allocator->enqueueMemSet(Base, 0, BlockSize);
@@ -515,7 +525,10 @@ Expected<void *> MemAllocatorTy::alloc(size_t Size, size_t Align, int32_t Kind,
515525
}
516526
}
517527

518-
AllocBase = allocL0(AllocSize, Align, Kind, Size);
528+
auto AllocBaseOrErr = allocL0(AllocSize, Align, Kind, Size);
529+
if (!AllocBaseOrErr)
530+
return AllocBaseOrErr.takeError();
531+
AllocBase = *AllocBaseOrErr;
519532
if (AllocBase) {
520533
Mem = (void *)((uintptr_t)AllocBase + Offset);
521534
AllocInfo.add(Mem, AllocBase, Size, Kind, false, UserAlloc);
@@ -581,8 +594,8 @@ Error MemAllocatorTy::enqueueMemCopy(void *Dst, const void *Src, size_t Size) {
581594
return Device->enqueueMemCopy(Dst, Src, Size);
582595
}
583596

584-
void *MemAllocatorTy::allocL0(size_t Size, size_t Align, int32_t Kind,
585-
size_t ActiveSize) {
597+
Expected<void *> MemAllocatorTy::allocL0(size_t Size, size_t Align,
598+
int32_t Kind, size_t ActiveSize) {
586599
void *Mem = nullptr;
587600
ze_device_mem_alloc_desc_t DeviceDesc{ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC,
588601
nullptr, 0, 0};
@@ -604,17 +617,17 @@ void *MemAllocatorTy::allocL0(size_t Size, size_t Align, int32_t Kind,
604617
switch (Kind) {
605618
case TARGET_ALLOC_DEVICE:
606619
makeResident = true;
607-
CALL_ZE_RET_NULL(zeMemAllocDevice, zeContext, &DeviceDesc, Size, Align,
608-
zeDevice, &Mem);
620+
CALL_ZE_RET_ERROR(zeMemAllocDevice, zeContext, &DeviceDesc, Size, Align,
621+
zeDevice, &Mem);
609622
DP("Allocated a device memory " DPxMOD "\n", DPxPTR(Mem));
610623
break;
611624
case TARGET_ALLOC_HOST:
612-
CALL_ZE_RET_NULL(zeMemAllocHost, zeContext, &HostDesc, Size, Align, &Mem);
625+
CALL_ZE_RET_ERROR(zeMemAllocHost, zeContext, &HostDesc, Size, Align, &Mem);
613626
DP("Allocated a host memory " DPxMOD "\n", DPxPTR(Mem));
614627
break;
615628
case TARGET_ALLOC_SHARED:
616-
CALL_ZE_RET_NULL(zeMemAllocShared, zeContext, &DeviceDesc, &HostDesc, Size,
617-
Align, zeDevice, &Mem);
629+
CALL_ZE_RET_ERROR(zeMemAllocShared, zeContext, &DeviceDesc, &HostDesc, Size,
630+
Align, zeDevice, &Mem);
618631
DP("Allocated a shared memory " DPxMOD "\n", DPxPTR(Mem));
619632
break;
620633
default:
@@ -626,8 +639,10 @@ void *MemAllocatorTy::allocL0(size_t Size, size_t Align, int32_t Kind,
626639
if (makeResident) {
627640
assert(Device &&
628641
"Device is not set for memory allocation. Is this a Device Pool?");
629-
if (Device->makeMemoryResident(Mem, Size) != OFFLOAD_SUCCESS)
642+
if (auto Err = Device->makeMemoryResident(Mem, Size)) {
630643
Mem = nullptr;
644+
return std::move(Err);
645+
}
631646
}
632647
return Mem;
633648
}

0 commit comments

Comments
 (0)