[libc-commits] [libc] [libc] Use is aligned builtin instead of ptrtoint (PR #146402)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Mon Jun 30 11:30:47 PDT 2025


https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/146402

Summary:
This avoids a ptrtoint by just using the clang builtin. This is clang
specific but only clang can compile GPU code anyway so I do not bother
with a fallback.


>From ba66df6c026562c004341892d02fbceed738db67 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Mon, 30 Jun 2025 13:28:32 -0500
Subject: [PATCH] [libc] Use is aligned builtin instead of ptrtoint

Summary:
This avoids a ptrtoint by just using the clang builtin. This is clang
specific but only clang can compile GPU code anyway so I do not bother
with a fallback.
---
 libc/src/__support/GPU/allocator.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/__support/GPU/allocator.cpp b/libc/src/__support/GPU/allocator.cpp
index b2f2953e4f285..dfa03782a442e 100644
--- a/libc/src/__support/GPU/allocator.cpp
+++ b/libc/src/__support/GPU/allocator.cpp
@@ -541,7 +541,7 @@ void deallocate(void *ptr) {
     return;
 
   // All non-slab allocations will be aligned on a 2MiB boundary.
-  if ((reinterpret_cast<uintptr_t>(ptr) & SLAB_ALIGNMENT) == 0)
+  if (__builtin_is_aligned(ptr, SLAB_ALIGNMENT + 1))
     return impl::rpc_free(ptr);
 
   // The original slab pointer is the 2MiB boundary using the given pointer.
@@ -556,7 +556,7 @@ void *reallocate(void *ptr, uint64_t size) {
     return gpu::allocate(size);
 
   // Non-slab allocations are considered foreign pointers so we fail.
-  if ((reinterpret_cast<uintptr_t>(ptr) & SLAB_ALIGNMENT) == 0)
+  if (__builtin_is_aligned(ptr, SLAB_ALIGNMENT + 1))
     return nullptr;
 
   // The original slab pointer is the 2MiB boundary using the given pointer.



More information about the libc-commits mailing list