summaryrefslogtreecommitdiff
path: root/libc/test/integration/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/test/integration/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/test/integration/src/stdlib/gpu/aligned_alloc.cpp')
-rw-r--r--libc/test/integration/src/stdlib/gpu/aligned_alloc.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/libc/test/integration/src/stdlib/gpu/aligned_alloc.cpp b/libc/test/integration/src/stdlib/gpu/aligned_alloc.cpp
new file mode 100644
index 000000000000..b966e6953cc2
--- /dev/null
+++ b/libc/test/integration/src/stdlib/gpu/aligned_alloc.cpp
@@ -0,0 +1,29 @@
+#include "test/IntegrationTest/test.h"
+
+#include "src/__support/GPU/utils.h"
+#include "src/stdlib/aligned_alloc.h" // Adjust path if needed
+#include "src/stdlib/free.h"
+
+using namespace LIBC_NAMESPACE;
+
+TEST_MAIN(int, char **, char **) {
+ // aligned_alloc with valid alignment and size
+ void *ptr = LIBC_NAMESPACE::aligned_alloc(32, 16);
+ EXPECT_NE(ptr, nullptr);
+ EXPECT_EQ(__builtin_is_aligned(ptr, 32), 0U);
+
+ LIBC_NAMESPACE::free(ptr);
+
+ // aligned_alloc fails if alignment is not power of two
+ void *bad_align = LIBC_NAMESPACE::aligned_alloc(30, 99);
+ EXPECT_EQ(bad_align, nullptr);
+
+ // aligned_alloc with a divergent size.
+ size_t alignment = 1 << (__gpu_lane_id() % 8 + 1);
+ void *div =
+ LIBC_NAMESPACE::aligned_alloc(alignment, (gpu::get_thread_id() + 1) * 4);
+ EXPECT_NE(div, nullptr);
+ EXPECT_EQ(__builtin_is_aligned(div, alignment), 0U);
+
+ return 0;
+}