This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Add options in LTO mode when cross compiling for AMDGPU
ClosedPublic

Authored by jhuber6 on Feb 21 2023, 9:45 AM.

Details

Summary

The AMDGPU toolchain support directly compiling GPU images using
cross-compilation such as clang --target=amdgcn-amd-amdhsa foo.c.
However, when attempting to link bitcode this does not work because the
-mcpu options are not forwarded to the linker among others. This patch
simply adds them so that clang --target=amdgcn-amd-amdhsa foo.c -flto
works correctly.

Diff Detail

Event Timeline

jhuber6 created this revision.Feb 21 2023, 9:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 21 2023, 9:45 AM
jhuber6 requested review of this revision.Feb 21 2023, 9:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 21 2023, 9:45 AM
clang/lib/Driver/ToolChains/AMDGPU.cpp
545

What's the test against OK_Thin for? ThinLTO is a thing but I don't know if it exists (/works) on amdgpu, is this that?

JonChesterfield accepted this revision.EditedFeb 22 2023, 8:06 AM

NVM the above, all the other call sites to addLTOOptions have that test in them so it must be fine

void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
                          ArgStringList &CmdArgs, const InputInfo &Output,
                          const InputInfo &Input, bool IsThinLTO);

seems strange that it's a bool argument and not the result of getLTOMode, but pre-existing feature.

This revision is now accepted and ready to land.Feb 22 2023, 8:06 AM
jhuber6 added inline comments.Feb 22 2023, 8:10 AM
clang/lib/Driver/ToolChains/AMDGPU.cpp
545

AMDGPU's linker is lld so it works "in theory". But in practice, separate linking of object files isn't fully supported by the ABI. So I consider it a "your mileage may vary" scenario. FWIW, it functioned properly with my experimental libc setup

$ cat main.cpp
int foo();

int main() {
  return foo();
}
$ cat foo.cpp
int foo() { return 3; }
$ clang++ crt1.o main.cpp foo.cpp --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -Wl,-plugin-opt=mcpu=gfx1030 -nogpulib -o image -flto=thin
$ amdhsa_loader image; echo $?
3
arsenm added inline comments.Feb 22 2023, 10:17 AM
clang/test/Driver/amdgpu-toolchain.c
7

should add a test for thinlto?

jhuber6 added inline comments.Feb 22 2023, 10:19 AM
clang/test/Driver/amdgpu-toolchain.c
7

Only thing it would change is adding -plugin-opt=thinlto. Not sure if it's worth a test since it's probably checked by other uses of addLTOOptions.