Skip to content

Commit 44b2f48

Browse files
committed
push some error checking down
1 parent 6c5278f commit 44b2f48

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class MemAllocatorTy {
307307
bool IsHostMem = false;
308308
// Internal deallocation function to be called when already
309309
// hondling the Mtx lock
310-
int32_t dealloc_locked(void *Ptr);
310+
Error dealloc_locked(void *Ptr);
311311

312312
public:
313313
MemAllocatorTy() = default;
@@ -337,12 +337,12 @@ class MemAllocatorTy {
337337
void *allocL0(size_t Size, size_t Align, int32_t Kind, size_t ActiveSize = 0);
338338

339339
/// Allocate memory with the specified information from a memory pool
340-
void *alloc(size_t Size, size_t Align, int32_t Kind, intptr_t Offset,
341-
bool UserAlloc, bool DevMalloc, uint32_t MemAdvice,
342-
AllocOptionTy AllocOpt);
340+
Expected<void *> alloc(size_t Size, size_t Align, int32_t Kind,
341+
intptr_t Offset, bool UserAlloc, bool DevMalloc,
342+
uint32_t MemAdvice, AllocOptionTy AllocOpt);
343343

344344
/// Deallocate memory
345-
int32_t dealloc(void *Ptr) {
345+
Error dealloc(void *Ptr) {
346346
std::lock_guard<std::mutex> Lock(Mtx);
347347
return dealloc_locked(Ptr);
348348
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ Expected<void *> L0DeviceTy::dataAlloc(size_t Size, size_t Align, int32_t Kind,
816816

817817
Error L0DeviceTy::dataDelete(void *Ptr) {
818818
auto &Allocator = getMemAllocator(Ptr);
819-
return Plugin::check(Allocator.dealloc(Ptr), "Error deleting ptr");
819+
return Allocator.dealloc(Ptr);
820820
}
821821

822822
int32_t L0DeviceTy::makeMemoryResident(void *Mem, size_t Size) {

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,11 @@ void MemAllocatorTy::updateMaxAllocSize(L0DeviceTy &L0Device) {
405405
void MemAllocatorTy::deinit() {
406406
std::lock_guard<std::mutex> Lock(Mtx);
407407
// Release RTL-owned memory
408-
for (auto *M : MemOwned)
409-
dealloc_locked(M);
408+
for (auto *M : MemOwned) {
409+
auto Err = dealloc_locked(M);
410+
if (Err)
411+
consumeError(std::move(Err));
412+
}
410413
// Release resources used in the pool
411414
Pools.clear();
412415
ReductionPool.reset(nullptr);
@@ -436,9 +439,10 @@ void MemAllocatorTy::deinit() {
436439
}
437440

438441
/// Allocate memory with the specified information
439-
void *MemAllocatorTy::alloc(size_t Size, size_t Align, int32_t Kind,
440-
intptr_t Offset, bool UserAlloc, bool DevMalloc,
441-
uint32_t MemAdvice, AllocOptionTy AllocOpt) {
442+
Expected<void *> MemAllocatorTy::alloc(size_t Size, size_t Align, int32_t Kind,
443+
intptr_t Offset, bool UserAlloc,
444+
bool DevMalloc, uint32_t MemAdvice,
445+
AllocOptionTy AllocOpt) {
442446
assert((Kind == TARGET_ALLOC_DEVICE || Kind == TARGET_ALLOC_HOST ||
443447
Kind == TARGET_ALLOC_SHARED) &&
444448
"Unknown memory kind while allocating target memory");
@@ -503,12 +507,13 @@ void *MemAllocatorTy::alloc(size_t Size, size_t Align, int32_t Kind,
503507
}
504508

505509
/// Deallocate memory
506-
int32_t MemAllocatorTy::dealloc_locked(void *Ptr) {
510+
Error MemAllocatorTy::dealloc_locked(void *Ptr) {
507511
MemAllocInfoTy Info;
508512
if (!AllocInfo.remove(Ptr, &Info)) {
509-
DP("Error: Cannot find memory allocation information for " DPxMOD "\n",
510-
DPxPTR(Ptr));
511-
return OFFLOAD_FAIL;
513+
return Plugin::error(ErrorCode::BACKEND_FAILURE,
514+
"Cannot find memory allocation information for " DPxMOD
515+
"\n",
516+
DPxPTR(Ptr));
512517
}
513518
if (Info.InPool) {
514519
size_t DeallocSize = 0;
@@ -521,24 +526,27 @@ int32_t MemAllocatorTy::dealloc_locked(void *Ptr) {
521526
if (DeallocSize == 0)
522527
DeallocSize = CounterPool->dealloc(Info.Base);
523528
if (DeallocSize == 0) {
524-
DP("Error: Cannot return memory " DPxMOD " to pool\n", DPxPTR(Ptr));
525-
return OFFLOAD_FAIL;
529+
return Plugin::error(ErrorCode::BACKEND_FAILURE,
530+
"Cannot return memory " DPxMOD " to pool\n",
531+
DPxPTR(Ptr));
526532
}
527533
}
528534
log(0, DeallocSize, Info.Kind, true /* Pool */);
529-
return OFFLOAD_SUCCESS;
535+
return Plugin::success();
530536
}
531537
if (!Info.Base) {
532538
DP("Error: Cannot find base address of " DPxMOD "\n", DPxPTR(Ptr));
533-
return OFFLOAD_FAIL;
539+
return Plugin::error(ErrorCode::INVALID_ARGUMENT,
540+
"Cannot find base address of " DPxMOD "\n",
541+
DPxPTR(Ptr));
534542
}
535-
CALL_ZE_RET_FAIL(zeMemFree, L0Context->getZeContext(), Info.Base);
543+
CALL_ZE_RET_ERROR(zeMemFree, L0Context->getZeContext(), Info.Base);
536544
log(0, Info.Size, Info.Kind);
537545

538546
DP("Deleted device memory " DPxMOD " (Base: " DPxMOD ", Size: %zu)\n",
539547
DPxPTR(Ptr), DPxPTR(Info.Base), Info.Size);
540548

541-
return OFFLOAD_SUCCESS;
549+
return Plugin::success();
542550
}
543551

544552
int32_t MemAllocatorTy::enqueueMemCopy(void *Dst, const void *Src,

0 commit comments

Comments
 (0)