This is an archive of the discontinued LLVM Phabricator instance.

[Object] Add ELF section type SHT_LLVM_BITCODE for LLVM bitcode
ClosedPublic

Authored by MaskRay on Jun 17 2023, 6:57 PM.

Details

Summary

clang -ffat-lto-objects can use this new ELF section type for the .llvm.lto
section for fat LTO support (D146776).

Original RFC: https://discourse.llvm.org/t/rfc-ffat-lto-objects-support/63977

Diff Detail

Event Timeline

MaskRay created this revision.Jun 17 2023, 6:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2023, 6:57 PM
MaskRay requested review of this revision.Jun 17 2023, 6:57 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 17 2023, 6:57 PM
nikic added a subscriber: nikic.Jun 18 2023, 12:14 AM

Why is a new section type necessary? Will this cause any compatibility issues with old binutils versions?

Is it worth an llvm-objdump test too?

Functionally looks fine, but I have no opinion on the overall feature itself (and therefore whether a new section type etc is needed for it).

I will say that if the feature has been accepted, a new section type would seem appropriate, so that tooling can handle it appropriately without having to check section names.

llvm/include/llvm/BinaryFormat/ELF.h
1039

Perhaps SHT_LLVM_FAT_LTO so that it is a little more self-descriptive? (Equivalent comment applies to the section name and similar bits of data)

paulkirth added inline comments.Jun 20 2023, 11:01 AM
llvm/include/llvm/BinaryFormat/ELF.h
1039

While this is for FatLTO now, I don't see why the section flag needs that distinction, since its a bit more generic (i.e., it's bitcode for LTO whether you use it for FatLTO or not). I don't have super strong feelings here, but I don't see much value in labeling it differently.

Why is a new section type necessary? Will this cause any compatibility issues with old binutils versions?

ELF's spirit is to distinguish special sections with a section type instead of a name. (Older object file formats (COFF,Mach-O) don't use this convention, it's fine.)
If we need to perform special tasks with the section, we can use an integer comparison instead of a string comparison.

Is it worth an llvm-objdump test too?

Functionally looks fine, but I have no opinion on the overall feature itself (and therefore whether a new section type etc is needed for it).

I will say that if the feature has been accepted, a new section type would seem appropriate, so that tooling can handle it appropriately without having to check section names.

I think normal code paths of llvm-objdump do not print section types. (llvm-objdump -h doesn't).
If there is an error, llvm-objdump will use llvm::object::describe to give an error, but the message is indirect. `llvm::object::describe is tested by llvm-readobj`.

Is it worth an llvm-objdump test too?

Functionally looks fine, but I have no opinion on the overall feature itself (and therefore whether a new section type etc is needed for it).

I will say that if the feature has been accepted, a new section type would seem appropriate, so that tooling can handle it appropriately without having to check section names.

I think normal code paths of llvm-objdump do not print section types. (llvm-objdump -h doesn't).
If there is an error, llvm-objdump will use llvm::object::describe to give an error, but the message is indirect. `llvm::object::describe is tested by llvm-readobj`.

Good point, thanks.

llvm/include/llvm/BinaryFormat/ELF.h
1039

Fair point. In which case, SHT_LLVM_BITCODE seems more self-descriptive (describing what it is rather than one potential use for it)? I imagine you could do things other than LTO with it in the future too after all :)

I'm not necessarily opposed to SHT_LLVM_LTO, but as this name is going to live a very long time, it's important we get it right!

paulkirth added inline comments.Jun 21 2023, 10:09 AM
llvm/include/llvm/BinaryFormat/ELF.h
1039

Fair point. In which case, SHT_LLVM_BITCODE seems more self-descriptive (describing what it is rather than one potential use for it)?

Those are all good points, but I think we want to describe that it's bitcode that is LTO ready. Using .llvmbc code is problematic for LTO use, so I could easily imagine that if this is SHT_LLVM_BITCODE that would lead to confusion or to the .lvmbc section being marked that way...

I imagine you could do things other than LTO with it in the future too after all :)

Yeah, it sounds like a mismatch to imagine using the section for analysis if you're not doing LTO.

I'm not necessarily opposed to SHT_LLVM_LTO, but as this name is going to live a very long time, it's important we get it right!

Naming is hard :(

Regardless of what we arrive at, lets be sure we document what its intended uses are, and why we chose that. As long as we do that, I think it will be fine, so I'm not going to oppose alternate names.

MaskRay added inline comments.Jun 21 2023, 11:14 AM
llvm/include/llvm/BinaryFormat/ELF.h
1039

One thought is to assign SHT_LLVM_BITCODE to both .llvm.lto and .llvmbc. We may need a function similar to IRObjectFile::findBitcodeInMemBuffer (in D146776) that picks a SHT_LLVM_BITCODE section only when it is not called .llvmbc, i.e.

type == SHT_LLVM_BITCODE && name != ".llvmbc"

jhenderson added inline comments.Jun 21 2023, 11:43 PM
llvm/include/llvm/BinaryFormat/ELF.h
1039

Those are all good points, but I think we want to describe that it's bitcode that is LTO ready

I wasn't aware that there was a difference between different kinds of bitcode - I've never really looked into how bitcode and LTO work. Given this point, I'm okay with either SHT_LLVM_LTO or SHT_LLVM_BITCODE, depending on whether we go with @MaskRay's suggestion above. We could also try e.g. SHT_LLVM_LTO_INPUT maybe? But that might be starting to get a bit long.

Naming is hard :(

Amen to that.

MaskRay updated this revision to Diff 533822.Jun 22 2023, 5:13 PM
MaskRay retitled this revision from [Object] Add ELF section type SHT_LLVM_LTO for fat LTO to [Object] Add ELF section type SHT_LLVM_BITCODE for LLVM bitcode.
MaskRay edited the summary of this revision. (Show Details)

rename to SHT_LLVM_BITCODE

jhenderson accepted this revision.Jun 23 2023, 12:05 AM

The pre-merge clang-format check appears to be failing.

LGTM apart from that and a couple of nits.

llvm/docs/Extensions.rst
467

This line probably wants adjusting.

470

(and reflow if necessary)

This revision is now accepted and ready to land.Jun 23 2023, 12:05 AM

Doesn't the rename defeat the purpose stated here...

ELF's spirit is to distinguish special sections with a section type instead of a name.

...because we now need to compare the section name anyway to distinguish LTO bitcode from embedded bitcode?

Doesn't the rename defeat the purpose stated here...

ELF's spirit is to distinguish special sections with a section type instead of a name.

...because we now need to compare the section name anyway to distinguish LTO bitcode from embedded bitcode?

We can do one of the schemes:

  • make just .llvm.lto SHT_LLVM_BITCODE. Make findBitcodeInObject used by lld/LLVMgold.so use this section type
  • make both .llvm.lto/.llvmbc SHT_LLVM_BITCODE. In this case findBitcodeInObject hard codes that the section is not .llvmbc

Dropping support for -embed-bitcode was brought up in the original RFC for FatLTO. The plan I recall was to deprecate the option, make it CC1 , and see if MLGO (the only remaining user we're aware of) could use a different solution and if they could, then drop it altogether.

The important bits start roughly here https://discourse.llvm.org/t/rfc-ffat-lto-objects-support/63977/15, but there are some other points raised earlier in the thread too. In light of that plan, I'm not too sure how important it is to provide support for .llvmbc.

@mtrofin, since I think you would be impacted by significant changes in this area, do you have thoughts here?

I'll land this soon, as I do wish that .llvm.lto has a proper section type. This aligns with other SHT_LLVM_* we have.
I am not concerned whether .llvmbc has this section type or not and whether we want to special case it.

This revision was landed with ongoing or failed builds.Jun 28 2023, 2:01 PM
This revision was automatically updated to reflect the committed changes.