Don't use an allocator shim for #[global_allocator]

This makes it possible to use liballoc/libstd in combination with
`--emit obj` if you use `#[global_allocator]`. Making it work for the
default libstd allocator would require weak functions, which are not
well supported on all systems.
This commit is contained in:
bjorn3 2021-07-03 16:46:41 +02:00 committed by bjorn3
parent 79fa6ce7a1
commit 4ce20663f7
4 changed files with 176 additions and 159 deletions

View File

@ -1,6 +1,6 @@
use rustc_span::symbol::{sym, Symbol};
#[derive(Clone, Debug, Copy, HashStable_Generic)]
#[derive(Clone, Debug, Copy, Eq, PartialEq, HashStable_Generic)]
pub enum AllocatorKind {
Global,
Default,
@ -9,12 +9,19 @@ pub enum AllocatorKind {
impl AllocatorKind {
pub fn fn_name(&self, base: Symbol) -> String {
match *self {
AllocatorKind::Global => format!("__rg_{base}"),
AllocatorKind::Global => format!("__rust_{base}"),
AllocatorKind::Default => format!("__rdl_{base}"),
}
}
}
pub fn alloc_error_handler_name(alloc_error_handler_kind: AllocatorKind) -> &'static str {
match alloc_error_handler_kind {
AllocatorKind::Global => "__rg_oom",
AllocatorKind::Default => "__rdl_oom",
}
}
pub enum AllocatorTy {
Layout,
Ptr,

View File

@ -3,10 +3,11 @@
use crate::prelude::*;
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
use rustc_ast::expand::allocator::{
alloc_error_handler_name, AllocatorKind, AllocatorTy, ALLOCATOR_METHODS,
};
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
use rustc_session::config::OomStrategy;
use rustc_span::symbol::sym;
/// Returns whether an allocator shim was created
pub(crate) fn codegen(
@ -34,6 +35,7 @@ fn codegen_inner(
) {
let usize_ty = module.target_config().pointer_type();
if kind == AllocatorKind::Default {
for method in ALLOCATOR_METHODS {
let mut arg_tys = Vec::with_capacity(method.inputs.len());
for ty in method.inputs.iter() {
@ -67,9 +69,10 @@ fn codegen_inner(
unwind_context,
sig,
&format!("__rust_{}", method.name),
&kind.fn_name(method.name),
&AllocatorKind::Default.fn_name(method.name),
);
}
}
let sig = Signature {
call_conv: module.target_config().default_call_conv,
@ -81,7 +84,7 @@ fn codegen_inner(
unwind_context,
sig,
"__rust_alloc_error_handler",
&alloc_error_handler_kind.fn_name(sym::oom),
&alloc_error_handler_name(alloc_error_handler_kind),
);
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();

View File

@ -1,11 +1,12 @@
#[cfg(feature="master")]
use gccjit::FnAttribute;
use gccjit::{FunctionType, GlobalKind, ToRValue};
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
use rustc_ast::expand::allocator::{
alloc_error_handler_name, AllocatorKind, AllocatorTy, ALLOCATOR_METHODS,
};
use rustc_middle::bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::OomStrategy;
use rustc_span::symbol::sym;
use crate::GccContext;
@ -22,6 +23,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
let i8p = i8.make_pointer();
let void = context.new_type::<()>();
if kind == AllocatorKind::Default {
for method in ALLOCATOR_METHODS {
let mut types = Vec::with_capacity(method.inputs.len());
for ty in method.inputs.iter() {
@ -59,7 +61,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
// TODO(antoyo): emit unwind tables.
}
let callee = kind.fn_name(method.name);
let callee = AllocatorKind::Default.fn_name(method.name);
let args: Vec<_> = types.iter().enumerate()
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
.collect();
@ -86,6 +88,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
// TODO(@Commeownist): Check if we need to emit some extra debugging info in certain circumstances
// as described in https://github.com/rust-lang/rust/commit/77a96ed5646f7c3ee8897693decc4626fe380643
}
}
let types = [usize, usize];
let name = "__rust_alloc_error_handler".to_string();
@ -99,7 +102,7 @@ pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_nam
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
}
let callee = alloc_error_handler_kind.fn_name(sym::oom);
let callee = alloc_error_handler_name(alloc_error_handler_kind);
let args: Vec<_> = types.iter().enumerate()
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
.collect();

View File

@ -1,10 +1,11 @@
use crate::attributes;
use libc::c_uint;
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
use rustc_ast::expand::allocator::{
alloc_error_handler_name, AllocatorKind, AllocatorTy, ALLOCATOR_METHODS,
};
use rustc_middle::bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{DebugInfo, OomStrategy};
use rustc_span::symbol::sym;
use crate::debuginfo;
use crate::llvm::{self, False, True};
@ -29,6 +30,7 @@ pub(crate) unsafe fn codegen(
let i8p = llvm::LLVMPointerType(i8, 0);
let void = llvm::LLVMVoidTypeInContext(llcx);
if kind == AllocatorKind::Default {
for method in ALLOCATOR_METHODS {
let mut args = Vec::with_capacity(method.inputs.len());
for ty in method.inputs.iter() {
@ -58,7 +60,8 @@ pub(crate) unsafe fn codegen(
False,
);
let name = format!("__rust_{}", method.name);
let llfn = llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
let llfn =
llvm::LLVMRustGetOrInsertFunction(llmod, name.as_ptr().cast(), name.len(), ty);
if tcx.sess.target.default_hidden_visibility {
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);
@ -68,7 +71,7 @@ pub(crate) unsafe fn codegen(
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
}
let callee = kind.fn_name(method.name);
let callee = AllocatorKind::Default.fn_name(method.name);
let callee =
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
@ -99,6 +102,7 @@ pub(crate) unsafe fn codegen(
}
llvm::LLVMDisposeBuilder(llbuilder);
}
}
// rust alloc error handler
let args = [usize, usize]; // size, align
@ -118,7 +122,7 @@ pub(crate) unsafe fn codegen(
attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[uwtable]);
}
let callee = alloc_error_handler_kind.fn_name(sym::oom);
let callee = alloc_error_handler_name(alloc_error_handler_kind);
let callee = llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
// -> ! DIFlagNoReturn
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);