Skip to content

Commit

Permalink
[OpenMP] Handle function calls from libomp to libomptarget correctly
Browse files Browse the repository at this point in the history
D132005 introduced function calls from `libomp` to `libomptarget` if offloading
is enabled. However, the external function declaration may not always work. For
example, it causes a link error on macOS. Currently it is guarded properly by
a macro, but in order to get OpenMP target offloading working on macOS, it has
to be handled correctly. This patch applies the same idea of how we support
target memory extension by using function pointer indirect call for that function.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D149557
  • Loading branch information
shiltian committed May 1, 2023
1 parent d572cd1 commit c3efd7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions openmp/runtime/src/kmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,12 @@ extern void __kmp_init_target_mem();

/* ------------------------------------------------------------------------ */

#if ENABLE_LIBOMPTARGET
extern void __kmp_init_target_task();
#endif

/* ------------------------------------------------------------------------ */

#define KMP_UINT64_MAX \
(~((kmp_uint64)1 << ((sizeof(kmp_uint64) * (1 << 3)) - 1)))

Expand Down
13 changes: 13 additions & 0 deletions openmp/runtime/src/kmp_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ static void __kmp_fini_allocator() { __kmp_fini_memkind(); }

/* ------------------------------------------------------------------------ */

#if ENABLE_LIBOMPTARGET
static void __kmp_init_omptarget() {
__kmp_init_target_task();
}
#endif

/* ------------------------------------------------------------------------ */

#if KMP_DYNAMIC_LIB
#if KMP_OS_WINDOWS

Expand Down Expand Up @@ -7041,6 +7049,11 @@ static void __kmp_do_serial_initialize(void) {

__kmp_validate_locks();

#if ENABLE_LIBOMPTARGET
/* Initialize functions from libomptarget */
__kmp_init_omptarget();
#endif

/* Initialize internal memory allocator */
__kmp_init_allocator();

Expand Down
10 changes: 7 additions & 3 deletions openmp/runtime/src/kmp_tasking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
#endif

#if ENABLE_LIBOMPTARGET
// Declaration of synchronization function from libomptarget.
extern "C" void __tgt_target_nowait_query(void **) KMP_WEAK_ATTRIBUTE_INTERNAL;
static void (*tgt_target_nowait_query)(void **);

void __kmp_init_target_task() {
*(void **)(&tgt_target_nowait_query) = KMP_DLSYM("__tgt_target_nowait_query");
}
#endif

/* forward declaration */
Expand Down Expand Up @@ -1805,7 +1808,8 @@ static void __kmp_invoke_task(kmp_int32 gtid, kmp_task_t *task,
// If we have a valid target async handle, that means that we have already
// executed the task routine once. We must query for the handle completion
// instead of re-executing the routine.
__tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
KMP_ASSERT(tgt_target_nowait_query);
tgt_target_nowait_query(&taskdata->td_target_data.async_handle);
} else
#endif
if (task->routine != NULL) {
Expand Down

0 comments on commit c3efd7e

Please sign in to comment.