Skip to content

Can no longer set custom section flags in LLVM IR #50551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nikic opened this issue Jul 24, 2021 · 10 comments
Closed

Can no longer set custom section flags in LLVM IR #50551

nikic opened this issue Jul 24, 2021 · 10 comments
Labels
bugzilla Issues migrated from bugzilla

Comments

@nikic
Copy link
Contributor

nikic commented Jul 24, 2021

Bugzilla Link 51207
Version trunk
OS Linux
Blocks #50580
CC @MaskRay,@cuviper,@tstellar
Fixed by commit(s) 1c198b3

Extended Description

After 82d0749 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?

@MaskRay
Copy link
Member

MaskRay commented Jul 29, 2021

82d0749 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.

@nikic
Copy link
Contributor Author

nikic commented Jul 29, 2021

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.

@nikic
Copy link
Contributor Author

nikic commented Jul 30, 2021

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.

@nikic
Copy link
Contributor Author

nikic commented Jul 31, 2021

Revert posted at https://reviews.llvm.org/D107216.

@nikic
Copy link
Contributor Author

nikic commented Aug 21, 2021

We have now finished the LLVM 13 upgrade with D100944 reverted in our fork.

@cuviper
Copy link
Member

cuviper commented Sep 10, 2021

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.

@tstellar
Copy link
Collaborator

Merged: 1c198b3

@tstellar
Copy link
Collaborator

mentioned in issue #50580

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@asl
Copy link
Collaborator

asl commented Dec 15, 2021

@tstellar Move to 13.0.1?

@nikic
Copy link
Contributor Author

nikic commented Dec 15, 2021

This issue has been addressed on the rust side (see rust-lang/rust#90326), so closing this. Adding proper support for section flags in LLVM would still be nice, but it's no longer relevant as far as the release is concerned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

5 participants