[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.
diff --git a/offload/plugins-nextgen/amdgpu/src/rtl.cpp b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
index 2733796..abb8368 100644
--- a/offload/plugins-nextgen/amdgpu/src/rtl.cpp
+++ b/offload/plugins-nextgen/amdgpu/src/rtl.cpp
@@ -420,7 +420,7 @@
assert(PtrStorage && "Invalid pointer storage");
*PtrStorage = MemoryManager->allocate(Size, nullptr);
- if (*PtrStorage == nullptr)
+ if (Size && *PtrStorage == nullptr)
return Plugin::error(ErrorCode::OUT_OF_RESOURCES,
"failure to allocate from AMDGPU memory manager");
@@ -429,8 +429,6 @@
/// Release an allocation to be reused.
Error deallocate(void *Ptr) {
- assert(Ptr && "Invalid pointer");
-
if (MemoryManager->free(Ptr))
return Plugin::error(ErrorCode::UNKNOWN,
"failure to deallocate from AMDGPU memory manager");
@@ -1204,7 +1202,6 @@
ReleaseBufferArgsTy *Args = reinterpret_cast<ReleaseBufferArgsTy *>(Data);
assert(Args && "Invalid arguments");
assert(Args->MemoryManager && "Invalid memory manager");
- assert(Args->Buffer && "Invalid buffer");
// Release the allocation to the memory manager.
return Args->MemoryManager->deallocate(Args->Buffer);
diff --git a/offload/test/api/omp_device_memory.c b/offload/test/api/omp_device_memory.c
index 60f47e9..8876fc9 100644
--- a/offload/test/api/omp_device_memory.c
+++ b/offload/test/api/omp_device_memory.c
@@ -2,6 +2,7 @@
#include <omp.h>
#include <stdio.h>
+#include <assert.h>
int main() {
const int N = 64;
@@ -24,4 +25,9 @@
printf("PASS\n");
omp_free(device_ptr, llvm_omp_target_device_mem_alloc);
+
+ // Make sure this interface works.
+ void *ptr = omp_alloc(0, llvm_omp_target_device_mem_alloc);
+ assert(!ptr && "Ptr not (nullptr)");
+ omp_free(ptr, llvm_omp_target_device_mem_alloc);
}