[libc-commits] [libc] dea4f32 - [libc] Use is aligned builtin instead of ptrtoint (#146402)
via libc-commits
libc-commits at lists.llvm.org
Wed Jul 2 05:03:15 PDT 2025
Author: Joseph Huber
Date: 2025-07-02T07:03:11-05:00
New Revision: dea4f3213d84507ca9741faccab789f48c44e00d
URL: https://github.com/llvm/llvm-project/commit/dea4f3213d84507ca9741faccab789f48c44e00d
DIFF: https://github.com/llvm/llvm-project/commit/dea4f3213d84507ca9741faccab789f48c44e00d.diff
LOG: [libc] Use is aligned builtin instead of ptrtoint (#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.
Added:
Modified:
libc/src/__support/GPU/allocator.cpp
Removed:
################################################################################
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