Skip to content

Commit d393932

Browse files
committed
rustc: Clean up allocator injection logic
This commit cleans up allocator injection logic found in the compiler around selecting the global allocator. It turns out that now that jemalloc is gone the compiler never actually injects anything! This means that basically everything around loading crates here and there can be easily pruned. This also removes the `exe_allocation_crate` option from custom target specs as it's no longer used by the compiler anywhere.
1 parent a88613c commit d393932

29 files changed

+17
-177
lines changed

src/liballoc_system/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
feature(integer_atomics, stdsimd)
2626
)]
2727
#![cfg_attr(any(unix, target_os = "cloudabi", target_os = "redox"), feature(libc))]
28-
#![rustc_alloc_kind = "lib"]
2928

3029
// The minimum alignment guaranteed by the architecture. This value is used to
3130
// add fast paths for low alignment values.

src/librustc/middle/dependency_format.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
6464
use hir::def_id::CrateNum;
6565

66-
use session;
6766
use session::config;
6867
use ty::TyCtxt;
6968
use middle::cstore::{self, DepKind};
@@ -224,7 +223,6 @@ fn calculate_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
224223
// quite yet, so do so here.
225224
activate_injected_dep(*sess.injected_panic_runtime.get(), &mut ret,
226225
&|cnum| tcx.is_panic_runtime(cnum));
227-
activate_injected_allocator(sess, &mut ret);
228226

229227
// When dylib B links to dylib A, then when using B we must also link to A.
230228
// It could be the case, however, that the rlib for A is present (hence we
@@ -303,7 +301,6 @@ fn attempt_static<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<DependencyLis
303301
// that here and activate them.
304302
activate_injected_dep(*sess.injected_panic_runtime.get(), &mut ret,
305303
&|cnum| tcx.is_panic_runtime(cnum));
306-
activate_injected_allocator(sess, &mut ret);
307304

308305
Some(ret)
309306
}
@@ -336,18 +333,6 @@ fn activate_injected_dep(injected: Option<CrateNum>,
336333
}
337334
}
338335

339-
fn activate_injected_allocator(sess: &session::Session,
340-
list: &mut DependencyList) {
341-
let cnum = match sess.injected_allocator.get() {
342-
Some(cnum) => cnum,
343-
None => return,
344-
};
345-
let idx = cnum.as_usize() - 1;
346-
if list[idx] == Linkage::NotLinked {
347-
list[idx] = Linkage::Static;
348-
}
349-
}
350-
351336
// After the linkage for a crate has been determined we need to verify that
352337
// there's only going to be one allocator in the output.
353338
fn verify_ok<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, list: &[Linkage]) {

src/librustc/session/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ pub struct Session {
112112
/// The metadata::creader module may inject an allocator/panic_runtime
113113
/// dependency if it didn't already find one, and this tracks what was
114114
/// injected.
115-
pub injected_allocator: Once<Option<CrateNum>>,
116115
pub allocator_kind: Once<Option<AllocatorKind>>,
117116
pub injected_panic_runtime: Once<Option<CrateNum>>,
118117

@@ -1162,7 +1161,6 @@ pub fn build_session_(
11621161
type_length_limit: Once::new(),
11631162
const_eval_stack_frame_limit: 100,
11641163
next_node_id: OneThread::new(Cell::new(NodeId::new(1))),
1165-
injected_allocator: Once::new(),
11661164
allocator_kind: Once::new(),
11671165
injected_panic_runtime: Once::new(),
11681166
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),

src/librustc_metadata/creader.rs

Lines changed: 17 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -864,28 +864,25 @@ impl<'a> CrateLoader<'a> {
864864
needs_allocator = needs_allocator || data.root.needs_allocator;
865865
});
866866
if !needs_allocator {
867-
self.sess.injected_allocator.set(None);
868867
self.sess.allocator_kind.set(None);
869868
return
870869
}
871870

872871
// At this point we've determined that we need an allocator. Let's see
873872
// if our compilation session actually needs an allocator based on what
874873
// we're emitting.
875-
let mut need_lib_alloc = false;
876-
let mut need_exe_alloc = false;
874+
let mut all_rlib = true;
877875
for ct in self.sess.crate_types.borrow().iter() {
878876
match *ct {
879-
config::CrateType::Executable => need_exe_alloc = true,
877+
config::CrateType::Executable |
880878
config::CrateType::Dylib |
881879
config::CrateType::ProcMacro |
882880
config::CrateType::Cdylib |
883-
config::CrateType::Staticlib => need_lib_alloc = true,
881+
config::CrateType::Staticlib => all_rlib = false,
884882
config::CrateType::Rlib => {}
885883
}
886884
}
887-
if !need_lib_alloc && !need_exe_alloc {
888-
self.sess.injected_allocator.set(None);
885+
if all_rlib {
889886
self.sess.allocator_kind.set(None);
890887
return
891888
}
@@ -924,103 +921,27 @@ impl<'a> CrateLoader<'a> {
924921
});
925922
if global_allocator.is_some() {
926923
self.sess.allocator_kind.set(Some(AllocatorKind::Global));
927-
self.sess.injected_allocator.set(None);
928924
return
929925
}
930926

931927
// Ok we haven't found a global allocator but we still need an
932-
// allocator. At this point we'll either fall back to the "library
933-
// allocator" or the "exe allocator" depending on a few variables. Let's
934-
// figure out which one.
935-
//
936-
// Note that here we favor linking to the "library allocator" as much as
937-
// possible. If we're not creating rustc's version of libstd
938-
// (need_lib_alloc and prefer_dynamic) then we select `None`, and if the
939-
// exe allocation crate doesn't exist for this target then we also
940-
// select `None`.
941-
let exe_allocation_crate_data =
942-
if need_lib_alloc && !self.sess.opts.cg.prefer_dynamic {
943-
None
944-
} else {
945-
self.sess
946-
.target
947-
.target
948-
.options
949-
.exe_allocation_crate
950-
.as_ref()
951-
.map(|name| {
952-
// We've determined that we're injecting an "exe allocator" which means
953-
// that we're going to load up a whole new crate. An example of this is
954-
// that we're producing a normal binary on Linux which means we need to
955-
// load the `alloc_jemalloc` crate to link as an allocator.
956-
let name = Symbol::intern(name);
957-
let (cnum, data) = self.resolve_crate(&None,
958-
name,
959-
name,
960-
None,
961-
None,
962-
DUMMY_SP,
963-
PathKind::Crate,
964-
DepKind::Implicit)
965-
.unwrap_or_else(|err| err.report());
966-
self.sess.injected_allocator.set(Some(cnum));
967-
data
968-
})
969-
};
970-
971-
let allocation_crate_data = exe_allocation_crate_data.or_else(|| {
972-
// No allocator was injected
973-
self.sess.injected_allocator.set(None);
974-
975-
if attr::contains_name(&krate.attrs, "default_lib_allocator") {
976-
// Prefer self as the allocator if there's a collision
977-
return None;
928+
// allocator. At this point our allocator request is typically fulfilled
929+
// by the standard library, denoted by the `#![default_lib_allocator]`
930+
// attribute.
931+
let mut has_default = attr::contains_name(&krate.attrs, "default_lib_allocator");
932+
self.cstore.iter_crate_data(|_, data| {
933+
if data.root.has_default_lib_allocator {
934+
has_default = true;
978935
}
979-
// We're not actually going to inject an allocator, we're going to
980-
// require that something in our crate graph is the default lib
981-
// allocator. This is typically libstd, so this'll rarely be an
982-
// error.
983-
let mut allocator = None;
984-
self.cstore.iter_crate_data(|_, data| {
985-
if allocator.is_none() && data.root.has_default_lib_allocator {
986-
allocator = Some(data.clone());
987-
}
988-
});
989-
allocator
990936
});
991937

992-
match allocation_crate_data {
993-
Some(data) => {
994-
// We have an allocator. We detect separately what kind it is, to allow for some
995-
// flexibility in misconfiguration.
996-
let attrs = data.get_item_attrs(CRATE_DEF_INDEX, self.sess);
997-
let kind_interned = attr::first_attr_value_str_by_name(&attrs, "rustc_alloc_kind")
998-
.map(Symbol::as_str);
999-
let kind_str = kind_interned
1000-
.as_ref()
1001-
.map(|s| s as &str);
1002-
let alloc_kind = match kind_str {
1003-
None |
1004-
Some("lib") => AllocatorKind::DefaultLib,
1005-
Some("exe") => AllocatorKind::DefaultExe,
1006-
Some(other) => {
1007-
self.sess.err(&format!("Allocator kind {} not known", other));
1008-
return;
1009-
}
1010-
};
1011-
self.sess.allocator_kind.set(Some(alloc_kind));
1012-
},
1013-
None => {
1014-
if !attr::contains_name(&krate.attrs, "default_lib_allocator") {
1015-
self.sess.err("no global memory allocator found but one is \
1016-
required; link to std or \
1017-
add #[global_allocator] to a static item \
1018-
that implements the GlobalAlloc trait.");
1019-
return;
1020-
}
1021-
self.sess.allocator_kind.set(Some(AllocatorKind::DefaultLib));
1022-
}
938+
if !has_default {
939+
self.sess.err("no global memory allocator found but one is \
940+
required; link to std or \
941+
add #[global_allocator] to a static item \
942+
that implements the GlobalAlloc trait.");
1023943
}
944+
self.sess.allocator_kind.set(Some(AllocatorKind::DefaultLib));
1024945

1025946
fn has_global_allocator(krate: &ast::Crate) -> bool {
1026947
struct Finder(bool);

src/librustc_target/spec/aarch64_unknown_freebsd.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ pub fn target() -> TargetResult {
1414
let mut base = super::freebsd_base::opts();
1515
base.max_atomic_width = Some(128);
1616

17-
// see #36994
18-
base.exe_allocation_crate = None;
19-
2017
Ok(Target {
2118
llvm_target: "aarch64-unknown-freebsd".to_string(),
2219
target_endian: "little".to_string(),

src/librustc_target/spec/aarch64_unknown_linux_gnu.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ pub fn target() -> TargetResult {
1414
let mut base = super::linux_base::opts();
1515
base.max_atomic_width = Some(128);
1616

17-
// see #36994
18-
base.exe_allocation_crate = None;
19-
2017
Ok(Target {
2118
llvm_target: "aarch64-unknown-linux-gnu".to_string(),
2219
target_endian: "little".to_string(),

src/librustc_target/spec/aarch64_unknown_linux_musl.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ pub fn target() -> TargetResult {
1414
let mut base = super::linux_musl_base::opts();
1515
base.max_atomic_width = Some(128);
1616

17-
// see #36994
18-
base.exe_allocation_crate = None;
19-
2017
Ok(Target {
2118
llvm_target: "aarch64-unknown-linux-musl".to_string(),
2219
target_endian: "little".to_string(),

src/librustc_target/spec/hermit_base.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub fn opts() -> TargetOptions {
2121
]);
2222

2323
TargetOptions {
24-
exe_allocation_crate: None,
2524
executables: true,
2625
has_elf_tls: true,
2726
linker_is_gnu: true,

src/librustc_target/spec/l4re_base.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub fn opts() -> TargetOptions {
3030
TargetOptions {
3131
executables: true,
3232
has_elf_tls: false,
33-
exe_allocation_crate: None,
3433
panic_strategy: PanicStrategy::Abort,
3534
linker: Some("ld".to_string()),
3635
pre_link_args: args,

src/librustc_target/spec/mips64_unknown_linux_gnuabi64.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ pub fn target() -> TargetResult {
2828
features: "+mips64r2".to_string(),
2929
max_atomic_width: Some(64),
3030

31-
// see #36994
32-
exe_allocation_crate: None,
33-
3431
..super::linux_base::opts()
3532
},
3633
})

src/librustc_target/spec/mips64el_unknown_linux_gnuabi64.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ pub fn target() -> TargetResult {
2828
features: "+mips64r2".to_string(),
2929
max_atomic_width: Some(64),
3030

31-
// see #36994
32-
exe_allocation_crate: None,
33-
3431
..super::linux_base::opts()
3532
},
3633
})

src/librustc_target/spec/mips_unknown_linux_gnu.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ pub fn target() -> TargetResult {
2727
features: "+mips32r2,+fpxx,+nooddspreg".to_string(),
2828
max_atomic_width: Some(32),
2929

30-
// see #36994
31-
exe_allocation_crate: None,
32-
3330
..super::linux_base::opts()
3431
},
3532
})

src/librustc_target/spec/mips_unknown_linux_musl.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ pub fn target() -> TargetResult {
1515
base.cpu = "mips32r2".to_string();
1616
base.features = "+mips32r2,+soft-float".to_string();
1717
base.max_atomic_width = Some(32);
18-
// see #36994
19-
base.exe_allocation_crate = None;
2018
base.crt_static_default = false;
2119
Ok(Target {
2220
llvm_target: "mips-unknown-linux-musl".to_string(),

src/librustc_target/spec/mips_unknown_linux_uclibc.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ pub fn target() -> TargetResult {
2727
features: "+mips32r2,+soft-float".to_string(),
2828
max_atomic_width: Some(32),
2929

30-
// see #36994
31-
exe_allocation_crate: None,
32-
3330
..super::linux_base::opts()
3431
},
3532
})

src/librustc_target/spec/mipsel_unknown_linux_gnu.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ pub fn target() -> TargetResult {
2828
features: "+mips32r2,+fpxx,+nooddspreg".to_string(),
2929
max_atomic_width: Some(32),
3030

31-
// see #36994
32-
exe_allocation_crate: None,
33-
3431
..super::linux_base::opts()
3532
},
3633
})

src/librustc_target/spec/mipsel_unknown_linux_musl.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ pub fn target() -> TargetResult {
1515
base.cpu = "mips32r2".to_string();
1616
base.features = "+mips32r2,+soft-float".to_string();
1717
base.max_atomic_width = Some(32);
18-
// see #36994
19-
base.exe_allocation_crate = None;
2018
base.crt_static_default = false;
2119
Ok(Target {
2220
llvm_target: "mipsel-unknown-linux-musl".to_string(),

src/librustc_target/spec/mipsel_unknown_linux_uclibc.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ pub fn target() -> TargetResult {
2828
features: "+mips32r2,+soft-float".to_string(),
2929
max_atomic_width: Some(32),
3030

31-
// see #36994
32-
exe_allocation_crate: None,
33-
3431
..super::linux_base::opts()
3532
},
3633
})

src/librustc_target/spec/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,6 @@ pub struct TargetOptions {
596596
/// `eh_unwind_resume` lang item.
597597
pub custom_unwind_resume: bool,
598598

599-
/// If necessary, a different crate to link exe allocators by default
600-
pub exe_allocation_crate: Option<String>,
601-
602599
/// Flag indicating whether ELF TLS (e.g. #[thread_local]) is available for
603600
/// this target.
604601
pub has_elf_tls: bool,
@@ -740,7 +737,6 @@ impl Default for TargetOptions {
740737
link_env: Vec::new(),
741738
archive_format: "gnu".to_string(),
742739
custom_unwind_resume: false,
743-
exe_allocation_crate: None,
744740
allow_asm: true,
745741
has_elf_tls: false,
746742
obj_is_bitcode: false,
@@ -1025,7 +1021,6 @@ impl Target {
10251021
key!(archive_format);
10261022
key!(allow_asm, bool);
10271023
key!(custom_unwind_resume, bool);
1028-
key!(exe_allocation_crate, optional);
10291024
key!(has_elf_tls, bool);
10301025
key!(obj_is_bitcode, bool);
10311026
key!(no_integrated_as, bool);
@@ -1235,7 +1230,6 @@ impl ToJson for Target {
12351230
target_option_val!(archive_format);
12361231
target_option_val!(allow_asm);
12371232
target_option_val!(custom_unwind_resume);
1238-
target_option_val!(exe_allocation_crate);
12391233
target_option_val!(has_elf_tls);
12401234
target_option_val!(obj_is_bitcode);
12411235
target_option_val!(no_integrated_as);

src/librustc_target/spec/powerpc64_unknown_linux_gnu.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ pub fn target() -> TargetResult {
2020
// for now. https://github.com/rust-lang/rust/pull/43170#issuecomment-315411474
2121
base.relro_level = RelroLevel::Partial;
2222

23-
// see #36994
24-
base.exe_allocation_crate = None;
25-
2623
Ok(Target {
2724
llvm_target: "powerpc64-unknown-linux-gnu".to_string(),
2825
target_endian: "big".to_string(),

src/librustc_target/spec/powerpc64le_unknown_linux_gnu.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ pub fn target() -> TargetResult {
1616
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
1717
base.max_atomic_width = Some(64);
1818

19-
// see #36994
20-
base.exe_allocation_crate = None;
21-
2219
Ok(Target {
2320
llvm_target: "powerpc64le-unknown-linux-gnu".to_string(),
2421
target_endian: "little".to_string(),

0 commit comments

Comments
 (0)