Skip to content

Commit a3c1f42

Browse files
committed
Auto merge of #86844 - bjorn3:global_alloc_improvements, r=pnkfelix
Support #[global_allocator] without the allocator shim This makes it possible to use liballoc/libstd in combination with `--emit obj` if you use `#[global_allocator]`. This is what rust-for-linux uses right now and systemd may use in the future. Currently they have to depend on the exact implementation of the allocator shim to create one themself as `--emit obj` doesn't create an allocator shim. Note that currently the allocator shim also defines the oom error handler, which is normally required too. Once `#![feature(default_alloc_error_handler)]` becomes the only option, this can be avoided. In addition when using only fallible allocator methods and either `--cfg no_global_oom_handling` for liballoc (like rust-for-linux) or `--gc-sections` no references to the oom error handler will exist. To avoid this feature being insta-stable, you will have to define `__rust_no_alloc_shim_is_unstable` to avoid linker errors. (Labeling this with both T-compiler and T-lang as it originally involved both an implementation detail and had an insta-stable user facing change. As noted above, the `__rust_no_alloc_shim_is_unstable` symbol requirement should prevent unintended dependence on this unstable feature.)
2 parents e5b5842 + da466a2 commit a3c1f42

File tree

1 file changed

+67
-58
lines changed

1 file changed

+67
-58
lines changed

src/allocator.rs

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#[cfg(feature="master")]
22
use gccjit::FnAttribute;
33
use gccjit::{FunctionType, GlobalKind, ToRValue};
4-
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
4+
use rustc_ast::expand::allocator::{
5+
alloc_error_handler_name, default_fn_name, global_fn_name, AllocatorKind, AllocatorTy,
6+
ALLOCATOR_METHODS, NO_ALLOC_SHIM_IS_UNSTABLE,
7+
};
58
use rustc_middle::bug;
69
use rustc_middle::ty::TyCtxt;
710
use rustc_session::config::OomStrategy;
8-
use rustc_span::symbol::sym;
911

1012
use crate::GccContext;
1113

@@ -22,69 +24,71 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
2224
let i8p = i8.make_pointer();
2325
let void = context.new_type::<()>();
2426

25-
for method in ALLOCATOR_METHODS {
26-
let mut types = Vec::with_capacity(method.inputs.len());
27-
for ty in method.inputs.iter() {
28-
match *ty {
29-
AllocatorTy::Layout => {
30-
types.push(usize);
31-
types.push(usize);
27+
if kind == AllocatorKind::Default {
28+
for method in ALLOCATOR_METHODS {
29+
let mut types = Vec::with_capacity(method.inputs.len());
30+
for ty in method.inputs.iter() {
31+
match *ty {
32+
AllocatorTy::Layout => {
33+
types.push(usize);
34+
types.push(usize);
35+
}
36+
AllocatorTy::Ptr => types.push(i8p),
37+
AllocatorTy::Usize => types.push(usize),
38+
39+
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
3240
}
33-
AllocatorTy::Ptr => types.push(i8p),
34-
AllocatorTy::Usize => types.push(usize),
35-
36-
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
3741
}
38-
}
39-
let output = match method.output {
40-
AllocatorTy::ResultPtr => Some(i8p),
41-
AllocatorTy::Unit => None,
42+
let output = match method.output {
43+
AllocatorTy::ResultPtr => Some(i8p),
44+
AllocatorTy::Unit => None,
4245

43-
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
44-
panic!("invalid allocator output")
45-
}
46-
};
47-
let name = format!("__rust_{}", method.name);
46+
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
47+
panic!("invalid allocator output")
48+
}
49+
};
50+
let name = global_fn_name(method.name);
4851

49-
let args: Vec<_> = types.iter().enumerate()
50-
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
51-
.collect();
52-
let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, name, false);
52+
let args: Vec<_> = types.iter().enumerate()
53+
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
54+
.collect();
55+
let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, name, false);
5356

54-
if tcx.sess.target.options.default_hidden_visibility {
57+
if tcx.sess.target.options.default_hidden_visibility {
58+
#[cfg(feature="master")]
59+
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
60+
}
61+
if tcx.sess.must_emit_unwind_tables() {
62+
// TODO(antoyo): emit unwind tables.
63+
}
64+
65+
let callee = default_fn_name(method.name);
66+
let args: Vec<_> = types.iter().enumerate()
67+
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
68+
.collect();
69+
let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, callee, false);
5570
#[cfg(feature="master")]
56-
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
57-
}
58-
if tcx.sess.must_emit_unwind_tables() {
59-
// TODO(antoyo): emit unwind tables.
60-
}
71+
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
72+
73+
let block = func.new_block("entry");
74+
75+
let args = args
76+
.iter()
77+
.enumerate()
78+
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
79+
.collect::<Vec<_>>();
80+
let ret = context.new_call(None, callee, &args);
81+
//llvm::LLVMSetTailCall(ret, True);
82+
if output.is_some() {
83+
block.end_with_return(None, ret);
84+
}
85+
else {
86+
block.end_with_void_return(None);
87+
}
6188

62-
let callee = kind.fn_name(method.name);
63-
let args: Vec<_> = types.iter().enumerate()
64-
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
65-
.collect();
66-
let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, callee, false);
67-
#[cfg(feature="master")]
68-
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
69-
70-
let block = func.new_block("entry");
71-
72-
let args = args
73-
.iter()
74-
.enumerate()
75-
.map(|(i, _)| func.get_param(i as i32).to_rvalue())
76-
.collect::<Vec<_>>();
77-
let ret = context.new_call(None, callee, &args);
78-
//llvm::LLVMSetTailCall(ret, True);
79-
if output.is_some() {
80-
block.end_with_return(None, ret);
81-
}
82-
else {
83-
block.end_with_void_return(None);
89+
// TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
90+
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
8491
}
85-
86-
// TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
87-
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
8892
}
8993

9094
let types = [usize, usize];
@@ -99,7 +103,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
99103
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
100104
}
101105

102-
let callee = alloc_error_handler_kind.fn_name(sym::oom);
106+
let callee = alloc_error_handler_name(alloc_error_handler_kind);
103107
let args: Vec<_> = types.iter().enumerate()
104108
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
105109
.collect();
@@ -123,4 +127,9 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
123127
let value = tcx.sess.opts.unstable_opts.oom.should_panic();
124128
let value = context.new_rvalue_from_int(i8, value as i32);
125129
global.global_set_initializer_rvalue(value);
130+
131+
let name = NO_ALLOC_SHIM_IS_UNSTABLE.to_string();
132+
let global = context.new_global(None, GlobalKind::Exported, i8, name);
133+
let value = context.new_rvalue_from_int(i8, 0);
134+
global.global_set_initializer_rvalue(value);
126135
}

0 commit comments

Comments
 (0)