Skip to content

Commit 4f2e555

Browse files
committed
Auto merge of #117930 - thomcc:const_str-unnamed, r=<try>
Ensure strings created with `const_str` get the `unnamed_addr` attribute This function (`const_str`) is only used when we need to invent a string during codegen -- for example, for a panic message to pass when codegening some of the assert/panic/etc terminators (for stuff like divide by zero). AFAICT all other consts, such as the user-defined ones from const eval, should already be getting this attribute (things that come from a ConstAllocation do, for example). Which means that the "unnamed" part is even more true than usual here, these aren't strings that even exist as far as the user can tell. Setting this attribute allows LLVM to merge these constants, leading to significant binary size savings (much more than I would expect). On x86_64-unknown-linux-gnu, t takes a build of ripgrep (release without debug info) from 9.7MiB to 6.0MiB (a savings of over 30%!?), and a build of rustc_driver's shared object from ~123MiB to ~112MiB (less drastic, but still over 10% reduced). The effect on ripgrep is substantially reduced on macOS for reasons beyond me (I may have fucked up the test), only saving around 0.2MiB, although rustc_driver is still around 10MB or smaller than it had been previously. This raises some questions, such as "does that mean 1/3 of ripgrep was made of division by zero complaints?" I'm not sure, that may be the case. The output of `strings path/to/rg` is ~2MB smaller, so it seems like a lot of it was. Allowing these to be merged presumably also allow functions that contain them to be merged (if the addresses had semantic meaning, then it stands). I intend to do some more analysis here, but I got this up as soon as I realized that this attribute was only missing for internal const strings, and all other ones already get it.
2 parents 698fcc8 + 268f5c5 commit 4f2e555

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

compiler/rustc_codegen_llvm/src/common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
203203
unsafe {
204204
llvm::LLVMSetInitializer(g, sc);
205205
llvm::LLVMSetGlobalConstant(g, True);
206+
llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
206207
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
207208
}
208209
(s.to_owned(), g)

0 commit comments

Comments
 (0)