Skip to content

Commit 27b2be8

Browse files
committed
Add -Z small-data-threshold
This flag allows specifying the threshold size above which LLVM should consider placing small objects in a .sdata or .sbss section.
1 parent b66fe58 commit 27b2be8

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

compiler/rustc_codegen_llvm/src/context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,17 @@ pub unsafe fn create_module<'ll>(
351351
);
352352
}
353353

354+
if let Some(threshold) = sess.opts.unstable_opts.small_data_threshold {
355+
if sess.target.arch.starts_with("riscv") {
356+
llvm::LLVMRustAddModuleFlag(
357+
llmod,
358+
llvm::LLVMModFlagBehavior::Error,
359+
"SmallDataLimit\0".as_ptr().cast(),
360+
threshold as u32,
361+
)
362+
}
363+
}
364+
354365
// Insert `llvm.ident` metadata.
355366
//
356367
// On the wasm targets it will get hooked up to the "producer" sections

compiler/rustc_codegen_llvm/src/llvm_util.rs

+15
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ unsafe fn configure_llvm(sess: &Session) {
118118
for arg in sess_args {
119119
add(&(*arg), true);
120120
}
121+
122+
if let Some(threshold) = sess.opts.unstable_opts.small_data_threshold {
123+
match sess.target.arch.as_ref() {
124+
"hexagon" => {
125+
add(&format!("--hexagon-small-data-threshold={threshold}"), false);
126+
add("--hexagon-statics-in-small-datax", false)
127+
}
128+
"m68k" => add(&format!("--m68k-ssection-threshold={threshold}"), false),
129+
arch @ _ => {
130+
if arch.starts_with("mips") {
131+
add(&format!("--mips-ssection-threshold={threshold}"), false);
132+
}
133+
}
134+
}
135+
}
121136
}
122137

123138
if sess.opts.unstable_opts.llvm_time_trace {

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,8 @@ written to standard error output)"),
18051805
simulate_remapped_rust_src_base: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
18061806
"simulate the effect of remap-debuginfo = true at bootstrapping by remapping path \
18071807
to rust's source base directory. only meant for testing purposes"),
1808+
small_data_threshold: Option<usize> = (None, parse_opt_number, [TRACKED],
1809+
"Set the threshold for objects to be stored in a \"small data\" section"),
18081810
span_debug: bool = (false, parse_bool, [UNTRACKED],
18091811
"forward proc_macro::Span's `Debug` impl to `Span`"),
18101812
/// o/w tests have closure@path
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Test for -Z small_data_threshold=...
2+
// revisions: RISCV MIPS HEXAGON M68K
3+
// assembly-output: emit-asm
4+
// compile-flags: -Z small_data_threshold=4
5+
// [RISCV] compile-flags:--target=riscv32im-unknown-none-elf
6+
// [MIPS] compile-flags:--target=mips-unknown-linux-musl
7+
// [HEXAGON] compile-flags:--target=hexagon-unknown-linux-musl
8+
// [M68K] compile-flags:--target=m68k-unknown-linux-gnu
9+
10+
#![feature(no_core, lang_items)]
11+
#![no_std]
12+
#![no_core]
13+
#![crate_type = "lib"]
14+
15+
#[lang = "sized"]
16+
trait Sized {}
17+
18+
#[lang = "drop_in_place"]
19+
fn drop_in_place<T>(_: *mut T) {}
20+
21+
#[used]
22+
#[no_mangle]
23+
/// X is below the threshold, should be in sdata
24+
static mut X: u16 = 123;
25+
26+
#[used]
27+
#[no_mangle]
28+
/// Y is at the threshold, should be in sdata
29+
static mut Y: u32 = 123;
30+
31+
#[used]
32+
#[no_mangle]
33+
/// Z is over the threshold, should be in its own section
34+
static mut Z: u64 = 123;
35+
36+
// Currently, only RISCV successfully puts any objects in the small data
37+
// section.
38+
39+
// RISCV: .section .sdata,
40+
// RISCV-NOT: .section
41+
// RISCV: X:
42+
// RISCV: Y:
43+
// CHECK: .section .data.Z,
44+
// CHECK-NOT: .section
45+
// CHECK: Z:

0 commit comments

Comments
 (0)