1use std::borrow::Borrow;
2use std::cmp;
3
4use libc::c_uint;
5use rustc_abi::{
6 ArmCall, BackendRepr, CanonAbi, HasDataLayout, InterruptKind, Primitive, Reg, RegKind, Size,
7 X86Call,
8};
9use rustc_codegen_ssa::MemFlags;
10use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
11use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
12use rustc_codegen_ssa::traits::*;
13use rustc_middle::ty::Ty;
14use rustc_middle::ty::layout::LayoutOf;
15use rustc_middle::{bug, ty};
16use rustc_session::config;
17use rustc_target::callconv::{
18 ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, CastTarget, FnAbi, PassMode,
19};
20use rustc_target::spec::SanitizerSet;
21use smallvec::SmallVec;
22
23use crate::attributes::{self, llfn_attrs_from_instance};
24use crate::builder::Builder;
25use crate::context::CodegenCx;
26use crate::llvm::{self, Attribute, AttributePlace};
27use crate::llvm_util;
28use crate::type_::Type;
29use crate::type_of::LayoutLlvmExt;
30use crate::value::Value;
31
32trait ArgAttributesExt {
33 fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value);
34 fn apply_attrs_to_callsite(
35 &self,
36 idx: AttributePlace,
37 cx: &CodegenCx<'_, '_>,
38 callsite: &Value,
39 );
40}
41
42const ABI_AFFECTING_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 1] =
43 [(ArgAttribute::InReg, llvm::AttributeKind::InReg)];
44
45const OPTIMIZATION_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 6] = [
46 (ArgAttribute::NoAlias, llvm::AttributeKind::NoAlias),
47 (ArgAttribute::CapturesAddress, llvm::AttributeKind::CapturesAddress),
48 (ArgAttribute::NonNull, llvm::AttributeKind::NonNull),
49 (ArgAttribute::ReadOnly, llvm::AttributeKind::ReadOnly),
50 (ArgAttribute::NoUndef, llvm::AttributeKind::NoUndef),
51 (ArgAttribute::CapturesReadOnly, llvm::AttributeKind::CapturesReadOnly),
52];
53
54fn get_attrs<'ll>(this: &ArgAttributes, cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'ll Attribute; 8]> {
55 let mut regular = this.regular;
56
57 let mut attrs = SmallVec::new();
58
59 for (attr, llattr) in ABI_AFFECTING_ATTRIBUTES {
61 if regular.contains(attr) {
62 attrs.push(llattr.create_attr(cx.llcx));
63 }
64 }
65 if let Some(align) = this.pointee_align {
66 attrs.push(llvm::CreateAlignmentAttr(cx.llcx, align.bytes()));
67 }
68 match this.arg_ext {
69 ArgExtension::None => {}
70 ArgExtension::Zext => attrs.push(llvm::AttributeKind::ZExt.create_attr(cx.llcx)),
71 ArgExtension::Sext => attrs.push(llvm::AttributeKind::SExt.create_attr(cx.llcx)),
72 }
73
74 if cx.sess().opts.optimize != config::OptLevel::No {
76 let deref = this.pointee_size.bytes();
77 if deref != 0 {
78 if regular.contains(ArgAttribute::NonNull) {
79 attrs.push(llvm::CreateDereferenceableAttr(cx.llcx, deref));
80 } else {
81 attrs.push(llvm::CreateDereferenceableOrNullAttr(cx.llcx, deref));
82 }
83 regular -= ArgAttribute::NonNull;
84 }
85 for (attr, llattr) in OPTIMIZATION_ATTRIBUTES {
86 if regular.contains(attr) {
87 if (attr == ArgAttribute::CapturesReadOnly || attr == ArgAttribute::CapturesAddress)
89 && llvm_util::get_version() < (21, 0, 0)
90 {
91 continue;
92 }
93 attrs.push(llattr.create_attr(cx.llcx));
94 }
95 }
96 } else if cx.tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
97 if regular.contains(ArgAttribute::NoUndef) {
101 attrs.push(llvm::AttributeKind::NoUndef.create_attr(cx.llcx));
102 }
103 }
104
105 attrs
106}
107
108impl ArgAttributesExt for ArgAttributes {
109 fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value) {
110 let attrs = get_attrs(self, cx);
111 attributes::apply_to_llfn(llfn, idx, &attrs);
112 }
113
114 fn apply_attrs_to_callsite(
115 &self,
116 idx: AttributePlace,
117 cx: &CodegenCx<'_, '_>,
118 callsite: &Value,
119 ) {
120 let attrs = get_attrs(self, cx);
121 attributes::apply_to_callsite(callsite, idx, &attrs);
122 }
123}
124
125pub(crate) trait LlvmType {
126 fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type;
127}
128
129impl LlvmType for Reg {
130 fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
131 match self.kind {
132 RegKind::Integer => cx.type_ix(self.size.bits()),
133 RegKind::Float => match self.size.bits() {
134 16 => cx.type_f16(),
135 32 => cx.type_f32(),
136 64 => cx.type_f64(),
137 128 => cx.type_f128(),
138 _ => bug!("unsupported float: {:?}", self),
139 },
140 RegKind::Vector => cx.type_vector(cx.type_i8(), self.size.bytes()),
141 }
142 }
143}
144
145impl LlvmType for CastTarget {
146 fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
147 let rest_ll_unit = self.rest.unit.llvm_type(cx);
148 let rest_count = if self.rest.total == Size::ZERO {
149 0
150 } else {
151 assert_ne!(
152 self.rest.unit.size,
153 Size::ZERO,
154 "total size {:?} cannot be divided into units of zero size",
155 self.rest.total
156 );
157 if !self.rest.total.bytes().is_multiple_of(self.rest.unit.size.bytes()) {
158 assert_eq!(self.rest.unit.kind, RegKind::Integer, "only int regs can be split");
159 }
160 self.rest.total.bytes().div_ceil(self.rest.unit.size.bytes())
161 };
162
163 if self.prefix.iter().all(|x| x.is_none()) {
166 if rest_count == 1 && (!self.rest.is_consecutive || self.rest.unit != Reg::i128()) {
170 return rest_ll_unit;
171 }
172
173 return cx.type_array(rest_ll_unit, rest_count);
174 }
175
176 let prefix_args =
178 self.prefix.iter().flat_map(|option_reg| option_reg.map(|reg| reg.llvm_type(cx)));
179 let rest_args = (0..rest_count).map(|_| rest_ll_unit);
180 let args: Vec<_> = prefix_args.chain(rest_args).collect();
181 cx.type_struct(&args, false)
182 }
183}
184
185trait ArgAbiExt<'ll, 'tcx> {
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
200impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
201 fn store(
206 &self,
207 bx: &mut Builder<'_, 'll, 'tcx>,
208 val: &'ll Value,
209 dst: PlaceRef<'tcx, &'ll Value>,
210 ) {
211 match &self.mode {
212 PassMode::Ignore => {}
213 PassMode::Indirect { attrs, meta_attrs: None, on_stack: _ } => {
215 let align = attrs.pointee_align.unwrap_or(self.layout.align.abi);
216 OperandValue::Ref(PlaceValue::new_sized(val, align)).store(bx, dst);
217 }
218 PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
220 bug!("unsized `ArgAbi` must be handled through `store_fn_arg`");
221 }
222 PassMode::Cast { cast, pad_i32: _ } => {
223 let scratch_size = cast.size(bx);
227 let scratch_align = cast.align(bx);
228 let copy_bytes =
235 cmp::min(cast.unaligned_size(bx).bytes(), self.layout.size.bytes());
236 let llscratch = bx.alloca(scratch_size, scratch_align);
238 bx.lifetime_start(llscratch, scratch_size);
239 rustc_codegen_ssa::mir::store_cast(bx, cast, val, llscratch, scratch_align);
241 bx.memcpy(
243 dst.val.llval,
244 self.layout.align.abi,
245 llscratch,
246 scratch_align,
247 bx.const_usize(copy_bytes),
248 MemFlags::empty(),
249 );
250 bx.lifetime_end(llscratch, scratch_size);
251 }
252 _ => {
253 OperandRef::from_immediate_or_packed_pair(bx, val, self.layout).val.store(bx, dst);
254 }
255 }
256 }
257
258 fn store_fn_arg(
259 &self,
260 bx: &mut Builder<'_, 'll, 'tcx>,
261 idx: &mut usize,
262 dst: PlaceRef<'tcx, &'ll Value>,
263 ) {
264 let mut next = || {
265 let val = llvm::get_param(bx.llfn(), *idx as c_uint);
266 *idx += 1;
267 val
268 };
269 match self.mode {
270 PassMode::Ignore => {}
271 PassMode::Pair(..) => {
272 OperandValue::Pair(next(), next()).store(bx, dst);
273 }
274 PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
275 let place_val = PlaceValue {
276 llval: next(),
277 llextra: Some(next()),
278 align: self.layout.align.abi,
279 };
280 OperandValue::Ref(place_val).store(bx, dst);
281 }
282 PassMode::Direct(_)
283 | PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ }
284 | PassMode::Cast { .. } => {
285 let next_arg = next();
286 self.store(bx, next_arg, dst);
287 }
288 }
289 }
290}
291
292impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
293 fn store_fn_arg(
294 &mut self,
295 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
296 idx: &mut usize,
297 dst: PlaceRef<'tcx, Self::Value>,
298 ) {
299 arg_abi.store_fn_arg(self, idx, dst)
300 }
301 fn store_arg(
302 &mut self,
303 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
304 val: &'ll Value,
305 dst: PlaceRef<'tcx, &'ll Value>,
306 ) {
307 arg_abi.store(self, val, dst)
308 }
309}
310
311pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {
312 fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
313 fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
314 fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv;
315
316 fn apply_attrs_llfn(
318 &self,
319 cx: &CodegenCx<'ll, 'tcx>,
320 llfn: &'ll Value,
321 instance: Option<ty::Instance<'tcx>>,
322 );
323
324 fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value);
326}
327
328impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
329 fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
330 let args =
333 if self.c_variadic { &self.args[..self.fixed_count as usize] } else { &self.args };
334
335 let mut llargument_tys = Vec::with_capacity(
337 self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 },
338 );
339
340 let llreturn_ty = match &self.ret.mode {
341 PassMode::Ignore => cx.type_void(),
342 PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
343 PassMode::Cast { cast, pad_i32: _ } => cast.llvm_type(cx),
344 PassMode::Indirect { .. } => {
345 llargument_tys.push(cx.type_ptr());
346 cx.type_void()
347 }
348 };
349
350 for arg in args {
351 let llarg_ty = match &arg.mode {
355 PassMode::Ignore => continue,
356 PassMode::Direct(_) => {
357 arg.layout.immediate_llvm_type(cx)
361 }
362 PassMode::Pair(..) => {
363 llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 0, true));
367 llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 1, true));
368 continue;
369 }
370 PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
371 let ptr_ty = Ty::new_mut_ptr(cx.tcx, arg.layout.ty);
376 let ptr_layout = cx.layout_of(ptr_ty);
377 llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 0, true));
378 llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 1, true));
379 continue;
380 }
381 PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => cx.type_ptr(),
382 PassMode::Cast { cast, pad_i32 } => {
383 if *pad_i32 {
385 llargument_tys.push(Reg::i32().llvm_type(cx));
386 }
387 cast.llvm_type(cx)
390 }
391 };
392 llargument_tys.push(llarg_ty);
393 }
394
395 if self.c_variadic {
396 cx.type_variadic_func(&llargument_tys, llreturn_ty)
397 } else {
398 cx.type_func(&llargument_tys, llreturn_ty)
399 }
400 }
401
402 fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
403 cx.type_ptr_ext(cx.data_layout().instruction_address_space)
404 }
405
406 fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv {
407 llvm::CallConv::from_conv(self.conv, cx.tcx.sess.target.arch.borrow())
408 }
409
410 fn apply_attrs_llfn(
411 &self,
412 cx: &CodegenCx<'ll, 'tcx>,
413 llfn: &'ll Value,
414 instance: Option<ty::Instance<'tcx>>,
415 ) {
416 let mut func_attrs = SmallVec::<[_; 3]>::new();
417 if self.ret.layout.is_uninhabited() {
418 func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(cx.llcx));
419 }
420 if !self.can_unwind {
421 func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(cx.llcx));
422 }
423 match self.conv {
424 CanonAbi::Interrupt(InterruptKind::RiscvMachine) => {
425 func_attrs.push(llvm::CreateAttrStringValue(cx.llcx, "interrupt", "machine"))
426 }
427 CanonAbi::Interrupt(InterruptKind::RiscvSupervisor) => {
428 func_attrs.push(llvm::CreateAttrStringValue(cx.llcx, "interrupt", "supervisor"))
429 }
430 CanonAbi::Arm(ArmCall::CCmseNonSecureEntry) => {
431 func_attrs.push(llvm::CreateAttrString(cx.llcx, "cmse_nonsecure_entry"))
432 }
433 _ => (),
434 }
435 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &{ func_attrs });
436
437 let mut i = 0;
438 let mut apply = |attrs: &ArgAttributes| {
439 attrs.apply_attrs_to_llfn(llvm::AttributePlace::Argument(i), cx, llfn);
440 i += 1;
441 i - 1
442 };
443
444 let apply_range_attr = |idx: AttributePlace, scalar: rustc_abi::Scalar| {
445 if cx.sess().opts.optimize != config::OptLevel::No
446 && matches!(scalar.primitive(), Primitive::Int(..))
447 && !scalar.is_bool()
451 && !scalar.is_always_valid(cx)
453 {
454 attributes::apply_to_llfn(
455 llfn,
456 idx,
457 &[llvm::CreateRangeAttr(cx.llcx, scalar.size(cx), scalar.valid_range(cx))],
458 );
459 }
460 };
461
462 match &self.ret.mode {
463 PassMode::Direct(attrs) => {
464 attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
465 if let BackendRepr::Scalar(scalar) = self.ret.layout.backend_repr {
466 apply_range_attr(llvm::AttributePlace::ReturnValue, scalar);
467 }
468 }
469 PassMode::Indirect { attrs, meta_attrs: _, on_stack } => {
470 assert!(!on_stack);
471 let i = apply(attrs);
472 let sret = llvm::CreateStructRetAttr(
473 cx.llcx,
474 cx.type_array(cx.type_i8(), self.ret.layout.size.bytes()),
475 );
476 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[sret]);
477 if cx.sess().opts.optimize != config::OptLevel::No {
478 attributes::apply_to_llfn(
479 llfn,
480 llvm::AttributePlace::Argument(i),
481 &[
482 llvm::AttributeKind::Writable.create_attr(cx.llcx),
483 llvm::AttributeKind::DeadOnUnwind.create_attr(cx.llcx),
484 ],
485 );
486 }
487 }
488 PassMode::Cast { cast, pad_i32: _ } => {
489 cast.attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
490 }
491 _ => {}
492 }
493 for arg in self.args.iter() {
494 match &arg.mode {
495 PassMode::Ignore => {}
496 PassMode::Indirect { attrs, meta_attrs: None, on_stack: true } => {
497 let i = apply(attrs);
498 let byval = llvm::CreateByValAttr(
499 cx.llcx,
500 cx.type_array(cx.type_i8(), arg.layout.size.bytes()),
501 );
502 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[byval]);
503 }
504 PassMode::Direct(attrs) => {
505 let i = apply(attrs);
506 if let BackendRepr::Scalar(scalar) = arg.layout.backend_repr {
507 apply_range_attr(llvm::AttributePlace::Argument(i), scalar);
508 }
509 }
510 PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
511 let i = apply(attrs);
512 if cx.sess().opts.optimize != config::OptLevel::No
513 && llvm_util::get_version() >= (21, 0, 0)
514 {
515 attributes::apply_to_llfn(
516 llfn,
517 llvm::AttributePlace::Argument(i),
518 &[llvm::AttributeKind::DeadOnReturn.create_attr(cx.llcx)],
519 );
520 }
521 }
522 PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
523 assert!(!on_stack);
524 apply(attrs);
525 apply(meta_attrs);
526 }
527 PassMode::Pair(a, b) => {
528 let i = apply(a);
529 let ii = apply(b);
530 if let BackendRepr::ScalarPair(scalar_a, scalar_b) = arg.layout.backend_repr {
531 apply_range_attr(llvm::AttributePlace::Argument(i), scalar_a);
532 apply_range_attr(llvm::AttributePlace::Argument(ii), scalar_b);
533 }
534 }
535 PassMode::Cast { cast, pad_i32 } => {
536 if *pad_i32 {
537 apply(&ArgAttributes::new());
538 }
539 apply(&cast.attrs);
540 }
541 }
542 }
543
544 if let Some(instance) = instance {
546 llfn_attrs_from_instance(cx, llfn, instance);
547 }
548 }
549
550 fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {
551 let mut func_attrs = SmallVec::<[_; 2]>::new();
552 if self.ret.layout.is_uninhabited() {
553 func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx));
554 }
555 if !self.can_unwind {
556 func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(bx.cx.llcx));
557 }
558 attributes::apply_to_callsite(callsite, llvm::AttributePlace::Function, &{ func_attrs });
559
560 let mut i = 0;
561 let mut apply = |cx: &CodegenCx<'_, '_>, attrs: &ArgAttributes| {
562 attrs.apply_attrs_to_callsite(llvm::AttributePlace::Argument(i), cx, callsite);
563 i += 1;
564 i - 1
565 };
566 match &self.ret.mode {
567 PassMode::Direct(attrs) => {
568 attrs.apply_attrs_to_callsite(llvm::AttributePlace::ReturnValue, bx.cx, callsite);
569 }
570 PassMode::Indirect { attrs, meta_attrs: _, on_stack } => {
571 assert!(!on_stack);
572 let i = apply(bx.cx, attrs);
573 let sret = llvm::CreateStructRetAttr(
574 bx.cx.llcx,
575 bx.cx.type_array(bx.cx.type_i8(), self.ret.layout.size.bytes()),
576 );
577 attributes::apply_to_callsite(callsite, llvm::AttributePlace::Argument(i), &[sret]);
578 }
579 PassMode::Cast { cast, pad_i32: _ } => {
580 cast.attrs.apply_attrs_to_callsite(
581 llvm::AttributePlace::ReturnValue,
582 bx.cx,
583 callsite,
584 );
585 }
586 _ => {}
587 }
588 for arg in self.args.iter() {
589 match &arg.mode {
590 PassMode::Ignore => {}
591 PassMode::Indirect { attrs, meta_attrs: None, on_stack: true } => {
592 let i = apply(bx.cx, attrs);
593 let byval = llvm::CreateByValAttr(
594 bx.cx.llcx,
595 bx.cx.type_array(bx.cx.type_i8(), arg.layout.size.bytes()),
596 );
597 attributes::apply_to_callsite(
598 callsite,
599 llvm::AttributePlace::Argument(i),
600 &[byval],
601 );
602 }
603 PassMode::Direct(attrs)
604 | PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
605 apply(bx.cx, attrs);
606 }
607 PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack: _ } => {
608 apply(bx.cx, attrs);
609 apply(bx.cx, meta_attrs);
610 }
611 PassMode::Pair(a, b) => {
612 apply(bx.cx, a);
613 apply(bx.cx, b);
614 }
615 PassMode::Cast { cast, pad_i32 } => {
616 if *pad_i32 {
617 apply(bx.cx, &ArgAttributes::new());
618 }
619 apply(bx.cx, &cast.attrs);
620 }
621 }
622 }
623
624 let cconv = self.llvm_cconv(&bx.cx);
625 if cconv != llvm::CCallConv {
626 llvm::SetInstructionCallConv(callsite, cconv);
627 }
628
629 if self.conv == CanonAbi::Arm(ArmCall::CCmseNonSecureCall) {
630 let cmse_nonsecure_call = llvm::CreateAttrString(bx.cx.llcx, "cmse_nonsecure_call");
633 attributes::apply_to_callsite(
634 callsite,
635 llvm::AttributePlace::Function,
636 &[cmse_nonsecure_call],
637 );
638 }
639
640 let element_type_index = unsafe { llvm::LLVMRustGetElementTypeArgIndex(callsite) };
643 if element_type_index >= 0 {
644 let arg_ty = self.args[element_type_index as usize].layout.ty;
645 let pointee_ty = arg_ty.builtin_deref(true).expect("Must be pointer argument");
646 let element_type_attr = unsafe {
647 llvm::LLVMRustCreateElementTypeAttr(bx.llcx, bx.layout_of(pointee_ty).llvm_type(bx))
648 };
649 attributes::apply_to_callsite(
650 callsite,
651 llvm::AttributePlace::Argument(element_type_index as u32),
652 &[element_type_attr],
653 );
654 }
655 }
656}
657
658impl AbiBuilderMethods for Builder<'_, '_, '_> {
659 fn get_param(&mut self, index: usize) -> Self::Value {
660 llvm::get_param(self.llfn(), index as c_uint)
661 }
662}
663
664impl llvm::CallConv {
665 pub(crate) fn from_conv(conv: CanonAbi, arch: &str) -> Self {
666 match conv {
667 CanonAbi::C | CanonAbi::Rust => llvm::CCallConv,
668 CanonAbi::RustCold => llvm::PreserveMost,
669 CanonAbi::Custom => llvm::CCallConv,
673 CanonAbi::GpuKernel => {
674 if arch == "amdgpu" {
675 llvm::AmdgpuKernel
676 } else if arch == "nvptx64" {
677 llvm::PtxKernel
678 } else {
679 panic!("Architecture {arch} does not support GpuKernel calling convention");
680 }
681 }
682 CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
683 InterruptKind::Avr => llvm::AvrInterrupt,
684 InterruptKind::AvrNonBlocking => llvm::AvrNonBlockingInterrupt,
685 InterruptKind::Msp430 => llvm::Msp430Intr,
686 InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => llvm::CCallConv,
687 InterruptKind::X86 => llvm::X86_Intr,
688 },
689 CanonAbi::Arm(arm_call) => match arm_call {
690 ArmCall::Aapcs => llvm::ArmAapcsCallConv,
691 ArmCall::CCmseNonSecureCall | ArmCall::CCmseNonSecureEntry => llvm::CCallConv,
692 },
693 CanonAbi::X86(x86_call) => match x86_call {
694 X86Call::Fastcall => llvm::X86FastcallCallConv,
695 X86Call::Stdcall => llvm::X86StdcallCallConv,
696 X86Call::SysV64 => llvm::X86_64_SysV,
697 X86Call::Thiscall => llvm::X86_ThisCall,
698 X86Call::Vectorcall => llvm::X86_VectorCall,
699 X86Call::Win64 => llvm::X86_64_Win64,
700 },
701 }
702 }
703}