LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 51207 - Can no longer set custom section flags in LLVM IR
Summary: Can no longer set custom section flags in LLVM IR
Status: NEW
Alias: None
Product: new-bugs
Classification: Unclassified
Component: new bugs (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks: release-13.0.0
  Show dependency tree
 
Reported: 2021-07-24 13:58 PDT by Nikita Popov
Modified: 2021-10-14 17:23 PDT (History)
5 users (show)

See Also:
Fixed By Commit(s): 1c198b3032e899003b5c1ffaa15c7648f24f1e69


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nikita Popov 2021-07-24 13:58:55 PDT
After https://github.com/llvm/llvm-project/commit/82d0749749fafb0303414131dc570781376fdcfb this IR

module asm ".section .llvmbc,\22e\22"
@rustc.embedded.module = private constant [3 x i8] c"ABC", section ".llvmbc"

when run through llc results in an "Unknown section kind" assertion failure.

rustc emits this IR here, which has a comment explaining why it does so: https://github.com/rust-lang/rust/blob/bddb59cf07efcf6e606f16b87f85e3ecd2c1ca69/compiler/rustc_codegen_llvm/src/back/write.rs#L970-L1017

I'm not sure where the bug is here -- what rustc is doing certainly looks fishy, but I'm not sure what it is supposed to be doing instead. Any advice?
Comment 1 Fangrui Song 2021-07-29 13:40:49 PDT
82d0749749fafb0303414131dc570781376fdcfb is innocent. The Rust code is incorrect.

With the inline asm .section directive, `SeenSectionNameBefore` is true and we don't take the `return MCContext::GenericSectionID;` shortcut.
`getELFSectionNameForGlobal` doesn't intend to work for non-SHF_ALLOC sections => assertion failure.

The Rust abused the previous LLVM behavior when an inline asm .section directive can override the section attributes of `section ".llvmbc"`.
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp doesn't have such guarantee.
D100944 may be when this changed.

To support such usage properly, we need an LLVM IR construct to encode the section attributes.
It can be quite challenging to make a design suitable for multiple binary formats.
In the absence of that, the best way forward for Rust is to change the section after MC object emission.
Comment 2 Nikita Popov 2021-07-29 14:07:50 PDT
I don't think that modifying object files after the fact is an acceptable outcome for us. Prior to your change, LLVM provided a way to set section flags on the llvmbc section, even if it was roundabout -- and after your change there is no longer any IR-level way to achieve that, if I understood correctly.

As such, I believe this change should be reverted for the LLVM 13 release, and be reapplied once there is an IR-level mechanism to specify the flags.
Comment 3 Nikita Popov 2021-07-30 13:13:59 PDT
After further testing, it looks like reverting https://reviews.llvm.org/D86374 just fixes the assertion failure, but still results in undesirable behavior: We'll get two .llvmbc sections, one with the EXCLUDE flag, and one (containing the actual bitcode) with the ALLOC flag. This breaks bitcode extraction entirely.

The actual problem here is https://reviews.llvm.org/D100944. Reverting that change (and just that change) restores the previous behavior.

Actually, it looks like this problem was brought up in post-commit review for D100944, but didn't reach a satisfactory conclusion.
Comment 4 Nikita Popov 2021-07-31 01:17:54 PDT
Revert posted at https://reviews.llvm.org/D107216.
Comment 5 Nikita Popov 2021-08-21 12:58:19 PDT
We have now finished the LLVM 13 upgrade with D100944 reverted in our fork.
Comment 6 Josh Stone 2021-09-10 11:22:47 PDT
In further discussion on D107216, we agreed to apply the revert on 13.x only, leaving the change on main, so Rust will have time to find a new way for 14. Fangrui suggested different wording for the commit message though.
Comment 7 Tom Stellard 2021-09-10 17:01:56 PDT
Merged: 1c198b3032e899003b5c1ffaa15c7648f24f1e69