Skip to content

[libc] Simplifiy slab waiting in GPU memory allocator #152872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions libc/src/__support/GPU/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ static inline uint32_t get_leader_id(uint64_t ballot, uint32_t id) {

// We use a sentinal value to indicate a failed or in-progress allocation.
template <typename T> bool is_sentinel(const T &x) {
return x == cpp::numeric_limits<T>::max();
if constexpr (cpp::is_pointer_v<T>)
return reinterpret_cast<uintptr_t>(x) ==
cpp::numeric_limits<uintptr_t>::max();
else
return x == cpp::numeric_limits<T>::max();
}

} // namespace impl
Expand Down Expand Up @@ -446,7 +450,13 @@ struct GuardPtr {
return new (raw) Slab(cpp::forward<Args>(args)...);
}

if (!expected || impl::is_sentinel(reinterpret_cast<uintptr_t>(expected)))
// If there is a slab allocation in progress we retry a few times.
for (uint32_t t = 0; impl::is_sentinel(expected) && t < MAX_TRIES; ++t) {
sleep_briefly();
expected = ptr.load(cpp::MemoryOrder::RELAXED);
}

if (!expected || impl::is_sentinel(expected))
return nullptr;

if (!ref.acquire(n, count))
Expand Down Expand Up @@ -557,16 +567,6 @@ static Slab *find_slab(uint32_t chunk_size, uint64_t &uniform,
Slab *slab = slots[index].try_lock(lane_mask, uniform & lane_mask,
reserved, chunk_size, index);

// If there is a slab allocation in progress we retry a few times.
for (uint32_t retries = 0;
!slab && !impl::is_sentinel(reserved) && retries < MAX_TRIES;
retries++) {
uint64_t lane_mask = gpu::get_lane_mask();
slab = slots[index].try_lock(lane_mask, uniform & lane_mask, reserved,
chunk_size, index);
sleep_briefly();
}

// If we find a slab with a matching chunk size then we store the result.
// Otherwise, we need to free the claimed lock and continue. In the case
// of out-of-memory we receive a sentinel value and return a failure.
Expand Down
Loading