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] [Flang] Resolved Issue llvm#76121: Implemented Check for Unhandled Arguments in __kmpc_fork_call_if #82221

Merged
merged 10 commits into from
May 9, 2024

Conversation

chandankds
Copy link
Contributor

Root cause: Segmentation fault is caused by null pointer dereference inside the __kmpc_fork_call_if function at https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/z_Linux_asm.S#L1186 . __kmpc_fork_call_if is missing case to handle argc=0 .

Fix: Added a check inside the __kmp_invoke_microtask function to handle the case when argc is 0.

Root cause: Segmentation fault is caused by null pointer dereference inside the __kmpc_fork_call_if function at https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/z_Linux_asm.S#L1186 . __kmpc_fork_call_if is missing case to handle argc=0 .

Fix: Added a check inside the __kmp_invoke_microtask function to handle the case when argc is 0.
@chandankds
Copy link
Contributor Author

Ping for review @jdoerfert @kiranchandramohan

@shiltian
Copy link
Contributor

Looks good to me. Can you add a test?

@llvmbot llvmbot added the flang Flang issues not falling into any other category label Mar 3, 2024
@@ -0,0 +1,27 @@
!RUN: %flang %s -o %t -fopenmp && %t | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this test can work here. It needs to be moved into openmp'/runtime/test, but it currently lacks of the infrastructure to handle it in libomp. Check openmp/libomptarget/test/lit.site.cfg.in on how we handle Fortran test cases in libomptarget.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for suggestion, I'll look into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @shiltian I've added the test, could you please review.

@shiltian
Copy link
Contributor

It looks like the replacement of %flang to the corresponding executable is still missing. In addition, this will require the system to have flang, which creates a dependence between libomp and flang. Does it have to be flang? Does GCC's fortran compiler expose this issue?

Added testcase
@chandankds chandankds changed the title [OpenMP] [Flang] Fix Issue llvm#76121 [OpenMP] [Flang] Resolved Issue llvm#76121: Implemented Check for Unhandled Arguments in __kmpc_fork_call_if Apr 12, 2024
Copy link

github-actions bot commented Apr 12, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@chandankds
Copy link
Contributor Author

@shiltian Thanks for sharing your insights, I understand we would like to avoid creating dependency between flang and libomp. Gcc fortan compiler does't reproduce this issue. I have added a test case which exposes the issue and validates the fix in c, please review the same.

@chandankds
Copy link
Contributor Author

@shiltian @jdoerfert humble request for review.

Copy link
Contributor

@shiltian shiltian left a comment

Choose a reason for hiding this comment

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

generally looks good

@@ -0,0 +1,22 @@
// RUN: %libomp-compile -Wno-implicit-function-declaration && %t | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need -Wno-implicit-function-declaration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think LLVM has moved to C99 standards recently because of which I was getting error: call to undeclared function '__kmpc_fork_call_if'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] so i've added this flag suppresses this error.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then you need to add declaration of __kmpc_fork_call_if

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had tried to declare void __kmpc_fork_call_if(ident_t *loc, kmp_int32 argc, kmpc_micro microtask, kmp_int32 cond, void *args) but it contains certain types which are not being found.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can define those types on your own. See openmp/runtime/test/atomic/kmp_atomic_cas.c for more details. Essentially you will just need ident_t.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for sharing your insights, I've defined required types and removed -Wno-implicit-function-declaration

// Call __kmpc_fork_call_if
__kmpc_fork_call_if(NULL, 0, microtask, cond, NULL);
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

format please, empty line EOF.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -1195,6 +1198,7 @@ KMP_LABEL(kmp_1_exit):
cmovnsq (%r11), %rdx // p_argv[0] -> %rdx (store 3rd parm to pkfn)
#endif // KMP_MIC

KMP_LABEL(kmp_no_args):
Copy link
Contributor

Choose a reason for hiding this comment

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

alignment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@shiltian shiltian left a comment

Choose a reason for hiding this comment

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

LG

@chandankds chandankds merged commit 2a57657 into llvm:main May 9, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang Flang issues not falling into any other category openmp:libomp OpenMP host runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants