clangir icon indicating copy to clipboard operation
clangir copied to clipboard

Possible bug in OpenMP implementation

Open eZWALT opened this issue 11 months ago • 3 comments

I am seeking insights regarding an issue encountered during testing. When including the <omp.h> library in the test code as shown below (Bearing in mind that this simplified #pragma omp parallel is already implemented):

#include <omp.h>

int main(){
     #pragma omp parallel{
     
     }
}

I observed unusual exceptions. This inclusion leads to the CIRGenerator.cpp file reaching a conditional statement that terminates with a llvm_unreachable. While I understand this may be expected behavior, I lack sufficient context to fully comprehend the issue. Could someone explain why the code functions without the inclusion of <omp.h> but raises an exception when included?

This is the most relevant part of the error, if it helps:

NYI
UNREACHABLE executed at /home/walterjtv/Escritorio/Q8/TFG/clangir/clang/lib/CIR/CodeGen/CIRGenerator.cpp:164!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.	Program arguments: /home/walterjtv/Escritorio/Q8/TFG/clangir-install/bin/clang-19 -fopenmp -fclangir-enable -emit-cir 2_basic_parallel.c -o 2_basic_parallel.cir
1.	/usr/include/x86_64-linux-gnu/bits/types.h:155:26: current parser token '__fsid_t'
2.	/usr/include/x86_64-linux-gnu/bits/types.h:155:12 <Spelling=/usr/include/x86_64-linux-gnu/bits/typesizes.h:73:24>: parsing struct/union body 'struct (unnamed at /usr/include/x86_64-linux-gnu/bits/types.h:155:12)'
3.	/usr/include/x86_64-linux-gnu/bits/types.h:155:12 <Spelling=/usr/include/x86_64-linux-gnu/bits/typesizes.h:73:24>: CIR generation of declaration 'struct (unnamed at /usr/include/x86_64-linux-gnu/bits/types.h:155:12)'

Thank you for your support and efforts!

eZWALT avatar Mar 10 '24 17:03 eZWALT

The second line of the CIR error output gives the position of the error:

https://github.com/llvm/clangir/blob/b26f21d9d7d5f2a614d77bfb47a62d18f9344b6e/clang/lib/CIR/CodeGen/CIRGenerator.cpp#L162-L165

Basically this code checks whether we have -fopenmp flag in the command line. If the flag is present, then we crash with llvm_unreachable. So if you are implementing OpenMP stuff, you will need to replace this llvm_unreachable with your implementation code.

Note that this code only gets executed when we meet a TagDecl (which represents a struct / class / enum / union) on the clang AST. The inclusing of omp.h introduces a bunch of TagDecls so you get the error once you include omp.h.

If you are not sure what to do at here, a good reference is always the original clang CodeGen module. The corresponding part in the clang CodeGen is:

https://github.com/llvm/clangir/blob/b26f21d9d7d5f2a614d77bfb47a62d18f9344b6e/clang/lib/CodeGen/ModuleBuilder.cpp#L258-L268

You can start by understanding what the original clang CodeGen is doing and port it to CIRGen accordingly.

Lancern avatar Mar 11 '24 13:03 Lancern

Thanks for the detailed explanation @Lancern.

@eZWALT another possibility is to mock the relevant constructs out of #include <omp.h> and use them directly into your testcase, so you don't need to handle all the things not yet supported from that header. It's also possible that under clang/test/OpenMP you will find those mocked versions to copy from.

bcardosolopes avatar Mar 12 '24 17:03 bcardosolopes

Great explanation @Lancern , thanks for all the time invested, after implementing the task construct I will be checking this out!

eZWALT avatar Mar 14 '24 11:03 eZWALT