Skip to main content

rustc_codegen_llvm/
attributes.rs

1//! Set and unset common attributes on LLVM values.
2use 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(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
30    if !attrs.is_empty() {
31        llvm::AddFunctionAttributes(llfn, idx, attrs);
32    }
33}
34
35pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
36    if !attrs.is_empty() {
37        llvm::AddCallSiteAttributes(callsite, idx, attrs);
38    }
39}
40
41pub(crate) fn has_string_attr(llfn: &Value, name: &str) -> bool {
42    llvm::HasStringAttribute(llfn, name)
43}
44
45pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
46    llvm::RemoveStringAttrFromFn(llfn, name);
47}
48
49/// Get LLVM attribute for the provided inline heuristic.
50#[inline]
51pub(crate) fn inline_attr<'tcx, 'll>(
52    cx: &SimpleCx<'ll>,
53    tcx: TyCtxt<'tcx>,
54    instance: Instance<'tcx>,
55    codegen_fn_attrs: &CodegenFnAttrs,
56) -> Option<&'ll Attribute> {
57    if !tcx.sess.opts.unstable_opts.inline_llvm {
58        // disable LLVM inlining
59        return Some(AttributeKind::NoInline.create_attr(cx.llcx));
60    }
61
62    // `optnone` requires `noinline`
63    let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
64        (_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
65        (InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
66        (inline, _) => inline,
67    };
68
69    match inline {
70        InlineAttr::Hint => Some(AttributeKind::InlineHint.create_attr(cx.llcx)),
71        // LLVM 22 and older may inline a target-featured function into a caller
72        // that lacks those features. Keep the function inlineable, but do not force it.
73        InlineAttr::Always
74            if !codegen_fn_attrs.target_features.is_empty()
75                && llvm_util::get_version() < (23, 0, 0) =>
76        {
77            Some(AttributeKind::InlineHint.create_attr(cx.llcx))
78        }
79        InlineAttr::Always | InlineAttr::Force { .. } => {
80            Some(AttributeKind::AlwaysInline.create_attr(cx.llcx))
81        }
82        InlineAttr::Never => {
83            if tcx.sess.target.arch != Arch::AmdGpu {
84                Some(AttributeKind::NoInline.create_attr(cx.llcx))
85            } else {
86                None
87            }
88        }
89        InlineAttr::None => None,
90    }
91}
92
93#[inline]
94fn patchable_function_entry_attrs<'ll>(
95    cx: &SimpleCx<'ll>,
96    sess: &Session,
97    attr: Option<PatchableFunctionEntry>,
98) -> SmallVec<[&'ll Attribute; 2]> {
99    let mut attrs = SmallVec::new();
100
101    let mut entry = sess.opts.unstable_opts.patchable_function_entry.entry();
102    let mut prefix = sess.opts.unstable_opts.patchable_function_entry.prefix();
103    let mut section = sess.opts.unstable_opts.patchable_function_entry.section();
104    let section_sym;
105
106    // Apply attribute specified overrides, if any.
107    if let Some(patchable_spec) = attr {
108        if let Some(sym) = patchable_spec.section() {
109            section_sym = sym;
110            section = Some(section_sym.as_str());
111        }
112        // Override the nop counts if either is present. If only one is present, the
113        // other count is implied to be 0.
114        if patchable_spec.entry().is_some() || patchable_spec.prefix().is_some() {
115            entry = patchable_spec.entry().unwrap_or(0);
116            prefix = patchable_spec.prefix().unwrap_or(0);
117        }
118    }
119
120    if entry > 0 {
121        attrs.push(llvm::CreateAttrStringValue(
122            cx.llcx,
123            "patchable-function-entry",
124            &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", entry))
    })format!("{}", entry),
125        ));
126    }
127    if prefix > 0 {
128        attrs.push(llvm::CreateAttrStringValue(
129            cx.llcx,
130            "patchable-function-prefix",
131            &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}", prefix))
    })format!("{}", prefix),
132        ));
133    }
134    if let Some(section) = section {
135        attrs.push(llvm::CreateAttrStringValue(
136            cx.llcx,
137            "patchable-function-entry-section",
138            section,
139        ));
140    }
141    attrs
142}
143
144/// Get LLVM sanitize attributes.
145#[inline]
146pub(crate) fn sanitize_attrs<'ll, 'tcx>(
147    cx: &SimpleCx<'ll>,
148    tcx: TyCtxt<'tcx>,
149    sanitizer_fn_attr: SanitizerFnAttrs,
150    instance: Option<ty::Instance<'tcx>>,
151    sanitizer_ignorelist: Option<&SanitizerIgnoreList>,
152) -> SmallVec<[&'ll Attribute; 4]> {
153    let mut attrs = SmallVec::new();
154    let mut enabled = tcx.sess.sanitizers() - sanitizer_fn_attr.disabled;
155    if let Some(ignorelist) = sanitizer_ignorelist {
156        if let Some(instance) = instance {
157            let result = ignorelist.filter_instance_sanitizers(tcx, instance, enabled);
158            enabled = result.enabled;
159            if result.ignore_cfi {
160                attrs.push(llvm::CreateAttrString(cx.llcx, "no-sanitize-cfi"));
161            }
162            if result.ignore_kcfi {
163                attrs.push(llvm::CreateAttrString(cx.llcx, "no-sanitize-kcfi"));
164            }
165        }
166    }
167
168    if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
169        attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
170    }
171    if enabled.contains(SanitizerSet::MEMORY) {
172        attrs.push(llvm::AttributeKind::SanitizeMemory.create_attr(cx.llcx));
173    }
174    if enabled.contains(SanitizerSet::THREAD) {
175        attrs.push(llvm::AttributeKind::SanitizeThread.create_attr(cx.llcx));
176    }
177    if enabled.contains(SanitizerSet::HWADDRESS) || enabled.contains(SanitizerSet::KERNELHWADDRESS)
178    {
179        attrs.push(llvm::AttributeKind::SanitizeHWAddress.create_attr(cx.llcx));
180    }
181    if enabled.contains(SanitizerSet::SHADOWCALLSTACK) {
182        attrs.push(llvm::AttributeKind::ShadowCallStack.create_attr(cx.llcx));
183    }
184    if enabled.contains(SanitizerSet::MEMTAG) {
185        // Check to make sure the mte target feature is actually enabled.
186        let features = &tcx.sess.global_backend_features;
187        let mte_feature =
188            features.iter().map(|s| &s[..]).rfind(|n| ["+mte", "-mte"].contains(&&n[..]));
189        if let None | Some("-mte") = mte_feature {
190            tcx.dcx().emit_err(SanitizerMemtagRequiresMte);
191        }
192
193        attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx));
194    }
195    if enabled.contains(SanitizerSet::SAFESTACK) {
196        attrs.push(llvm::AttributeKind::SanitizeSafeStack.create_attr(cx.llcx));
197    }
198    if tcx.sess.sanitizers().contains(SanitizerSet::REALTIME) {
199        match sanitizer_fn_attr.rtsan_setting {
200            RtsanSetting::Nonblocking => {
201                attrs.push(llvm::AttributeKind::SanitizeRealtimeNonblocking.create_attr(cx.llcx))
202            }
203            RtsanSetting::Blocking => {
204                attrs.push(llvm::AttributeKind::SanitizeRealtimeBlocking.create_attr(cx.llcx))
205            }
206            // caller is the default, so no llvm attribute
207            RtsanSetting::Caller => (),
208        }
209    }
210    attrs
211}
212
213/// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
214#[inline]
215pub(crate) fn uwtable_attr<'ll>(
216    llcx: llvm::Context<'ll>,
217    use_sync_unwind: Option<bool>,
218) -> &'ll Attribute {
219    // NOTE: We should determine if we even need async unwind tables, as they
220    // take have more overhead and if we can use sync unwind tables we
221    // probably should.
222    //
223    // Similar logic exists for the per-module uwtable annotation in `context.rs`.
224    let async_unwind = !use_sync_unwind.unwrap_or(false);
225    llvm::CreateUWTableAttr(llcx, async_unwind)
226}
227
228pub(crate) fn frame_pointer(sess: &Session) -> FramePointer {
229    let mut fp = sess.target.frame_pointer;
230    let opts = &sess.opts;
231    // "mcount" function relies on stack pointer.
232    // See <https://sourceware.org/binutils/docs/gprof/Implementation.html>.
233    if let InstrumentMcount::Mcount(_) = opts.unstable_opts.instrument_mcount {
234        fp.ratchet(FramePointer::Always);
235    }
236    fp.ratchet(opts.cg.force_frame_pointers);
237    fp
238}
239
240pub(crate) fn frame_pointer_type_attr<'ll>(
241    cx: &SimpleCx<'ll>,
242    sess: &Session,
243) -> Option<&'ll Attribute> {
244    let fp = frame_pointer(sess);
245    let attr_value = match fp {
246        FramePointer::Always => "all",
247        FramePointer::NonLeaf => "non-leaf",
248        FramePointer::MayOmit => return None,
249    };
250    Some(llvm::CreateAttrStringValue(cx.llcx, "frame-pointer", attr_value))
251}
252
253fn function_return_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
254    let function_return_attr = match sess.opts.unstable_opts.function_return {
255        FunctionReturn::Keep => return None,
256        FunctionReturn::ThunkExtern => AttributeKind::FnRetThunkExtern,
257    };
258
259    Some(function_return_attr.create_attr(cx.llcx))
260}
261
262/// Tell LLVM what instrument function to insert.
263#[inline]
264fn instrument_function_attr<'ll>(
265    cx: &SimpleCx<'ll>,
266    sess: &Session,
267    instrument_fn: Option<InstrumentFnAttr>,
268) -> SmallVec<[&'ll Attribute; 4]> {
269    let mut attrs = SmallVec::new();
270    if sess.opts.unstable_opts.instrument_mcount != InstrumentMcount::Disabled {
271        // Similar to `clang -pg` behavior. Handled by the
272        // `post-inline-ee-instrument` LLVM pass.
273
274        let instrument_entry = match instrument_fn {
275            Some(InstrumentFnAttr::On) | None => true,
276            Some(InstrumentFnAttr::Off) => false,
277        };
278
279        if instrument_entry {
280            let mut opts = InstrumentMcountOpts::default();
281            match sess.opts.unstable_opts.instrument_mcount {
282                InstrumentMcount::Mcount(mopts) => {
283                    // The function name varies on platforms.
284                    // See test/CodeGen/mcount.c in clang.
285                    let mcount_name = match &sess.target.llvm_mcount_intrinsic {
286                        Some(llvm_mcount_intrinsic) => llvm_mcount_intrinsic.as_ref(),
287                        None => sess.target.mcount.as_ref(),
288                    };
289
290                    attrs.push(llvm::CreateAttrStringValue(
291                        cx.llcx,
292                        "instrument-function-entry-inlined",
293                        mcount_name,
294                    ));
295                    opts = mopts;
296                }
297                InstrumentMcount::Fentry(fopts) => {
298                    attrs.push(llvm::CreateAttrStringValue(cx.llcx, "fentry-call", "true"));
299                    opts = fopts;
300                }
301                InstrumentMcount::Disabled => {}
302            }
303            if opts.no_call {
304                attrs.push(llvm::CreateAttrString(cx.llcx, "mnop-mcount"));
305            }
306            if opts.record {
307                attrs.push(llvm::CreateAttrString(cx.llcx, "mrecord-mcount"));
308            }
309        }
310    }
311    if let Some(options) = &sess.opts.unstable_opts.instrument_xray {
312        // XRay instrumentation is similar to __cyg_profile_func_{enter,exit}.
313        // Function prologue and epilogue are instrumented with NOP sleds,
314        // a runtime library later replaces them with detours into tracing code.
315
316        let mut never = options.never;
317        let mut always = options.always;
318
319        // always and never may be overridden by the #[instrument_fn = ...] attribute.
320        match instrument_fn {
321            Some(InstrumentFnAttr::On) => {
322                always = true;
323            }
324            Some(InstrumentFnAttr::Off) => {
325                never = true;
326            }
327            None => {}
328        }
329
330        if never {
331            attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-never"));
332        }
333        if always {
334            attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-always"));
335        }
336
337        if options.ignore_loops {
338            attrs.push(llvm::CreateAttrString(cx.llcx, "xray-ignore-loops"));
339        }
340        // LLVM will not choose the default for us, but rather requires specific
341        // threshold in absence of "xray-always". Use the same default as Clang.
342        let threshold = options.instruction_threshold.unwrap_or(200);
343        attrs.push(llvm::CreateAttrStringValue(
344            cx.llcx,
345            "xray-instruction-threshold",
346            &threshold.to_string(),
347        ));
348        if options.skip_entry {
349            attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-entry"));
350        }
351        if options.skip_exit {
352            attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-exit"));
353        }
354    }
355    attrs
356}
357
358fn nojumptables_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
359    if sess.opts.cg.jump_tables {
360        return None;
361    }
362
363    Some(llvm::CreateAttrStringValue(cx.llcx, "no-jump-tables", "true"))
364}
365
366fn probestack_attr<'ll, 'tcx>(cx: &SimpleCx<'ll>, tcx: TyCtxt<'tcx>) -> Option<&'ll Attribute> {
367    // Currently stack probes seem somewhat incompatible with the address
368    // sanitizer and thread sanitizer. With asan we're already protected from
369    // stack overflow anyway so we don't really need stack probes regardless.
370    if tcx.sess.sanitizers().intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD) {
371        return None;
372    }
373
374    // probestack doesn't play nice either with `-C profile-generate`.
375    if tcx.sess.opts.cg.profile_generate.enabled() {
376        return None;
377    }
378
379    let attr_value = match tcx.sess.target.stack_probes {
380        StackProbeType::None => return None,
381        // Request LLVM to generate the probes inline. If the given LLVM version does not support
382        // this, no probe is generated at all (even if the attribute is specified).
383        StackProbeType::Inline => "inline-asm",
384        // Flag our internal `__rust_probestack` function as the stack probe symbol.
385        // This is defined in the `compiler-builtins` crate for each architecture.
386        StackProbeType::Call => &mangle_internal_symbol(tcx, "__rust_probestack"),
387        // Pick from the two above based on the LLVM version.
388        StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
389            if llvm_util::get_version() < min_llvm_version_for_inline {
390                &mangle_internal_symbol(tcx, "__rust_probestack")
391            } else {
392                "inline-asm"
393            }
394        }
395    };
396    Some(llvm::CreateAttrStringValue(cx.llcx, "probe-stack", attr_value))
397}
398
399fn stackprotector_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
400    let sspattr = match sess.stack_protector() {
401        StackProtector::None => return None,
402        StackProtector::All => AttributeKind::StackProtectReq,
403        StackProtector::Strong => AttributeKind::StackProtectStrong,
404        StackProtector::Basic => AttributeKind::StackProtect,
405    };
406
407    Some(sspattr.create_attr(cx.llcx))
408}
409
410fn packed_stack_attr<'ll>(
411    cx: &SimpleCx<'ll>,
412    sess: &Session,
413    function_attributes: &Vec<TargetFeature>,
414) -> Option<&'ll Attribute> {
415    if sess.target.arch != Arch::S390x {
416        return None;
417    }
418    if !sess.opts.unstable_opts.packed_stack {
419        return None;
420    }
421
422    // The backchain and softfloat flags can be set via -Ctarget-features=...
423    // or via #[target_features(enable = ...)] so we have to check both possibilities
424    let have_backchain = sess.internal_target_features.contains(&sym::backchain)
425        || function_attributes.iter().any(|feature| feature.name == sym::backchain);
426    let have_softfloat = sess.internal_target_features.contains(&sym::soft_float)
427        || function_attributes.iter().any(|feature| feature.name == sym::soft_float);
428
429    // If both, backchain and packedstack, are enabled LLVM cannot generate valid function entry points
430    // with the default ABI. However if the softfloat flag is set LLVM will switch to the softfloat
431    // ABI, where this works.
432    if have_backchain && !have_softfloat {
433        sess.dcx().emit_err(PackedStackBackchainNeedsSoftfloat);
434        return None;
435    }
436
437    Some(llvm::CreateAttrString(cx.llcx, "packed-stack"))
438}
439
440pub(crate) fn target_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> &'ll Attribute {
441    let target_cpu = llvm_util::target_cpu(sess);
442    llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu)
443}
444
445pub(crate) fn tune_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
446    llvm_util::tune_cpu(sess)
447        .map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu))
448}
449
450/// Get the `target-features` LLVM attribute.
451pub(crate) fn target_features_attr<'ll, 'tcx>(
452    cx: &SimpleCx<'ll>,
453    tcx: TyCtxt<'tcx>,
454    function_features: Vec<String>,
455) -> Option<&'ll Attribute> {
456    let global_features = tcx.sess.global_backend_features.iter().map(String::as_str);
457    let function_features = function_features.iter().map(String::as_str);
458    let target_features =
459        global_features.chain(function_features).intersperse(",").collect::<String>();
460    (!target_features.is_empty())
461        .then(|| llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features))
462}
463
464/// Get the `NonLazyBind` LLVM attribute,
465/// if the codegen options allow skipping the PLT.
466pub(crate) fn non_lazy_bind_attr<'ll>(
467    cx: &SimpleCx<'ll>,
468    sess: &Session,
469) -> Option<&'ll Attribute> {
470    // Don't generate calls through PLT if it's not necessary
471    if !sess.needs_plt() { Some(AttributeKind::NonLazyBind.create_attr(cx.llcx)) } else { None }
472}
473
474/// Get the default optimizations attrs for a function.
475#[inline]
476pub(crate) fn default_optimisation_attrs<'ll>(
477    cx: &SimpleCx<'ll>,
478    sess: &Session,
479) -> SmallVec<[&'ll Attribute; 2]> {
480    let mut attrs = SmallVec::new();
481    match sess.opts.optimize {
482        OptLevel::Size => {
483            attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
484        }
485        OptLevel::SizeMin => {
486            attrs.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
487            attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
488        }
489        _ => {}
490    }
491    attrs
492}
493
494fn create_alloc_family_attr<'ll>(llcx: llvm::Context<'ll>) -> &'ll llvm::Attribute {
495    llvm::CreateAttrStringValue(llcx, "alloc-family", "__rust_alloc")
496}
497
498/// Helper for `FnAbiLlvmExt::apply_attrs_llfn`:
499/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
500/// attributes.
501pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
502    cx: &SimpleCx<'ll>,
503    tcx: TyCtxt<'tcx>,
504    llfn: &'ll Value,
505    codegen_fn_attrs: &CodegenFnAttrs,
506    instance: Option<ty::Instance<'tcx>>,
507    sanitizer_ignorelist: Option<&SanitizerIgnoreList>,
508) {
509    let sess = tcx.sess;
510    let mut to_add = SmallVec::<[_; 16]>::new();
511
512    match codegen_fn_attrs.optimize {
513        OptimizeAttr::Default => {
514            to_add.extend(default_optimisation_attrs(cx, sess));
515        }
516        OptimizeAttr::DoNotOptimize => {
517            to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
518        }
519        OptimizeAttr::Size => {
520            to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
521            to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
522        }
523        OptimizeAttr::Speed => {}
524    }
525
526    if let Some(instance) = instance {
527        to_add.extend(inline_attr(cx, tcx, instance, codegen_fn_attrs));
528    }
529
530    if sess.must_emit_unwind_tables() {
531        to_add.push(uwtable_attr(cx.llcx, sess.opts.unstable_opts.use_sync_unwind));
532    }
533
534    if sess.opts.cg.profile_sample_use.is_some() {
535        to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
536    }
537
538    // FIXME: none of these functions interact with source level attributes.
539    to_add.extend(frame_pointer_type_attr(cx, sess));
540    to_add.extend(function_return_attr(cx, sess));
541    to_add.extend(instrument_function_attr(cx, sess, codegen_fn_attrs.instrument_fn));
542    to_add.extend(nojumptables_attr(cx, sess));
543    to_add.extend(probestack_attr(cx, tcx));
544    to_add.extend(stackprotector_attr(cx, sess));
545
546    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_BUILTINS) {
547        to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins"));
548    }
549
550    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::OFFLOAD_KERNEL) {
551        to_add.push(llvm::CreateAttrString(cx.llcx, "offload-kernel"))
552    }
553
554    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
555        to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
556    }
557    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
558        to_add.push(MemoryEffects::ReadOnly.create_attr(cx.llcx));
559    }
560    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
561        to_add.push(MemoryEffects::None.create_attr(cx.llcx));
562    }
563    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
564        // do nothing; a naked function is converted into an extern function
565        // and a global assembly block. LLVM's support for naked functions is
566        // not used.
567    } else {
568        // Do not set sanitizer attributes for naked functions.
569        to_add.extend(sanitize_attrs(
570            cx,
571            tcx,
572            codegen_fn_attrs.sanitizers,
573            instance,
574            sanitizer_ignorelist,
575        ));
576
577        // For non-naked functions, set branch protection attributes on aarch64.
578        if let Some(BranchProtection { bti, pac_ret, gcs }) = sess.branch_protection() {
579            if !(sess.target.arch == Arch::AArch64) {
    ::core::panicking::panic("assertion failed: sess.target.arch == Arch::AArch64")
};assert!(sess.target.arch == Arch::AArch64);
580            if bti {
581                to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement"));
582            }
583            if gcs {
584                to_add.push(llvm::CreateAttrString(cx.llcx, "guarded-control-stack"));
585            }
586            if let Some(PacRet { leaf, pc, key }) = pac_ret {
587                if pc {
588                    to_add.push(llvm::CreateAttrString(cx.llcx, "branch-protection-pauth-lr"));
589                }
590                to_add.push(llvm::CreateAttrStringValue(
591                    cx.llcx,
592                    "sign-return-address",
593                    if leaf { "all" } else { "non-leaf" },
594                ));
595                to_add.push(llvm::CreateAttrStringValue(
596                    cx.llcx,
597                    "sign-return-address-key",
598                    if key == PAuthKey::A { "a_key" } else { "b_key" },
599                ));
600            }
601        }
602    }
603    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR)
604        || codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR_ZEROED)
605    {
606        to_add.push(create_alloc_family_attr(cx.llcx));
607        if let Some(instance) = instance
608            && let Some(name) =
609                {
    {
        '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)
610        {
611            to_add.push(llvm::CreateAttrStringValue(
612                cx.llcx,
613                "alloc-variant-zeroed",
614                &mangle_internal_symbol(tcx, name.as_str()),
615            ));
616        }
617        // apply to argument place instead of function
618        let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
619        attributes::apply_to_llfn(llfn, AttributePlace::Argument(1), &[alloc_align]);
620        to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 0));
621        let mut flags = AllocKindFlags::Alloc | AllocKindFlags::Aligned;
622        if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
623            flags |= AllocKindFlags::Uninitialized;
624        } else {
625            flags |= AllocKindFlags::Zeroed;
626        }
627        to_add.push(llvm::CreateAllocKindAttr(cx.llcx, flags));
628        // apply to return place instead of function (unlike all other attributes applied in this
629        // function)
630        let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
631        attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
632    }
633    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::REALLOCATOR) {
634        to_add.push(create_alloc_family_attr(cx.llcx));
635        to_add.push(llvm::CreateAllocKindAttr(
636            cx.llcx,
637            AllocKindFlags::Realloc | AllocKindFlags::Aligned,
638        ));
639        // applies to argument place instead of function place
640        let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
641        attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
642        // apply to argument place instead of function
643        let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
644        attributes::apply_to_llfn(llfn, AttributePlace::Argument(2), &[alloc_align]);
645        to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 3));
646        let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
647        attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
648    }
649    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::DEALLOCATOR) {
650        to_add.push(create_alloc_family_attr(cx.llcx));
651        to_add.push(llvm::CreateAllocKindAttr(cx.llcx, AllocKindFlags::Free));
652        // applies to argument place instead of function place
653        let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
654        // "Does not capture provenance" means "if the function call stashes the pointer somewhere,
655        // accessing that pointer after the function returns is UB". That is definitely the case here since
656        // freeing will destroy the provenance.
657        let captures_addr = AttributeKind::CapturesAddress.create_attr(cx.llcx);
658        let attrs = &[allocated_pointer, captures_addr];
659        attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), attrs);
660    }
661    if let Some(align) = codegen_fn_attrs.alignment {
662        llvm::set_alignment(llfn, align);
663    }
664    if let Some(packed_stack) = packed_stack_attr(cx, sess, &codegen_fn_attrs.target_features) {
665        to_add.push(packed_stack);
666    }
667    to_add.extend(patchable_function_entry_attrs(
668        cx,
669        sess,
670        codegen_fn_attrs.patchable_function_entry,
671    ));
672
673    // Always annotate functions with the target-cpu they are compiled for.
674    // Without this, ThinLTO won't inline Rust functions into Clang generated
675    // functions (because Clang annotates functions this way too).
676    to_add.push(target_cpu_attr(cx, sess));
677    // tune-cpu is only conveyed through the attribute for our purpose.
678    // The target doesn't care; the subtarget reads our attribute.
679    to_add.extend(tune_cpu_attr(cx, sess));
680
681    let function_features =
682        codegen_fn_attrs.target_features.iter().map(|f| f.name.as_str()).collect::<Vec<&str>>();
683
684    let function_features = function_features
685        .iter()
686        // Convert to LLVMFeatures and filter out unavailable ones
687        .flat_map(|feat| llvm_util::to_llvm_features(&sess.target, feat))
688        // Convert LLVMFeatures & dependencies to +<feats>s
689        .flat_map(|feat| feat.into_iter().map(|f| ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("+{0}", f)) })format!("+{f}")))
690        .chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x {
691            InstructionSetAttr::ArmA32 => "-thumb-mode".to_string(),
692            InstructionSetAttr::ArmT32 => "+thumb-mode".to_string(),
693        }))
694        .collect::<Vec<String>>();
695
696    if sess.target.is_like_wasm {
697        // If this function is an import from the environment but the wasm
698        // import has a specific module/name, apply them here.
699        if let Some(instance) = instance
700            && let Some(module) = wasm_import_module(tcx, instance.def_id())
701        {
702            to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-module", module));
703
704            let name =
705                codegen_fn_attrs.symbol_name.unwrap_or_else(|| tcx.item_name(instance.def_id()));
706            let name = name.as_str();
707            to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-name", name));
708        }
709    }
710
711    if sess.pointer_authentication() {
712        let cfg = sess.pointer_auth_config.as_ref().unwrap();
713        for ptrauth_attr in cfg.fn_attrs() {
714            to_add.push(llvm::CreateAttrString(cx.llcx, ptrauth_attr));
715        }
716    }
717
718    to_add.extend(target_features_attr(cx, tcx, function_features));
719
720    attributes::apply_to_llfn(llfn, Function, &to_add);
721}
722
723fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<&String> {
724    tcx.wasm_import_module_map(id.krate).get(&id)
725}