summaryrefslogtreecommitdiff
path: root/libc/src/stdlib/gpu/aligned_alloc.cpp
diff options
context:
space:
mode:
authorJoseph Huber <huberjn@outlook.com>2025-07-02 09:25:57 -0500
committerGitHub <noreply@github.com>2025-07-02 09:25:57 -0500
commit24828c8c45d7f258159afca251aa853a5fc59150 (patch)
tree7707f48030aad1c67788990bb8321d1fa30703ec /libc/src/stdlib/gpu/aligned_alloc.cpp
parentbca79ec0d21e582901b77038121dd3d738ff6aae (diff)
[libc] Efficiently implement `aligned_alloc` for AMDGPU (#146585)
Summary: This patch uses the actual allocator interface to implement `aligned_alloc`. We do this by simply rounding up the amount allocated. Because of how index calculation works, any offset within an allocated pointer will still map to the same chunk, so we can just adjust internally and it will free all the same.
Diffstat (limited to 'libc/src/stdlib/gpu/aligned_alloc.cpp')
-rw-r--r--libc/src/stdlib/gpu/aligned_alloc.cpp18
1 files changed, 9 insertions, 9 deletions
diff --git a/libc/src/stdlib/gpu/aligned_alloc.cpp b/libc/src/stdlib/gpu/aligned_alloc.cpp
index cd2c7e55128f..34a7eae618fe 100644
--- a/libc/src/stdlib/gpu/aligned_alloc.cpp
+++ b/libc/src/stdlib/gpu/aligned_alloc.cpp
@@ -15,15 +15,15 @@
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(void *, aligned_alloc, (size_t alignment, size_t size)) {
- if ((alignment & -alignment) != alignment)
- return nullptr;
-
- void *ptr = gpu::allocate(size);
- if ((reinterpret_cast<uintptr_t>(ptr) & (alignment - 1)) != 0) {
- gpu::deallocate(ptr);
- return nullptr;
- }
- return ptr;
+ // FIXME: NVIDIA targets currently use the built-in 'malloc' which we cannot
+ // reason with. But we still need to provide this function for compatibility.
+#ifndef LIBC_TARGET_ARCH_IS_NVPTX
+ return gpu::aligned_allocate(static_cast<uint32_t>(alignment), size);
+#else
+ (void)alignment;
+ (void)size;
+ return nullptr;
+#endif
}
} // namespace LIBC_NAMESPACE_DECL