1use rustc_attr_ir::{
3 InlineAttr, InstructionSetAttr, InstrumentFnAttr, OptimizeAttr, RtsanSetting, find_attr,
4};
5use rustc_hir::def_id::DefId;
6use rustc_middle::middle::codegen_fn_attrs::{
7 CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs, TargetFeature,
8};
9use rustc_middle::ty::{self, Instance, TyCtxt};
10use rustc_sanitizers::ignorelist::SanitizerIgnoreList;
11use rustc_session::Session;
12use rustc_session::config::{
13 BranchProtection, FunctionReturn, InstrumentMcount, InstrumentMcountOpts, OptLevel, PAuthKey,
14 PacRet,
15};
16use rustc_span::sym;
17use rustc_symbol_mangling::mangle_internal_symbol;
18use rustc_target::spec::{Arch, FramePointer, SanitizerSet, StackProbeType, StackProtector};
19use smallvec::SmallVec;
20
21use crate::context::SimpleCx;
22use crate::diagnostics::{PackedStackBackchainNeedsSoftfloat, SanitizerMemtagRequiresMte};
23use crate::llvm::AttributePlace::Function;
24use crate::llvm::{
25 self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects, Value,
26};
27use crate::{attributes, llvm_util};
28
29pub(crate) fn apply_to_llfn<'ll>(llfn: Value<'ll>, idx: AttributePlace, attrs: &[Attribute<'ll>]) {
30 if !attrs.is_empty() {
31 llvm::AddFunctionAttributes(llfn, idx, attrs);
32 }
33}
34
35pub(crate) fn apply_to_callsite<'ll>(
36 callsite: Value<'ll>,
37 idx: AttributePlace,
38 attrs: &[Attribute<'ll>],
39) {
40 if !attrs.is_empty() {
41 llvm::AddCallSiteAttributes(callsite, idx, attrs);
42 }
43}
44
45pub(crate) fn has_string_attr<'ll>(llfn: Value<'ll>, name: &str) -> bool {
46 llvm::HasStringAttribute(llfn, name)
47}
48
49pub(crate) fn remove_string_attr_from_llfn<'ll>(llfn: Value<'ll>, name: &str) {
50 llvm::RemoveStringAttrFromFn(llfn, name);
51}
52
53#[inline]
55pub(crate) fn inline_attr<'tcx, 'll>(
56 cx: &SimpleCx<'ll>,
57 tcx: TyCtxt<'tcx>,
58 instance: Instance<'tcx>,
59 codegen_fn_attrs: &CodegenFnAttrs,
60) -> Option<Attribute<'ll>> {
61 if !tcx.sess.opts.unstable_opts.inline_llvm {
62 return Some(AttributeKind::NoInline.create_attr(cx.llcx));
64 }
65
66 let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
68 (_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
69 (InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
70 (inline, _) => inline,
71 };
72
73 match inline {
74 InlineAttr::Hint => Some(AttributeKind::InlineHint.create_attr(cx.llcx)),
75 InlineAttr::Always
78 if !codegen_fn_attrs.target_features.is_empty()
79 && llvm_util::get_version() < (23, 0, 0) =>
80 {
81 Some(AttributeKind::InlineHint.create_attr(cx.llcx))
82 }
83 InlineAttr::Always | InlineAttr::Force { .. } => {
84 Some(AttributeKind::AlwaysInline.create_attr(cx.llcx))
85 }
86 InlineAttr::Never => {
87 if tcx.sess.target.arch != Arch::AmdGpu {
88 Some(AttributeKind::NoInline.create_attr(cx.llcx))
89 } else {
90 None
91 }
92 }
93 InlineAttr::None => None,
94 }
95}
96
97#[inline]
98fn patchable_function_entry_attrs<'ll>(
99 cx: &SimpleCx<'ll>,
100 sess: &Session,
101 attr: Option<PatchableFunctionEntry>,
102) -> SmallVec<[Attribute<'ll>; 2]> {
103 let mut attrs = SmallVec::new();
104
105 let mut entry = sess.opts.unstable_opts.patchable_function_entry.entry();
106 let mut prefix = sess.opts.unstable_opts.patchable_function_entry.prefix();
107 let mut section = sess.opts.unstable_opts.patchable_function_entry.section();
108 let section_sym;
109
110 if let Some(patchable_spec) = attr {
112 if let Some(sym) = patchable_spec.section() {
113 section_sym = sym;
114 section = Some(section_sym.as_str());
115 }
116 if patchable_spec.entry().is_some() || patchable_spec.prefix().is_some() {
119 entry = patchable_spec.entry().unwrap_or(0);
120 prefix = patchable_spec.prefix().unwrap_or(0);
121 }
122 }
123
124 if entry > 0 {
125 attrs.push(llvm::CreateAttrStringValue(
126 cx.llcx,
127 "patchable-function-entry",
128 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", entry))
})format!("{}", entry),
129 ));
130 }
131 if prefix > 0 {
132 attrs.push(llvm::CreateAttrStringValue(
133 cx.llcx,
134 "patchable-function-prefix",
135 &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", prefix))
})format!("{}", prefix),
136 ));
137 }
138 if let Some(section) = section {
139 attrs.push(llvm::CreateAttrStringValue(
140 cx.llcx,
141 "patchable-function-entry-section",
142 section,
143 ));
144 }
145 attrs
146}
147
148#[inline]
150pub(crate) fn sanitize_attrs<'ll, 'tcx>(
151 cx: &SimpleCx<'ll>,
152 tcx: TyCtxt<'tcx>,
153 sanitizer_fn_attr: SanitizerFnAttrs,
154 instance: Option<ty::Instance<'tcx>>,
155 sanitizer_ignorelist: Option<&SanitizerIgnoreList>,
156) -> SmallVec<[Attribute<'ll>; 4]> {
157 let mut attrs = SmallVec::new();
158 let mut enabled = tcx.sess.sanitizers() - sanitizer_fn_attr.disabled;
159 if let Some(ignorelist) = sanitizer_ignorelist {
160 if let Some(instance) = instance {
161 let result = ignorelist.filter_instance_sanitizers(tcx, instance, enabled);
162 enabled = result.enabled;
163 if result.ignore_cfi {
164 attrs.push(llvm::CreateAttrString(cx.llcx, "no-sanitize-cfi"));
165 }
166 if result.ignore_kcfi {
167 attrs.push(llvm::CreateAttrString(cx.llcx, "no-sanitize-kcfi"));
168 }
169 }
170 }
171
172 if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
173 attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
174 }
175 if enabled.contains(SanitizerSet::MEMORY) {
176 attrs.push(llvm::AttributeKind::SanitizeMemory.create_attr(cx.llcx));
177 }
178 if enabled.contains(SanitizerSet::THREAD) {
179 attrs.push(llvm::AttributeKind::SanitizeThread.create_attr(cx.llcx));
180 }
181 if enabled.contains(SanitizerSet::HWADDRESS) || enabled.contains(SanitizerSet::KERNELHWADDRESS)
182 {
183 attrs.push(llvm::AttributeKind::SanitizeHWAddress.create_attr(cx.llcx));
184 }
185 if enabled.contains(SanitizerSet::SHADOWCALLSTACK) {
186 attrs.push(llvm::AttributeKind::ShadowCallStack.create_attr(cx.llcx));
187 }
188 if enabled.contains(SanitizerSet::MEMTAG) {
189 let features = &tcx.sess.backend_target_features;
191 let mte_feature =
192 features.iter().map(|s| &s[..]).rfind(|n| ["+mte", "-mte"].contains(&&n[..]));
193 if let None | Some("-mte") = mte_feature {
194 tcx.dcx().emit_err(SanitizerMemtagRequiresMte);
195 }
196
197 attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx));
198 }
199 if enabled.contains(SanitizerSet::SAFESTACK) {
200 attrs.push(llvm::AttributeKind::SanitizeSafeStack.create_attr(cx.llcx));
201 }
202 if tcx.sess.sanitizers().contains(SanitizerSet::REALTIME) {
203 match sanitizer_fn_attr.rtsan_setting {
204 RtsanSetting::Nonblocking => {
205 attrs.push(llvm::AttributeKind::SanitizeRealtimeNonblocking.create_attr(cx.llcx))
206 }
207 RtsanSetting::Blocking => {
208 attrs.push(llvm::AttributeKind::SanitizeRealtimeBlocking.create_attr(cx.llcx))
209 }
210 RtsanSetting::Caller => (),
212 }
213 }
214 attrs
215}
216
217#[inline]
219pub(crate) fn uwtable_attr<'ll>(
220 llcx: llvm::Context<'ll>,
221 use_sync_unwind: Option<bool>,
222) -> Attribute<'ll> {
223 let async_unwind = !use_sync_unwind.unwrap_or(false);
229 llvm::CreateUWTableAttr(llcx, async_unwind)
230}
231
232pub(crate) fn frame_pointer(sess: &Session) -> FramePointer {
233 let mut fp = sess.target.frame_pointer;
234 let opts = &sess.opts;
235 if let InstrumentMcount::Mcount(_) = opts.unstable_opts.instrument_mcount {
238 fp.ratchet(FramePointer::Always);
239 }
240 if !(sess.target.arch == Arch::S390x) {
243 fp.ratchet(opts.cg.force_frame_pointers);
244 }
245 fp
246}
247
248pub(crate) fn frame_pointer_type_attr<'ll>(
249 cx: &SimpleCx<'ll>,
250 sess: &Session,
251) -> Option<Attribute<'ll>> {
252 let fp = frame_pointer(sess);
253 let attr_value = match fp {
254 FramePointer::Always => "all",
255 FramePointer::NonLeaf => "non-leaf",
256 FramePointer::MayOmit => return None,
257 };
258 Some(llvm::CreateAttrStringValue(cx.llcx, "frame-pointer", attr_value))
259}
260
261fn function_return_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<Attribute<'ll>> {
262 let function_return_attr = match sess.opts.unstable_opts.function_return {
263 FunctionReturn::Keep => return None,
264 FunctionReturn::ThunkExtern => AttributeKind::FnRetThunkExtern,
265 };
266
267 Some(function_return_attr.create_attr(cx.llcx))
268}
269
270#[inline]
272fn instrument_function_attr<'ll>(
273 cx: &SimpleCx<'ll>,
274 sess: &Session,
275 instrument_fn: Option<InstrumentFnAttr>,
276) -> SmallVec<[Attribute<'ll>; 4]> {
277 let mut attrs = SmallVec::new();
278 if sess.opts.unstable_opts.instrument_mcount != InstrumentMcount::Disabled {
279 let instrument_entry = match instrument_fn {
283 Some(InstrumentFnAttr::On) | None => true,
284 Some(InstrumentFnAttr::Off) => false,
285 };
286
287 if instrument_entry {
288 let mut opts = InstrumentMcountOpts::default();
289 match sess.opts.unstable_opts.instrument_mcount {
290 InstrumentMcount::Mcount(mopts) => {
291 let mcount_name = match &sess.target.llvm_mcount_intrinsic {
294 Some(llvm_mcount_intrinsic) => llvm_mcount_intrinsic.as_ref(),
295 None => sess.target.mcount.as_ref(),
296 };
297
298 attrs.push(llvm::CreateAttrStringValue(
299 cx.llcx,
300 "instrument-function-entry-inlined",
301 mcount_name,
302 ));
303 opts = mopts;
304 }
305 InstrumentMcount::Fentry(fopts) => {
306 attrs.push(llvm::CreateAttrStringValue(cx.llcx, "fentry-call", "true"));
307 opts = fopts;
308 }
309 InstrumentMcount::Disabled => {}
310 }
311 if opts.no_call {
312 attrs.push(llvm::CreateAttrString(cx.llcx, "mnop-mcount"));
313 }
314 if opts.record {
315 attrs.push(llvm::CreateAttrString(cx.llcx, "mrecord-mcount"));
316 }
317 }
318 }
319 if let Some(options) = &sess.opts.unstable_opts.instrument_xray {
320 let mut never = options.never;
325 let mut always = options.always;
326
327 match instrument_fn {
329 Some(InstrumentFnAttr::On) => {
330 always = true;
331 }
332 Some(InstrumentFnAttr::Off) => {
333 never = true;
334 }
335 None => {}
336 }
337
338 if never {
339 attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-never"));
340 }
341 if always {
342 attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-always"));
343 }
344
345 if options.ignore_loops {
346 attrs.push(llvm::CreateAttrString(cx.llcx, "xray-ignore-loops"));
347 }
348 let threshold = options.instruction_threshold.unwrap_or(200);
351 attrs.push(llvm::CreateAttrStringValue(
352 cx.llcx,
353 "xray-instruction-threshold",
354 &threshold.to_string(),
355 ));
356 if options.skip_entry {
357 attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-entry"));
358 }
359 if options.skip_exit {
360 attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-exit"));
361 }
362 }
363 attrs
364}
365
366fn nojumptables_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<Attribute<'ll>> {
367 if sess.opts.cg.jump_tables {
368 return None;
369 }
370
371 Some(llvm::CreateAttrStringValue(cx.llcx, "no-jump-tables", "true"))
372}
373
374fn probestack_attr<'ll, 'tcx>(cx: &SimpleCx<'ll>, tcx: TyCtxt<'tcx>) -> Option<Attribute<'ll>> {
375 if tcx.sess.sanitizers().intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD) {
379 return None;
380 }
381
382 if tcx.sess.opts.cg.profile_generate.enabled() {
384 return None;
385 }
386
387 let attr_value = match tcx.sess.target.stack_probes {
388 StackProbeType::None => return None,
389 StackProbeType::Inline => "inline-asm",
392 StackProbeType::Call => &mangle_internal_symbol(tcx, "__rust_probestack"),
395 StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
397 if llvm_util::get_version() < min_llvm_version_for_inline {
398 &mangle_internal_symbol(tcx, "__rust_probestack")
399 } else {
400 "inline-asm"
401 }
402 }
403 };
404 Some(llvm::CreateAttrStringValue(cx.llcx, "probe-stack", attr_value))
405}
406
407fn stackprotector_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<Attribute<'ll>> {
408 let sspattr = match sess.stack_protector() {
409 StackProtector::None => return None,
410 StackProtector::All => AttributeKind::StackProtectReq,
411 StackProtector::Strong => AttributeKind::StackProtectStrong,
412 };
413
414 Some(sspattr.create_attr(cx.llcx))
415}
416
417fn s390x_fn_attrs<'ll>(
421 cx: &SimpleCx<'ll>,
422 sess: &Session,
423 fn_target_features: &[TargetFeature],
424) -> Vec<Attribute<'ll>> {
425 if sess.target.arch != Arch::S390x {
426 return ::alloc::vec::Vec::new()vec![];
427 }
428
429 let mut attrs = Vec::new();
430
431 let have_backchain = sess.opts.cg.force_frame_pointers != FramePointer::MayOmit;
433 if have_backchain {
434 attrs.push(llvm::CreateAttrString(cx.llcx, "backchain"));
435 }
436
437 if !sess.opts.unstable_opts.packed_stack {
439 return attrs;
440 }
441
442 let have_softfloat = sess.rust_target_features.contains(&sym::soft_float)
443 || fn_target_features.iter().any(|f| f.name == sym::soft_float);
444
445 if have_backchain && !have_softfloat {
449 sess.dcx().emit_err(PackedStackBackchainNeedsSoftfloat);
450 return attrs;
451 }
452
453 attrs.push(llvm::CreateAttrString(cx.llcx, "packed-stack"));
454 attrs
455}
456
457pub(crate) fn target_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Attribute<'ll> {
458 let target_cpu = llvm_util::target_cpu(sess);
459 llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu)
460}
461
462pub(crate) fn tune_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<Attribute<'ll>> {
463 llvm_util::tune_cpu(sess)
464 .map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu))
465}
466
467pub(crate) fn target_features_attr<'ll, 'tcx>(
469 cx: &SimpleCx<'ll>,
470 tcx: TyCtxt<'tcx>,
471 function_features: Vec<String>,
472) -> Option<Attribute<'ll>> {
473 let global_features = tcx.sess.backend_target_features.iter().map(String::as_str);
474 let function_features = function_features.iter().map(String::as_str);
475 let target_features =
476 global_features.chain(function_features).intersperse(",").collect::<String>();
477 (!target_features.is_empty())
478 .then(|| llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features))
479}
480
481pub(crate) fn non_lazy_bind_attr<'ll>(
484 cx: &SimpleCx<'ll>,
485 sess: &Session,
486) -> Option<Attribute<'ll>> {
487 if !sess.needs_plt() { Some(AttributeKind::NonLazyBind.create_attr(cx.llcx)) } else { None }
489}
490
491#[inline]
493pub(crate) fn default_optimisation_attrs<'ll>(
494 cx: &SimpleCx<'ll>,
495 sess: &Session,
496) -> SmallVec<[Attribute<'ll>; 2]> {
497 let mut attrs = SmallVec::new();
498 match sess.opts.optimize {
501 OptLevel::Size => {
502 attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
503 }
504 OptLevel::SizeMin => {
505 attrs.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
506 attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
507 }
508 _ => {}
509 }
510 attrs
511}
512
513fn create_alloc_family_attr<'ll>(llcx: llvm::Context<'ll>) -> llvm::Attribute<'ll> {
514 llvm::CreateAttrStringValue(llcx, "alloc-family", "__rust_alloc")
515}
516
517pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
521 cx: &SimpleCx<'ll>,
522 tcx: TyCtxt<'tcx>,
523 llfn: Value<'ll>,
524 codegen_fn_attrs: &CodegenFnAttrs,
525 instance: Option<ty::Instance<'tcx>>,
526 sanitizer_ignorelist: Option<&SanitizerIgnoreList>,
527) {
528 let sess = tcx.sess;
529 let mut to_add = SmallVec::<[_; 16]>::new();
530
531 match codegen_fn_attrs.optimize {
532 OptimizeAttr::Default => {
533 to_add.extend(default_optimisation_attrs(cx, sess));
534 }
535 OptimizeAttr::DoNotOptimize => {
536 to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
537 }
538 OptimizeAttr::Size => {
539 to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
540 to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
541 }
542 OptimizeAttr::Speed => {}
543 }
544
545 if let Some(instance) = instance {
546 to_add.extend(inline_attr(cx, tcx, instance, codegen_fn_attrs));
547 }
548
549 if sess.must_emit_unwind_tables() {
550 to_add.push(uwtable_attr(cx.llcx, sess.opts.unstable_opts.use_sync_unwind));
551 }
552
553 if sess.opts.cg.profile_sample_use.is_some() {
554 to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
555 }
556
557 to_add.extend(frame_pointer_type_attr(cx, sess));
559 to_add.extend(function_return_attr(cx, sess));
560 to_add.extend(instrument_function_attr(cx, sess, codegen_fn_attrs.instrument_fn));
561 to_add.extend(nojumptables_attr(cx, sess));
562 to_add.extend(probestack_attr(cx, tcx));
563 to_add.extend(stackprotector_attr(cx, sess));
564
565 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_BUILTINS) {
566 to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins"));
567 }
568
569 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::OFFLOAD_KERNEL) {
570 to_add.push(llvm::CreateAttrString(cx.llcx, "offload-kernel"))
571 }
572
573 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
574 to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
575 }
576 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
577 to_add.push(MemoryEffects::ReadOnly.create_attr(cx.llcx));
578 }
579 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
580 to_add.push(MemoryEffects::None.create_attr(cx.llcx));
581 }
582 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
583 } else {
587 to_add.extend(sanitize_attrs(
589 cx,
590 tcx,
591 codegen_fn_attrs.sanitizers,
592 instance,
593 sanitizer_ignorelist,
594 ));
595
596 if let Some(BranchProtection { bti, pac_ret, gcs }) = sess.branch_protection() {
598 if !(sess.target.arch == Arch::AArch64) {
::core::panicking::panic("assertion failed: sess.target.arch == Arch::AArch64")
};assert!(sess.target.arch == Arch::AArch64);
599 if bti {
600 to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement"));
601 }
602 if gcs {
603 to_add.push(llvm::CreateAttrString(cx.llcx, "guarded-control-stack"));
604 }
605 if let Some(PacRet { leaf, pc, key }) = pac_ret {
606 if pc {
607 to_add.push(llvm::CreateAttrString(cx.llcx, "branch-protection-pauth-lr"));
608 }
609 to_add.push(llvm::CreateAttrStringValue(
610 cx.llcx,
611 "sign-return-address",
612 if leaf { "all" } else { "non-leaf" },
613 ));
614 to_add.push(llvm::CreateAttrStringValue(
615 cx.llcx,
616 "sign-return-address-key",
617 if key == PAuthKey::A { "a_key" } else { "b_key" },
618 ));
619 }
620 }
621 }
622 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR)
623 || codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR_ZEROED)
624 {
625 to_add.push(create_alloc_family_attr(cx.llcx));
626 if let Some(instance) = instance
627 && let Some(name) =
628 {
{
'done:
{
for i in
::rustc_attr_ir::HasAttrs::get_attrs(instance.def_id(), &tcx)
{
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(RustcAllocatorZeroedVariant {
name }) => {
break 'done Some(name);
}
::rustc_attr_ir::Attribute::Tool { .. } |
::rustc_attr_ir::Attribute::DeriveHelper(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}find_attr!(tcx, instance.def_id(), RustcAllocatorZeroedVariant {name} => name)
629 {
630 to_add.push(llvm::CreateAttrStringValue(
631 cx.llcx,
632 "alloc-variant-zeroed",
633 &mangle_internal_symbol(tcx, name.as_str()),
634 ));
635 }
636 let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
638 attributes::apply_to_llfn(llfn, AttributePlace::Argument(1), &[alloc_align]);
639 to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 0));
640 let mut flags = AllocKindFlags::Alloc | AllocKindFlags::Aligned;
641 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
642 flags |= AllocKindFlags::Uninitialized;
643 } else {
644 flags |= AllocKindFlags::Zeroed;
645 }
646 to_add.push(llvm::CreateAllocKindAttr(cx.llcx, flags));
647 let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
650 attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
651 }
652 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::REALLOCATOR) {
653 to_add.push(create_alloc_family_attr(cx.llcx));
654 to_add.push(llvm::CreateAllocKindAttr(
655 cx.llcx,
656 AllocKindFlags::Realloc | AllocKindFlags::Aligned,
657 ));
658 let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
660 attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
661 let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
663 attributes::apply_to_llfn(llfn, AttributePlace::Argument(2), &[alloc_align]);
664 to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 3));
665 let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
666 attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
667 }
668 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::DEALLOCATOR) {
669 to_add.push(create_alloc_family_attr(cx.llcx));
670 to_add.push(llvm::CreateAllocKindAttr(cx.llcx, AllocKindFlags::Free));
671 let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
673 let captures_addr = AttributeKind::CapturesAddress.create_attr(cx.llcx);
677 let attrs = &[allocated_pointer, captures_addr];
678 attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), attrs);
679 }
680 if let Some(align) = codegen_fn_attrs.alignment {
681 llvm::set_alignment(llfn, align);
682 }
683
684 if sess.target.arch == Arch::S390x {
686 to_add.extend(s390x_fn_attrs(cx, sess, &codegen_fn_attrs.target_features));
687 }
688
689 to_add.extend(patchable_function_entry_attrs(
690 cx,
691 sess,
692 codegen_fn_attrs.patchable_function_entry,
693 ));
694
695 to_add.push(target_cpu_attr(cx, sess));
699 to_add.extend(tune_cpu_attr(cx, sess));
702
703 let function_features =
704 codegen_fn_attrs.target_features.iter().map(|f| f.name.as_str()).collect::<Vec<&str>>();
705
706 let function_features = function_features
707 .iter()
708 .flat_map(|feat| llvm_util::to_llvm_features(&sess.target, feat))
710 .flat_map(|feat| feat.into_iter().map(|f| ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("+{0}", f)) })format!("+{f}")))
712 .chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x {
713 InstructionSetAttr::ArmA32 => "-thumb-mode".to_string(),
714 InstructionSetAttr::ArmT32 => "+thumb-mode".to_string(),
715 }))
716 .collect::<Vec<String>>();
717
718 if sess.target.is_like_wasm {
719 if let Some(instance) = instance
722 && let Some(module) = wasm_import_module(tcx, instance.def_id())
723 {
724 to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-module", module));
725
726 let name =
727 codegen_fn_attrs.symbol_name.unwrap_or_else(|| tcx.item_name(instance.def_id()));
728 let name = name.as_str();
729 to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-name", name));
730 }
731 }
732
733 if sess.pointer_authentication() {
734 let cfg = sess.pointer_auth_config.as_ref().unwrap();
735 for ptrauth_attr in cfg.fn_attrs() {
736 to_add.push(llvm::CreateAttrString(cx.llcx, ptrauth_attr));
737 }
738 }
739
740 to_add.extend(target_features_attr(cx, tcx, function_features));
741
742 attributes::apply_to_llfn(llfn, Function, &to_add);
743}
744
745fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<&String> {
746 tcx.wasm_import_module_map(id.krate).get(&id)
747}