This is an archive of the discontinued LLVM Phabricator instance.

[Clang][NVPTX] Allow passing arguments to the linker while standalone
Needs RevisionPublic

Authored by jhuber6 on May 5 2023, 12:03 PM.

Details

Summary

We support standalone compilation for the NVPTX architecture using
'nvlink' as our linker. Because of the special handling required to
transform input files to cubins, as nvlink expects for some reason, we
didn't use the standard AddLinkerInput method. However, this also
meant that we weren't forwarding options passed with -Wl to the
linker. Add this support in for the standalone toolchain path.

Diff Detail

Event Timeline

jhuber6 created this revision.May 5 2023, 12:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2023, 12:03 PM
jhuber6 requested review of this revision.May 5 2023, 12:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 5 2023, 12:03 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
hliao added a subscriber: hliao.May 5 2023, 12:04 PM
tra added inline comments.May 5 2023, 12:18 PM
clang/lib/Driver/ToolChains/Cuda.cpp
594

Is removal of this line intentional?

637–646

I'd prefer to replace it with

if (II.isFileName()) {
  do stuff...
} else {
  if (!II.isNothing()
      II.getInputArg().renderAsInput(Args, CmdArgs); 
}
jhuber6 marked an inline comment as done.May 5 2023, 1:07 PM
jhuber6 added inline comments.
clang/lib/Driver/ToolChains/Cuda.cpp
594

No, thanks that was from when I originally tried to use AddLinkerInput but it didn't work because of the cubin thing.

jhuber6 updated this revision to Diff 519957.May 5 2023, 1:09 PM

Addressing comments

tra accepted this revision.May 5 2023, 1:26 PM
This revision is now accepted and ready to land.May 5 2023, 1:26 PM

Somewhat annoying, I've discovered that LLVM adds -Wl,-fcolor-diagnostics which obviously isn't supported by nvlink so it fails while including this in libc's CMake. Any clue if there's a way to work around that?

The main reason I made this patch was to allow passing --suppress-stack-size-warning to nvlink. But it turns out it's a little more difficult there.

tra added a comment.May 5 2023, 2:12 PM

Somewhat annoying, I've discovered that LLVM adds -Wl,-fcolor-diagnostics which obviously isn't supported by nvlink so it fails while including this in libc's CMake. Any clue if there's a way to work around that?

I guess the options are to either filter out the automatically added option or to avoid adding that particular argument if we know that the target is NVPTX. The latter would probably be preferable as there would be only one place where the decision is made.

Somewhat annoying, I've discovered that LLVM adds -Wl,-fcolor-diagnostics which obviously isn't supported by nvlink so it fails while including this in libc's CMake. Any clue if there's a way to work around that?

I guess the options are to either filter out the automatically added option or to avoid adding that particular argument if we know that the target is NVPTX. The latter would probably be preferable as there would be only one place where the decision is made.

The latter is a little difficult, the logic adds it based off of the host linker, but we explicitly override the host triple when we build via --target=. So there's be no way to turn it off in LLVM unless it's a blanket check on building libc. And since it's a global flag I can't just disable it only for the target. So I think the options are, either filter it out manually here or make a new flag called -Xcuda-nvlink, which I wouldn't like to do.

jhuber6 updated this revision to Diff 519977.May 5 2023, 2:43 PM

Putting up the hack that works around my problem with libc. Definitely not a good solution though.

tra added a comment.May 5 2023, 2:53 PM

The latter is a little difficult,

The more we dig, the more we want GPU-capable lld. :-)

clang/lib/Driver/ToolChains/Cuda.cpp
641

Can there ever be more than one value returned by II.getInputArg().getValues()?

If so, we probably don't want to skip all of them if one of them is --color-diagnostics. We may want to ignore only singleton --color-diagnostics and let all other combinations error out.

The latter is a little difficult,

The more we dig, the more we want GPU-capable lld. :-)

My thoughts exactly. I had a small chat with @MaskRay about how difficult it would be to spin up support for NVPTX. But it would probably be a reasonably large project, and considering who I work for would be difficult for me to do it as more than a hobby.

clang/lib/Driver/ToolChains/Cuda.cpp
641

Yeah, you can do -Wl,arg1,arg2,arg3. This was just because I couldn't think of an easy way to separate them out, considering that we rely on renderAsInput we'd need to create an entirely new arg. Which is doable, but I wasn't sure if it was worth the effort.

tra added a comment.May 5 2023, 3:37 PM

I've discovered that LLVM adds -Wl,-fcolor-diagnostics

Can you tell me where it's done?

I've discovered that LLVM adds -Wl,-fcolor-diagnostics

Can you tell me where it's done?

llvm/cmake/modules/HandleLLVMOptions.cmake:994

tra requested changes to this revision.May 5 2023, 4:04 PM

llvm/cmake/modules/HandleLLVMOptions.cmake:994

I do not think that we should work around this particular source of options in clang driver.

This sounds like something that may need to be dealt with in cmake.
The root cause is that cmake assumes that if the linker accepts the flag for target X, it will accept that flag for target Y. Or that the flags will be used only for compiling for the default target. Considering that clang is a cross-compiler, neither of the assumptions is universally true.

We may need to add a concept of per-offload-arch options that would be checked with specific --target=.... It's a bigger can of worms than just filtering the argument out, but I think we'll need to deal with it sooner or later anyways.

As a short-term stop-gap solution, I would suggest adding a cmake knob to disable linker color output altogether. This should unblock you and would not affect anybody else until we have a better fix.

This revision now requires changes to proceed.May 5 2023, 4:04 PM
barannikov88 added a comment.EditedMay 5 2023, 4:07 PM

I've discovered that LLVM adds -Wl,-fcolor-diagnostics

Can you tell me where it's done?

llvm/cmake/modules/HandleLLVMOptions.cmake:994

This might have to do something with

# Handle common options used by all runtimes.
include(AddLLVM)

in runtimes/CMakeLists.txt

which is totally wrong in my opinion.
In a LLVM_ENABLE_PROJECTS build (not sure about LLVM_ENABLE_RUNTIMES build) AddLLVM determines flags for building LLVM itself, not for building runtime libraries.
The results of the tests of the host toolchain therefore affect the invocation of the target toolchain.
I came across this once in https://reviews.llvm.org/D146920#4240370