diff --git a/openmp/libomptarget/DeviceRTL/include/Interface.h b/openmp/libomptarget/DeviceRTL/include/Interface.h --- a/openmp/libomptarget/DeviceRTL/include/Interface.h +++ b/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. diff --git a/openmp/libomptarget/DeviceRTL/src/Parallelism.cpp b/openmp/libomptarget/DeviceRTL/src/Parallelism.cpp --- a/openmp/libomptarget/DeviceRTL/src/Parallelism.cpp +++ b/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 diff --git a/openmp/libomptarget/DeviceRTL/src/State.cpp b/openmp/libomptarget/DeviceRTL/src/State.cpp --- a/openmp/libomptarget/DeviceRTL/src/State.cpp +++ b/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) {