[OpenMP] Avoid calling `isSPMDMode` during RT initialization

Until we hit the first barrier we should not call `mapping::isSPMDMode`
with all threads. Instead, we now have (and use during initialization) a
`mapping::isMainThreadInGenericMode` overload that takes the known
SPMD-mode state and one that queries it.

Reviewed By: tianshilei1992

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

GitOrigin-RevId: 85ad56633593d011ab00f4eb431ffd879677bffc
diff --git a/libomptarget/DeviceRTL/include/Mapping.h b/libomptarget/DeviceRTL/include/Mapping.h
index b34ecf4..a3193f3 100644
--- a/libomptarget/DeviceRTL/include/Mapping.h
+++ b/libomptarget/DeviceRTL/include/Mapping.h
@@ -35,6 +35,7 @@
 
 /// Return true if the executing thread is the main thread in generic mode.
 bool isMainThreadInGenericMode();
+bool isMainThreadInGenericMode(bool IsSPMD);
 
 /// Return true if the executing thread has the lowest Id of the active threads
 /// in the warp.
diff --git a/libomptarget/DeviceRTL/src/Kernel.cpp b/libomptarget/DeviceRTL/src/Kernel.cpp
index ead6085..f834754 100644
--- a/libomptarget/DeviceRTL/src/Kernel.cpp
+++ b/libomptarget/DeviceRTL/src/Kernel.cpp
@@ -81,7 +81,7 @@
     return -1;
   }
 
-  if (mapping::isMainThreadInGenericMode())
+  if (mapping::isMainThreadInGenericMode(IsSPMD))
     return -1;
 
   if (UseGenericStateMachine)
diff --git a/libomptarget/DeviceRTL/src/Mapping.cpp b/libomptarget/DeviceRTL/src/Mapping.cpp
index 96a612d..66089ea 100644
--- a/libomptarget/DeviceRTL/src/Mapping.cpp
+++ b/libomptarget/DeviceRTL/src/Mapping.cpp
@@ -165,8 +165,8 @@
 } // namespace impl
 } // namespace _OMP
 
-bool mapping::isMainThreadInGenericMode() {
-  if (mapping::isSPMDMode() || icv::Level)
+bool mapping::isMainThreadInGenericMode(bool IsSPMD) {
+  if (IsSPMD || icv::Level)
     return false;
 
   // Check if this is the last warp in the block.
@@ -175,6 +175,10 @@
   return mapping::getThreadIdInBlock() == MainTId;
 }
 
+bool mapping::isMainThreadInGenericMode() {
+  return mapping::isMainThreadInGenericMode(mapping::isSPMDMode());
+}
+
 bool mapping::isLeaderInWarp() {
   __kmpc_impl_lanemask_t Active = mapping::activemask();
   __kmpc_impl_lanemask_t LaneMaskLT = mapping::lanemaskLT();
diff --git a/libomptarget/DeviceRTL/src/Reduction.cpp b/libomptarget/DeviceRTL/src/Reduction.cpp
index cd56581..a06ac23 100644
--- a/libomptarget/DeviceRTL/src/Reduction.cpp
+++ b/libomptarget/DeviceRTL/src/Reduction.cpp
@@ -72,7 +72,7 @@
                                             InterWarpCopyFnTy cpyFct,
                                             bool isSPMDExecutionMode, bool) {
   uint32_t BlockThreadId = mapping::getThreadIdInBlock();
-  if (mapping::isMainThreadInGenericMode())
+  if (mapping::isMainThreadInGenericMode(/* IsSPMD */ false))
     BlockThreadId = 0;
   uint32_t NumThreads = omp_get_num_threads();
   if (NumThreads == 1)