This is an archive of the discontinued LLVM Phabricator instance.

[HIP] Fix lld failure when devie object is empty
ClosedPublic

Authored by yaxunl on Nov 17 2022, 9:14 AM.

Details

Summary

When -fgpu-rdc is used for linking relocatable objects, clang driver launches
clang-offload-bundler to extract a device relocatable object from each input
relocatable object file and passes the extracted files to lld. The input relocatable
object file could either come from HIP program or C++ program. The relocatable
object file from C++ program does not contain device relocatable objects, therefore
clang-offload-bundler extracts an empty file and passes it to lld, which causes lld to fail.

This patch adds "elf64_amdgpu" to lld so that lld always know the target
no matter input file is empty or not.

Diff Detail

Event Timeline

yaxunl created this revision.Nov 17 2022, 9:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 17 2022, 9:14 AM
yaxunl requested review of this revision.Nov 17 2022, 9:14 AM
yaxunl updated this revision to Diff 476157.Nov 17 2022, 9:19 AM

fix typo

tra accepted this revision.Nov 17 2022, 9:38 AM
This revision is now accepted and ready to land.Nov 17 2022, 9:38 AM
yaxunl updated this revision to Diff 476454.Nov 18 2022, 6:33 AM

need to specify osabi for elf64_amdgpu

MaskRay requested changes to this revision.Nov 19 2022, 3:49 PM
MaskRay added inline comments.
lld/ELF/Driver.cpp
179

This needs an emulation-amdgpu.s test.

This revision now requires changes to proceed.Nov 19 2022, 3:49 PM
MaskRay added a comment.EditedNov 19 2022, 3:50 PM

Some host relocatable objects may not contain device relocatable objects, where an empty file is passed to lld, which causes lld to fail.

How is an empty file (size=0) passed to lld? If a dummy relocatable object file is parsed to lld, lld can infer the machine type from e_machine in the ELF header.
How does it work in other cases? Because a non-empty relocatable object file is used?

I do not object to a new emulation which does not exist in GNU ld, but the description needs to be clarified.

yaxunl updated this revision to Diff 476892.Nov 21 2022, 6:53 AM

add test emulation-amdgpu.s

yaxunl edited the summary of this revision. (Show Details)Nov 21 2022, 7:03 AM
yaxunl marked an inline comment as done.

Some host relocatable objects may not contain device relocatable objects, where an empty file is passed to lld, which causes lld to fail.

How is an empty file (size=0) passed to lld? If a dummy relocatable object file is parsed to lld, lld can infer the machine type from e_machine in the ELF header.
How does it work in other cases? Because a non-empty relocatable object file is used?

I do not object to a new emulation which does not exist in GNU ld, but the description needs to be clarified.

Updated description. The empty file is generated by clang-offload-bundler, which does not know how to create a dummy relocatable object file for a device target.

lld/ELF/Driver.cpp
179

added

MaskRay accepted this revision.Nov 21 2022, 1:08 PM

LGTM.

This revision is now accepted and ready to land.Nov 21 2022, 1:08 PM
MaskRay added a comment.EditedNov 21 2022, 1:19 PM

Some host relocatable objects may not contain device relocatable objects, where an empty file is passed to lld, which causes lld to fail.

How is an empty file (size=0) passed to lld? If a dummy relocatable object file is parsed to lld, lld can infer the machine type from e_machine in the ELF header.
How does it work in other cases? Because a non-empty relocatable object file is used?

I do not object to a new emulation which does not exist in GNU ld, but the description needs to be clarified.

Updated description. The empty file is generated by clang-offload-bundler, which does not know how to create a dummy relocatable object file for a device target.

A size=0 file is handled as a linker script, not a relocatable object file. The empty linker script has no-op.

yaxunl marked an inline comment as done.Nov 21 2022, 2:13 PM

Some host relocatable objects may not contain device relocatable objects, where an empty file is passed to lld, which causes lld to fail.

How is an empty file (size=0) passed to lld? If a dummy relocatable object file is parsed to lld, lld can infer the machine type from e_machine in the ELF header.
How does it work in other cases? Because a non-empty relocatable object file is used?

I do not object to a new emulation which does not exist in GNU ld, but the description needs to be clarified.

Updated description. The empty file is generated by clang-offload-bundler, which does not know how to create a dummy relocatable object file for a device target.

A size=0 file is handled as a linker script, not a relocatable object file. The empty linker script has no-op.

I see. We got into a situation where all input files were empty and lld emitted the error:

lld: error: target emulation unknown: -m or at least one .o file required

adding -m fixes that.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptNov 22 2022, 7:39 AM