Skip to content

Commit 242d148

Browse files
committed
Auto merge of #140809 - bjorn3:panic_runtime_cleanup, r=wesleywiser,ibraheemdev
Reduce special casing for the panic runtime See the individual commits for more info.
2 parents 55d4364 + 6df5456 commit 242d148

File tree

14 files changed

+44
-143
lines changed

14 files changed

+44
-143
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,7 @@ pub(crate) fn linked_symbols(
18291829
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
18301830
if info.level.is_below_threshold(export_threshold) && !tcx.is_compiler_builtins(cnum)
18311831
|| info.used
1832+
|| info.rustc_std_internal_symbol
18321833
{
18331834
symbols.push((
18341835
symbol_export::linking_symbol_name_for_instance_in_crate(tcx, symbol, cnum),

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
131131
used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
132132
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
133133
|| used,
134+
rustc_std_internal_symbol: codegen_attrs
135+
.flags
136+
.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL),
134137
};
135138
(def_id.to_def_id(), info)
136139
})
@@ -143,6 +146,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
143146
level: SymbolExportLevel::C,
144147
kind: SymbolExportKind::Data,
145148
used: false,
149+
rustc_std_internal_symbol: false,
146150
},
147151
);
148152
}
@@ -191,6 +195,7 @@ fn exported_symbols_provider_local<'tcx>(
191195
level: info.level,
192196
kind: SymbolExportKind::Text,
193197
used: info.used,
198+
rustc_std_internal_symbol: info.rustc_std_internal_symbol,
194199
},
195200
)
196201
})
@@ -207,6 +212,7 @@ fn exported_symbols_provider_local<'tcx>(
207212
level: SymbolExportLevel::C,
208213
kind: SymbolExportKind::Text,
209214
used: false,
215+
rustc_std_internal_symbol: false,
210216
},
211217
));
212218
}
@@ -229,6 +235,7 @@ fn exported_symbols_provider_local<'tcx>(
229235
level: SymbolExportLevel::Rust,
230236
kind: SymbolExportKind::Text,
231237
used: false,
238+
rustc_std_internal_symbol: true,
232239
},
233240
));
234241
}
@@ -243,6 +250,7 @@ fn exported_symbols_provider_local<'tcx>(
243250
level: SymbolExportLevel::Rust,
244251
kind: SymbolExportKind::Data,
245252
used: false,
253+
rustc_std_internal_symbol: true,
246254
},
247255
))
248256
}
@@ -262,6 +270,7 @@ fn exported_symbols_provider_local<'tcx>(
262270
level: SymbolExportLevel::C,
263271
kind: SymbolExportKind::Data,
264272
used: false,
273+
rustc_std_internal_symbol: false,
265274
},
266275
)
267276
}));
@@ -287,6 +296,7 @@ fn exported_symbols_provider_local<'tcx>(
287296
level: SymbolExportLevel::C,
288297
kind: SymbolExportKind::Data,
289298
used: false,
299+
rustc_std_internal_symbol: false,
290300
},
291301
)
292302
}));
@@ -304,6 +314,7 @@ fn exported_symbols_provider_local<'tcx>(
304314
level: SymbolExportLevel::C,
305315
kind: SymbolExportKind::Data,
306316
used: true,
317+
rustc_std_internal_symbol: false,
307318
},
308319
));
309320
}
@@ -379,6 +390,8 @@ fn exported_symbols_provider_local<'tcx>(
379390
}
380391
}
381392

393+
// Note: These all set rustc_std_internal_symbol to false as generic functions must not
394+
// be marked with this attribute and we are only handling generic functions here.
382395
match *mono_item {
383396
MonoItem::Fn(Instance { def: InstanceKind::Item(def), args }) => {
384397
let has_generics = args.non_erasable_generics().next().is_some();
@@ -394,6 +407,7 @@ fn exported_symbols_provider_local<'tcx>(
394407
level: SymbolExportLevel::Rust,
395408
kind: SymbolExportKind::Text,
396409
used: false,
410+
rustc_std_internal_symbol: false,
397411
},
398412
));
399413
}
@@ -416,6 +430,7 @@ fn exported_symbols_provider_local<'tcx>(
416430
level: SymbolExportLevel::Rust,
417431
kind: SymbolExportKind::Text,
418432
used: false,
433+
rustc_std_internal_symbol: false,
419434
},
420435
));
421436
}
@@ -432,6 +447,7 @@ fn exported_symbols_provider_local<'tcx>(
432447
level: SymbolExportLevel::Rust,
433448
kind: SymbolExportKind::Text,
434449
used: false,
450+
rustc_std_internal_symbol: false,
435451
},
436452
));
437453
}
@@ -442,6 +458,7 @@ fn exported_symbols_provider_local<'tcx>(
442458
level: SymbolExportLevel::Rust,
443459
kind: SymbolExportKind::Text,
444460
used: false,
461+
rustc_std_internal_symbol: false,
445462
},
446463
));
447464
}

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::time::{Duration, Instant};
66
use itertools::Itertools;
77
use rustc_abi::FIRST_VARIANT;
88
use rustc_ast as ast;
9-
use rustc_ast::expand::allocator::{ALLOCATOR_METHODS, AllocatorKind, global_fn_name};
9+
use rustc_ast::expand::allocator::AllocatorKind;
1010
use rustc_attr_data_structures::OptimizeAttr;
1111
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1212
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
@@ -1039,26 +1039,6 @@ impl CrateInfo {
10391039
.collect::<Vec<_>>();
10401040
symbols.sort_unstable_by(|a, b| a.0.cmp(&b.0));
10411041
linked_symbols.extend(symbols);
1042-
if tcx.allocator_kind(()).is_some() {
1043-
// At least one crate needs a global allocator. This crate may be placed
1044-
// after the crate that defines it in the linker order, in which case some
1045-
// linkers return an error. By adding the global allocator shim methods to
1046-
// the linked_symbols list, linking the generated symbols.o will ensure that
1047-
// circular dependencies involving the global allocator don't lead to linker
1048-
// errors.
1049-
linked_symbols.extend(ALLOCATOR_METHODS.iter().map(|method| {
1050-
(
1051-
format!(
1052-
"{prefix}{}",
1053-
mangle_internal_symbol(
1054-
tcx,
1055-
global_fn_name(method.name).as_str()
1056-
)
1057-
),
1058-
SymbolExportKind::Text,
1059-
)
1060-
}));
1061-
}
10621042
});
10631043
}
10641044

compiler/rustc_metadata/src/creader.rs

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Validates all used crates and extern libraries and loads their metadata
22
33
use std::error::Error;
4-
use std::ops::Fn;
54
use std::path::Path;
65
use std::str::FromStr;
76
use std::time::Duration;
@@ -276,12 +275,6 @@ impl CStore {
276275
.filter_map(|(cnum, data)| data.as_deref().map(|data| (cnum, data)))
277276
}
278277

279-
fn iter_crate_data_mut(&mut self) -> impl Iterator<Item = (CrateNum, &mut CrateMetadata)> {
280-
self.metas
281-
.iter_enumerated_mut()
282-
.filter_map(|(cnum, data)| data.as_deref_mut().map(|data| (cnum, data)))
283-
}
284-
285278
fn push_dependencies_in_postorder(&self, deps: &mut IndexSet<CrateNum>, cnum: CrateNum) {
286279
if !deps.contains(&cnum) {
287280
let data = self.get_crate_data(cnum);
@@ -307,13 +300,6 @@ impl CStore {
307300
deps
308301
}
309302

310-
fn crate_dependencies_in_reverse_postorder(
311-
&self,
312-
cnum: CrateNum,
313-
) -> impl Iterator<Item = CrateNum> {
314-
self.crate_dependencies_in_postorder(cnum).into_iter().rev()
315-
}
316-
317303
pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> {
318304
self.injected_panic_runtime
319305
}
@@ -966,47 +952,27 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
966952
// If we need a panic runtime, we try to find an existing one here. At
967953
// the same time we perform some general validation of the DAG we've got
968954
// going such as ensuring everything has a compatible panic strategy.
969-
//
970-
// The logic for finding the panic runtime here is pretty much the same
971-
// as the allocator case with the only addition that the panic strategy
972-
// compilation mode also comes into play.
973-
let desired_strategy = self.sess.panic_strategy();
974-
let mut runtime_found = false;
975955
let mut needs_panic_runtime = attr::contains_name(&krate.attrs, sym::needs_panic_runtime);
976-
977-
let mut panic_runtimes = Vec::new();
978-
for (cnum, data) in self.cstore.iter_crate_data() {
979-
needs_panic_runtime = needs_panic_runtime || data.needs_panic_runtime();
980-
if data.is_panic_runtime() {
981-
// Inject a dependency from all #![needs_panic_runtime] to this
982-
// #![panic_runtime] crate.
983-
panic_runtimes.push(cnum);
984-
runtime_found = runtime_found || data.dep_kind() == CrateDepKind::Explicit;
985-
}
986-
}
987-
for cnum in panic_runtimes {
988-
self.inject_dependency_if(cnum, "a panic runtime", &|data| data.needs_panic_runtime());
956+
for (_cnum, data) in self.cstore.iter_crate_data() {
957+
needs_panic_runtime |= data.needs_panic_runtime();
989958
}
990959

991-
// If an explicitly linked and matching panic runtime was found, or if
992-
// we just don't need one at all, then we're done here and there's
993-
// nothing else to do.
994-
if !needs_panic_runtime || runtime_found {
960+
// If we just don't need a panic runtime at all, then we're done here
961+
// and there's nothing else to do.
962+
if !needs_panic_runtime {
995963
return;
996964
}
997965

998-
// By this point we know that we (a) need a panic runtime and (b) no
999-
// panic runtime was explicitly linked. Here we just load an appropriate
1000-
// default runtime for our panic strategy and then inject the
1001-
// dependencies.
966+
// By this point we know that we need a panic runtime. Here we just load
967+
// an appropriate default runtime for our panic strategy.
1002968
//
1003969
// We may resolve to an already loaded crate (as the crate may not have
1004-
// been explicitly linked prior to this) and we may re-inject
1005-
// dependencies again, but both of those situations are fine.
970+
// been explicitly linked prior to this), but this is fine.
1006971
//
1007972
// Also note that we have yet to perform validation of the crate graph
1008973
// in terms of everyone has a compatible panic runtime format, that's
1009974
// performed later as part of the `dependency_format` module.
975+
let desired_strategy = self.sess.panic_strategy();
1010976
let name = match desired_strategy {
1011977
PanicStrategy::Unwind => sym::panic_unwind,
1012978
PanicStrategy::Abort => sym::panic_abort,
@@ -1031,7 +997,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
1031997
}
1032998

1033999
self.cstore.injected_panic_runtime = Some(cnum);
1034-
self.inject_dependency_if(cnum, "a panic runtime", &|data| data.needs_panic_runtime());
10351000
}
10361001

10371002
fn inject_profiler_runtime(&mut self) {
@@ -1212,45 +1177,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
12121177
}
12131178
}
12141179

1215-
fn inject_dependency_if(
1216-
&mut self,
1217-
krate: CrateNum,
1218-
what: &str,
1219-
needs_dep: &dyn Fn(&CrateMetadata) -> bool,
1220-
) {
1221-
// Don't perform this validation if the session has errors, as one of
1222-
// those errors may indicate a circular dependency which could cause
1223-
// this to stack overflow.
1224-
if self.dcx().has_errors().is_some() {
1225-
return;
1226-
}
1227-
1228-
// Before we inject any dependencies, make sure we don't inject a
1229-
// circular dependency by validating that this crate doesn't
1230-
// transitively depend on any crates satisfying `needs_dep`.
1231-
for dep in self.cstore.crate_dependencies_in_reverse_postorder(krate) {
1232-
let data = self.cstore.get_crate_data(dep);
1233-
if needs_dep(&data) {
1234-
self.dcx().emit_err(errors::NoTransitiveNeedsDep {
1235-
crate_name: self.cstore.get_crate_data(krate).name(),
1236-
needs_crate_name: what,
1237-
deps_crate_name: data.name(),
1238-
});
1239-
}
1240-
}
1241-
1242-
// All crates satisfying `needs_dep` do not explicitly depend on the
1243-
// crate provided for this compile, but in order for this compilation to
1244-
// be successfully linked we need to inject a dependency (to order the
1245-
// crates on the command line correctly).
1246-
for (cnum, data) in self.cstore.iter_crate_data_mut() {
1247-
if needs_dep(data) {
1248-
info!("injecting a dep from {} to {}", cnum, krate);
1249-
data.add_dependency(krate);
1250-
}
1251-
}
1252-
}
1253-
12541180
fn report_unused_deps(&mut self, krate: &ast::Crate) {
12551181
// Make a point span rather than covering the whole file
12561182
let span = krate.spans.inner_span.shrink_to_lo();

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,15 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
370370
Some(ret)
371371
}
372372

373-
// Given a list of how to link upstream dependencies so far, ensure that an
374-
// injected dependency is activated. This will not do anything if one was
375-
// transitively included already (e.g., via a dylib or explicitly so).
376-
//
377-
// If an injected dependency was not found then we're guaranteed the
378-
// metadata::creader module has injected that dependency (not listed as
379-
// a required dependency) in one of the session's field. If this field is not
380-
// set then this compilation doesn't actually need the dependency and we can
381-
// also skip this step entirely.
373+
/// Given a list of how to link upstream dependencies so far, ensure that an
374+
/// injected dependency is activated. This will not do anything if one was
375+
/// transitively included already (e.g., via a dylib or explicitly so).
376+
///
377+
/// If an injected dependency was not found then we're guaranteed the
378+
/// metadata::creader module has injected that dependency (not listed as
379+
/// a required dependency) in one of the session's field. If this field is not
380+
/// set then this compilation doesn't actually need the dependency and we can
381+
/// also skip this step entirely.
382382
fn activate_injected_dep(
383383
injected: Option<CrateNum>,
384384
list: &mut DependencyList,

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,10 +1868,6 @@ impl CrateMetadata {
18681868
self.dependencies.iter().copied()
18691869
}
18701870

1871-
pub(crate) fn add_dependency(&mut self, cnum: CrateNum) {
1872-
self.dependencies.push(cnum);
1873-
}
1874-
18751871
pub(crate) fn target_modifiers(&self) -> TargetModifiers {
18761872
self.root.decode_target_modifiers(&self.blob).collect()
18771873
}

compiler/rustc_middle/src/middle/exported_symbols.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ pub enum SymbolExportKind {
3535
pub struct SymbolExportInfo {
3636
pub level: SymbolExportLevel,
3737
pub kind: SymbolExportKind,
38+
/// Was the symbol marked as `#[used(compiler)]` or `#[used(linker)]`?
3839
pub used: bool,
40+
/// Was the symbol marked as `#[rustc_std_internal_symbol]`?
41+
pub rustc_std_internal_symbol: bool,
3942
}
4043

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

library/std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ backtrace = [
9393
'miniz_oxide/rustc-dep-of-std',
9494
]
9595

96-
panic-unwind = ["panic_unwind"]
96+
panic-unwind = ["dep:panic_unwind"]
9797
compiler-builtins-c = ["alloc/compiler-builtins-c"]
9898
compiler-builtins-mem = ["alloc/compiler-builtins-mem"]
9999
compiler-builtins-no-asm = ["alloc/compiler-builtins-no-asm"]

library/sysroot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ debug_typeid = ["std/debug_typeid"]
2626
llvm-libunwind = ["std/llvm-libunwind"]
2727
system-llvm-libunwind = ["std/system-llvm-libunwind"]
2828
optimize_for_size = ["std/optimize_for_size"]
29-
panic-unwind = ["std/panic_unwind"]
29+
panic-unwind = ["std/panic-unwind"]
3030
panic_immediate_abort = ["std/panic_immediate_abort"]
3131
profiler = ["dep:profiler_builtins"]
3232
std_detect_file_io = ["std/std_detect_file_io"]

src/tools/miri/cargo-miri/src/setup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn setup(
8383
SysrootConfig::NoStd
8484
} else {
8585
SysrootConfig::WithStd {
86-
std_features: ["panic_unwind", "backtrace"].into_iter().map(Into::into).collect(),
86+
std_features: ["panic-unwind", "backtrace"].into_iter().map(Into::into).collect(),
8787
}
8888
};
8989
let cargo_cmd = {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
295295
level: SymbolExportLevel::C,
296296
kind: SymbolExportKind::Text,
297297
used: false,
298+
rustc_std_internal_symbol: false,
298299
},
299300
))
300301
} else {

tests/ui/panic-runtime/auxiliary/depends.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)