Skip to content

[libc][stdlib] Remove LIBC_THREAD_LOCAL from rand_next #96245

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

Closed
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions libc/src/stdlib/rand_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ namespace LIBC_NAMESPACE {
#ifdef LIBC_TARGET_ARCH_IS_GPU
// FIXME: Local GPU memory cannot be initialized so we cannot currently provide
// a standard compliant default value.
ThreadLocal<unsigned long> rand_next;
unsigned long rand_next;
#else
// C standard 7.10p2: If 'rand' is called before 'srand' it is to proceed as if
// the 'srand' function was called with a value of '1'.
LIBC_THREAD_LOCAL unsigned long rand_next = 1;
unsigned long rand_next = 1;
#endif

} // namespace LIBC_NAMESPACE
23 changes: 1 addition & 22 deletions libc/src/stdlib/rand_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,7 @@

namespace LIBC_NAMESPACE {

#ifdef LIBC_TARGET_ARCH_IS_GPU
// Implement thread local storage on the GPU using local memory. Each thread
// gets its slot in the local memory array and is private to the group.
// TODO: We need to implement the 'thread_local' keyword on the GPU. This is an
// inefficient and incomplete stand-in until that is done.
template <typename T> class ThreadLocal {
private:
static constexpr long MAX_THREADS = 1024;
[[clang::loader_uninitialized]] static inline gpu::Local<T>
storage[MAX_THREADS];

public:
LIBC_INLINE operator T() const { return storage[gpu::get_thread_id()]; }
LIBC_INLINE void operator=(const T &value) {
storage[gpu::get_thread_id()] = value;
}
};

extern ThreadLocal<unsigned long> rand_next;
#else
extern LIBC_THREAD_LOCAL unsigned long rand_next;
#endif
extern unsigned long rand_next;

} // namespace LIBC_NAMESPACE

Expand Down
Loading