Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c82a59

Browse files
committed
Add test and comment
1 parent a5fa12b commit 5c82a59

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

‎compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ pub(crate) fn const_alloc_to_llvm<'ll>(
128128
append_chunks_of_init_and_uninit_bytes(&mut llvals, cx, alloc, range);
129129
}
130130

131+
// Avoid wrapping in a struct if there is only a single value. This ensures
132+
// that LLVM is able to perform the string merging optimization if the constant
133+
// is a valid C string. LLVM only considers bare arrays for this optimization,
134+
// not arrays wrapped in a struct. LLVM handles this at:
135+
// https://github.com/rust-lang/llvm-project/blob/acaea3d2bb8f351b740db7ebce7d7a40b9e21488/llvm/lib/Target/TargetLoweringObjectFile.cpp#L249-L280
131136
if let &[data] = &*llvals { data } else { cx.const_struct(&llvals, true) }
132137
}
133138

‎tests/assembly/cstring-merging.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ only-linux
2+
//@ assembly-output: emit-asm
3+
//@ compile-flags: --crate-type=lib -Copt-level=3 --edition 2024
4+
5+
use std::ffi::CStr;
6+
7+
// CHECK: .section .rodata.str1.1,"aMS"
8+
// CHECK: .Lanon.{{.+}}:
9+
// CHECK-NEXT: .asciz "foo"
10+
#[unsafe(no_mangle)]
11+
static CSTR: &[u8; 4] = b"foo0円";
12+
13+
// CHECK-NOT: .section
14+
// CHECK: .Lanon.{{.+}}:
15+
// CHECK-NEXT: .asciz "bar"
16+
#[unsafe(no_mangle)]
17+
pub fn cstr() -> &'static CStr {
18+
c"bar"
19+
}
20+
21+
// CHECK-NOT: .section
22+
// CHECK: .Lanon.{{.+}}:
23+
// CHECK-NEXT: .asciz "baz"
24+
#[unsafe(no_mangle)]
25+
pub fn manual_cstr() -> &'static str {
26+
"baz0円"
27+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /