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

[Driver] Old compiler-rt library name (e.g. libclang_rt.builtins-aarch64.a) is reported when neither old/new library directory exists #87150

Closed
wzssyqa opened this issue Mar 30, 2024 · 11 comments · Fixed by #87866
Assignees
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl'

Comments

@wzssyqa
Copy link
Contributor

wzssyqa commented Mar 30, 2024

When we build llvm with this cmd

cmake ../llvm -G Ninja -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind" -DLLVM_ENABLE_PROJECTS="mlir;clang;clang-tools-extra;lld" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=RelWithDebInfo

It builds successfully, while if we try do something like

./bin/clang --rtlib=compiler-rt hello.c

Something wrong happens

/usr/bin/aarch64-linux-gnu-ld: cannot find <PATH>/build-aarch64/lib/clang/19/lib/linux/libclang_rt.builtins-aarch64.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

libclang_rt.builtins-aarch64.a does exists, while in a different path, with a different name

./lib/clang/19/lib/aarch64-linux-gnu/libclang_rt.builtins.a
@github-actions github-actions bot added the clang Clang issues not falling into any other category label Mar 30, 2024
@EugeneZelenko EugeneZelenko added clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' and removed clang Clang issues not falling into any other category labels Mar 30, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 30, 2024

@llvm/issue-subscribers-clang-driver

Author: YunQiang Su (wzssyqa)

When we build llvm with this cmd
cmake ../llvm -G Ninja -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind" -DLLVM_ENABLE_PROJECTS="mlir;clang;clang-tools-extra;lld" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=RelWithDebInfo

It builds successfully, while if we try do something like

./bin/clang --rtlib=compiler-rt hello.c

Something wrong happens

/usr/bin/aarch64-linux-gnu-ld: cannot find &lt;PATH&gt;/build-aarch64/lib/clang/19/lib/linux/libclang_rt.builtins-aarch64.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

libclang_rt.builtins-aarch64.a does exists, while in a different path, with a different name

./lib/clang/19/lib/aarch64-linux-gnu/libclang_rt.builtins.a

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 1, 2024

Maybe we should fix our build system to always set the directory to the normalized one?

@MaskRay

@MaskRay
Copy link
Member

MaskRay commented Apr 1, 2024

cmake ../llvm -G Ninja -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind" -DLLVM_ENABLE_PROJECTS="mlir;clang;clang-tools-extra;lld" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=RelWithDebInfo

Are you building a x86-64 clang that produces aarch64 code on an x86-64 host? The command line does not look correct to me.

I've used the following using /tmp/Rel/bin/clang. LLVM_HOST_TRIPLE is important

cmake -GNinja -Sllvm -B/tmp/out/a64-0 -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/tmp/Rel/bin/clang -DCMAKE_CXX_COMPILER=/tmp/Rel/bin/clang++ -DCMAKE_{C,CXX}_FLAGS='--target=aarch64-linux-gnu' -DLLVM_NATIVE_TOOL_DIR=/tmp/Rel/bin -DLLVM_HOST_TRIPLE=aarch64-linux-gnu -DLLVM_TARGETS_TO_BUILD=AArch64

/usr/bin/aarch64-linux-gnu-ld: cannot find <PATH>/build-aarch64/lib/clang/19/lib/linux/libclang_rt.builtins-aarch64.a: No such file or directory

Newer Clang will not suggest libclang_rt.builtins-${arch}.a. See https://discourse.llvm.org/t/runtime-directory-fallback/76860

@MaskRay MaskRay added question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! cmake Build system in general and CMake in particular and removed clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Apr 1, 2024
@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 2, 2024

cmake ../llvm -G Ninja -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind" -DLLVM_ENABLE_PROJECTS="mlir;clang;clang-tools-extra;lld" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=RelWithDebInfo

Are you building a x86-64 clang that produces aarch64 code on an x86-64 host? The command line does not look correct to me.

Yes, the normal 'cross toolchain', just like (almost) all embedded cross toolchain with GCC.
In fact I am trying to run mips test with qemu-user.
"Not correct" means that we don't support the change the default output arch on build time?

I've used the following using /tmp/Rel/bin/clang. LLVM_HOST_TRIPLE is important

cmake -GNinja -Sllvm -B/tmp/out/a64-0 -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=/tmp/Rel/bin/clang -DCMAKE_CXX_COMPILER=/tmp/Rel/bin/clang++ -DCMAKE_{C,CXX}_FLAGS='--target=aarch64-linux-gnu' -DLLVM_NATIVE_TOOL_DIR=/tmp/Rel/bin -DLLVM_HOST_TRIPLE=aarch64-linux-gnu -DLLVM_TARGETS_TO_BUILD=AArch64

It is not "normal" cross toolchain.
This cmd will generate a native ARM64 toolchain instead of a cross toolchain for ARM64.

/usr/bin/aarch64-linux-gnu-ld: cannot find <PATH>/build-aarch64/lib/clang/19/lib/linux/libclang_rt.builtins-aarch64.a: No such file or directory

Newer Clang will not suggest libclang_rt.builtins-${arch}.a. See https://discourse.llvm.org/t/runtime-directory-fallback/76860

Yes. That's a bug then.
I noticed some code in ToolChain::getCompilerRT/clang/lib/Driver/ToolChain.cpp

  // Check the filename for the old layout if the new one does not exist.
  CRTBasename =
      buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/true);
  SmallString<128> OldPath(getCompilerRTPath());
  llvm::sys::path::append(OldPath, CRTBasename);
  if (Path.empty() || getVFS().exists(OldPath))     // <----------------  **Path will be empty in this case.**
    return std::string(OldPath);

  // If none is found, use a file name from the new layout, which may get
  // printed in an error message, aiding users in knowing what Clang is
  // looking for.
  return std::string(Path);

I have tried to fix this problem. See: #87319

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 2, 2024

In fact, it will fail even for native toolchain, if the -DLLVM_DEFAULT_TARGET_TRIPLE is given with a non-normalized triple.
We have two way:
1. Also search the subdir what -DLLVM_DEFAULT_TARGET_TRIPLE gives us, just as wzssyqa@b0be3f9
2. Fix our build system, install libclang_rt into a normalized subdir always.

@MaskRay
Copy link
Member

MaskRay commented Apr 6, 2024

I think the issue is at ToolChain::getTargetSubDirPath. When the directory does not exist, we should still add it to getLibraryPaths() so that ToolChain::getCompilerRT will report the new compiler-rt file path.

I am testing a fix for ToolChain::getTargetSubDirPath

@MaskRay MaskRay changed the title cross build clang cannot find libclang_rt.builtins [Driver] Old compiler-rt library name (e.g. libclang_rt.builtins-aarch64.a) is reported when neither old/new library directory exists Apr 6, 2024
@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 8, 2024

@MaskRay This report is not about the wrong warning message. It is another problem.

If llvm is configured with a non-normalized triple target, such as the triple-without-vendor section.

-DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu

libclang_rt cannot be find then.

In this case: libclang_rt is installed into /aarch64-linux-gnu, while clang try to find it in
<libpath>/aarch64-unknown-linux-gnu.

MaskRay added a commit that referenced this issue Apr 8, 2024
Follow-up to #81037.

ToolChain::LibraryPaths holds the new compiler-rt library directory
(e.g. `/tmp/Debug/lib/clang/19/lib/x86_64-unknown-linux-gnu`). However,
it might be empty when the directory does not exist (due to the `if
(getVFS().exists(P))` change in https://reviews.llvm.org/D158475).

If neither the old/new compiler-rt library directories exists, we would
suggest the undesired old compiler-rt file name:

```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=memory -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/linux/libclang_rt.msan-x86_64.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

With this change, we will correctly suggest the new compiler-rt file name.

Fix #87150

Pull Request: #87866
@MaskRay MaskRay self-assigned this Apr 8, 2024
@MaskRay MaskRay added clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' and removed question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! cmake Build system in general and CMake in particular labels Apr 8, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 8, 2024

@llvm/issue-subscribers-clang-driver

Author: YunQiang Su (wzssyqa)

When we build llvm with this cmd
cmake ../llvm -G Ninja -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind" -DLLVM_ENABLE_PROJECTS="mlir;clang;clang-tools-extra;lld" -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=RelWithDebInfo

It builds successfully, while if we try do something like

./bin/clang --rtlib=compiler-rt hello.c

Something wrong happens

/usr/bin/aarch64-linux-gnu-ld: cannot find &lt;PATH&gt;/build-aarch64/lib/clang/19/lib/linux/libclang_rt.builtins-aarch64.a: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)

libclang_rt.builtins-aarch64.a does exists, while in a different path, with a different name

./lib/clang/19/lib/aarch64-linux-gnu/libclang_rt.builtins.a

@MaskRay
Copy link
Member

MaskRay commented Apr 9, 2024

@MaskRay This report is not about the wrong warning message. It is another problem.

If llvm is configured with a non-normalized triple target, such as the triple-without-vendor section.

-DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu

libclang_rt cannot be find then.

In this case: libclang_rt is installed into /aarch64-linux-gnu, while clang try to find it in <libpath>/aarch64-unknown-linux-gnu.

Right. This are two issues.

The clang driver issue about an imprecise error message (suggesting the old compiler-rt path) has been fixed by #87866.

The CMake issue is about how to build libclang_rt.builtins.a for aarch64 on an x86-64 machine. I haven't done this for a long time. Something like the following does not build compiler-rt. You can create a new issue (or find an existing one).

cmake -Sllvm -B/tmp/out/d-a64 -G Ninja -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind" -DLLVM_ENABLE_PROJECTS="clang;lld" -DCMAKE_C_COMPILER=~/Stable/bin/clang -DCMAKE_CXX_COMPILER=~/Stable/bin/clang++ -DCMAKE_BUILD_TYPE=Debug

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 10, 2024

The CMake issue is about how to build libclang_rt.builtins.a for aarch64 on an x86-64 machine. I haven't done this for a long time. Something like the following does not build compiler-rt. You can create a new issue (or find an existing one).

In fact compiler-rt is built, while it is present in a different path.
A proposal is: wzssyqa@b0be3f9

cmake -Sllvm -B/tmp/out/d-a64 -G Ninja -DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-linux-gnu -DLLVM_USE_LINKER=lld -DLLVM_ENABLE_RUNTIMES="compiler-rt;libunwind" -DLLVM_ENABLE_PROJECTS="clang;lld" -DCMAKE_C_COMPILER=~/Stable/bin/clang -DCMAKE_CXX_COMPILER=~/Stable/bin/clang++ -DCMAKE_BUILD_TYPE=Debug

In fact,

-DLLVM_DEFAULT_TARGET_TRIPLE=aarch64-unknown-linux-gnu
#Note -unknown-

will work well.

wzssyqa added a commit to wzssyqa/llvm-project that referenced this issue Apr 11, 2024
Currently, clang looks for compiler-rt only from the normalized
triple subdir. While if we are configured with a non-normalized
triple with -DLLVM_DEFAULT_TARGET_TRIPLE, such as triples without
vendor section, clang will fail to find compiler_rt.

Let's look for compiler_rt from the subdir with name from --target
option.

To archive this, we add a new member called Origin to class Triple.

Fixes: llvm#87150.
@MaskRay
Copy link
Member

MaskRay commented Apr 11, 2024

In fact compiler-rt is built, while it is present in a different path.

What's the different path? Can you name the paths to help readers understand the issue without building and debugging cmake or clang driver themselves?

wzssyqa pushed a commit to wzssyqa/llvm-project that referenced this issue Apr 14, 2024
Follow-up to llvm#81037.

ToolChain::LibraryPaths holds the new compiler-rt library directory
(e.g. `/tmp/Debug/lib/clang/19/lib/x86_64-unknown-linux-gnu`). However,
it might be empty when the directory does not exist (due to the `if
(getVFS().exists(P))` change in https://reviews.llvm.org/D158475).

If neither the old/new compiler-rt library directories exists, we would
suggest the undesired old compiler-rt file name:

```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=memory -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/linux/libclang_rt.msan-x86_64.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

With this change, we will correctly suggest the new compiler-rt file name.

Fix llvm#87150

Pull Request: llvm#87866
MaskRay added a commit that referenced this issue Apr 15, 2024
Follow-up to #81037.

ToolChain::LibraryPaths holds the new compiler-rt library directory
(e.g. `/tmp/Debug/lib/clang/19/lib/x86_64-unknown-linux-gnu`). However,
it might be empty when the directory does not exist (due to the `if
(getVFS().exists(P))` change in https://reviews.llvm.org/D158475).

If neither the old/new compiler-rt library directories exists, we would
suggest the undesired old compiler-rt file name:

```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=memory -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/linux/libclang_rt.msan-x86_64.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

With this change, we will correctly suggest the new compiler-rt file name.

Fix #87150

Pull Request: #87866
bazuzi pushed a commit to bazuzi/llvm-project that referenced this issue Apr 15, 2024
Follow-up to llvm#81037.

ToolChain::LibraryPaths holds the new compiler-rt library directory
(e.g. `/tmp/Debug/lib/clang/19/lib/x86_64-unknown-linux-gnu`). However,
it might be empty when the directory does not exist (due to the `if
(getVFS().exists(P))` change in https://reviews.llvm.org/D158475).

If neither the old/new compiler-rt library directories exists, we would
suggest the undesired old compiler-rt file name:

```
% /tmp/Debug/bin/clang++ a.cc -fsanitize=memory -o a
ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/linux/libclang_rt.msan-x86_64.a: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
```

With this change, we will correctly suggest the new compiler-rt file name.

Fix llvm#87150

Pull Request: llvm#87866
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl'
Projects
None yet
4 participants