Skip to content

Commit 4629f31

Browse files
authored
Rollup merge of rust-lang#147526 - bjorn3:alloc_shim_weak_shape, r=petrochenkov,RalfJung
Move computation of allocator shim contents to cg_ssa In the future this should make it easier to use weak symbols for the allocator shim on platforms that properly support weak symbols. And it would allow reusing the allocator shim code for handling default implementations of the upcoming externally implementable items feature on platforms that don't properly support weak symbols. In addition to make this possible, the alloc error handler is now handled in a way such that it is possible to avoid using the allocator shim when liballoc is compiled without `no_global_oom_handling` if you use `#[alloc_error_handler]`. Previously this was only possible if you avoided liballoc entirely or compiled it with `no_global_oom_handling`. You still need to avoid libstd and to define the symbol that indicates that avoiding the allocator shim is unstable.
2 parents cb72965 + b453c19 commit 4629f31

File tree

1 file changed

+36
-55
lines changed

1 file changed

+36
-55
lines changed

src/allocator.rs

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
55
use rustc_ast::expand::allocator::{
6-
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
7-
alloc_error_handler_name, default_fn_name, global_fn_name,
6+
AllocatorMethod, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, default_fn_name, global_fn_name,
87
};
9-
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
8+
use rustc_codegen_ssa::base::{allocator_kind_for_codegen, allocator_shim_contents};
109
use rustc_session::config::OomStrategy;
1110
use rustc_symbol_mangling::mangle_internal_symbol;
1211

@@ -15,75 +14,57 @@ use crate::prelude::*;
1514
/// Returns whether an allocator shim was created
1615
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
1716
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
18-
codegen_inner(
19-
tcx,
20-
module,
21-
kind,
22-
tcx.alloc_error_handler_kind(()).unwrap(),
23-
tcx.sess.opts.unstable_opts.oom,
24-
);
17+
let methods = allocator_shim_contents(tcx, kind);
18+
codegen_inner(tcx, module, &methods, tcx.sess.opts.unstable_opts.oom);
2519
true
2620
}
2721

2822
fn codegen_inner(
2923
tcx: TyCtxt<'_>,
3024
module: &mut dyn Module,
31-
kind: AllocatorKind,
32-
alloc_error_handler_kind: AllocatorKind,
25+
methods: &[AllocatorMethod],
3326
oom_strategy: OomStrategy,
3427
) {
3528
let usize_ty = module.target_config().pointer_type();
3629

37-
if kind == AllocatorKind::Default {
38-
for method in ALLOCATOR_METHODS {
39-
let mut arg_tys = Vec::with_capacity(method.inputs.len());
40-
for input in method.inputs.iter() {
41-
match input.ty {
42-
AllocatorTy::Layout => {
43-
arg_tys.push(usize_ty); // size
44-
arg_tys.push(usize_ty); // align
45-
}
46-
AllocatorTy::Ptr => arg_tys.push(usize_ty),
47-
AllocatorTy::Usize => arg_tys.push(usize_ty),
30+
for method in methods {
31+
let mut arg_tys = Vec::with_capacity(method.inputs.len());
32+
for input in method.inputs.iter() {
33+
match input.ty {
34+
AllocatorTy::Layout => {
35+
arg_tys.push(usize_ty); // size
36+
arg_tys.push(usize_ty); // align
37+
}
38+
AllocatorTy::Ptr => arg_tys.push(usize_ty),
39+
AllocatorTy::Usize => arg_tys.push(usize_ty),
4840

49-
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
41+
AllocatorTy::Never | AllocatorTy::ResultPtr | AllocatorTy::Unit => {
42+
panic!("invalid allocator arg")
5043
}
5144
}
52-
let output = match method.output {
53-
AllocatorTy::ResultPtr => Some(usize_ty),
54-
AllocatorTy::Unit => None,
45+
}
46+
let output = match method.output {
47+
AllocatorTy::ResultPtr => Some(usize_ty),
48+
AllocatorTy::Never | AllocatorTy::Unit => None,
5549

56-
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
57-
panic!("invalid allocator output")
58-
}
59-
};
50+
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
51+
panic!("invalid allocator output")
52+
}
53+
};
6054

61-
let sig = Signature {
62-
call_conv: module.target_config().default_call_conv,
63-
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
64-
returns: output.into_iter().map(AbiParam::new).collect(),
65-
};
66-
crate::common::create_wrapper_function(
67-
module,
68-
sig,
69-
&mangle_internal_symbol(tcx, &global_fn_name(method.name)),
70-
&mangle_internal_symbol(tcx, &default_fn_name(method.name)),
71-
);
72-
}
55+
let sig = Signature {
56+
call_conv: module.target_config().default_call_conv,
57+
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
58+
returns: output.into_iter().map(AbiParam::new).collect(),
59+
};
60+
crate::common::create_wrapper_function(
61+
module,
62+
sig,
63+
&mangle_internal_symbol(tcx, &global_fn_name(method.name)),
64+
&mangle_internal_symbol(tcx, &default_fn_name(method.name)),
65+
);
7366
}
7467

75-
let sig = Signature {
76-
call_conv: module.target_config().default_call_conv,
77-
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
78-
returns: vec![],
79-
};
80-
crate::common::create_wrapper_function(
81-
module,
82-
sig,
83-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
84-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
85-
);
86-
8768
{
8869
let sig = Signature {
8970
call_conv: module.target_config().default_call_conv,

0 commit comments

Comments
 (0)