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

CompilerRT: Normalize COMPILER_RT_DEFAULT_TARGET_TRIPLE #88835

Merged
merged 2 commits into from
Apr 17, 2024

Conversation

wzssyqa
Copy link
Contributor

@wzssyqa wzssyqa commented Apr 16, 2024

If LLVM is configured with -DLLVM_DEFAULT_TARGET_TRIPLE, or compiler_rt is configured with -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE, while the argument is not normalized, such as Debian-style vendor-less triple, clang will try to find libclang_rt in lib/<normalized_triple>, while libclang_rt is placed into lib/<triple_arg>.

Let's also place libclang_rt into lib/<normalized_triple>.

If LLVM is configured with -DLLVM_DEFAULT_TARGET_TRIPLE, or
compiler_rt is configured with -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE,
while the argument is not normalized, such as Debian-style vendor-less
triple, clang will try to find libclang_rt in lib/<normalized_triple>,
while libclang_rt is placed into lib/<triple_arg>.

Let's also place libclang_rt into lib/<normalized_triple>.
@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 16, 2024

The previous PR introduced 2 test fails:
https://lab.llvm.org/buildbot/#/builders/98/builds/36366
Should be resolved by 1693009

https://lab.llvm.org/buildbot/#/builders/77/builds/36372
due to the previous patch overwrite the var COMPILER_RT_DEFAULT_TARGET_TRIPLE.
It has been fixed in this version.

@wzssyqa wzssyqa requested a review from MaskRay April 16, 2024 03:24
@wzssyqa wzssyqa merged commit 16f1887 into llvm:main Apr 17, 2024
4 checks passed
ZijunZhaoCCK added a commit that referenced this pull request Apr 17, 2024
@mstorsjo
Copy link
Member

As this is a reland of an old patch, it would be good to link explicitly to the previously relanded commit or PR, or even the commit where the previous one was reverted - as things stand right now, it's not easy to go from this PR review to the previous one.

Anyway, this patch did break my build... I agree with the idea of this patch, but -print-effective-triple doesn't seem to work exactly as it should for my targets.

When building compiler-rt builtins for the armv7-w64-windows-gnu target, -print-effective-triple prints thumbv7-w64-windows-gnu. This is correct (this is an arm target, but one where we always generate code in thumb mode), but unexpected for most of the ecosystem. In practice, this doesn't build any builtins at all for this target.

We can patch around that by with the following patch:

diff --git a/compiler-rt/cmake/builtin-config-ix.cmake b/compiler-rt/cmake/builtin-config-ix.cmake
index 33c97b1ac28a..cf02f47e4c1e 100644
--- a/compiler-rt/cmake/builtin-config-ix.cmake
+++ b/compiler-rt/cmake/builtin-config-ix.cmake
@@ -53,7 +53,7 @@ else()
 endif()
 
 set(ARM64 aarch64)
-set(ARM32 arm armhf armv4t armv5te armv6 armv6m armv7m armv7em armv7 armv7s armv7k armv8m.base armv8m.main armv8.1m.main)
+set(ARM32 arm armhf armv4t armv5te armv6 armv6m armv7m armv7em armv7 armv7s armv7k armv8m.base armv8m.main armv8.1m.main thumbv7)
 set(AVR avr)
 set(HEXAGON hexagon)
 set(X86 i386)
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index f9611574a562..e94912103755 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -615,6 +615,7 @@ set(armhf_SOURCES ${arm_SOURCES})
 set(armv7_SOURCES ${arm_SOURCES})
 set(armv7s_SOURCES ${arm_SOURCES})
 set(armv7k_SOURCES ${arm_SOURCES})
+set(thumbv7_SOURCES ${arm_SOURCES})
 set(arm64_SOURCES ${aarch64_SOURCES})
 set(arm64e_SOURCES ${aarch64_SOURCES})
 set(arm64_32_SOURCES ${aarch64_SOURCES})

However, for the purposes of this patch, getting the right normalized triple for the output of the build so clang will find it, this target also behaves weirdly:

$ clang --target=armv7-w64-windows-gnu -print-effective-triple
thumbv7-w64-windows-gnu
$ clang --target=armv7-w64-windows-gnu -print-runtime-dir
<prefix>/lib/clang/19/lib/armv7-w64-windows-gnu

So for this target, the output of -print-effective-triple will place the output in a directory different from the one where clang actually will look.

So I'd go ahead and revert this for now, so we can unbreak my continous builds at e.g. https://github.com/mstorsjo/llvm-mingw/actions/runs/8730621159 that were broken by this.

@mstorsjo
Copy link
Member

@MaskRay Can you comment on the core of this issue, i.e. this:

$ clang --target=armv7-w64-windows-gnu -print-effective-triple
thumbv7-w64-windows-gnu
$ clang --target=armv7-w64-windows-gnu -print-runtime-dir
<prefix>/lib/clang/19/lib/armv7-w64-windows-gnu

We could work around this by applying a replacement back from thumbv* to armv* on the output from clang -print-effective-triple, but that's not very pretty...

@mstorsjo
Copy link
Member

Oh, and also for the non-per-target runtime directory config, even if we patch the builtins build to recognize thumbv7, this produces libclang_rt.builtins-thumbv7.a, while clang looks for libclang_rt.builtins-arm.a.

@DavidSpickett
Copy link
Collaborator

Thanks for digging Martin, I was pinged about this too today.

This also broke libcxx Picolib checks at the install step. This may be because this build does not use the per target runtime dir at all, being an embedded build, not sure yet.

https://buildkite.com/llvm-project/libcxx-ci/builds/34897#018eec06-612c-47f1-9931-d3bd88bf7ced

+ mv '/home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib/armv7m-none-eabi/*' /home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib
mv: cannot stat '/home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib/armv7m-none-eabi/*': No such file or directory

To reproduce this you'll need qemu-system-arm 8.1.3 or newer installed, set CC/CXX to clang 17/18 (latest is ideal) and then from the llvm-project dir run ./libcxx/utils/ci/run-buildbot armv7m-picolibc. If it gets to the test stage it's got past the install step.

The triple ends up converted to thumbv7:

-- cmake c compiler target: armv7m-none-eabi
-- armv7m-none-eabi
CMake Error at cmake/Modules/CompilerRTUtils.cmake:378 (message):
  thumbv7m-none-unknown-eabi
Call Stack (most recent call first):

And I don't see any sign of either triple in any folder names in the /lib.

DavidSpickett added a commit that referenced this pull request Apr 18, 2024
…)"

This reverts commit 16f1887.

This broke Libcxx Picolib testing at the install step, and builds
for Windows builtins. Revert while we figure out the cause.
@mstorsjo
Copy link
Member

Thanks @DavidSpickett! It's good to see that this issue isn't specific for the windows target - I guess it goes for any armv* target arch, where the target is implicitly thumb only.

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 18, 2024

Sorry for this trouble. I misunderstanding the usage of -print-effective-triple.
I will dig more about why these happens.

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 18, 2024

@mstorsjo @DavidSpickett can you have a try to use -print-target-triple instead of -print-effective-triple?

@mstorsjo
Copy link
Member

@mstorsjo @DavidSpickett can you have a try to use -print-target-triple instead of -print-effective-triple?

That does seem to do the right thing:

$ clang --target=armv7-w64-windows-gnu -print-effective-triple
thumbv7-w64-windows-gnu
$ clang --target=armv7-w64-windows-gnu -print-target-triple
armv7-w64-windows-gnu
$ clang --target=armv7m-none-eabi -print-effective-triple
thumbv7m-none-unknown-eabi
$ clang --target=armv7m-none-eabi -print-target-triple
armv7m-none-unknown-eabi

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 18, 2024

There still some problem for armv7m-none-eabi:

  1. clang --target=armv7m-none-eabi -print-target-triple out put
armv7m-none-unknown-eabi

Is it correct? Should we fix the C++ code to normalize triples?

  1. in livcxx/utils/ci/run-buildbot, install/lib/armv7m-none-eabi is hardcoded.
    should we use clang -print-target-triple for it?

@DavidSpickett
Copy link
Collaborator

DavidSpickett commented Apr 18, 2024

-print-effective-triple gives:

$ /home/david.spickett/build-llvm-aarch64/bin/clang --target=armv7m-none-eabi -print-target-triple
armv7m-none-unknown-eabi

Which is more normal, however I still get:

-- Installing: /home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib/armv7m-none-unknown-eabi/libclang_rt.builtins.a
+ mv '/home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib/armv7m-none-eabi/*' /home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib
mv: cannot stat '/home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib/armv7m-none-eabi/*': No such file or directory

That subdir doesn't exist but the one clang printed does:

$ ls /home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib/
armv7m-none-unknown-eabi  crt0-minimal.o   crt0.o  libcrt0-hosted.a   libcrt0-semihost.a  libdummyhost.a  libm.a         picolibc.ld
crt0-hosted.o             crt0-semihost.o  libc.a  libcrt0-minimal.a  libcrt0.a           libg.a          libsemihost.a  picolibcpp.ld
$ ls /home/david.spickett/llvm-project/build/armv7m-picolibc/install/lib/armv7m-none-unknown-eabi/
libclang_rt.builtins.a

Maybe something in the install step also needs to be updated?

Also it is a bit weird that this is adding a PER_TARGET_RUNTIME_DIR style folder path, in a build that doesn't enable that setting. But I don't have the full context so I'm not sure that's wrong necessarily.

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 18, 2024

Maybe something in the install step also needs to be updated?

Yes. It is hardcoded in libcxx/utils/ci/run-buildbot.

Also it is a bit weird that this is adding a PER_TARGET_RUNTIME_DIR style folder path, in a build that doesn't enable that setting. But I don't have the full context so I'm not sure that's wrong necessarily.

See #87866
PER_TARGRT_RUNTIME_DIR is explicitly enabled in libcxx/utils/ci/run-buildbot.

@MaskRay

@DavidSpickett
Copy link
Collaborator

in livcxx/utils/ci/run-buildbot, install/lib/armv7m-none-eabi is hardcoded.

You're right, this isn't part of the compiler-rt cmake at all, and it does this too:

-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=ON

Which explains the layout.

should we use clang -print-target-triple for it?

I'd rather be explicit and specify the path. One less layer to unpick when it goes wrong.

The following fixes the libcxx build:

$ git diff
diff --git a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake
index 6d413f6753bc..a6c6ef93500d 100644
--- a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake
+++ b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake
@@ -369,7 +369,7 @@ macro(construct_compiler_rt_default_triple)
   endif()

   if ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
-    execute_process(COMMAND ${CMAKE_C_COMPILER} --target=${COMPILER_RT_DEFAULT_TARGET_TRIPLE} -print-effective-triple
+    execute_process(COMMAND ${CMAKE_C_COMPILER} --target=${COMPILER_RT_DEFAULT_TARGET_TRIPLE} -print-target-triple
                     OUTPUT_VARIABLE COMPILER_RT_DEFAULT_TARGET_TRIPLE
                     OUTPUT_STRIP_TRAILING_WHITESPACE)
   endif()
diff --git a/libcxx/utils/ci/run-buildbot b/libcxx/utils/ci/run-buildbot
index 23a2a1bbbc63..60307a7d4f35 100755
--- a/libcxx/utils/ci/run-buildbot
+++ b/libcxx/utils/ci/run-buildbot
@@ -217,7 +217,7 @@ function test-armv7m-picolibc() {
         "${@}"

     ${NINJA} -vC "${BUILD_DIR}/compiler-rt" install
-    mv "${BUILD_DIR}/install/lib/armv7m-none-eabi"/* "${BUILD_DIR}/install/lib"
+    mv "${BUILD_DIR}/install/lib/armv7m-none-unknown-eabi"/* "${BUILD_DIR}/install/lib"

     check-runtimes
 }

So does -print-target-triple:

  • Achieve the original goals of this PR?
  • Work for the Windows builds?

@wzssyqa
Copy link
Contributor Author

wzssyqa commented Apr 18, 2024

  • Achieve the original goals of this PR?

Yes. The previous is wrong due to I misunderstood -print-effective-triple.

  • Work for the Windows builds?

I will file a new PR. And add Martin as reviewer.

@DavidSpickett
Copy link
Collaborator

If you add the libcxx build script changes to that PR it should be run by the libcxx CI so we can confirm that that works too.

@ldionne
Copy link
Member

ldionne commented Apr 18, 2024

Yup, or you could also just touch libcxx/foo if you only want to trigger the CI as-is.

wzssyqa added a commit that referenced this pull request Apr 19, 2024
If LLVM is configured with -DLLVM_DEFAULT_TARGET_TRIPLE, or compiler_rt
is configured with -DCOMPILER_RT_DEFAULT_TARGET_TRIPLE, while the
argument is not normalized, such as Debian-style vendor-less triple,
clang will try to find libclang_rt in lib/<normalized_triple>, while
libclang_rt is placed into lib/<triple_arg>.

Let's also place libclang_rt into lib/<normalized_triple>.
`libcxx/utils/ci/run-buildbot` is also updated to use
`armv7m-none-unknown-eabi` as normalized triple instead of
current `armv7m-none-eabi`.

ChangeLog of this PR:
This patch has been applied and revert twice:
1. The first try is #88407, and
then it is found that it causes some CI failures.
    https://lab.llvm.org/buildbot/#/builders/98/builds/36366
It is then resolved by another commit:
1693009

    https://lab.llvm.org/buildbot/#/builders/77/builds/36372
It is caused that `COMPILER_RT_DEFAULT_TARGET_TRIPLE` is overwrite
without taking care about `CACHE`.

2. The second try #88835,
     resolves https://lab.llvm.org/buildbot/#/builders/77/builds/36372
     and in fact only one `execute_process` is needed.

Then we find some other CI failures. 
https://github.com/mstorsjo/llvm-mingw/actions/runs/8730621159

https://buildkite.com/llvm-project/libcxx-ci/builds/34897#018eec06-612c-47f1-9931-d3bd88bf7ced

It is due to misunderstanding `-print-effective-triple`: which will
output `thumbv7-w64-windows-gnu` for `armv7-w64-windows-gnu` or some
other thumb-enabled arm triples.
In fact we should use `-print-target-triple`.

For armv7m-picolibc, `armv7m-none-eabi` is hardcoded in
libcxx/utils/ci/run-buildbot, while in fact `armv7m-none-unknown-eabi`
is the real normalized triple.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants