Page MenuHomePhabricator

Handle flags such as -m32 when computing the prefix for programs/runtime libs
Needs ReviewPublic

Authored by arichardson on Wed, Apr 7, 11:18 AM.

Details

Summary

I was trying to use -DLLVM_ENABLE_RUNTIMES=compiler-rt on FreeBSD and this
failed since the 32-bit build ended up linking against the 64-bit ASAN
runtime and linking with "/usr/local/bin/x86_64-unknown-freebsd12.2-ld".
Looking at the driver code shows that the triple used as a prefix for tools
and libaries is always the raw value passed to -target and is not affected
by flags such as -m32/-march/etc.

While the 64-bit linker can usually link 32-bit programs just fine, the real
problem here is that search paths for the compiler-rt builtins/sanitizer
runtimes use the raw target triple and we can therefore end up linking
against the 64-bit runtime instead of the 32-bit one. In my case the build
of compiler-rt failed because "--whole-archive" "....llvm-project-build/./lib/clang/13.0.0/lib/x86_64-unknown-freebsd12.2/libclang_rt.asan_cxx.a"
was being passed to the linker instead of using the 32-bit library that
can be found in ".../lib/clang/13.0.0/lib/i386-unknown-freebsd12.2/".

This commit uses computeTargetTriple() to update the triple used as a
prefix for tools such as the linker. If computeTargetTriple() results in
a different triple, we update the RawTargetTriple and now search for
i386-unknown-freebsd12.2-ld when -m32 is passed. It is important to note
that we do not normalize the triple passed to -target since adding
additional could result in an unexpected behaviour change. For example,
clang -target x86_64-freebsd13 should search for x86_64-freebsd13-ld
and not the normalized x86_64-unknown-freebsd13-ld and
clang -target x86_64-freebsd13 -m32 tries to find i386-unknown-freebsd13-ld.

Diff Detail

Unit TestsFailed

TimeTest
2,750 msx64 debian > libomp.tasking/hidden_helper_task::taskgroup.cpp
Script: -- : 'RUN: at line 1'; /mnt/disks/ssd0/agent/llvm-project/build/./bin/clang++ -fopenmp -pthread -fno-experimental-isel -I /mnt/disks/ssd0/agent/llvm-project/openmp/runtime/test -I /mnt/disks/ssd0/agent/llvm-project/build/projects/openmp/runtime/src -L /mnt/disks/ssd0/agent/llvm-project/build/lib -I /mnt/disks/ssd0/agent/llvm-project/openmp/runtime/test/ompt -std=c++14 /mnt/disks/ssd0/agent/llvm-project/openmp/runtime/test/tasking/hidden_helper_task/taskgroup.cpp -o /mnt/disks/ssd0/agent/llvm-project/build/projects/openmp/runtime/test/tasking/hidden_helper_task/Output/taskgroup.cpp.tmp -lm -latomic && /mnt/disks/ssd0/agent/llvm-project/build/projects/openmp/runtime/test/tasking/hidden_helper_task/Output/taskgroup.cpp.tmp
670 msx64 windows > Clang.Driver::freebsd-m32.c
Script: -- : 'RUN: at line 7'; env PATH=C:\ws\w16-1\llvm-project\premerge-checks\clang\test\Driver/Inputs/basic_freebsd64_tree/usr/bin c:\ws\w16-1\llvm-project\premerge-checks\build\bin\clang.exe -no-canonical-prefixes -target x86_64-unknown-freebsd12.2 C:\ws\w16-1\llvm-project\premerge-checks\clang\test\Driver\freebsd-m32.c -BC:\ws\w16-1\llvm-project\premerge-checks\clang\test\Driver/Inputs/basic_freebsd64_tree/usr/bin -### 2>&1 | c:\ws\w16-1\llvm-project\premerge-checks\build\bin\filecheck.exe C:\ws\w16-1\llvm-project\premerge-checks\clang\test\Driver\freebsd-m32.c --check-prefix=PREFIXED-64

Event Timeline

arichardson created this revision.Wed, Apr 7, 11:18 AM
arichardson requested review of this revision.Wed, Apr 7, 11:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptWed, Apr 7, 11:18 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
joerg added a subscriber: joerg.Wed, Apr 7, 2:12 PM

This sounds wrong. If you are using 'x86_64-freebsd' as triple and -m32, it should still call 'x86_64-freebsd-ld', but it is the responsibility of the driver to ensure that also the right set of linker flags are passed as well. Compare netbsd::Linker::ConstructJob for one way to handle this.

arichardson planned changes to this revision.Wed, Apr 7, 2:38 PM

This sounds wrong. If you are using 'x86_64-freebsd' as triple and -m32, it should still call 'x86_64-freebsd-ld', but it is the responsibility of the driver to ensure that also the right set of linker flags are passed as well. Compare netbsd::Linker::ConstructJob for one way to handle this.

Good point. The real problem was that clang was passing the 64-bit asan runtime library to the linker, not so much that it tried to invoke the x86_64-prefixed linker. I'll see if I can limit this to the library paths.

arichardson retitled this revision from Handle flags such as -m32 when computing the triple prefix for programs to Handle flags such as -m32 when computing the prefix for programs/runtime libs.Thu, Apr 8, 4:40 AM
arichardson edited the summary of this revision. (Show Details)
arichardson added a reviewer: joerg.

Add the raw triple prefix for programs (but prefer the adjusted triple) and always use the adjusted triple for libraries.
Should hopefully address the feedback from @joerg.

Also improve tests to check that we don't insert additional empty/"-unknown" triple components.

Drop chunk that is no longer necessary

  • Fix Windows path regex
phosek added inline comments.Fri, Apr 9, 10:54 AM
clang/lib/Driver/Driver.cpp
1176–1210

I think this should be handled by each toolchain. For example Linux toolchain already has getMultiarchTriple which takes into account target handling on various existing platforms like Debian.

What I've been thinking of doing is to move that method to the ToolChain class as virtual so it could be overriden and customized by each toolchain, and then use it in getRuntimePath.