Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OpenMP] Fix issue of indirect function call in __kmpc_fork_call_if #65436

Merged
merged 1 commit into from
Sep 6, 2023

Conversation

shiltian
Copy link
Contributor

@shiltian shiltian commented Sep 6, 2023

The outlined function is typically invoked by using __kmp_invoke_microtask,
which is written in asm. D138495 introduces a new interface function for parallel
region for OpenMPIRBuilder, where the outlined function is called via the function
pointer. For some reason, it works perfectly well on x86 and x86-64 system, but
doesn't work on Apple Silicon. The 3rd argument in the callee is always nullptr, even
if it is not in caller. It appears x2 always contains 0x0. This patch adopts
the typical method to invoke the function pointer. It works on my M2 Ultra Mac.

Fix #63194.

The outlined function is typically invoked by using `__kmp_invoke_microtask`,
which is written in asm. D138495 introduces a new interface function for parallel
region for OpenMPIRBuilder, where the outlined function is called via the function
pointer. For some reason, it works perfectly well on x86 and x86-64 system, but
doesn't work on aarch64. The 3rd argument in the callee is always `nullptr`, even
if it is not in caller. It appears `x2` always contains `0x0`. This patch adopts
the typical method to invoke the function pointer. It works on my M2 Ultra Mac.

Fix llvm#63194.
@shiltian shiltian requested review from a team and DavidTruby September 6, 2023 04:10
Copy link
Member

@DavidTruby DavidTruby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

Copy link
Member

@DavidTruby DavidTruby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It might be worth clarifying in the commit message that this fix is for macOS AArch64 rather than AArch64 in genral, as this does work already on Linux.

@shiltian shiltian merged commit 518b08c into llvm:main Sep 6, 2023
2 checks passed
@shiltian shiltian deleted the shiltian/issue63194 branch September 6, 2023 16:17
@dpalermo
Copy link
Contributor

dpalermo commented Sep 7, 2023

Seeing the following compilation errors with this change:

FAILED: openmp/runtime/src/CMakeFiles/omp.dir/kmp_csupport.cpp.o
...
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp_csupport.cpp:359:7: error: no matching function for call to '__kmp_invoke_microtask'
  359 |       __kmp_invoke_microtask(VOLATILE_CAST(microtask_t) microtask, gtid,
      |       ^~~~~~~~~~~~~~~~~~~~~~
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp.h:4037:12: note: candidate function not viable: requires 5 arguments, but 6 were provided
 4037 | extern int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int npr, int argc,
      |            ^                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 4038 |                                   void *argv[]
      |                                   ~~~~~~~~~~~~
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp_csupport.cpp:368:7: error: no matching function for call to '__kmp_invoke_microtask'
  368 |       __kmp_invoke_microtask(VOLATILE_CAST(microtask_t) microtask, gtid,
      |       ^~~~~~~~~~~~~~~~~~~~~~
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp.h:4037:12: note: candidate function not viable: requires 5 arguments, but 6 were provided
 4037 | extern int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int npr, int argc,
      |            ^                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 4038 |                                   void *argv[]
      |                                   ~~~~~~~~~~~~
2 errors generated.

Can be fixed by replacing the added #ifdef OMPT_SUPPORT with #if OMPT_SUPPORT:

[r5 /work1/omp-nightly/build/git/trunk17.0/llvm-project]$ git diff
diff --git a/openmp/runtime/src/kmp_csupport.cpp b/openmp/runtime/src/kmp_csupport.cpp
index fb79adb93641..9eeaeb88fb9e 100644
--- a/openmp/runtime/src/kmp_csupport.cpp
+++ b/openmp/runtime/src/kmp_csupport.cpp
@@ -351,7 +351,7 @@ void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
   } else {
     __kmpc_serialized_parallel(loc, gtid);

-#ifdef OMPT_SUPPORT
+#if OMPT_SUPPORT
     void *exit_frame_ptr;
 #endif

@@ -359,7 +359,7 @@ void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
       __kmp_invoke_microtask(VOLATILE_CAST(microtask_t) microtask, gtid,
                              /*npr=*/0,
                              /*argc=*/1, &args
-#ifdef OMPT_SUPPORT
+#if OMPT_SUPPORT
                              ,
                              &exit_frame_ptr
 #endif
@@ -369,7 +369,7 @@ void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
                              /*npr=*/0,
                              /*argc=*/0,
                              /*args=*/nullptr
-#ifdef OMPT_SUPPORT
+#if OMPT_SUPPORT
                              ,
                              &exit_frame_ptr
 #endif

@shiltian
Copy link
Contributor Author

shiltian commented Sep 7, 2023

Seeing the following compilation errors with this change:

FAILED: openmp/runtime/src/CMakeFiles/omp.dir/kmp_csupport.cpp.o
...
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp_csupport.cpp:359:7: error: no matching function for call to '__kmp_invoke_microtask'
  359 |       __kmp_invoke_microtask(VOLATILE_CAST(microtask_t) microtask, gtid,
      |       ^~~~~~~~~~~~~~~~~~~~~~
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp.h:4037:12: note: candidate function not viable: requires 5 arguments, but 6 were provided
 4037 | extern int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int npr, int argc,
      |            ^                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 4038 |                                   void *argv[]
      |                                   ~~~~~~~~~~~~
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp_csupport.cpp:368:7: error: no matching function for call to '__kmp_invoke_microtask'
  368 |       __kmp_invoke_microtask(VOLATILE_CAST(microtask_t) microtask, gtid,
      |       ^~~~~~~~~~~~~~~~~~~~~~
/work1/omp-nightly/build/git/trunk17.0/llvm-project/openmp/runtime/src/kmp.h:4037:12: note: candidate function not viable: requires 5 arguments, but 6 were provided
 4037 | extern int __kmp_invoke_microtask(microtask_t pkfn, int gtid, int npr, int argc,
      |            ^                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 4038 |                                   void *argv[]
      |                                   ~~~~~~~~~~~~
2 errors generated.

Can be fixed by replacing the added #ifdef OMPT_SUPPORT with #if OMPT_SUPPORT:

[r5 /work1/omp-nightly/build/git/trunk17.0/llvm-project]$ git diff
diff --git a/openmp/runtime/src/kmp_csupport.cpp b/openmp/runtime/src/kmp_csupport.cpp
index fb79adb93641..9eeaeb88fb9e 100644
--- a/openmp/runtime/src/kmp_csupport.cpp
+++ b/openmp/runtime/src/kmp_csupport.cpp
@@ -351,7 +351,7 @@ void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
   } else {
     __kmpc_serialized_parallel(loc, gtid);

-#ifdef OMPT_SUPPORT
+#if OMPT_SUPPORT
     void *exit_frame_ptr;
 #endif

@@ -359,7 +359,7 @@ void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
       __kmp_invoke_microtask(VOLATILE_CAST(microtask_t) microtask, gtid,
                              /*npr=*/0,
                              /*argc=*/1, &args
-#ifdef OMPT_SUPPORT
+#if OMPT_SUPPORT
                              ,
                              &exit_frame_ptr
 #endif
@@ -369,7 +369,7 @@ void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc, kmpc_micro microtask,
                              /*npr=*/0,
                              /*argc=*/0,
                              /*args=*/nullptr
-#ifdef OMPT_SUPPORT
+#if OMPT_SUPPORT
                              ,
                              &exit_frame_ptr
 #endif

Yeah, thanks for that. Fixed.

avillega pushed a commit to avillega/llvm-project that referenced this pull request Sep 11, 2023
…llvm#65436)

The outlined function is typically invoked by using
`__kmp_invoke_microtask`,
which is written in asm. D138495 introduces a new interface function for
parallel
region for OpenMPIRBuilder, where the outlined function is called via
the function
pointer. For some reason, it works perfectly well on x86 and x86-64
system, but
doesn't work on Apple Silicon. The 3rd argument in the callee is always
`nullptr`, even
if it is not in caller. It appears `x2` always contains `0x0`. This
patch adopts
the typical method to invoke the function pointer. It works on my M2
Ultra Mac.

Fix llvm#63194.
__kmp_invoke_microtask(VOLATILE_CAST(microtask_t) microtask, gtid,
/*npr=*/0,
/*argc=*/0,
/*args=*/nullptr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shiltian This null pointer is causing segmentation fault in one of the flang test. I am new to Openmp runtimes, Could you please share what was the intention behind using nullptr here and can it be replaced with &args ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

check-openmp: some test cases failed on arm64/MacOS
5 participants