[libc-commits] [libc] [libc][GPU] Use the CUDA device allocator for NVPTX calloc (PR #238821)

via libc-commits libc-commits at lists.llvm.org
Thu Nov 5 14:03:10 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: 0z5a (0z5a)

<details>
<summary>Changes</summary>

On NVPTX, `free` uses the CUDA device heap, while GPU libc `calloc` currently obtains memory from the libc GPU allocator. A pointer returned by `calloc` therefore cannot safely be released by the available `free`.

Use the CUDA device `malloc` for NVPTX `calloc`; other GPU targets continue to use the libc allocator. Add a GPU integration test that checks zero initialization, writes to the allocation, and repeatedly releases it through the matching `free`.

Fixes #<!-- -->238406.

## Testing

- Built the GPU libc and the new integration-test executable for `nvptx64-nvidia-cuda` / `sm_90` using an LLVM 24 development compiler and CUDA 13.2.
- On a GH200, the allocator-only change passed the integration executable. The original implementation timed out in the allocation/free test on two GH200 systems. A deliberately failing assertion was also checked to verify that the device test reports failures.
- The test does not cover AMDGPU or B300.


---
Full diff: https://github.com/llvm/llvm-project/pull/238821.diff


3 Files Affected:

- (modified) libc/src/stdlib/gpu/calloc.cpp (+10) 
- (modified) libc/test/integration/src/stdlib/gpu/CMakeLists.txt (+13) 
- (added) libc/test/integration/src/stdlib/gpu/calloc.cpp (+34) 


``````````diff
diff --git a/libc/src/stdlib/gpu/calloc.cpp b/libc/src/stdlib/gpu/calloc.cpp
index 9150affc7c2005..6e35bee170cf3a 100644
--- a/libc/src/stdlib/gpu/calloc.cpp
+++ b/libc/src/stdlib/gpu/calloc.cpp
@@ -13,6 +13,12 @@
 #include "src/__support/macros/config.h"
 #include "src/string/memory_utils/inline_memset.h"
 
+// NVPTX malloc and free are provided by the CUDA device allocator rather than
+// by the GPU libc allocator. Use the same allocator as free on that target.
+#ifdef LIBC_TARGET_ARCH_IS_NVPTX
+extern "C" void *malloc(size_t);
+#endif
+
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
@@ -20,7 +26,11 @@ LLVM_LIBC_FUNCTION(void *, calloc, (size_t num, size_t size)) {
   if (bytes == 0)
     return nullptr;
 
+#ifdef LIBC_TARGET_ARCH_IS_NVPTX
+  void *ptr = ::malloc(bytes);
+#else
   void *ptr = gpu::allocate(bytes);
+#endif
   if (!ptr)
     return nullptr;
 
diff --git a/libc/test/integration/src/stdlib/gpu/CMakeLists.txt b/libc/test/integration/src/stdlib/gpu/CMakeLists.txt
index 1a6c900e22cb86..66b0e904e7e80b 100644
--- a/libc/test/integration/src/stdlib/gpu/CMakeLists.txt
+++ b/libc/test/integration/src/stdlib/gpu/CMakeLists.txt
@@ -1,5 +1,18 @@
 add_libc_testsuite(libc-stdlib-gpu-integration-tests PARENT libc-stdlib-integration-tests)
 
+add_integration_test(
+  calloc
+  SUITE
+    libc-stdlib-gpu-integration-tests
+  SRCS
+    calloc.cpp
+  DEPENDS
+    libc.src.stdlib.calloc
+    libc.src.stdlib.free
+  LOADER_ARGS
+    --threads 1 --blocks 1
+)
+
 # TODO: Test on NVPTX, requires CUDA VMEM API.
 if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
   add_integration_test(
diff --git a/libc/test/integration/src/stdlib/gpu/calloc.cpp b/libc/test/integration/src/stdlib/gpu/calloc.cpp
new file mode 100644
index 00000000000000..476f43c1ad7dd8
--- /dev/null
+++ b/libc/test/integration/src/stdlib/gpu/calloc.cpp
@@ -0,0 +1,34 @@
+//===-- GPU calloc allocator compatibility test ---------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "test/IntegrationTest/test.h"
+
+#include "src/stdlib/calloc.h"
+#include "src/stdlib/free.h"
+
+#ifdef LIBC_TARGET_ARCH_IS_NVPTX
+extern "C" void free(void *);
+#endif
+
+TEST_MAIN(int, char **, char **) {
+  for (int iteration = 0; iteration < 32; ++iteration) {
+    auto *p = static_cast<unsigned char *>(LIBC_NAMESPACE::calloc(17, 4));
+    ASSERT_NE(p, nullptr);
+    for (int i = 0; i < 68; ++i)
+      ASSERT_EQ(p[i], 0);
+    p[0] = 1;
+    p[67] = 2;
+#ifdef LIBC_TARGET_ARCH_IS_NVPTX
+    ::free(p);
+#else
+    LIBC_NAMESPACE::free(p);
+#endif
+  }
+
+  return 0;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/238821


More information about the libc-commits mailing list