Skip to main content

rustc_codegen_llvm/
allocator.rs

1use libc::c_uint;
2use rustc_ast::expand::allocator::{
3    AllocatorMethod, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, SpecialAllocatorMethod,
4    default_fn_name, global_fn_name,
5};
6use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
7use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
8use rustc_middle::ty::TyCtxt;
9use rustc_session::config::DebugInfo;
10use rustc_span::bug;
11use rustc_symbol_mangling::mangle_internal_symbol;
12
13use crate::attributes::llfn_attrs_from_instance;
14use crate::builder::SBuilder;
15use crate::context::SimpleCx;
16use crate::declare::declare_simple_fn;
17use crate::llvm::{self, FromGeneric, TRUE, Type};
18use crate::{attributes, debuginfo};
19
20pub(crate) unsafe fn codegen(
21    tcx: TyCtxt<'_>,
22    cx: SimpleCx<'_>,
23    module_name: &str,
24    methods: &[AllocatorMethod],
25) {
26    let usize = match tcx.sess.target.pointer_width {
27        16 => cx.type_i16(),
28        32 => cx.type_i32(),
29        64 => cx.type_i64(),
30        tws => ::rustc_span::macros::bug_impl(None,
    format_args!("Unsupported target word size for int: {0}", tws),
    Location::caller())bug!("Unsupported target word size for int: {}", tws),
31    };
32    let i8p = cx.type_ptr();
33
34    for method in methods {
35        let mut args = Vec::with_capacity(method.inputs.len());
36        for input in method.inputs.iter() {
37            match input.ty {
38                AllocatorTy::Layout => {
39                    args.push(usize); // size
40                    args.push(usize); // align
41                }
42                AllocatorTy::Ptr => args.push(i8p),
43                AllocatorTy::Usize => args.push(usize),
44
45                AllocatorTy::Never | AllocatorTy::ResultPtr | AllocatorTy::Unit => {
46                    { ::core::panicking::panic_fmt(format_args!("invalid allocator arg")); }panic!("invalid allocator arg")
47                }
48            }
49        }
50
51        let mut no_return = false;
52        let output = match method.output {
53            AllocatorTy::ResultPtr => Some(i8p),
54            AllocatorTy::Unit => None,
55            AllocatorTy::Never => {
56                no_return = true;
57                None
58            }
59
60            AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
61                { ::core::panicking::panic_fmt(format_args!("invalid allocator output")); }panic!("invalid allocator output")
62            }
63        };
64
65        let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
66        let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
67
68        let alloc_attr_flag = match method.special {
69            Some(SpecialAllocatorMethod::Alloc) => CodegenFnAttrFlags::ALLOCATOR,
70            Some(SpecialAllocatorMethod::Dealloc) => CodegenFnAttrFlags::DEALLOCATOR,
71            Some(SpecialAllocatorMethod::Realloc) => CodegenFnAttrFlags::REALLOCATOR,
72            Some(SpecialAllocatorMethod::AllocZeroed) => CodegenFnAttrFlags::ALLOCATOR_ZEROED,
73            None => CodegenFnAttrFlags::empty(),
74        };
75
76        let mut attrs = CodegenFnAttrs::new();
77        attrs.flags |= alloc_attr_flag;
78        create_wrapper_function(
79            tcx,
80            &cx,
81            &from_name,
82            Some(&to_name),
83            &args,
84            output,
85            no_return,
86            &attrs,
87        );
88    }
89
90    // __rust_no_alloc_shim_is_unstable_v2
91    create_wrapper_function(
92        tcx,
93        &cx,
94        &mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
95        None,
96        &[],
97        None,
98        false,
99        &CodegenFnAttrs::new(),
100    );
101
102    if tcx.sess.opts.debuginfo != DebugInfo::None {
103        let dbg_cx = debuginfo::CodegenUnitDebugContext::new(cx.llmod, tcx.sess);
104        debuginfo::metadata::build_compile_unit_di_node(tcx, module_name, &dbg_cx);
105        dbg_cx.finalize();
106    }
107}
108
109fn create_wrapper_function(
110    tcx: TyCtxt<'_>,
111    cx: &SimpleCx<'_>,
112    from_name: &str,
113    to_name: Option<&str>,
114    args: &[&Type],
115    output: Option<&Type>,
116    no_return: bool,
117    attrs: &CodegenFnAttrs,
118) {
119    let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void()));
120    let llfn = declare_simple_fn(
121        &cx,
122        from_name,
123        llvm::CallConv::CCallConv,
124        llvm::UnnamedAddr::Global,
125        llvm::Visibility::from_generic(tcx.sess.default_visibility()),
126        ty,
127    );
128
129    llfn_attrs_from_instance(cx, tcx, llfn, attrs, None, None);
130
131    let no_return = if no_return {
132        // -> ! DIFlagNoReturn
133        let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);
134        attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
135        Some(no_return)
136    } else {
137        None
138    };
139
140    if tcx.sess.target.is_like_gpu {
141        // Conservatively apply convergent to all functions in case they may call
142        // a convergent function. Rely on LLVM to optimize away the unnecessary
143        // convergent attributes.
144        attributes::apply_to_llfn(
145            llfn,
146            llvm::AttributePlace::Function,
147            &[llvm::AttributeKind::Convergent.create_attr(cx.llcx)],
148        );
149    }
150
151    let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
152    let mut bx = SBuilder::build(&cx, llbb);
153
154    if let Some(to_name) = to_name {
155        let callee = declare_simple_fn(
156            &cx,
157            to_name,
158            llvm::CallConv::CCallConv,
159            llvm::UnnamedAddr::Global,
160            llvm::Visibility::Hidden,
161            ty,
162        );
163        if let Some(no_return) = no_return {
164            // -> ! DIFlagNoReturn
165            attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
166        }
167        llvm::set_visibility(callee, llvm::Visibility::Hidden);
168
169        let args = args
170            .iter()
171            .enumerate()
172            .map(|(i, _)| llvm::get_param(llfn, i as c_uint))
173            .collect::<Vec<_>>();
174        let ret = bx.call(ty, callee, &args, None);
175        llvm::LLVMSetTailCall(ret, TRUE);
176        if output.is_some() {
177            bx.ret(ret);
178        } else {
179            bx.ret_void()
180        }
181    } else {
182        if !output.is_none() {
    ::core::panicking::panic("assertion failed: output.is_none()")
};assert!(output.is_none());
183        bx.ret_void()
184    }
185}