This is an archive of the discontinued LLVM Phabricator instance.

[HIP] Link with clang_rt.builtins
Needs ReviewPublic

Authored by yaxunl on Jun 6 2022, 12:00 PM.

Details

Reviewers
tra
MaskRay
Summary

HIP supports _Float16 by default in host programs, which
may cause calls of conversion functions for _Float16
emitted e.g. __truncdfhf2. These functions are not
available in libgcc but in libclang_rt.builtins. Therefore
--hip-link needs to link with libclang_rt.builtins by
default.

Diff Detail

Event Timeline

yaxunl created this revision.Jun 6 2022, 12:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 6 2022, 12:00 PM
yaxunl requested review of this revision.Jun 6 2022, 12:00 PM
MaskRay added inline comments.Jun 6 2022, 12:09 PM
clang/lib/Driver/ToolChains/MSVC.cpp
485 ↗(On Diff #434556)

Note that the path is different with LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on. See D107799

tra added a reviewer: MaskRay.Jun 6 2022, 12:44 PM

Looks OK syntax-wise. Library path should probably be fixed, though it appears to be a somewhat separate issue and could be done in its own patch, if the required change is not trivial.

clang/lib/Driver/ToolChains/Linux.cpp
684–690 ↗(On Diff #434556)

Nit: Collapse all of these into a single append()

clang/test/Driver/hip-runtime-libs-msvc.hip
14

What are we matching here? A more verbose pattern or some comments would be helpful.

16

CHECK-SAME?

yaxunl marked 3 inline comments as done.Jun 7 2022, 7:16 AM
yaxunl added inline comments.
clang/lib/Driver/ToolChains/MSVC.cpp
485 ↗(On Diff #434556)

will use getCompilerRT to get the compiler-rt builtin lib name so that it is always correct

clang/test/Driver/hip-runtime-libs-msvc.hip
14

this is for checking lib path for compiler lib.

As we will use getCompilerRT to get the complete path of compiler-rt builtin lib, this part will be changed. We will use a clang option to print the compiler-rt lib path and use it as a reference to check compiler-rt lib used by HIP.

16

will do

yaxunl updated this revision to Diff 434807.Jun 7 2022, 7:17 AM
yaxunl marked 3 inline comments as done.
yaxunl edited the summary of this revision. (Show Details)

use getCompilerRT to get compiler-rt lib path

MaskRay added a comment.EditedJun 8 2022, 7:34 PM

These functions are not available in libgcc but in libclang_rt.builtins. Therefore --hip-link needs to link with libclang_rt.builtins by default.

I think this is problematic.

The current link sequence is ... "-lamdhip64" "/tmp/Debug/lib/clang/15.0.0/lib/linux/libclang_rt.builtins-x86_64.a" "-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/crtendS.o" "/lib/x86_64-linux-gnu/crtn.o"

Linking in libclang_rt.builtins is incompatible with libgcc. There may be subtle symbol resolving issues. I don't think silently changing the behavior is desired.
Perhaps --rocm-path should require --rtlib=compiler-rt. See the --rtlib=libgcc requires --unwindlib=libgcc diagnostic.

Your user will need to do a bit more work if CLANG_DEFAULT_RTLIB is not compiler-rt.

clang/test/Driver/hip-runtime-libs-linux.hip
6

Note: -target is legacy spelling. --target= is the recommended name.

yaxunl added a comment.Jun 9 2022, 8:17 AM

These functions are not available in libgcc but in libclang_rt.builtins. Therefore --hip-link needs to link with libclang_rt.builtins by default.

I think this is problematic.

The current link sequence is ... "-lamdhip64" "/tmp/Debug/lib/clang/15.0.0/lib/linux/libclang_rt.builtins-x86_64.a" "-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/crtendS.o" "/lib/x86_64-linux-gnu/crtn.o"

Linking in libclang_rt.builtins is incompatible with libgcc. There may be subtle symbol resolving issues. I don't think silently changing the behavior is desired.
Perhaps --rocm-path should require --rtlib=compiler-rt. See the --rtlib=libgcc requires --unwindlib=libgcc diagnostic.

Your user will need to do a bit more work if CLANG_DEFAULT_RTLIB is not compiler-rt.

If I use --rtlib=compiler-rt, does that also requires --unwindlib=unwindlib ?

MaskRay added a comment.EditedJun 9 2022, 1:07 PM

These functions are not available in libgcc but in libclang_rt.builtins. Therefore --hip-link needs to link with libclang_rt.builtins by default.

I think this is problematic.

The current link sequence is ... "-lamdhip64" "/tmp/Debug/lib/clang/15.0.0/lib/linux/libclang_rt.builtins-x86_64.a" "-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/crtendS.o" "/lib/x86_64-linux-gnu/crtn.o"

Linking in libclang_rt.builtins is incompatible with libgcc. There may be subtle symbol resolving issues. I don't think silently changing the behavior is desired.
Perhaps --rocm-path should require --rtlib=compiler-rt. See the --rtlib=libgcc requires --unwindlib=libgcc diagnostic.

Your user will need to do a bit more work if CLANG_DEFAULT_RTLIB is not compiler-rt.

If I use --rtlib=compiler-rt, does that also requires --unwindlib=unwindlib ?

No. --unwindlib=libunwind requires --rtlib=compiler-rt. --rtlib=compiler-rt is compatible with both --unwindlib=libgcc and --unwindlib=libunwind.

clang/lib/Driver/ToolChains/Linux.cpp
684–690 ↗(On Diff #434556)

Note: I think multiple push_back isn't that bad...

tra added inline comments.Jun 9 2022, 2:37 PM
clang/lib/Driver/ToolChains/Linux.cpp
684–690 ↗(On Diff #434556)

That's why I've marked it as a "nit".

For arguments, push_back works best for single arguments. append(), when more than one is needed.
Here, I think an ideal arrangement would look like this:

CmdArgs.push_back(Args.MakeArgString(StringRef("-L") + RocmInstallation.getLibPath();
CmdArgs.append({"-rpath", Args.MakeArgString(RocmInstallation.getLibPath())});
CmdArgs.push_back("-lamdhip64");
CmdArgs.push_back(Args.MakeArgString(getCompilerRT(Args, "builtins")));

Single .append({}) works OK for me, too.

Passing a mix of single options and options with arguments via append() combined with passing multi-word arguments with push_back() looks rather odd in its inconsistency to me. It makes no practical difference either way, so it's merely a minor style/readability nit, as far as I'm concerned. I'm fine deferring to author's preferences.

yaxunl updated this revision to Diff 437587.Jun 16 2022, 10:12 AM

use compiler-rt as runtime lib by default for --hip-link

If I use --rtlib=compiler-rt, does that also requires --unwindlib=unwindlib ?

No. --unwindlib=libunwind requires --rtlib=compiler-rt. --rtlib=compiler-rt is compatible with both --unwindlib=libgcc and --unwindlib=libunwind.

If only use -rtlib=compiler-rt without changing unwind lib, I will get missing symbol:

[2022-06-16T20:05:28.644Z] ld.lld: error: undefined symbol: _Unwind_Resume

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(generate_skipahead_file())

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(main)

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(std::function<void (float&, float, bool&)> reduce::ReduceOpFn2<float>(miopenReduceTensorOp_t))

[2022-06-16T20:05:28.644Z] >>> referenced 1246 more times

[2022-06-16T20:05:28.644Z]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_Resume [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_Backtrace [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_GetIP [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

If I use --rtlib=compiler-rt, does that also requires --unwindlib=unwindlib ?

No. --unwindlib=libunwind requires --rtlib=compiler-rt. --rtlib=compiler-rt is compatible with both --unwindlib=libgcc and --unwindlib=libunwind.

If only use -rtlib=compiler-rt without changing unwind lib, I will get missing symbol:

[2022-06-16T20:05:28.644Z] ld.lld: error: undefined symbol: _Unwind_Resume

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(generate_skipahead_file())

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(main)

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(std::function<void (float&, float, bool&)> reduce::ReduceOpFn2<float>(miopenReduceTensorOp_t))

[2022-06-16T20:05:28.644Z] >>> referenced 1246 more times

[2022-06-16T20:05:28.644Z]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_Resume [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_Backtrace [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_GetIP [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

If I use --rtlib=compiler-rt, do I have to explicitly add --unwindlib=libgcc or --unwindlib=libunwind? I suspect the linker is not linking the unwind library when -rtlib=compiler-rt is specified.

If I use --rtlib=compiler-rt, does that also requires --unwindlib=unwindlib ?

No. --unwindlib=libunwind requires --rtlib=compiler-rt. --rtlib=compiler-rt is compatible with both --unwindlib=libgcc and --unwindlib=libunwind.

If only use -rtlib=compiler-rt without changing unwind lib, I will get missing symbol:

[2022-06-16T20:05:28.644Z] ld.lld: error: undefined symbol: _Unwind_Resume

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(generate_skipahead_file())

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(main)

[2022-06-16T20:05:28.644Z] >>> referenced by main.cpp

[2022-06-16T20:05:28.644Z] >>> CMakeFiles/MIOpenDriver.dir/main.cpp.o:(std::function<void (float&, float, bool&)> reduce::ReduceOpFn2<float>(miopenReduceTensorOp_t))

[2022-06-16T20:05:28.644Z] >>> referenced 1246 more times

[2022-06-16T20:05:28.644Z]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_Resume [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_Backtrace [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] ld.lld: error: ../lib/libMIOpen.so.1.0.50300: undefined reference to _Unwind_GetIP [--no-allow-shlib-undefined]

[2022-06-16T20:05:28.644Z] clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

If I use --rtlib=compiler-rt, do I have to explicitly add --unwindlib=libgcc or --unwindlib=libunwind? I suspect the linker is not linking the unwind library when -rtlib=compiler-rt is specified.

Either is fine.

If --rtlib=libgcc, --unwindlib must be libgcc (platform defaults to libgcc on Linux).

--rtlib and --unwindlib are orthogonal. To use libunwind, specify --unwindlib.

yaxunl updated this revision to Diff 438709.Jun 21 2022, 8:09 AM

add -unwindlib=libgcc by default for --hip-link since -rtlib=compiler-rt needs it

Magically deciding a default value for --unwindlib or --rtlib is not nice. You may emit a warning if the selected default happens to be incompatible with HIP.

Magically deciding a default value for --unwindlib or --rtlib is not nice. You may emit a warning if the selected default happens to be incompatible with HIP.

We build clang not just for compiling HIP programs. For C/C++ programs, we want to use the default runtime and unwind library.

We only want to change the default runtime and unwind library for HIP.

Magically deciding a default value for --unwindlib or --rtlib is not nice. You may emit a warning if the selected default happens to be incompatible with HIP.

We build clang not just for compiling HIP programs. For C/C++ programs, we want to use the default runtime and unwind library.

One choice is that ensure the user specify -DCMAKE_DEFAULT_RTLIB=compiler-rt.
(Since compiler-rt can be used with libgcc_s, -DCMAKE_DEFAULT_UNWINDLIB=libunwind is not strictly necessary.)

We only want to change the default runtime and unwind library for HIP.

The correct place is clang/lib/Driver/ToolChain.cpp ToolChain::GetUnwindLibType (see "platform").
HIP should have its derived ToolChain to override the member function.