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::bug;
8use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
9use rustc_middle::ty::TyCtxt;
10use rustc_session::config::{DebugInfo, OomStrategy};
11use rustc_symbol_mangling::mangle_internal_symbol;
12
13use crate::attributes::llfn_attrs_from_instance;
14use crate::builder::SBuilder;
15use crate::declare::declare_simple_fn;
16use crate::llvm::{self, FALSE, FromGeneric, TRUE, Type, Value};
17use crate::{SimpleCx, attributes, debuginfo};
18
19pub(crate) unsafe fn codegen(
20    tcx: TyCtxt<'_>,
21    cx: SimpleCx<'_>,
22    module_name: &str,
23    methods: &[AllocatorMethod],
24) {
25    let usize = match tcx.sess.target.pointer_width {
26        16 => cx.type_i16(),
27        32 => cx.type_i32(),
28        64 => cx.type_i64(),
29        tws => bug!("Unsupported target word size for int: {}", tws),
30    };
31    let i8 = cx.type_i8();
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                    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                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_alloc_error_handler_should_panic_v2
91    create_const_value_function(
92        tcx,
93        &cx,
94        &mangle_internal_symbol(tcx, OomStrategy::SYMBOL),
95        &i8,
96        unsafe {
97            llvm::LLVMConstInt(i8, tcx.sess.opts.unstable_opts.oom.should_panic() as u64, FALSE)
98        },
99    );
100
101    // __rust_no_alloc_shim_is_unstable_v2
102    create_wrapper_function(
103        tcx,
104        &cx,
105        &mangle_internal_symbol(tcx, NO_ALLOC_SHIM_IS_UNSTABLE),
106        None,
107        &[],
108        None,
109        false,
110        &CodegenFnAttrs::new(),
111    );
112
113    if tcx.sess.opts.debuginfo != DebugInfo::None {
114        let dbg_cx = debuginfo::CodegenUnitDebugContext::new(cx.llmod);
115        debuginfo::metadata::build_compile_unit_di_node(tcx, module_name, &dbg_cx);
116        dbg_cx.finalize(tcx.sess);
117    }
118}
119
120fn create_const_value_function(
121    tcx: TyCtxt<'_>,
122    cx: &SimpleCx<'_>,
123    name: &str,
124    output: &Type,
125    value: &Value,
126) {
127    let ty = cx.type_func(&[], output);
128    let llfn = declare_simple_fn(
129        &cx,
130        name,
131        llvm::CallConv::CCallConv,
132        llvm::UnnamedAddr::Global,
133        llvm::Visibility::from_generic(tcx.sess.default_visibility()),
134        ty,
135    );
136
137    attributes::apply_to_llfn(
138        llfn,
139        llvm::AttributePlace::Function,
140        &[llvm::AttributeKind::AlwaysInline.create_attr(cx.llcx)],
141    );
142
143    let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
144    let mut bx = SBuilder::build(&cx, llbb);
145    bx.ret(value);
146}
147
148fn create_wrapper_function(
149    tcx: TyCtxt<'_>,
150    cx: &SimpleCx<'_>,
151    from_name: &str,
152    to_name: Option<&str>,
153    args: &[&Type],
154    output: Option<&Type>,
155    no_return: bool,
156    attrs: &CodegenFnAttrs,
157) {
158    let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void()));
159    let llfn = declare_simple_fn(
160        &cx,
161        from_name,
162        llvm::CallConv::CCallConv,
163        llvm::UnnamedAddr::Global,
164        llvm::Visibility::from_generic(tcx.sess.default_visibility()),
165        ty,
166    );
167
168    llfn_attrs_from_instance(cx, tcx, llfn, attrs, None);
169
170    let no_return = if no_return {
171        // -> ! DIFlagNoReturn
172        let no_return = llvm::AttributeKind::NoReturn.create_attr(cx.llcx);
173        attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &[no_return]);
174        Some(no_return)
175    } else {
176        None
177    };
178
179    let llbb = unsafe { llvm::LLVMAppendBasicBlockInContext(cx.llcx, llfn, c"entry".as_ptr()) };
180    let mut bx = SBuilder::build(&cx, llbb);
181
182    if let Some(to_name) = to_name {
183        let callee = declare_simple_fn(
184            &cx,
185            to_name,
186            llvm::CallConv::CCallConv,
187            llvm::UnnamedAddr::Global,
188            llvm::Visibility::Hidden,
189            ty,
190        );
191        if let Some(no_return) = no_return {
192            // -> ! DIFlagNoReturn
193            attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
194        }
195        llvm::set_visibility(callee, llvm::Visibility::Hidden);
196
197        let args = args
198            .iter()
199            .enumerate()
200            .map(|(i, _)| llvm::get_param(llfn, i as c_uint))
201            .collect::<Vec<_>>();
202        let ret = bx.call(ty, callee, &args, None);
203        llvm::LLVMSetTailCall(ret, TRUE);
204        if output.is_some() {
205            bx.ret(ret);
206        } else {
207            bx.ret_void()
208        }
209    } else {
210        assert!(output.is_none());
211        bx.ret_void()
212    }
213}