[libc-commits] [libc] [libc] Implement efficient 'malloc' on the GPU (PR #140156)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Fri May 16 07:05:51 PDT 2025


================
@@ -27,21 +47,424 @@ void *rpc_allocate(uint64_t size) {
   return ptr;
 }
 
-void rpc_free(void *ptr) {
+// Deallocates the associated system memory.
+static void rpc_free(void *ptr) {
   rpc::Client::Port port = rpc::client.open<LIBC_FREE>();
   port.send([=](rpc::Buffer *buffer, uint32_t) {
     buffer->data[0] = reinterpret_cast<uintptr_t>(ptr);
   });
   port.close();
 }
 
-} // namespace
+// Convert a potentially disjoint bitmask into an increasing integer for use
+// with indexing.
+static inline uint32_t lane_count(uint64_t lane_mask) {
+  return cpp::popcount(lane_mask & ((1ull << gpu::get_lane_id()) - 1));
+}
+
+// Obtain an initial value to seed a random number generator.
+static inline uint32_t entropy() {
+  return (static_cast<uint32_t>(gpu::processor_clock()) ^
----------------
jhuber6 wrote:

If it's pseudorandom then you'd need to seed it similarly to here. I was hoping for something like x86's `RDRAND` and `RDSEED` which use hardware effect to generate real entropy.

https://github.com/llvm/llvm-project/pull/140156


More information about the libc-commits mailing list