]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/abi.rs
Auto merge of #101514 - nvzqz:nvzqz/stabilize-nonzero-bits, r=thomcc
[rust.git] / compiler / rustc_codegen_llvm / src / abi.rs
1 use crate::attributes;
2 use crate::builder::Builder;
3 use crate::context::CodegenCx;
4 use crate::llvm::{self, Attribute, AttributePlace};
5 use crate::type_::Type;
6 use crate::type_of::LayoutLlvmExt;
7 use crate::value::Value;
8
9 use rustc_codegen_ssa::mir::operand::OperandValue;
10 use rustc_codegen_ssa::mir::place::PlaceRef;
11 use rustc_codegen_ssa::traits::*;
12 use rustc_codegen_ssa::MemFlags;
13 use rustc_middle::bug;
14 use rustc_middle::ty::layout::LayoutOf;
15 pub use rustc_middle::ty::layout::{FAT_PTR_ADDR, FAT_PTR_EXTRA};
16 use rustc_middle::ty::Ty;
17 use rustc_session::config;
18 use rustc_target::abi::call::ArgAbi;
19 pub use rustc_target::abi::call::*;
20 use rustc_target::abi::{self, HasDataLayout, Int};
21 pub use rustc_target::spec::abi::Abi;
22 use rustc_target::spec::SanitizerSet;
23
24 use libc::c_uint;
25 use smallvec::SmallVec;
26
27 pub trait ArgAttributesExt {
28     fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value);
29     fn apply_attrs_to_callsite(
30         &self,
31         idx: AttributePlace,
32         cx: &CodegenCx<'_, '_>,
33         callsite: &Value,
34     );
35 }
36
37 fn should_use_mutable_noalias(cx: &CodegenCx<'_, '_>) -> bool {
38     // LLVM prior to version 12 had known miscompiles in the presence of
39     // noalias attributes (see #54878), but we don't support earlier
40     // versions at all anymore. We now enable mutable noalias by default.
41     cx.tcx.sess.opts.unstable_opts.mutable_noalias.unwrap_or(true)
42 }
43
44 const ABI_AFFECTING_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 1] =
45     [(ArgAttribute::InReg, llvm::AttributeKind::InReg)];
46
47 const OPTIMIZATION_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 5] = [
48     (ArgAttribute::NoAlias, llvm::AttributeKind::NoAlias),
49     (ArgAttribute::NoCapture, llvm::AttributeKind::NoCapture),
50     (ArgAttribute::NonNull, llvm::AttributeKind::NonNull),
51     (ArgAttribute::ReadOnly, llvm::AttributeKind::ReadOnly),
52     (ArgAttribute::NoUndef, llvm::AttributeKind::NoUndef),
53 ];
54
55 fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'ll Attribute; 8]> {
56     let mut regular = this.regular;
57
58     let mut attrs = SmallVec::new();
59
60     // ABI-affecting attributes must always be applied
61     for (attr, llattr) in ABI_AFFECTING_ATTRIBUTES {
62         if regular.contains(attr) {
63             attrs.push(llattr.create_attr(cx.llcx));
64         }
65     }
66     if let Some(align) = this.pointee_align {
67         attrs.push(llvm::CreateAlignmentAttr(cx.llcx, align.bytes()));
68     }
69     match this.arg_ext {
70         ArgExtension::None => {}
71         ArgExtension::Zext => attrs.push(llvm::AttributeKind::ZExt.create_attr(cx.llcx)),
72         ArgExtension::Sext => attrs.push(llvm::AttributeKind::SExt.create_attr(cx.llcx)),
73     }
74
75     // Only apply remaining attributes when optimizing
76     if cx.sess().opts.optimize != config::OptLevel::No {
77         let deref = this.pointee_size.bytes();
78         if deref != 0 {
79             if regular.contains(ArgAttribute::NonNull) {
80                 attrs.push(llvm::CreateDereferenceableAttr(cx.llcx, deref));
81             } else {
82                 attrs.push(llvm::CreateDereferenceableOrNullAttr(cx.llcx, deref));
83             }
84             regular -= ArgAttribute::NonNull;
85         }
86         for (attr, llattr) in OPTIMIZATION_ATTRIBUTES {
87             if regular.contains(attr) {
88                 attrs.push(llattr.create_attr(cx.llcx));
89             }
90         }
91         if regular.contains(ArgAttribute::NoAliasMutRef) && should_use_mutable_noalias(cx) {
92             attrs.push(llvm::AttributeKind::NoAlias.create_attr(cx.llcx));
93         }
94     } else if cx.tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
95         // If we're not optimising, *but* memory sanitizer is on, emit noundef, since it affects
96         // memory sanitizer's behavior.
97
98         if regular.contains(ArgAttribute::NoUndef) {
99             attrs.push(llvm::AttributeKind::NoUndef.create_attr(cx.llcx));
100         }
101     }
102
103     attrs
104 }
105
106 impl ArgAttributesExt for ArgAttributes {
107     fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value) {
108         let attrs = get_attrs(self, cx);
109         attributes::apply_to_llfn(llfn, idx, &attrs);
110     }
111
112     fn apply_attrs_to_callsite(
113         &self,
114         idx: AttributePlace,
115         cx: &CodegenCx<'_, '_>,
116         callsite: &Value,
117     ) {
118         let attrs = get_attrs(self, cx);
119         attributes::apply_to_callsite(callsite, idx, &attrs);
120     }
121 }
122
123 pub trait LlvmType {
124     fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type;
125 }
126
127 impl LlvmType for Reg {
128     fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
129         match self.kind {
130             RegKind::Integer => cx.type_ix(self.size.bits()),
131             RegKind::Float => match self.size.bits() {
132                 32 => cx.type_f32(),
133                 64 => cx.type_f64(),
134                 _ => bug!("unsupported float: {:?}", self),
135             },
136             RegKind::Vector => cx.type_vector(cx.type_i8(), self.size.bytes()),
137         }
138     }
139 }
140
141 impl LlvmType for CastTarget {
142     fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
143         let rest_ll_unit = self.rest.unit.llvm_type(cx);
144         let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
145             (0, 0)
146         } else {
147             (
148                 self.rest.total.bytes() / self.rest.unit.size.bytes(),
149                 self.rest.total.bytes() % self.rest.unit.size.bytes(),
150             )
151         };
152
153         if self.prefix.iter().all(|x| x.is_none()) {
154             // Simplify to a single unit when there is no prefix and size <= unit size
155             if self.rest.total <= self.rest.unit.size {
156                 return rest_ll_unit;
157             }
158
159             // Simplify to array when all chunks are the same size and type
160             if rem_bytes == 0 {
161                 return cx.type_array(rest_ll_unit, rest_count);
162             }
163         }
164
165         // Create list of fields in the main structure
166         let mut args: Vec<_> = self
167             .prefix
168             .iter()
169             .flat_map(|option_reg| option_reg.map(|reg| reg.llvm_type(cx)))
170             .chain((0..rest_count).map(|_| rest_ll_unit))
171             .collect();
172
173         // Append final integer
174         if rem_bytes != 0 {
175             // Only integers can be really split further.
176             assert_eq!(self.rest.unit.kind, RegKind::Integer);
177             args.push(cx.type_ix(rem_bytes * 8));
178         }
179
180         cx.type_struct(&args, false)
181     }
182 }
183
184 pub trait ArgAbiExt<'ll, 'tcx> {
185     fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
186     fn store(
187         &self,
188         bx: &mut Builder<'_, 'll, 'tcx>,
189         val: &'ll Value,
190         dst: PlaceRef<'tcx, &'ll Value>,
191     );
192     fn store_fn_arg(
193         &self,
194         bx: &mut Builder<'_, 'll, 'tcx>,
195         idx: &mut usize,
196         dst: PlaceRef<'tcx, &'ll Value>,
197     );
198 }
199
200 impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
201     /// Gets the LLVM type for a place of the original Rust type of
202     /// this argument/return, i.e., the result of `type_of::type_of`.
203     fn memory_ty(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
204         self.layout.llvm_type(cx)
205     }
206
207     /// Stores a direct/indirect value described by this ArgAbi into a
208     /// place for the original Rust type of this argument/return.
209     /// Can be used for both storing formal arguments into Rust variables
210     /// or results of call/invoke instructions into their destinations.
211     fn store(
212         &self,
213         bx: &mut Builder<'_, 'll, 'tcx>,
214         val: &'ll Value,
215         dst: PlaceRef<'tcx, &'ll Value>,
216     ) {
217         if self.is_ignore() {
218             return;
219         }
220         if self.is_sized_indirect() {
221             OperandValue::Ref(val, None, self.layout.align.abi).store(bx, dst)
222         } else if self.is_unsized_indirect() {
223             bug!("unsized `ArgAbi` must be handled through `store_fn_arg`");
224         } else if let PassMode::Cast(cast, _) = &self.mode {
225             // FIXME(eddyb): Figure out when the simpler Store is safe, clang
226             // uses it for i16 -> {i8, i8}, but not for i24 -> {i8, i8, i8}.
227             let can_store_through_cast_ptr = false;
228             if can_store_through_cast_ptr {
229                 let cast_ptr_llty = bx.type_ptr_to(cast.llvm_type(bx));
230                 let cast_dst = bx.pointercast(dst.llval, cast_ptr_llty);
231                 bx.store(val, cast_dst, self.layout.align.abi);
232             } else {
233                 // The actual return type is a struct, but the ABI
234                 // adaptation code has cast it into some scalar type.  The
235                 // code that follows is the only reliable way I have
236                 // found to do a transform like i64 -> {i32,i32}.
237                 // Basically we dump the data onto the stack then memcpy it.
238                 //
239                 // Other approaches I tried:
240                 // - Casting rust ret pointer to the foreign type and using Store
241                 //   is (a) unsafe if size of foreign type > size of rust type and
242                 //   (b) runs afoul of strict aliasing rules, yielding invalid
243                 //   assembly under -O (specifically, the store gets removed).
244                 // - Truncating foreign type to correct integral type and then
245                 //   bitcasting to the struct type yields invalid cast errors.
246
247                 // We instead thus allocate some scratch space...
248                 let scratch_size = cast.size(bx);
249                 let scratch_align = cast.align(bx);
250                 let llscratch = bx.alloca(cast.llvm_type(bx), scratch_align);
251                 bx.lifetime_start(llscratch, scratch_size);
252
253                 // ... where we first store the value...
254                 bx.store(val, llscratch, scratch_align);
255
256                 // ... and then memcpy it to the intended destination.
257                 bx.memcpy(
258                     dst.llval,
259                     self.layout.align.abi,
260                     llscratch,
261                     scratch_align,
262                     bx.const_usize(self.layout.size.bytes()),
263                     MemFlags::empty(),
264                 );
265
266                 bx.lifetime_end(llscratch, scratch_size);
267             }
268         } else {
269             OperandValue::Immediate(val).store(bx, dst);
270         }
271     }
272
273     fn store_fn_arg(
274         &self,
275         bx: &mut Builder<'_, 'll, 'tcx>,
276         idx: &mut usize,
277         dst: PlaceRef<'tcx, &'ll Value>,
278     ) {
279         let mut next = || {
280             let val = llvm::get_param(bx.llfn(), *idx as c_uint);
281             *idx += 1;
282             val
283         };
284         match self.mode {
285             PassMode::Ignore => {}
286             PassMode::Pair(..) => {
287                 OperandValue::Pair(next(), next()).store(bx, dst);
288             }
289             PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
290                 OperandValue::Ref(next(), Some(next()), self.layout.align.abi).store(bx, dst);
291             }
292             PassMode::Direct(_)
293             | PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ }
294             | PassMode::Cast(..) => {
295                 let next_arg = next();
296                 self.store(bx, next_arg, dst);
297             }
298         }
299     }
300 }
301
302 impl<'ll, 'tcx> ArgAbiMethods<'tcx> for Builder<'_, 'll, 'tcx> {
303     fn store_fn_arg(
304         &mut self,
305         arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
306         idx: &mut usize,
307         dst: PlaceRef<'tcx, Self::Value>,
308     ) {
309         arg_abi.store_fn_arg(self, idx, dst)
310     }
311     fn store_arg(
312         &mut self,
313         arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
314         val: &'ll Value,
315         dst: PlaceRef<'tcx, &'ll Value>,
316     ) {
317         arg_abi.store(self, val, dst)
318     }
319     fn arg_memory_ty(&self, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>) -> &'ll Type {
320         arg_abi.memory_ty(self)
321     }
322 }
323
324 pub trait FnAbiLlvmExt<'ll, 'tcx> {
325     fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
326     fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
327     fn llvm_cconv(&self) -> llvm::CallConv;
328     fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value);
329     fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value);
330 }
331
332 impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
333     fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
334         // Ignore "extra" args from the call site for C variadic functions.
335         // Only the "fixed" args are part of the LLVM function signature.
336         let args =
337             if self.c_variadic { &self.args[..self.fixed_count as usize] } else { &self.args };
338
339         // This capacity calculation is approximate.
340         let mut llargument_tys = Vec::with_capacity(
341             self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 },
342         );
343
344         let llreturn_ty = match &self.ret.mode {
345             PassMode::Ignore => cx.type_void(),
346             PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
347             PassMode::Cast(cast, _) => cast.llvm_type(cx),
348             PassMode::Indirect { .. } => {
349                 llargument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
350                 cx.type_void()
351             }
352         };
353
354         for arg in args {
355             let llarg_ty = match &arg.mode {
356                 PassMode::Ignore => continue,
357                 PassMode::Direct(_) => arg.layout.immediate_llvm_type(cx),
358                 PassMode::Pair(..) => {
359                     llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 0, true));
360                     llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 1, true));
361                     continue;
362                 }
363                 PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
364                     let ptr_ty = cx.tcx.mk_mut_ptr(arg.layout.ty);
365                     let ptr_layout = cx.layout_of(ptr_ty);
366                     llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 0, true));
367                     llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 1, true));
368                     continue;
369                 }
370                 PassMode::Cast(cast, pad_i32) => {
371                     // add padding
372                     if *pad_i32 {
373                         llargument_tys.push(Reg::i32().llvm_type(cx));
374                     }
375                     cast.llvm_type(cx)
376                 }
377                 PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
378                     cx.type_ptr_to(arg.memory_ty(cx))
379                 }
380             };
381             llargument_tys.push(llarg_ty);
382         }
383
384         if self.c_variadic {
385             cx.type_variadic_func(&llargument_tys, llreturn_ty)
386         } else {
387             cx.type_func(&llargument_tys, llreturn_ty)
388         }
389     }
390
391     fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
392         unsafe {
393             llvm::LLVMPointerType(
394                 self.llvm_type(cx),
395                 cx.data_layout().instruction_address_space.0 as c_uint,
396             )
397         }
398     }
399
400     fn llvm_cconv(&self) -> llvm::CallConv {
401         self.conv.into()
402     }
403
404     fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value) {
405         let mut func_attrs = SmallVec::<[_; 2]>::new();
406         if self.ret.layout.abi.is_uninhabited() {
407             func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(cx.llcx));
408         }
409         if !self.can_unwind {
410             func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(cx.llcx));
411         }
412         attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &{ func_attrs });
413
414         let mut i = 0;
415         let mut apply = |attrs: &ArgAttributes| {
416             attrs.apply_attrs_to_llfn(llvm::AttributePlace::Argument(i), cx, llfn);
417             i += 1;
418             i - 1
419         };
420         match &self.ret.mode {
421             PassMode::Direct(attrs) => {
422                 attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
423             }
424             PassMode::Indirect { attrs, extra_attrs: _, on_stack } => {
425                 assert!(!on_stack);
426                 let i = apply(attrs);
427                 let sret = llvm::CreateStructRetAttr(cx.llcx, self.ret.layout.llvm_type(cx));
428                 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[sret]);
429             }
430             PassMode::Cast(cast, _) => {
431                 cast.attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
432             }
433             _ => {}
434         }
435         for arg in self.args.iter() {
436             match &arg.mode {
437                 PassMode::Ignore => {}
438                 PassMode::Indirect { attrs, extra_attrs: None, on_stack: true } => {
439                     let i = apply(attrs);
440                     let byval = llvm::CreateByValAttr(cx.llcx, arg.layout.llvm_type(cx));
441                     attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[byval]);
442                 }
443                 PassMode::Direct(attrs)
444                 | PassMode::Indirect { attrs, extra_attrs: None, on_stack: false } => {
445                     apply(attrs);
446                 }
447                 PassMode::Indirect { attrs, extra_attrs: Some(extra_attrs), on_stack } => {
448                     assert!(!on_stack);
449                     apply(attrs);
450                     apply(extra_attrs);
451                 }
452                 PassMode::Pair(a, b) => {
453                     apply(a);
454                     apply(b);
455                 }
456                 PassMode::Cast(cast, pad_i32) => {
457                     if *pad_i32 {
458                         apply(&ArgAttributes::new());
459                     }
460                     apply(&cast.attrs);
461                 }
462             }
463         }
464     }
465
466     fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {
467         let mut func_attrs = SmallVec::<[_; 2]>::new();
468         if self.ret.layout.abi.is_uninhabited() {
469             func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx));
470         }
471         if !self.can_unwind {
472             func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(bx.cx.llcx));
473         }
474         attributes::apply_to_callsite(callsite, llvm::AttributePlace::Function, &{ func_attrs });
475
476         let mut i = 0;
477         let mut apply = |cx: &CodegenCx<'_, '_>, attrs: &ArgAttributes| {
478             attrs.apply_attrs_to_callsite(llvm::AttributePlace::Argument(i), cx, callsite);
479             i += 1;
480             i - 1
481         };
482         match &self.ret.mode {
483             PassMode::Direct(attrs) => {
484                 attrs.apply_attrs_to_callsite(llvm::AttributePlace::ReturnValue, bx.cx, callsite);
485             }
486             PassMode::Indirect { attrs, extra_attrs: _, on_stack } => {
487                 assert!(!on_stack);
488                 let i = apply(bx.cx, attrs);
489                 let sret = llvm::CreateStructRetAttr(bx.cx.llcx, self.ret.layout.llvm_type(bx));
490                 attributes::apply_to_callsite(callsite, llvm::AttributePlace::Argument(i), &[sret]);
491             }
492             PassMode::Cast(cast, _) => {
493                 cast.attrs.apply_attrs_to_callsite(
494                     llvm::AttributePlace::ReturnValue,
495                     &bx.cx,
496                     callsite,
497                 );
498             }
499             _ => {}
500         }
501         if let abi::Abi::Scalar(scalar) = self.ret.layout.abi {
502             // If the value is a boolean, the range is 0..2 and that ultimately
503             // become 0..0 when the type becomes i1, which would be rejected
504             // by the LLVM verifier.
505             if let Int(..) = scalar.primitive() {
506                 if !scalar.is_bool() && !scalar.is_always_valid(bx) {
507                     bx.range_metadata(callsite, scalar.valid_range(bx));
508                 }
509             }
510         }
511         for arg in self.args.iter() {
512             match &arg.mode {
513                 PassMode::Ignore => {}
514                 PassMode::Indirect { attrs, extra_attrs: None, on_stack: true } => {
515                     let i = apply(bx.cx, attrs);
516                     let byval = llvm::CreateByValAttr(bx.cx.llcx, arg.layout.llvm_type(bx));
517                     attributes::apply_to_callsite(
518                         callsite,
519                         llvm::AttributePlace::Argument(i),
520                         &[byval],
521                     );
522                 }
523                 PassMode::Direct(attrs)
524                 | PassMode::Indirect { attrs, extra_attrs: None, on_stack: false } => {
525                     apply(bx.cx, attrs);
526                 }
527                 PassMode::Indirect { attrs, extra_attrs: Some(extra_attrs), on_stack: _ } => {
528                     apply(bx.cx, attrs);
529                     apply(bx.cx, extra_attrs);
530                 }
531                 PassMode::Pair(a, b) => {
532                     apply(bx.cx, a);
533                     apply(bx.cx, b);
534                 }
535                 PassMode::Cast(cast, pad_i32) => {
536                     if *pad_i32 {
537                         apply(bx.cx, &ArgAttributes::new());
538                     }
539                     apply(bx.cx, &cast.attrs);
540                 }
541             }
542         }
543
544         let cconv = self.llvm_cconv();
545         if cconv != llvm::CCallConv {
546             llvm::SetInstructionCallConv(callsite, cconv);
547         }
548
549         if self.conv == Conv::CCmseNonSecureCall {
550             // This will probably get ignored on all targets but those supporting the TrustZone-M
551             // extension (thumbv8m targets).
552             let cmse_nonsecure_call = llvm::CreateAttrString(bx.cx.llcx, "cmse_nonsecure_call");
553             attributes::apply_to_callsite(
554                 callsite,
555                 llvm::AttributePlace::Function,
556                 &[cmse_nonsecure_call],
557             );
558         }
559
560         // Some intrinsics require that an elementtype attribute (with the pointee type of a
561         // pointer argument) is added to the callsite.
562         let element_type_index = unsafe { llvm::LLVMRustGetElementTypeArgIndex(callsite) };
563         if element_type_index >= 0 {
564             let arg_ty = self.args[element_type_index as usize].layout.ty;
565             let pointee_ty = arg_ty.builtin_deref(true).expect("Must be pointer argument").ty;
566             let element_type_attr = unsafe {
567                 llvm::LLVMRustCreateElementTypeAttr(bx.llcx, bx.layout_of(pointee_ty).llvm_type(bx))
568             };
569             attributes::apply_to_callsite(
570                 callsite,
571                 llvm::AttributePlace::Argument(element_type_index as u32),
572                 &[element_type_attr],
573             );
574         }
575     }
576 }
577
578 impl<'tcx> AbiBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
579     fn get_param(&mut self, index: usize) -> Self::Value {
580         llvm::get_param(self.llfn(), index as c_uint)
581     }
582 }
583
584 impl From<Conv> for llvm::CallConv {
585     fn from(conv: Conv) -> Self {
586         match conv {
587             Conv::C | Conv::Rust | Conv::CCmseNonSecureCall => llvm::CCallConv,
588             Conv::RustCold => llvm::ColdCallConv,
589             Conv::AmdGpuKernel => llvm::AmdGpuKernel,
590             Conv::AvrInterrupt => llvm::AvrInterrupt,
591             Conv::AvrNonBlockingInterrupt => llvm::AvrNonBlockingInterrupt,
592             Conv::ArmAapcs => llvm::ArmAapcsCallConv,
593             Conv::Msp430Intr => llvm::Msp430Intr,
594             Conv::PtxKernel => llvm::PtxKernel,
595             Conv::X86Fastcall => llvm::X86FastcallCallConv,
596             Conv::X86Intr => llvm::X86_Intr,
597             Conv::X86Stdcall => llvm::X86StdcallCallConv,
598             Conv::X86ThisCall => llvm::X86_ThisCall,
599             Conv::X86VectorCall => llvm::X86_VectorCall,
600             Conv::X86_64SysV => llvm::X86_64_SysV,
601             Conv::X86_64Win64 => llvm::X86_64_Win64,
602         }
603     }
604 }