diff --git a/openmp/libomptarget/DeviceRTL/include/Interface.h b/openmp/libomptarget/DeviceRTL/include/Interface.h index 1c4c877b7662e3..21ff48c30d2cc9 100644 --- a/openmp/libomptarget/DeviceRTL/include/Interface.h +++ b/openmp/libomptarget/DeviceRTL/include/Interface.h @@ -186,7 +186,7 @@ void __kmpc_begin_sharing_variables(void ***GlobalArgs, uint64_t NumArgs); /// 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 index 7b505545f92fed..367c2966f10e93 100644 --- a/openmp/libomptarget/DeviceRTL/src/Parallelism.cpp +++ b/openmp/libomptarget/DeviceRTL/src/Parallelism.cpp @@ -143,8 +143,7 @@ void __kmpc_parallel_51(IdentTy *ident, int32_t, int32_t if_expr, } 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 index fb472c9bbe4d23..dae262a0408204 100644 --- a/openmp/libomptarget/DeviceRTL/src/State.cpp +++ b/openmp/libomptarget/DeviceRTL/src/State.cpp @@ -497,19 +497,32 @@ __attribute__((noinline)) void __kmpc_free_shared(void *Ptr, uint64_t Bytes) { 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) {