[flang-commits] [clang] [flang] [flang][Driver] Support -pthread in the frontend (PR #77360)
Kareem Ergawy via flang-commits
flang-commits at lists.llvm.org
Tue Jan 9 02:44:42 PST 2024
ergawy wrote:
Thanks @tarunprabhu for opening this. I indeed closed my original PR but was about to reopen it after last week's discussion.
At least for the GNU toolchain, it won't be easy to come up with a simple test that fails without `-pthread`. The reason is that the pthread API is actually exported by `libc` and that `-lc` is added by the GNU toolchain in the driver in any case.
Just to give more context, I tested with the following program:
```
program main
INTERFACE
SUBROUTINE pthread_create() BIND(C)
USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_INT
IMPLICIT NONE
END SUBROUTINE pthread_create
END INTERFACE
print*, "======= Calling pthread_create() ======="
call pthread_create()
end program main
```
And expected `./bin/flang-new /tmp/test_pthread.f90 -o /tmp/test_pthread_2 -v` to complain that the `pthread_create()` symbol is undefined. However, it compiles and links fine.
And if you look at the linker command, you find it looks like this:
```
"/work/kaergawy/git/trunk18.0/build/llvm-project/bin/ld.lld" -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /tmp/test_pthread_2 /lib/x86_64-linux-gnu/crt1.o /lib/x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/12/crtbegin.o -L/work/kaergawy/git/trunk18.0/build/llvm-project/lib/clang/18/lib/x86_64-unknown-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/12 -L/usr/lib/gcc/x86_64-linux-gnu/12/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test_pthread_fortran.o -L/work/kaergawy/git/trunk18.0/build/llvm-project/lib --whole-archive -lFortran_main --no-whole-archive -lFortranRuntime -lFortranDecimal -lm -lgcc --as-needed -lgcc_s --no-as-needed >> -lc << -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/12/crtend.o /lib/x86_64-linux-gnu/crtn.o
```
Note the `-lc` flag that I highligted with `>> ... <<` above. If you remove that flag from the linker command, you get:
```
ld.lld: error: undefined symbol: pthread_create
>>> referenced by FIRModule
>>> /tmp/test_pthread_fortran.o:(_QQmain)
ld.lld: error: undefined symbol: __libc_start_main
>>> referenced by /lib/x86_64-linux-gnu/crt1.o:(_start)
```
And indeed if you `nm --defined-only /usr/lib/x86_64-linux-gnu/libc.a`, you find that `pthread_create` is actually defined by `libc`.
---
That said, what @tarunprabhu mentioned about OpenMPI wrappers adding the flag is indeed correct as mentioned by Brian Cornille in the last flang bi-weekly. So adding the flag would indeed make sense even if it is redundant for the GNU toolchain but it would be consistent with `clang`. However, coming up with a failing test will prove more difficult that it initially seems.
I shared the PR with Brian since I cannot add him as a reviewer.
https://github.com/llvm/llvm-project/pull/77360
More information about the flang-commits
mailing list