1use std::cmp;
2
3use libc::c_uint;
4use rustc_abi::{
5 ArmCall, BackendRepr, CanonAbi, Float, HasDataLayout, Integer, InterruptKind, Primitive, Reg,
6 RegKind, Size, X86Call,
7};
8use rustc_codegen_ssa::MemFlags;
9use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
10use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
11use rustc_codegen_ssa::traits::*;
12use rustc_middle::ty::Ty;
13use rustc_middle::ty::layout::LayoutOf;
14use rustc_middle::{bug, ty};
15use rustc_session::{Session, config};
16use rustc_target::callconv::{
17 ArgAbi, ArgAttribute, ArgAttributes, ArgExtension, CastTarget, FnAbi, PassMode,
18};
19use rustc_target::spec::{Arch, SanitizerSet};
20use smallvec::SmallVec;
21
22use crate::attributes::{self, llfn_attrs_from_instance};
23use crate::builder::Builder;
24use crate::context::CodegenCx;
25use crate::llvm::{self, Attribute, AttributePlace, Type, Value};
26use crate::type_of::LayoutLlvmExt;
27
28trait ArgAttributesExt {
29 fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value);
30 fn apply_attrs_to_callsite(
31 &self,
32 idx: AttributePlace,
33 cx: &CodegenCx<'_, '_>,
34 callsite: &Value,
35 );
36}
37
38const ABI_AFFECTING_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 1] =
39 [(ArgAttribute::InReg, llvm::AttributeKind::InReg)];
40
41const OPTIMIZATION_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 5] = [
42 (ArgAttribute::NoAlias, llvm::AttributeKind::NoAlias),
43 (ArgAttribute::NonNull, llvm::AttributeKind::NonNull),
44 (ArgAttribute::ReadOnly, llvm::AttributeKind::ReadOnly),
45 (ArgAttribute::NoUndef, llvm::AttributeKind::NoUndef),
46 (ArgAttribute::Writable, llvm::AttributeKind::Writable),
47];
48
49const CAPTURES_ATTRIBUTES: [(ArgAttribute, llvm::AttributeKind); 3] = [
50 (ArgAttribute::CapturesNone, llvm::AttributeKind::CapturesNone),
51 (ArgAttribute::CapturesAddress, llvm::AttributeKind::CapturesAddress),
52 (ArgAttribute::CapturesReadOnly, llvm::AttributeKind::CapturesReadOnly),
53];
54
55fn 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 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 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 for (attr, llattr) in CAPTURES_ATTRIBUTES {
92 if regular.contains(attr) {
93 attrs.push(llattr.create_attr(cx.llcx));
94 break;
95 }
96 }
97 } else if cx.tcx.sess.sanitizers().contains(SanitizerSet::MEMORY) {
98 if regular.contains(ArgAttribute::NoUndef) {
102 attrs.push(llvm::AttributeKind::NoUndef.create_attr(cx.llcx));
103 }
104 }
105
106 attrs
107}
108
109impl ArgAttributesExt for ArgAttributes {
110 fn apply_attrs_to_llfn(&self, idx: AttributePlace, cx: &CodegenCx<'_, '_>, llfn: &Value) {
111 let attrs = get_attrs(self, cx);
112 attributes::apply_to_llfn(llfn, idx, &attrs);
113 }
114
115 fn apply_attrs_to_callsite(
116 &self,
117 idx: AttributePlace,
118 cx: &CodegenCx<'_, '_>,
119 callsite: &Value,
120 ) {
121 let attrs = get_attrs(self, cx);
122 attributes::apply_to_callsite(callsite, idx, &attrs);
123 }
124}
125
126pub(crate) trait LlvmType {
127 fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type;
128}
129
130impl LlvmType for Reg {
131 fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
132 match self.kind {
133 RegKind::Integer => cx.type_ix(self.size.bits()),
134 RegKind::Float => match self.size.bits() {
135 16 => cx.type_f16(),
136 32 => cx.type_f32(),
137 64 => cx.type_f64(),
138 128 => cx.type_f128(),
139 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("unsupported float: {0:?}",
self))bug!("unsupported float: {:?}", self),
140 },
141 RegKind::Vector { hint_vector_elem } => {
142 let ty = match hint_vector_elem {
146 Primitive::Int(integer, _) => match integer {
147 Integer::I8 => cx.type_ix(8),
148 Integer::I16 => cx.type_ix(16),
149 Integer::I32 => cx.type_ix(32),
150 Integer::I64 => cx.type_ix(64),
151 Integer::I128 => cx.type_ix(128),
152 },
153 Primitive::Float(float) => match float {
154 Float::F16 => cx.type_f16(),
155 Float::F32 => cx.type_f32(),
156 Float::F64 => cx.type_f64(),
157 Float::F128 => cx.type_f128(),
158 },
159 Primitive::Pointer(_) => cx.type_ptr(),
160 };
161
162 if !self.size.bytes().is_multiple_of(hint_vector_elem.size(cx).bytes()) {
::core::panicking::panic("assertion failed: self.size.bytes().is_multiple_of(hint_vector_elem.size(cx).bytes())")
};assert!(self.size.bytes().is_multiple_of(hint_vector_elem.size(cx).bytes()));
163 let len = self.size.bytes() / hint_vector_elem.size(cx).bytes();
164 cx.type_vector(ty, len)
165 }
166 }
167 }
168}
169
170impl LlvmType for CastTarget {
171 fn llvm_type<'ll>(&self, cx: &CodegenCx<'ll, '_>) -> &'ll Type {
172 let rest_ll_unit = self.rest.unit.llvm_type(cx);
173 let rest_count = if self.rest.total == Size::ZERO {
174 0
175 } else {
176 match (&(self.rest.unit.size), &(Size::ZERO)) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("total size {0:?} cannot be divided into units of zero size",
self.rest.total)));
}
}
};assert_ne!(
177 self.rest.unit.size,
178 Size::ZERO,
179 "total size {:?} cannot be divided into units of zero size",
180 self.rest.total
181 );
182 if !self.rest.total.bytes().is_multiple_of(self.rest.unit.size.bytes()) {
183 match (&self.rest.unit.kind, &RegKind::Integer) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("only int regs can be split")));
}
}
};assert_eq!(self.rest.unit.kind, RegKind::Integer, "only int regs can be split");
184 }
185 self.rest.total.bytes().div_ceil(self.rest.unit.size.bytes())
186 };
187
188 if self.prefix.iter().all(|x| x.is_none()) {
191 if rest_count == 1 && (!self.rest.is_consecutive || self.rest.unit != Reg::i128()) {
195 return rest_ll_unit;
196 }
197
198 return cx.type_array(rest_ll_unit, rest_count);
199 }
200
201 let prefix_args =
203 self.prefix.iter().flat_map(|option_reg| option_reg.map(|reg| reg.llvm_type(cx)));
204 let rest_args = (0..rest_count).map(|_| rest_ll_unit);
205 let args: Vec<_> = prefix_args.chain(rest_args).collect();
206 cx.type_struct(&args, false)
207 }
208}
209
210trait ArgAbiExt<'ll, 'tcx> {
211 fn store(
212 &self,
213 bx: &mut Builder<'_, 'll, 'tcx>,
214 val: &'ll Value,
215 dst: PlaceRef<'tcx, &'ll Value>,
216 );
217 fn store_fn_arg(
218 &self,
219 bx: &mut Builder<'_, 'll, 'tcx>,
220 idx: &mut usize,
221 dst: PlaceRef<'tcx, &'ll Value>,
222 );
223}
224
225impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
226 fn store(
231 &self,
232 bx: &mut Builder<'_, 'll, 'tcx>,
233 val: &'ll Value,
234 dst: PlaceRef<'tcx, &'ll Value>,
235 ) {
236 match &self.mode {
237 PassMode::Ignore => {}
238 PassMode::Indirect { attrs, meta_attrs: None, on_stack: _ } => {
240 let align = attrs.pointee_align.unwrap_or(self.layout.align.abi);
241 OperandValue::Ref(PlaceValue::new_sized(val, align)).store(bx, dst);
242 }
243 PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
245 ::rustc_middle::util::bug::bug_fmt(format_args!("unsized `ArgAbi` cannot be stored"));bug!("unsized `ArgAbi` cannot be stored");
246 }
247 PassMode::Cast { cast, pad_i32: _ } => {
248 let scratch_size = cast.size(bx);
252 let scratch_align = cast.align(bx);
253 let copy_bytes =
260 cmp::min(cast.unaligned_size(bx).bytes(), self.layout.size.bytes());
261 let llscratch = bx.alloca(scratch_size, scratch_align);
263 bx.lifetime_start(llscratch, scratch_size);
264 rustc_codegen_ssa::mir::store_cast(bx, cast, val, llscratch, scratch_align);
266 bx.memcpy(
268 dst.val.llval,
269 self.layout.align.abi,
270 llscratch,
271 scratch_align,
272 bx.const_usize(copy_bytes),
273 MemFlags::empty(),
274 None,
275 );
276 bx.lifetime_end(llscratch, scratch_size);
277 }
278 PassMode::Pair(..) | PassMode::Direct { .. } => {
279 OperandRef::from_immediate_or_packed_pair(bx, val, self.layout).val.store(bx, dst);
280 }
281 }
282 }
283
284 fn store_fn_arg(
285 &self,
286 bx: &mut Builder<'_, 'll, 'tcx>,
287 idx: &mut usize,
288 dst: PlaceRef<'tcx, &'ll Value>,
289 ) {
290 let mut next = || {
291 let val = llvm::get_param(bx.llfn(), *idx as c_uint);
292 *idx += 1;
293 val
294 };
295 match self.mode {
296 PassMode::Ignore => {}
297 PassMode::Pair(..) => {
298 OperandValue::Pair(next(), next()).store(bx, dst);
299 }
300 PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
301 ::rustc_middle::util::bug::bug_fmt(format_args!("unsized `ArgAbi` cannot be stored"));bug!("unsized `ArgAbi` cannot be stored");
302 }
303 PassMode::Direct(_)
304 | PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ }
305 | PassMode::Cast { .. } => {
306 let next_arg = next();
307 self.store(bx, next_arg, dst);
308 }
309 }
310 }
311}
312
313impl<'ll, 'tcx> ArgAbiBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> {
314 fn store_fn_arg(
315 &mut self,
316 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
317 idx: &mut usize,
318 dst: PlaceRef<'tcx, Self::Value>,
319 ) {
320 arg_abi.store_fn_arg(self, idx, dst)
321 }
322 fn store_arg(
323 &mut self,
324 arg_abi: &ArgAbi<'tcx, Ty<'tcx>>,
325 val: &'ll Value,
326 dst: PlaceRef<'tcx, &'ll Value>,
327 ) {
328 arg_abi.store(self, val, dst)
329 }
330}
331
332pub(crate) trait FnAbiLlvmExt<'ll, 'tcx> {
333 fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
334 fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
335 fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv;
336
337 fn apply_attrs_llfn(
339 &self,
340 cx: &CodegenCx<'ll, 'tcx>,
341 llfn: &'ll Value,
342 instance: Option<ty::Instance<'tcx>>,
343 );
344
345 fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value);
347}
348
349impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
350 fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
351 let args =
354 if self.c_variadic { &self.args[..self.fixed_count as usize] } else { &self.args };
355
356 let mut llargument_tys = Vec::with_capacity(
358 self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 },
359 );
360
361 let llreturn_ty = match &self.ret.mode {
362 PassMode::Ignore => cx.type_void(),
363 PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_llvm_type(cx),
364 PassMode::Cast { cast, pad_i32: _ } => cast.llvm_type(cx),
365 PassMode::Indirect { .. } => {
366 llargument_tys.push(cx.type_ptr());
367 cx.type_void()
368 }
369 };
370
371 for arg in args {
372 let llarg_ty = match &arg.mode {
376 PassMode::Ignore => continue,
377 PassMode::Direct(_) => {
378 arg.layout.immediate_llvm_type(cx)
382 }
383 PassMode::Pair(..) => {
384 llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 0, true));
388 llargument_tys.push(arg.layout.scalar_pair_element_llvm_type(cx, 1, true));
389 continue;
390 }
391 PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
392 let ptr_ty = Ty::new_mut_ptr(cx.tcx, arg.layout.ty);
397 let ptr_layout = cx.layout_of(ptr_ty);
398 llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 0, true));
399 llargument_tys.push(ptr_layout.scalar_pair_element_llvm_type(cx, 1, true));
400 continue;
401 }
402 PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => cx.type_ptr(),
403 PassMode::Cast { cast, pad_i32 } => {
404 if *pad_i32 {
406 llargument_tys.push(Reg::i32().llvm_type(cx));
407 }
408 cast.llvm_type(cx)
411 }
412 };
413 llargument_tys.push(llarg_ty);
414 }
415
416 if self.c_variadic {
417 cx.type_variadic_func(&llargument_tys, llreturn_ty)
418 } else {
419 cx.type_func(&llargument_tys, llreturn_ty)
420 }
421 }
422
423 fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type {
424 cx.type_ptr_ext(cx.data_layout().instruction_address_space)
425 }
426
427 fn llvm_cconv(&self, cx: &CodegenCx<'ll, 'tcx>) -> llvm::CallConv {
428 to_llvm_calling_convention(cx.tcx.sess, self.conv)
429 }
430
431 fn apply_attrs_llfn(
432 &self,
433 cx: &CodegenCx<'ll, 'tcx>,
434 llfn: &'ll Value,
435 instance: Option<ty::Instance<'tcx>>,
436 ) {
437 let mut func_attrs = SmallVec::<[_; 3]>::new();
438 if self.ret.layout.is_uninhabited() {
439 func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(cx.llcx));
440 }
441 if !self.can_unwind {
442 func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(cx.llcx));
443 }
444 match self.conv {
445 CanonAbi::Interrupt(InterruptKind::RiscvMachine) => {
446 func_attrs.push(llvm::CreateAttrStringValue(cx.llcx, "interrupt", "machine"))
447 }
448 CanonAbi::Interrupt(InterruptKind::RiscvSupervisor) => {
449 func_attrs.push(llvm::CreateAttrStringValue(cx.llcx, "interrupt", "supervisor"))
450 }
451 CanonAbi::Arm(ArmCall::CCmseNonSecureEntry) => {
452 func_attrs.push(llvm::CreateAttrString(cx.llcx, "cmse_nonsecure_entry"))
453 }
454 _ => (),
455 }
456 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Function, &{ func_attrs });
457
458 let mut i = 0;
459 let mut apply = |attrs: &ArgAttributes| {
460 attrs.apply_attrs_to_llfn(llvm::AttributePlace::Argument(i), cx, llfn);
461 i += 1;
462 i - 1
463 };
464
465 let apply_range_attr = |idx: AttributePlace, scalar: rustc_abi::Scalar| {
466 if cx.sess().opts.optimize != config::OptLevel::No
467 && #[allow(non_exhaustive_omitted_patterns)] match scalar.primitive() {
Primitive::Int(..) => true,
_ => false,
}matches!(scalar.primitive(), Primitive::Int(..))
468 && !scalar.is_bool()
472 && !scalar.is_always_valid(cx)
474 {
475 attributes::apply_to_llfn(
476 llfn,
477 idx,
478 &[llvm::CreateRangeAttr(cx.llcx, scalar.size(cx), scalar.valid_range(cx))],
479 );
480 }
481 };
482
483 match &self.ret.mode {
484 PassMode::Direct(attrs) => {
485 attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
486 if let BackendRepr::Scalar(scalar) = self.ret.layout.backend_repr {
487 apply_range_attr(llvm::AttributePlace::ReturnValue, scalar);
488 }
489 }
490 PassMode::Indirect { attrs, meta_attrs: _, on_stack } => {
491 if !!on_stack { ::core::panicking::panic("assertion failed: !on_stack") };assert!(!on_stack);
492 let i = apply(attrs);
493 let sret = llvm::CreateStructRetAttr(
494 cx.llcx,
495 cx.type_array(cx.type_i8(), self.ret.layout.size.bytes()),
496 );
497 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[sret]);
498 if cx.sess().opts.optimize != config::OptLevel::No {
499 attributes::apply_to_llfn(
500 llfn,
501 llvm::AttributePlace::Argument(i),
502 &[
503 llvm::AttributeKind::Writable.create_attr(cx.llcx),
504 llvm::AttributeKind::DeadOnUnwind.create_attr(cx.llcx),
505 ],
506 );
507 }
508 }
509 PassMode::Cast { cast, pad_i32: _ } => {
510 cast.attrs.apply_attrs_to_llfn(llvm::AttributePlace::ReturnValue, cx, llfn);
511 }
512 _ => {}
513 }
514 for arg in self.args.iter() {
515 match &arg.mode {
516 PassMode::Ignore => {}
517 PassMode::Indirect { attrs, meta_attrs: None, on_stack: true } => {
518 let i = apply(attrs);
519 let byval = llvm::CreateByValAttr(
520 cx.llcx,
521 cx.type_array(cx.type_i8(), arg.layout.size.bytes()),
522 );
523 attributes::apply_to_llfn(llfn, llvm::AttributePlace::Argument(i), &[byval]);
524 }
525 PassMode::Direct(attrs) => {
526 let i = apply(attrs);
527 if let BackendRepr::Scalar(scalar) = arg.layout.backend_repr {
528 apply_range_attr(llvm::AttributePlace::Argument(i), scalar);
529 }
530 }
531 PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
532 let i = apply(attrs);
533 if cx.sess().opts.optimize != config::OptLevel::No {
534 attributes::apply_to_llfn(
535 llfn,
536 llvm::AttributePlace::Argument(i),
537 &[llvm::AttributeKind::DeadOnReturn.create_attr(cx.llcx)],
538 );
539 }
540 }
541 PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
542 if !!on_stack { ::core::panicking::panic("assertion failed: !on_stack") };assert!(!on_stack);
543 apply(attrs);
544 apply(meta_attrs);
545 }
546 PassMode::Pair(a, b) => {
547 let i = apply(a);
548 let ii = apply(b);
549 if let BackendRepr::ScalarPair(scalar_a, scalar_b) = arg.layout.backend_repr {
550 apply_range_attr(llvm::AttributePlace::Argument(i), scalar_a);
551 let primitive_b = scalar_b.primitive();
552 let scalar_b = if let rustc_abi::Primitive::Int(int, false) = primitive_b
553 && let ty::Ref(_, pointee_ty, _) = *arg.layout.ty.kind()
554 && let ty::Slice(element_ty) = *pointee_ty.kind()
555 && let elem_size = cx.layout_of(element_ty).size
556 && elem_size != rustc_abi::Size::ZERO
557 {
558 if true {
if !scalar_b.is_always_valid(cx) {
::core::panicking::panic("assertion failed: scalar_b.is_always_valid(cx)")
};
};debug_assert!(scalar_b.is_always_valid(cx));
562 let isize_max = int.signed_max() as u64;
563 rustc_abi::Scalar::Initialized {
564 value: primitive_b,
565 valid_range: rustc_abi::WrappingRange {
566 start: 0,
567 end: u128::from(isize_max / elem_size.bytes()),
568 },
569 }
570 } else {
571 scalar_b
572 };
573 apply_range_attr(llvm::AttributePlace::Argument(ii), scalar_b);
574 }
575 }
576 PassMode::Cast { cast, pad_i32 } => {
577 if *pad_i32 {
578 apply(&ArgAttributes::new());
579 }
580 apply(&cast.attrs);
581 }
582 }
583 }
584
585 if let Some(instance) = instance {
587 llfn_attrs_from_instance(
588 cx,
589 cx.tcx,
590 llfn,
591 &cx.tcx.codegen_instance_attrs(instance.def),
592 Some(instance),
593 );
594 }
595 }
596
597 fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {
598 let mut func_attrs = SmallVec::<[_; 2]>::new();
599 if self.ret.layout.is_uninhabited() {
600 func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(bx.cx.llcx));
601 }
602 if !self.can_unwind {
603 func_attrs.push(llvm::AttributeKind::NoUnwind.create_attr(bx.cx.llcx));
604 }
605 attributes::apply_to_callsite(callsite, llvm::AttributePlace::Function, &{ func_attrs });
606
607 let mut i = 0;
608 let mut apply = |cx: &CodegenCx<'_, '_>, attrs: &ArgAttributes| {
609 attrs.apply_attrs_to_callsite(llvm::AttributePlace::Argument(i), cx, callsite);
610 i += 1;
611 i - 1
612 };
613 match &self.ret.mode {
614 PassMode::Direct(attrs) => {
615 attrs.apply_attrs_to_callsite(llvm::AttributePlace::ReturnValue, bx.cx, callsite);
616 }
617 PassMode::Indirect { attrs, meta_attrs: _, on_stack } => {
618 if !!on_stack { ::core::panicking::panic("assertion failed: !on_stack") };assert!(!on_stack);
619 let i = apply(bx.cx, attrs);
620 let sret = llvm::CreateStructRetAttr(
621 bx.cx.llcx,
622 bx.cx.type_array(bx.cx.type_i8(), self.ret.layout.size.bytes()),
623 );
624 attributes::apply_to_callsite(callsite, llvm::AttributePlace::Argument(i), &[sret]);
625 }
626 PassMode::Cast { cast, pad_i32: _ } => {
627 cast.attrs.apply_attrs_to_callsite(
628 llvm::AttributePlace::ReturnValue,
629 bx.cx,
630 callsite,
631 );
632 }
633 _ => {}
634 }
635 for arg in self.args.iter() {
636 match &arg.mode {
637 PassMode::Ignore => {}
638 PassMode::Indirect { attrs, meta_attrs: None, on_stack: true } => {
639 let i = apply(bx.cx, attrs);
640 let byval = llvm::CreateByValAttr(
641 bx.cx.llcx,
642 bx.cx.type_array(bx.cx.type_i8(), arg.layout.size.bytes()),
643 );
644 attributes::apply_to_callsite(
645 callsite,
646 llvm::AttributePlace::Argument(i),
647 &[byval],
648 );
649 }
650 PassMode::Direct(attrs)
651 | PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
652 apply(bx.cx, attrs);
653 }
654 PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack: _ } => {
655 apply(bx.cx, attrs);
656 apply(bx.cx, meta_attrs);
657 }
658 PassMode::Pair(a, b) => {
659 apply(bx.cx, a);
660 apply(bx.cx, b);
661 }
662 PassMode::Cast { cast, pad_i32 } => {
663 if *pad_i32 {
664 apply(bx.cx, &ArgAttributes::new());
665 }
666 apply(bx.cx, &cast.attrs);
667 }
668 }
669 }
670
671 let cconv = self.llvm_cconv(&bx.cx);
672 if cconv != llvm::CCallConv {
673 llvm::SetInstructionCallConv(callsite, cconv);
674 }
675
676 if self.conv == CanonAbi::Arm(ArmCall::CCmseNonSecureCall) {
677 let cmse_nonsecure_call = llvm::CreateAttrString(bx.cx.llcx, "cmse_nonsecure_call");
680 attributes::apply_to_callsite(
681 callsite,
682 llvm::AttributePlace::Function,
683 &[cmse_nonsecure_call],
684 );
685 }
686
687 let element_type_index = unsafe { llvm::LLVMRustGetElementTypeArgIndex(callsite) };
690 if element_type_index >= 0 {
691 let arg_ty = self.args[element_type_index as usize].layout.ty;
692 let pointee_ty = arg_ty.builtin_deref(true).expect("Must be pointer argument");
693 let element_type_attr = unsafe {
694 llvm::LLVMRustCreateElementTypeAttr(bx.llcx, bx.layout_of(pointee_ty).llvm_type(bx))
695 };
696 attributes::apply_to_callsite(
697 callsite,
698 llvm::AttributePlace::Argument(element_type_index as u32),
699 &[element_type_attr],
700 );
701 }
702 }
703}
704
705impl AbiBuilderMethods for Builder<'_, '_, '_> {
706 fn get_param(&mut self, index: usize) -> Self::Value {
707 llvm::get_param(self.llfn(), index as c_uint)
708 }
709}
710
711pub(crate) fn to_llvm_calling_convention(sess: &Session, abi: CanonAbi) -> llvm::CallConv {
714 match abi {
715 CanonAbi::C | CanonAbi::Rust => llvm::CCallConv,
716 CanonAbi::RustCold => llvm::PreserveMost,
717 CanonAbi::RustPreserveNone => match &sess.target.arch {
718 Arch::X86_64 | Arch::AArch64 => llvm::PreserveNone,
719 _ => llvm::CCallConv,
720 },
721 CanonAbi::Custom => llvm::CCallConv,
725 CanonAbi::GpuKernel => match &sess.target.arch {
726 Arch::AmdGpu => llvm::AmdgpuKernel,
727 Arch::Nvptx64 => llvm::PtxKernel,
728 arch => {
::core::panicking::panic_fmt(format_args!("Architecture {0} does not support GpuKernel calling convention",
arch));
}panic!("Architecture {arch} does not support GpuKernel calling convention"),
729 },
730 CanonAbi::Interrupt(interrupt_kind) => match interrupt_kind {
731 InterruptKind::Avr => llvm::AvrInterrupt,
732 InterruptKind::AvrNonBlocking => llvm::AvrNonBlockingInterrupt,
733 InterruptKind::Msp430 => llvm::Msp430Intr,
734 InterruptKind::RiscvMachine | InterruptKind::RiscvSupervisor => llvm::CCallConv,
735 InterruptKind::X86 => llvm::X86_Intr,
736 },
737 CanonAbi::Arm(arm_call) => match arm_call {
738 ArmCall::Aapcs => llvm::ArmAapcsCallConv,
739 ArmCall::CCmseNonSecureCall | ArmCall::CCmseNonSecureEntry => llvm::CCallConv,
740 },
741 CanonAbi::X86(x86_call) => match x86_call {
742 X86Call::Fastcall => llvm::X86FastcallCallConv,
743 X86Call::Stdcall => llvm::X86StdcallCallConv,
744 X86Call::SysV64 => llvm::X86_64_SysV,
745 X86Call::Thiscall => llvm::X86_ThisCall,
746 X86Call::Vectorcall => llvm::X86_VectorCall,
747 X86Call::Win64 => llvm::X86_64_Win64,
748 },
749 }
750}