Skip to content

Commit 503e129

Browse files
committed
Auto merge of rust-lang#118568 - DianQK:no-builtins-symbols, r=pnkfelix
Avoid adding builtin functions to `symbols.o` We found performance regressions in rust-lang#113923. The problem seems to be that `--gc-sections` does not remove these symbols. I tested that lld removes these symbols, but ld and gold do not. I found that `used` adds symbols to `symbols.o` at https://github.com/rust-lang/rust/blob/3e202ead604be31f4c1a5798a296953d3159da7e/compiler/rustc_codegen_ssa/src/back/linker.rs#L1786-L1791. The PR removes builtin functions. Note that under LTO, ld still preserves these symbols. (lld will still remove them.) The first commit also fixes rust-lang#118559. But I think the second commit also makes sense.
2 parents 0e7f91b + ca0738f commit 503e129

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn prepare_lto(
6060
};
6161

6262
let symbol_filter = &|&(ref name, info): &(String, SymbolExportInfo)| {
63-
if info.level.is_below_threshold(export_threshold) || info.used {
63+
if info.level.is_below_threshold(export_threshold) || info.used || info.used_compiler {
6464
Some(CString::new(name.as_str()).unwrap())
6565
} else {
6666
None

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,21 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
105105
}
106106
})
107107
.map(|def_id| {
108+
let codegen_attrs = tcx.codegen_fn_attrs(def_id.to_def_id());
108109
// We won't link right if this symbol is stripped during LTO.
109110
let name = tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())).name;
110111
// We have to preserve the symbols of the built-in functions during LTO.
111112
let is_builtin_fn = is_compiler_builtins
112113
&& symbol_export_level(tcx, def_id.to_def_id())
113-
.is_below_threshold(SymbolExportLevel::C);
114-
let used = is_builtin_fn || name == "rust_eh_personality";
114+
.is_below_threshold(SymbolExportLevel::C)
115+
&& codegen_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE);
116+
let used = name == "rust_eh_personality";
115117

116118
let export_level = if special_runtime_crate {
117119
SymbolExportLevel::Rust
118120
} else {
119121
symbol_export_level(tcx, def_id.to_def_id())
120122
};
121-
let codegen_attrs = tcx.codegen_fn_attrs(def_id.to_def_id());
122123
debug!(
123124
"EXPORTED SYMBOL (local): {} ({:?})",
124125
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
@@ -138,6 +139,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
138139
used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED)
139140
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
140141
|| used,
142+
used_compiler: is_builtin_fn,
141143
};
142144
(def_id.to_def_id(), info)
143145
})
@@ -150,6 +152,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
150152
level: SymbolExportLevel::C,
151153
kind: SymbolExportKind::Data,
152154
used: false,
155+
used_compiler: false,
153156
},
154157
);
155158
}
@@ -198,6 +201,7 @@ fn exported_symbols_provider_local(
198201
level: info.level,
199202
kind: SymbolExportKind::Text,
200203
used: info.used,
204+
used_compiler: false,
201205
},
202206
)
203207
})
@@ -214,6 +218,7 @@ fn exported_symbols_provider_local(
214218
level: SymbolExportLevel::C,
215219
kind: SymbolExportKind::Text,
216220
used: false,
221+
used_compiler: false,
217222
},
218223
));
219224
}
@@ -233,6 +238,7 @@ fn exported_symbols_provider_local(
233238
level: SymbolExportLevel::Rust,
234239
kind: SymbolExportKind::Text,
235240
used: false,
241+
used_compiler: false,
236242
},
237243
));
238244
}
@@ -245,6 +251,7 @@ fn exported_symbols_provider_local(
245251
level: SymbolExportLevel::Rust,
246252
kind: SymbolExportKind::Data,
247253
used: false,
254+
used_compiler: false,
248255
},
249256
))
250257
}
@@ -264,6 +271,7 @@ fn exported_symbols_provider_local(
264271
level: SymbolExportLevel::C,
265272
kind: SymbolExportKind::Data,
266273
used: false,
274+
used_compiler: false,
267275
},
268276
)
269277
}));
@@ -289,6 +297,7 @@ fn exported_symbols_provider_local(
289297
level: SymbolExportLevel::C,
290298
kind: SymbolExportKind::Data,
291299
used: false,
300+
used_compiler: false,
292301
},
293302
)
294303
}));
@@ -306,6 +315,7 @@ fn exported_symbols_provider_local(
306315
level: SymbolExportLevel::C,
307316
kind: SymbolExportKind::Data,
308317
used: true,
318+
used_compiler: false,
309319
},
310320
));
311321
}
@@ -346,6 +356,7 @@ fn exported_symbols_provider_local(
346356
level: SymbolExportLevel::Rust,
347357
kind: SymbolExportKind::Text,
348358
used: false,
359+
used_compiler: false,
349360
},
350361
));
351362
}
@@ -362,6 +373,7 @@ fn exported_symbols_provider_local(
362373
level: SymbolExportLevel::Rust,
363374
kind: SymbolExportKind::Text,
364375
used: false,
376+
used_compiler: false,
365377
},
366378
));
367379
}

compiler/rustc_middle/src/middle/exported_symbols.rs

+5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ pub enum SymbolExportKind {
3535
pub struct SymbolExportInfo {
3636
pub level: SymbolExportLevel,
3737
pub kind: SymbolExportKind,
38+
/// Used to mark these symbols not to be internalized by LTO. These symbols
39+
/// are also added to `symbols.o` to avoid circular dependencies when linking.
3840
pub used: bool,
41+
/// Also used to mark these symbols not to be internalized by LTO. But will
42+
/// not be added to `symbols.o`. Currently there are only builtin functions.
43+
pub used_compiler: bool,
3944
}
4045

4146
#[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)]

src/tools/miri/src/bin/miri.rs

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
165165
level: SymbolExportLevel::C,
166166
kind: SymbolExportKind::Text,
167167
used: false,
168+
used_compiler: false,
168169
},
169170
))
170171
}),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include ../tools.mk
2+
3+
# only-x86_64-unknown-linux-gnu
4+
5+
all:
6+
$(RUSTC) main.rs -o $(TMPDIR)/main
7+
[ "$$("$(LLVM_BIN_DIR)"/llvm-nm -U $(TMPDIR)/main | grep -c __fixunssfti)" -eq "0" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn main() {}

0 commit comments

Comments
 (0)