llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] position of `-L$LLVM_DIR/lib`

Open kawashima-fj opened this issue 1 year ago • 10 comments

The Flang driver puts -L$LLVM_DIR/lib -lflang_rt.runtime after OS library directory -L options when linking.

$ flang -### test.f90 |& tail -1 | tr ' ' '\n' | grep '^"-[Ll]'
"-L/home/foo/llvm/bin/../lib/aarch64-unknown-linux-gnu"
"-L/usr/lib/gcc/aarch64-linux-gnu/13"
"-L/lib/aarch64-linux-gnu"
"-L/usr/lib/aarch64-linux-gnu"
"-L/lib"
"-L/usr/lib"
"-L/home/foo/llvm/lib"
"-lflang_rt.runtime"
"-lm"
"-lgcc"
"-lgcc_s"
"-lc"
"-lgcc"
"-lgcc_s"

If you install Flang (and Flang-RT) manually on an environment where the OS-provided Flang package is already installed, this has a problem.

When user-installed Flang is invoked, libflang_rt.runtime in the user-installed directory should be linked. However, OS-provided libflang_rt.runtime in /lib (or /lib/TRIPLE or /lib64, depending on OS) is linked instead.

https://github.com/llvm/llvm-project/issues/100403 and https://github.com/open-mpi/ompi/issues/13116 suffer from this problem.

Should we prioritize -L$LLVM_DIR/lib over -L/lib?

kawashima-fj avatar Mar 04 '25 11:03 kawashima-fj

@llvm/issue-subscribers-flang-driver

Author: KAWASHIMA Takahiro (kawashima-fj)

The Flang driver puts `-L$LLVM_DIR/lib -lflang_rt.runtime` after OS library directory `-L` options when linking.
$ flang -### test.f90 |& tail -1 | tr ' ' '\n' | grep '^"-[Ll]'
"-L/home/foo/llvm/bin/../lib/aarch64-unknown-linux-gnu"
"-L/usr/lib/gcc/aarch64-linux-gnu/13"
"-L/lib/aarch64-linux-gnu"
"-L/usr/lib/aarch64-linux-gnu"
"-L/lib"
"-L/usr/lib"
"-L/home/foo/llvm/lib"
"-lflang_rt.runtime"
"-lm"
"-lgcc"
"-lgcc_s"
"-lc"
"-lgcc"
"-lgcc_s"

If you install Flang (and Flang-RT) manually on an environment where the OS-provided Flang package is already installed, this has a problem.

When user-installed Flang is invoked, libflang_rt.runtime in the user-installed directory should be linked. However, OS-provided libflang_rt.runtime in /lib (or /lib/TRIPLE or /lib64, depending on OS) is linked instead.

https://github.com/llvm/llvm-project/issues/100403 and https://github.com/open-mpi/ompi/issues/13116 suffer from this problem.

Should we prioritize -L$LLVM_DIR/lib over -L/lib?

llvmbot avatar Mar 04 '25 11:03 llvmbot

I think that you're probably right and we should put the LLVM lib dir first, but I'm not a distribution expert so there may be a good reason for things to be this way. @mgorny usually has useful comments from the distribution side of things, so might have some insight.

DavidTruby avatar Mar 04 '25 12:03 DavidTruby

I think forcing an additional lib path is a problem in itself. Ideally, we should pass path to libflang_rt.a directly, much like the way clang links compiler-rt.

mgorny avatar Mar 04 '25 13:03 mgorny

As a side note, should libflang_rt.runtime be in (for example) $PREFIX/lib/aarch64-unknown-linux-gnu instead of $PREFIX/lib?

ggouaillardet avatar Mar 05 '25 08:03 ggouaillardet

Let's not invent more locations, please. It should really be in clang resource directory, and flang-rt's CMake is already partway there.

mgorny avatar Mar 05 '25 09:03 mgorny

This is not a new location I just made up.

$PREFIX/lib contains libraries used by the compiler (e.g. libLLVMFrontendAtomic.so) whereas $PREFIX/lib/aarch64-unknown-linux-gnu contains the runtime (e.g. libc++.so) that will be used by the generated binaries. My comment applies to 20.1.0-rc3 and I did not check if this has changed in the main branch.

ggouaillardet avatar Mar 05 '25 11:03 ggouaillardet

Yes, strictly speaking that is a location that Debian made up, and that is not FHS-compliant.

mgorny avatar Mar 05 '25 11:03 mgorny

Like I said, I did not just make it up, thanks for incorrecting me.

I guess you got the point though.

ggouaillardet avatar Mar 05 '25 13:03 ggouaillardet

@kawashima-fj this is fixed by 1264d7a53a4de3094672be2a248db57b213f33ac. Could you please retest and close?

ceseo avatar Jun 04 '25 13:06 ceseo

Thanks. I've confirmed this is fixed now.

On my environment (AArch64, Ubuntu 24.04, Flang/LLVM b68565b8c70f):

$ flang -### test.f90 |& tail -1 | tr ' ' '\n' | grep '^"-[Ll]'
"-L/home/foo/llvm/bin/../lib/aarch64-unknown-linux-gnu"
"-L/home/foo/llvm/lib/clang/21/lib/aarch64-unknown-linux-gnu"
"-L/usr/lib/gcc/aarch64-linux-gnu/13"
"-L/lib/aarch64-linux-gnu"
"-L/usr/lib/aarch64-linux-gnu"
"-L/lib"
"-L/usr/lib"
"-L/home/foo/llvm/lib"
"-lflang_rt.runtime"
"-lm"
"-lgcc"
"-lgcc_s"
"-lc"
"-lgcc"
"-lgcc_s"

libflang_rt.runtime.a exists in /home/foo/llvm/lib/clang/21/lib/aarch64-unknown-linux-gnu and its -L is located before -L/lib.

kawashima-fj avatar Jun 05 '25 01:06 kawashima-fj