[libc-commits] [libc] ebb7e0c - [libc] Fix chunk calculation in GPU allocator (#221583)

via libc-commits libc-commits at lists.llvm.org
Sun Sep 6 08:24:57 PDT 2026


Author: Joseph Huber
Date: 2026-09-06T10:24:52-05:00
New Revision: ebb7e0cc79c17b6a7db53e128dd3f9afb4f26b4e

URL: https://github.com/llvm/llvm-project/commit/ebb7e0cc79c17b6a7db53e128dd3f9afb4f26b4e
DIFF: https://github.com/llvm/llvm-project/commit/ebb7e0cc79c17b6a7db53e128dd3f9afb4f26b4e.diff

LOG: [libc] Fix chunk calculation in GPU allocator (#221583)

Summary:
This would pesismistically round up 48 to 64 and the previous s0 case
was unused.

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 c88a70708b964..75b9ea73e609c 100644
--- a/libc/src/__support/GPU/allocator.cpp
+++ b/libc/src/__support/GPU/allocator.cpp
@@ -94,24 +94,21 @@ static inline uint32_t xorshift32(uint32_t &state) {
 }
 
 // Rounds the input value to the closest permitted chunk size. Here we accept
-// the sum of the closest three powers of two. For a 2MiB slab size this is 48
+// the sum of the closest three powers of two. For a 2MiB slab size this is 46
 // 
diff erent chunk sizes. This gives us average internal fragmentation of 87.5%.
 static inline constexpr uint32_t get_chunk_size(uint32_t x) {
   uint32_t y = x < MIN_SIZE ? MIN_SIZE : x;
   uint32_t pow2 = BITS_IN_WORD - cpp::countl_zero(y - 1);
 
-  uint32_t s0 = 0b0100 << (pow2 - 3);
-  uint32_t s1 = 0b0110 << (pow2 - 3);
-  uint32_t s2 = 0b0111 << (pow2 - 3);
-  uint32_t s3 = 0b1000 << (pow2 - 3);
+  uint32_t s0 = 0b0110 << (pow2 - 3);
+  uint32_t s1 = 0b0111 << (pow2 - 3);
+  uint32_t s2 = 0b1000 << (pow2 - 3);
 
-  if (s0 > y)
+  if (s0 >= y)
     return (s0 + MIN_ALIGNMENT) & ~MIN_ALIGNMENT;
-  if (s1 > y)
+  if (s1 >= y)
     return (s1 + MIN_ALIGNMENT) & ~MIN_ALIGNMENT;
-  if (s2 > y)
-    return (s2 + MIN_ALIGNMENT) & ~MIN_ALIGNMENT;
-  return (s3 + MIN_ALIGNMENT) & ~MIN_ALIGNMENT;
+  return (s2 + MIN_ALIGNMENT) & ~MIN_ALIGNMENT;
 }
 
 // Converts a chunk size into an index suitable for a statically sized array.


        


More information about the libc-commits mailing list