[Flang][OpenMP] undefined reference to `__kmpc_fork_call_if'
This is an issue from Fujitsu testsuite.
Flang-new terminates with linker error when compiling if clause of OpenMP.
The following are the test program, Flang-new and gfortran compilation result.
! test.f90
logical x
!$omp parallel if(x)
print *,'test'
!$omp endparallel
end
$ flang-new -v -fopenmp test.f90
flang-new version 17.0.0 (https://github.com/llvm/llvm-project.git 289828b1c0a6bc6eed8a634376a5152adae2283b)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Selected GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Candidate multilib: .;@m64
Selected multilib: .;@m64
"/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fopenmp -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-f13bdb.o -x f95-cpp-input test.f90
"/usr/bin/ld" -pie -EL --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64/Scrt1.o /usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64/crti.o /usr/lib/gcc/aarch64-redhat-linux/8/crtbeginS.o -L/path/to/install/lib/clang/17/lib/aarch64-unknown-linux-gnu -L/usr/lib/gcc/aarch64-redhat-linux/8 -L/usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test-f13bdb.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lomp -L/path/to/install/lib -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/aarch64-redhat-linux/8/crtendS.o /usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64/crtn.o
/tmp/test-f13bdb.o: In function `_QQmain':
FIRModule:(.text+0x4c): undefined reference to `__kmpc_fork_call_if'
flang-new: error: linker command failed with exit code 1 (use -v to see invocation)
$ gfortran -v -fopenmp test.f90
Driving: gfortran -v -fopenmp test.f90 -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/aarch64-redhat-linux/8/lto-wrapper
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)
:
$
@llvm/issue-subscribers-flang-ir
@llvm/issue-subscribers-openmp
A new runtime call was added to support parallel if. Are you building the OpenMP library that comes with llvm or are you using the one installed on your system?
Is there a /path/to/install/lib/libomp.so? Is it built from the llvm-project along with Flang?
I'm sorry for the late reply.
I built /path/to/install/lib/libomp.so from the llvm-project along with Flang.
And it has the symbol of __kmpc_fork_call_if.
$ nm /path/to/install/lib/libomp.so | grep __kmpc_fork_call_if
000000000003b490 T __kmpc_fork_call_if
In addition, Clang can compile a similar code written in C.
// test.c
#include <stdio.h>
int main(){
int x;
#pragma omp parallel if(x)
{
puts("test");
}
return 0;
}
$ clang -v -fopenmp test.c
clang version 17.0.0 (https://github.com/llvm/llvm-project.git 99074aafc31593c9935da483edab1333d6ce5a5b)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Selected GCC installation: /usr/lib/gcc/aarch64-redhat-linux/8
Candidate multilib: .;@m64
Selected multilib: .;@m64
"/path/to/install/bin/clang-17" -cc1 -triple aarch64-unknown-linux-gnu -emit-obj -mrelax-all -dumpdir a- -disable-free -clear-ast-before-backend -main-file-name test.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=non-leaf -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu generic -target-feature +neon -target-feature +v8a -target-abi aapcs -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/yus3710/work/CT_run/rst.fort.f-new.20230419a/rt_result/openmp01 -resource-dir /path/to/install/lib/clang/17 -internal-isystem /path/to/install/lib/clang/17/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/aarch64-redhat-linux/8/../../../../aarch64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir=/home/yus3710/work/CT_run/rst.fort.f-new.20230419a/rt_result/openmp01 -ferror-limit 19 -fopenmp -fno-signed-char -fgnuc-version=4.2.1 -fcolor-diagnostics -target-feature -fmv -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/test-ff07b6.o -x c test.c
clang -cc1 version 17.0.0 based upon LLVM 17.0.0git default target aarch64-unknown-linux-gnu
ignoring nonexistent directory "/usr/lib/gcc/aarch64-redhat-linux/8/../../../../aarch64-redhat-linux/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/path/to/install/lib/clang/17/include
/usr/local/include
/usr/include
End of search list.
"/usr/bin/ld" -pie -EL --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64/Scrt1.o /usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64/crti.o /usr/lib/gcc/aarch64-redhat-linux/8/crtbeginS.o -L/path/to/install/lib/clang/17/lib/aarch64-unknown-linux-gnu -L/usr/lib/gcc/aarch64-redhat-linux/8 -L/usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test-ff07b6.o -lomp -L/path/to/install/lib -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/aarch64-redhat-linux/8/crtendS.o /usr/lib/gcc/aarch64-redhat-linux/8/../../../../lib64/crtn.o
The following is the difference of linker arguments.
< /tmp/test-ff07b6.o -lomp
---
> /tmp/test-f13bdb.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lomp
And the following command works well.
$ flang-new -fopenmp test.f90 -c
$ clang test.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lomp
I tried to reproduce this issue in another machine, but I couldn't. So, I closed this issue.
FWIW, your problem was that you did pick up a system libomp.so which was too old. Mixing dynamic libraries will almost always cause problems. Adjustment of your LD_LIBRARY_PATH is one way, or add a clang config file into the install/bin folder such that clang picks up the right libraries automagically.
See https://openmp.llvm.org//SupportAndFAQ.html#q-what-are-the-llvm-components-used-in-offloading-and-how-are-they-found for information on config files for finding libomptarget.
Thank you for your comments.
I missed /usr/lib64/libomp.so that was installed with clang-8 on the system.