Skip to content

Commit a3f423c

Browse files
committed
[OpenMP] Add dynamic memory function to omp.h and add documentation
This patch adds the `llvm_omp_target_dynamic_shared_alloc` function to the `omp.h` header file so users can access it by default. Also changed the name to keep it consistent with the other target allocators. Added some documentation so users know how to use it. Didn't add the interface for Fortran since there's no way to test it right now. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D123246
1 parent 840c040 commit a3f423c

File tree

8 files changed

+50
-10
lines changed

8 files changed

+50
-10
lines changed

openmp/docs/design/Runtimes.rst

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,9 @@ LIBOMPTARGET_SHARED_MEMORY_SIZE
10061006
"""""""""""""""""""""""""""""""
10071007

10081008
This environment variable sets the amount of dynamic shared memory in bytes used
1009-
by the kernel once it is launched. A pointer to the dynamic memory buffer can
1010-
currently only be accessed using the ``__kmpc_get_dynamic_shared`` device
1011-
runtime call.
1009+
by the kernel once it is launched. A pointer to the dynamic memory buffer can be
1010+
accessed using the ``llvm_omp_target_dynamic_shared_alloc`` function. An example
1011+
is shown in :ref:`libomptarget_dynamic_shared`.
10121012

10131013
.. toctree::
10141014
:hidden:
@@ -1104,6 +1104,40 @@ The target device runtime is an LLVM bitcode library that implements OpenMP
11041104
runtime functions on the target device. It is linked with the device code's LLVM
11051105
IR during compilation.
11061106

1107+
.. _libomptarget_dynamic_shared:
1108+
1109+
Dynamic Shared Memory
1110+
^^^^^^^^^^^^^^^^^^^^^
1111+
1112+
The target device runtime contains a pointer to the dynamic shared memory
1113+
buffer. This pointer can be obtained using the
1114+
``llvm_omp_target_dynamic_shared_alloc`` extension. If this function is called
1115+
from the host it will simply return a null pointer. In order to use this buffer
1116+
the kernel must be launched with an adequate amount of dynamic shared memory
1117+
allocated. Currently this is done using the ``LIBOMPTARGET_SHARED_MEMORY_SIZE``
1118+
environment variable. An example is given below.
1119+
1120+
.. code-block:: c++
1121+
1122+
void foo() {
1123+
int x;
1124+
#pragma omp target parallel map(from : x)
1125+
{
1126+
int *buf = llvm_omp_target_dynamic_shared_alloc();
1127+
#pragma omp barrier
1128+
if (omp_get_thread_num() == 0)
1129+
*buf = 1;
1130+
#pragma omp barrier
1131+
if (omp_get_thread_num() == 1)
1132+
x = *buf;
1133+
}
1134+
}
1135+
1136+
.. code-block:: console
1137+
1138+
$ clang++ -fopenmp -fopenmp-targets=nvptx64 shared.c
1139+
$ env LIBOMPTARGET_SHARED_MEMORY_SIZE=256 ./shared
1140+
11071141
.. _libomptarget_device_debugging:
11081142

11091143
Debugging

openmp/libomptarget/DeviceRTL/include/Interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int omp_get_team_num();
132132

133133
int omp_get_initial_device(void);
134134

135-
void *llvm_omp_get_dynamic_shared();
135+
void *llvm_omp_target_dynamic_shared_alloc();
136136

137137
/// Synchronization
138138
///

openmp/libomptarget/DeviceRTL/src/State.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,10 @@ __attribute__((noinline)) void __kmpc_free_shared(void *Ptr, uint64_t Bytes) {
530530

531531
void *__kmpc_get_dynamic_shared() { return memory::getDynamicBuffer(); }
532532

533+
void *llvm_omp_target_dynamic_shared_alloc() {
534+
return __kmpc_get_dynamic_shared();
535+
}
536+
533537
void *llvm_omp_get_dynamic_shared() { return __kmpc_get_dynamic_shared(); }
534538

535539
/// Allocate storage in shared memory to communicate arguments from the main

openmp/libomptarget/include/omptarget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void *llvm_omp_target_alloc_host(size_t size, int device_num);
226226
void *llvm_omp_target_alloc_shared(size_t size, int device_num);
227227

228228
/// Dummy target so we have a symbol for generating host fallback.
229-
void *llvm_omp_get_dynamic_shared();
229+
void *llvm_omp_target_dynamic_shared_alloc();
230230

231231
/// add the clauses of the requires directives in a given file
232232
void __tgt_register_requires(int64_t flags);

openmp/libomptarget/src/api.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ EXTERN void *llvm_omp_target_alloc_shared(size_t size, int device_num) {
5353
return targetAllocExplicit(size, device_num, TARGET_ALLOC_SHARED, __func__);
5454
}
5555

56+
EXTERN void *llvm_omp_target_dynamic_shared_alloc() { return nullptr; }
5657
EXTERN void *llvm_omp_get_dynamic_shared() { return nullptr; }
5758

5859
EXTERN void omp_target_free(void *device_ptr, int device_num) {

openmp/libomptarget/src/exports

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ VERS1.0 {
4040
llvm_omp_target_alloc_host;
4141
llvm_omp_target_alloc_shared;
4242
llvm_omp_target_alloc_device;
43-
llvm_omp_get_dynamic_shared;
43+
llvm_omp_target_dynamic_shared_alloc;
4444
__tgt_set_info_flag;
4545
__tgt_print_device_info;
4646
omp_get_interop_ptr;

openmp/libomptarget/test/api/omp_dynamic_shared_memory.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
#include <omp.h>
77
#include <stdio.h>
88

9-
void *llvm_omp_get_dynamic_shared();
10-
119
int main() {
1210
int x;
1311
#pragma omp target parallel map(from : x)
1412
{
15-
int *buf = llvm_omp_get_dynamic_shared() + 252;
13+
int *buf = llvm_omp_target_dynamic_shared_alloc() + 252;
1614
#pragma omp barrier
1715
if (omp_get_thread_num() == 0)
1816
*buf = 1;
@@ -22,6 +20,6 @@ int main() {
2220
}
2321

2422
// CHECK: PASS
25-
if (x == 1 && llvm_omp_get_dynamic_shared() == NULL)
23+
if (x == 1 && llvm_omp_target_dynamic_shared_alloc() == NULL)
2624
printf("PASS\n");
2725
}

openmp/runtime/src/include/omp.h.var

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@
496496
/* OpenMP 5.2 */
497497
extern int __KAI_KMPC_CONVENTION omp_in_explicit_task(void);
498498

499+
/* LLVM Extensions */
500+
extern void *llvm_omp_target_dynamic_shared_alloc();
501+
499502
# undef __KAI_KMPC_CONVENTION
500503
# undef __KMP_IMP
501504

0 commit comments

Comments
 (0)