This is an archive of the discontinued LLVM Phabricator instance.

[AMDGPU] Prepend --no-undefined option for linker instead of append
ClosedPublic

Authored by lamb-j on Aug 22 2023, 11:29 PM.

Details

Summary

Previously, for linking in amdgpu contexts, the --no-undefined was appended to the options passed to lld,
overriding any user-supplied options via "-Wl," or "-Xlinker". We now prepend --no-undefined so that
the user options are respected.

Diff Detail

Event Timeline

lamb-j created this revision.Aug 22 2023, 11:29 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 22 2023, 11:29 PM
lamb-j requested review of this revision.Aug 22 2023, 11:29 PM
lamb-j retitled this revision from WIP: [AMDGPU] Respect unresolved symbol option if forwarded to linker to [AMDGPU] Respect unresolved symbol option if forwarded to linker.
lamb-j edited the summary of this revision. (Show Details)

Remember to clang format.

clang/lib/Driver/ToolChains/AMDGPU.cpp
566–577

A little more concisely.

The -Wl and -Xlinker options are intended for the host linker and we intentionally do not pass them to the device linker.

If users want to pass options to the device linker, they need to use -Xoffload-linker.

There are multiple options affecting the handling of unresolved symbols. I think the proper way is to use --no-undefined as the default, which is always passed before those options from -Xoffload-linker so that the latter can override the former.

I believe the driver already passes -Xoffload-linker options to amdgpu::Linker::ConstructJob by Args. I think we probably only need to move all the default options (line 563-566) to 554 so that they can be overridden.

The -Wl and -Xlinker options are intended for the host linker and we intentionally do not pass them to the device linker.

If users want to pass options to the device linker, they need to use -Xoffload-linker.

There are multiple options affecting the handling of unresolved symbols. I think the proper way is to use --no-undefined as the default, which is always passed before those options from -Xoffload-linker so that the latter can override the former.

I believe the driver already passes -Xoffload-linker options to amdgpu::Linker::ConstructJob by Args. I think we probably only need to move all the default options (line 563-566) to 554 so that they can be overridden.

Yeah, the original problem was not being able to overload the defaults. This fundamentally is just because the -Wl options are being added in AddLinkerInputs before the defaults. Surprised I didn't catch that, thanks.

lamb-j added a comment.EditedAug 23 2023, 9:38 AM

Is void amdgpu::Linker::ConstructJob() constructing a job for the host linker or the device linker? Or both?

The -Wl and -Xlinker options are intended for the host linker and we intentionally do not pass them to the device linker.

In the example I'm testing, both -Wl and -Xlinker result in a forwarded option to the lld invocation (as shown in the behavior of the lit tests). However, when testing with -Xoffload-linker , I just get the following:

warning: argument unused during compilation: '-Xoffload-linker --unresolved-symbols=ignore-all' [-Wunused-command-line-argument]
lamb-j planned changes to this revision.Aug 23 2023, 10:00 AM
lamb-j updated this revision to Diff 552773.
lamb-j retitled this revision from [AMDGPU] Respect unresolved symbol option if forwarded to linker to WIP: [AMDGPU] Respect unresolved symbol option if forwarded to linker.
lamb-j edited the summary of this revision. (Show Details)
lamb-j requested review of this revision.Aug 23 2023, 10:03 AM
lamb-j updated this revision to Diff 552775.
lamb-j retitled this revision from WIP: [AMDGPU] Respect unresolved symbol option if forwarded to linker to [AMDGPU] Respect unresolved symbol option if forwarded to linker.
lamb-j updated this revision to Diff 552777.Aug 23 2023, 10:05 AM
lamb-j retitled this revision from [AMDGPU] Respect unresolved symbol option if forwarded to linker to [AMDGPU] Prepend --no-undefined option for linker instead of append.
jhuber6 accepted this revision.Aug 23 2023, 10:10 AM
This revision is now accepted and ready to land.Aug 23 2023, 10:10 AM
yaxunl added inline comments.Aug 23 2023, 10:15 AM
clang/test/Driver/amdgpu-toolchain-opencl.cl
34

we need to make sure "--unresolved-symbols=ignore-all" is after "--no-undefined" . same as below

lamb-j updated this revision to Diff 552803.Aug 23 2023, 10:59 AM
lamb-j marked 2 inline comments as done.Aug 23 2023, 10:59 AM
yaxunl accepted this revision.Aug 23 2023, 11:01 AM

LGTM. Thanks.

This revision was landed with ongoing or failed builds.Aug 23 2023, 12:49 PM
This revision was automatically updated to reflect the committed changes.