This is an archive of the discontinued LLVM Phabricator instance.

[AMDGPU] Add AMDGPU support for llvm-objcopy
Needs ReviewPublic

Authored by aakanksha555 on Feb 7 2023, 4:50 PM.

Details

Summary

llvm-objcopy does not support AMDGPU currently. It does not get target names where all other tools do but has it's own list. Update the list to support amdgpu.

Diff Detail

Event Timeline

aakanksha555 created this revision.Feb 7 2023, 4:50 PM
aakanksha555 requested review of this revision.Feb 7 2023, 4:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2023, 4:50 PM

Ping.
Would appreciate some feedback on this patch.

Sorry, I was away last week and am still catching up since then.

Is there a reason you haven't just added the new amdgpu case to the existing cross-arch-headers.test file? That seems to me like where it should belong.

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
305–306

I would suggest that this be moved to the end of the list, purely on the basis that the list has no specific ordering, and newer targets tend to be added at the end.

aakanksha555 marked an inline comment as done.Jul 20 2023, 10:53 AM

Sorry, I was away last week and am still catching up since then.

Is there a reason you haven't just added the new amdgpu case to the existing cross-arch-headers.test file? That seems to me like where it should belong.

Thanks for the feedback.

I ended up putting it in a separate testcase due to how amdgcn arch is handled - https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Object/ELFObjectFile.h#L1327
I'm specifying the relevant flag EF_AMDGPU_MACH_AMDGCN_GFX900 in this standalone test.

llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
305–306

I'll move it at the bottom of the list

aakanksha555 marked an inline comment as done.

Moved amdgpu to the bottom of the list

Sorry, I was away last week and am still catching up since then.

Is there a reason you haven't just added the new amdgpu case to the existing cross-arch-headers.test file? That seems to me like where it should belong.

Thanks for the feedback.

I ended up putting it in a separate testcase due to how amdgcn arch is handled - https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/Object/ELFObjectFile.h#L1327
I'm specifying the relevant flag EF_AMDGPU_MACH_AMDGCN_GFX900 in this standalone test.

There's no need for an entirely new test file just to add a flag. You can modify the existing YAML in cross-arch-headers.test to have a configurable Flags field. Simply add a field Flags: [[FLAGS=<none>]] to the YAML header and then invoke yaml2obj again with an additional option: -DFLAGS=[EF_AMDGPU_MACH_AMDGCN_GFX900]. It may also be worth checking whether the flag is present in the output. The =<none> part means the field will be treated as omitted by default, if the -D option isn't specified.

That all being said, the requirement for the flag is troubling me. Is there prior art to how to specify the ADMGPU target for an llvm-objcopy-like tool? The existing test shows that you can convert from one target (EM_NONE in the original test) to another target using the -O option, but the new test you've written doesn't demonstrate this behaviour (if I'm not mistaken, if you omitted the -O option, you'd get the same output, as the output target is derived from the input if not otherwise specified). Questions that I have are 1) if the input target was EM_AMDGPU and somebody specified e.g. -O elf32-hexagon, what should happen to any AMDGPU EF_* flag? 2) Similarly, how would you get a different AMDGPU flag? In other words, how would you get llvm-objcopy to add the EF_AMDGPU_MACH_* flag if it wasn't already present?

I'm wondering if a more natural solution to resolve the above questions is to, rather than support -O elf64-amdgpu is to support a family of values like -O elf64-amdgpu-amdgcn-gfx900. llvm-objcopy would then derive the appropriate EF_* and EM_* values from that. Similarly, it would learn to clear EF_* flags that aren't applicable. This would be new logic, as I don't believe currently llvm-objcopy does anything with ELF header flags. It would probably need to preserve non-machine-specific flags, and clear the other ones, similar to how it handles section header flags. WDYT?