[Openmp-commits] [openmp] 7472b42 - [OpenMP] Use Undef instead of null as pointer for inactive lanes

Johannes Doerfert via Openmp-commits openmp-commits at lists.llvm.org
Thu Jul 21 10:41:12 PDT 2022


Author: Johannes Doerfert
Date: 2022-07-21T12:28:45-05:00
New Revision: 7472b42b788e57b7b1ea255aa8670c96cc0aacd8

URL: https://github.com/llvm/llvm-project/commit/7472b42b788e57b7b1ea255aa8670c96cc0aacd8
DIFF: https://github.com/llvm/llvm-project/commit/7472b42b788e57b7b1ea255aa8670c96cc0aacd8.diff

LOG: [OpenMP] Use Undef instead of null as pointer for inactive lanes

Our conditional writes in the runtime look like this:
```
  if (active)
    *ptr = value;
```
In the RAII we need to assign `ptr` which comes from a lookup call.
If a thread that is not the main thread calls lookup with the intention
to write the pointer, we'll create a new thread state. As such, we need
to avoid calling lookup for inactive threads. We used to use `nullptr`
as their `ptr` value but that can cause pessimistic reasoning. We now
use `undef` instead.

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

Added: 
    

Modified: 
    openmp/libomptarget/DeviceRTL/include/State.h
    openmp/libomptarget/DeviceRTL/include/Utils.h

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/DeviceRTL/include/State.h b/openmp/libomptarget/DeviceRTL/include/State.h
index 08f08bd74a90..65d5b6d79220 100644
--- a/openmp/libomptarget/DeviceRTL/include/State.h
+++ b/openmp/libomptarget/DeviceRTL/include/State.h
@@ -277,7 +277,8 @@ template <typename Ty, ValueKind Kind> struct PtrValue {
 
 template <typename VTy, typename Ty> struct ValueRAII {
   ValueRAII(VTy &V, Ty NewValue, Ty OldValue, bool Active, IdentTy *Ident)
-      : Ptr(Active ? &V.lookup(/* IsReadonly */ false, Ident) : nullptr),
+      : Ptr(Active ? &V.lookup(/* IsReadonly */ false, Ident)
+                   : (Ty *)utils::UndefPtr),
         Val(OldValue), Active(Active) {
     if (!Active)
       return;

diff  --git a/openmp/libomptarget/DeviceRTL/include/Utils.h b/openmp/libomptarget/DeviceRTL/include/Utils.h
index dc4b1cd71a59..9e71b51cc507 100644
--- a/openmp/libomptarget/DeviceRTL/include/Utils.h
+++ b/openmp/libomptarget/DeviceRTL/include/Utils.h
@@ -14,6 +14,8 @@
 
 #include "Types.h"
 
+#pragma omp begin declare target device_type(nohost)
+
 namespace _OMP {
 namespace utils {
 
@@ -72,10 +74,15 @@ template <typename Ty1, typename Ty2> inline Ty1 align_down(Ty1 V, Ty2 Align) {
   return V - V % Align;
 }
 
+/// A  pointer variable that has by design an `undef` value. Use with care.
+__attribute__((loader_uninitialized)) static void *const UndefPtr;
+
 #define OMP_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
 #define OMP_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
 
 } // namespace utils
 } // namespace _OMP
 
+#pragma omp end declare target
+
 #endif


        


More information about the Openmp-commits mailing list