Allow anonymous constants to be folded across crates (weak linkage + COMDAT) #662
Closed
1 of 3 tasks
Labels
major-change
A proposal to make a major change to rustc
T-compiler
Add this label so rfcbot knows to poll the compiler team
Uh oh!
There was an error while loading. Please reload this page.
Proposal
Within the Rust compiler there are a number of places that we need to generate "anonymous" constants - for example, string literals that are directly used without being stored in a constant or the caller location information for
panic!
macros.These are generated by the
ConstMethods
traits, which are implemented in compiler/rustc_codegen_llvm/src/common.rs for LLVM.Within this implementation we use internal linkage for strings and private linkage for allocations, which instructs LLVM that each of these items should be kept by the linker, even if they share a name or contents with other items across translation units (i.e., crates). This results in common constants being duplicated across crates, for example the panic messages from
Option::unwrap
andResult::unwrap
.My proposal is to instruct LLVM that these can be folded by making the following changes in both
const_str
and inscalar_to_backend
(forScalar::Ptr
pointing toGlobalAlloc::Memory
with a mutability ofMutability::Not
):fewer_names
option).WeakODRLinkage
(i.e., only keep one, contents much match else fail to link).These changes can be seen at rust-lang/rust#114816
It's likely that this change will regress compiler performance, but projects that utilize a lot of crates should see a decent binary size win (for example, rustc_driver had a 3.5% size reduction for me locally (141,846kb to 136,762kb on x86_64-pc-windows-msvc built as release with assertions enabled).
Mentors or Reviewers
@wesleywiser
Process
The main points of the Major Change Process are as follows:
@rustbot second
.-C flag
, then full team check-off is required.@rfcbot fcp merge
on either the MCP or the PR.You can read more about Major Change Proposals on forge.
Comments
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.
The text was updated successfully, but these errors were encountered: