[Openmp-commits] [PATCH] D106905: [Libomptarget] Revert new variable sharing to use the old method

Joseph Huber via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Tue Jul 27 12:03:37 PDT 2021


jhuber6 created this revision.
jhuber6 added a reviewer: jdoerfert.
jhuber6 requested review of this revision.
Herald added subscribers: openmp-commits, sstefan1.
Herald added a project: OpenMP.

The new method of sharing variables introduces a `__kmpc_alloc_shared` call
that cannot be removed in the middle end because of its non-constant argument
and unconnected free. This patch reverts this to the old method that used a
static amount of shared memory for sharing variables.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106905

Files:
  openmp/libomptarget/DeviceRTL/include/Interface.h
  openmp/libomptarget/DeviceRTL/src/Parallelism.cpp
  openmp/libomptarget/DeviceRTL/src/State.cpp


Index: openmp/libomptarget/DeviceRTL/src/State.cpp
===================================================================
--- openmp/libomptarget/DeviceRTL/src/State.cpp
+++ openmp/libomptarget/DeviceRTL/src/State.cpp
@@ -497,19 +497,32 @@
   memory::freeShared(Ptr, Bytes, "Frontend free shared");
 }
 
+/// Allocate storage in shared memory to communicate arguments from the main
+/// thread to the workers in generic mode. If we exceed
+/// NUM_SHARED_VARIABLES_IN_SHARED_MEM we will malloc space for communication.
+constexpr uint64_t NUM_SHARED_VARIABLES_IN_SHARED_MEM = 64;
+
+[[clang::loader_uninitialized]] static void
+    *SharedMemVariableSharingSpace[NUM_SHARED_VARIABLES_IN_SHARED_MEM];
+#pragma omp allocate(SharedMemVariableSharingSpace)                            \
+    allocator(omp_pteam_mem_alloc)
 [[clang::loader_uninitialized]] static void **SharedMemVariableSharingSpacePtr;
 #pragma omp allocate(SharedMemVariableSharingSpacePtr)                         \
     allocator(omp_pteam_mem_alloc)
 
-void __kmpc_begin_sharing_variables(void ***GlobalArgs, uint64_t NumArgs) {
-  SharedMemVariableSharingSpacePtr =
-      (void **)__kmpc_alloc_shared(sizeof(void *) * NumArgs);
+void __kmpc_begin_sharing_variables(void ***GlobalArgs, uint64_t nArgs) {
+  if (nArgs <= NUM_SHARED_VARIABLES_IN_SHARED_MEM) {
+    SharedMemVariableSharingSpacePtr = &SharedMemVariableSharingSpace[0];
+  } else {
+    SharedMemVariableSharingSpacePtr = (void **)memory::allocGlobal(
+        nArgs * sizeof(void *), "new extended args");
+  }
   *GlobalArgs = SharedMemVariableSharingSpacePtr;
 }
 
-void __kmpc_end_sharing_variables(void **GlobalArgsPtr, uint64_t NumArgs) {
-  __kmpc_free_shared(SharedMemVariableSharingSpacePtr,
-                     sizeof(void *) * NumArgs);
+void __kmpc_end_sharing_variables() {
+  if (SharedMemVariableSharingSpacePtr != &SharedMemVariableSharingSpace[0])
+    memory::freeGlobal(SharedMemVariableSharingSpacePtr, "new extended args");
 }
 
 void __kmpc_get_shared_variables(void ***GlobalArgs) {
Index: openmp/libomptarget/DeviceRTL/src/Parallelism.cpp
===================================================================
--- openmp/libomptarget/DeviceRTL/src/Parallelism.cpp
+++ openmp/libomptarget/DeviceRTL/src/Parallelism.cpp
@@ -143,8 +143,7 @@
   }
 
   if (nargs)
-    memory::freeShared(GlobalArgs, nargs * sizeof(void *),
-                       "global args free shared");
+    __kmpc_end_sharing_variables();
 }
 
 __attribute__((noinline)) bool
Index: openmp/libomptarget/DeviceRTL/include/Interface.h
===================================================================
--- openmp/libomptarget/DeviceRTL/include/Interface.h
+++ openmp/libomptarget/DeviceRTL/include/Interface.h
@@ -186,7 +186,7 @@
 /// Deallocate the memory allocated by __kmpc_begin_sharing_variables.
 ///
 /// Called by the main thread after a parallel region.
-void __kmpc_end_sharing_variables(void **GlobalArgs, uint64_t NumArgs);
+void __kmpc_end_sharing_variables();
 
 /// Store the allocation address obtained via __kmpc_begin_sharing_variables in
 /// \p GlobalArgs.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106905.362132.patch
Type: text/x-patch
Size: 3096 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20210727/987aef36/attachment.bin>


More information about the Openmp-commits mailing list