Skip to content

Commit b26baf1

Browse files
authored
[Offload] Make AMDGPU plugin handle empty allocation properly (#142383)
Summary: `malloc(0)` and `free(nullptr)` are both defined by the standard but we current trigger erros and assertions on them. Fix that so this works with empty arguments.
1 parent 71093b8 commit b26baf1

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

offload/plugins-nextgen/amdgpu/src/rtl.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ struct AMDGPUMemoryManagerTy : public DeviceAllocatorTy {
420420
assert(PtrStorage && "Invalid pointer storage");
421421

422422
*PtrStorage = MemoryManager->allocate(Size, nullptr);
423-
if (*PtrStorage == nullptr)
423+
if (Size && *PtrStorage == nullptr)
424424
return Plugin::error(ErrorCode::OUT_OF_RESOURCES,
425425
"failure to allocate from AMDGPU memory manager");
426426

@@ -429,8 +429,6 @@ struct AMDGPUMemoryManagerTy : public DeviceAllocatorTy {
429429

430430
/// Release an allocation to be reused.
431431
Error deallocate(void *Ptr) {
432-
assert(Ptr && "Invalid pointer");
433-
434432
if (MemoryManager->free(Ptr))
435433
return Plugin::error(ErrorCode::UNKNOWN,
436434
"failure to deallocate from AMDGPU memory manager");
@@ -1204,7 +1202,6 @@ struct AMDGPUStreamTy {
12041202
ReleaseBufferArgsTy *Args = reinterpret_cast<ReleaseBufferArgsTy *>(Data);
12051203
assert(Args && "Invalid arguments");
12061204
assert(Args->MemoryManager && "Invalid memory manager");
1207-
assert(Args->Buffer && "Invalid buffer");
12081205

12091206
// Release the allocation to the memory manager.
12101207
return Args->MemoryManager->deallocate(Args->Buffer);

offload/test/api/omp_device_memory.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <omp.h>
44
#include <stdio.h>
5+
#include <assert.h>
56

67
int main() {
78
const int N = 64;
@@ -24,4 +25,9 @@ int main() {
2425
printf("PASS\n");
2526

2627
omp_free(device_ptr, llvm_omp_target_device_mem_alloc);
28+
29+
// Make sure this interface works.
30+
void *ptr = omp_alloc(0, llvm_omp_target_device_mem_alloc);
31+
assert(!ptr && "Ptr not (nullptr)");
32+
omp_free(ptr, llvm_omp_target_device_mem_alloc);
2733
}

0 commit comments

Comments
 (0)