[OpenMP][DeviceRTL] Fix an issue that thread array might be corrupted
authorShilei Tian <i@tianshilei.me>
Thu, 6 Oct 2022 20:13:22 +0000 (16:13 -0400)
committerShilei Tian <i@tianshilei.me>
Thu, 6 Oct 2022 20:13:33 +0000 (16:13 -0400)
The shared memory stack in the device runtime assumes no intervined uses.
D135037 breaks the assumption, potentially causing the shared stack corruption.
This patch moves the thread array to heap memory. Since it is already the slow
path, it doesn't matter that much anyway.

Reviewed By: jhuber6

Differential Revision: https://reviews.llvm.org/D135391

openmp/libomptarget/DeviceRTL/src/State.cpp

index fc0c734..9e00c1f 100644 (file)
@@ -263,11 +263,11 @@ void state::enterDataEnvironment(IdentTy *Ident) {
   if (!atomic::load(ThreadStatesBitsPtr, atomic::seq_cst)) {
     uint32_t Bytes = sizeof(ThreadStates[0]) * mapping::getBlockSize();
     void *ThreadStatesPtr =
-        memory::allocShared(Bytes, "Thread state array allocation");
+        memory::allocGlobal(Bytes, "Thread state array allocation");
     if (!atomic::cas(ThreadStatesBitsPtr, uintptr_t(0),
                      reinterpret_cast<uintptr_t>(ThreadStatesPtr),
                      atomic::seq_cst, atomic::seq_cst))
-      memory::freeShared(ThreadStatesPtr, Bytes,
+      memory::freeGlobal(ThreadStatesPtr, Bytes,
                          "Thread state array allocated multiple times");
     ASSERT(atomic::load(ThreadStatesBitsPtr, atomic::seq_cst) &&
            "Expected valid thread states bit!");