rustc_codegen_ssa/
codegen_attrs.rs

1use std::str::FromStr;
2
3use rustc_abi::{Align, ExternAbi};
4use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
5use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
6use rustc_hir::attrs::{
7    AttributeKind, InlineAttr, InstructionSetAttr, Linkage, RtsanSetting, UsedBy,
8};
9use rustc_hir::def::DefKind;
10use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
11use rustc_hir::{self as hir, Attribute, LangItem, find_attr, lang_items};
12use rustc_middle::middle::codegen_fn_attrs::{
13    CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs,
14};
15use rustc_middle::mir::mono::Visibility;
16use rustc_middle::query::Providers;
17use rustc_middle::span_bug;
18use rustc_middle::ty::{self as ty, Instance, TyCtxt};
19use rustc_session::lint;
20use rustc_session::parse::feature_err;
21use rustc_span::{Ident, Span, Symbol, sym};
22use rustc_target::spec::Os;
23
24use crate::errors;
25use crate::target_features::{
26    check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
27};
28
29/// In some cases, attributes are only valid on functions, but it's the `check_attr`
30/// pass that checks that they aren't used anywhere else, rather than this module.
31/// In these cases, we bail from performing further checks that are only meaningful for
32/// functions (such as calling `fn_sig`, which ICEs if given a non-function). We also
33/// report a delayed bug, just in case `check_attr` isn't doing its job.
34fn try_fn_sig<'tcx>(
35    tcx: TyCtxt<'tcx>,
36    did: LocalDefId,
37    attr_span: Span,
38) -> Option<ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>>> {
39    use DefKind::*;
40
41    let def_kind = tcx.def_kind(did);
42    if let Fn | AssocFn | Variant | Ctor(..) = def_kind {
43        Some(tcx.fn_sig(did))
44    } else {
45        tcx.dcx().span_delayed_bug(attr_span, "this attribute can only be applied to functions");
46        None
47    }
48}
49
50// FIXME(jdonszelmann): remove when instruction_set becomes a parsed attr
51fn parse_instruction_set_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> Option<InstructionSetAttr> {
52    let list = attr.meta_item_list()?;
53
54    match &list[..] {
55        [MetaItemInner::MetaItem(set)] => {
56            let segments = set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
57            match segments.as_slice() {
58                [sym::arm, sym::a32 | sym::t32] if !tcx.sess.target.has_thumb_interworking => {
59                    tcx.dcx().emit_err(errors::UnsupportedInstructionSet { span: attr.span() });
60                    None
61                }
62                [sym::arm, sym::a32] => Some(InstructionSetAttr::ArmA32),
63                [sym::arm, sym::t32] => Some(InstructionSetAttr::ArmT32),
64                _ => {
65                    tcx.dcx().emit_err(errors::InvalidInstructionSet { span: attr.span() });
66                    None
67                }
68            }
69        }
70        [] => {
71            tcx.dcx().emit_err(errors::BareInstructionSet { span: attr.span() });
72            None
73        }
74        _ => {
75            tcx.dcx().emit_err(errors::MultipleInstructionSet { span: attr.span() });
76            None
77        }
78    }
79}
80
81// FIXME(jdonszelmann): remove when patchable_function_entry becomes a parsed attr
82fn parse_patchable_function_entry(
83    tcx: TyCtxt<'_>,
84    attr: &Attribute,
85) -> Option<PatchableFunctionEntry> {
86    attr.meta_item_list().and_then(|l| {
87        let mut prefix = None;
88        let mut entry = None;
89        for item in l {
90            let Some(meta_item) = item.meta_item() else {
91                tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
92                continue;
93            };
94
95            let Some(name_value_lit) = meta_item.name_value_literal() else {
96                tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
97                continue;
98            };
99
100            let attrib_to_write = match meta_item.name() {
101                Some(sym::prefix_nops) => &mut prefix,
102                Some(sym::entry_nops) => &mut entry,
103                _ => {
104                    tcx.dcx().emit_err(errors::UnexpectedParameterName {
105                        span: item.span(),
106                        prefix_nops: sym::prefix_nops,
107                        entry_nops: sym::entry_nops,
108                    });
109                    continue;
110                }
111            };
112
113            let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
114                tcx.dcx().emit_err(errors::InvalidLiteralValue { span: name_value_lit.span });
115                continue;
116            };
117
118            let Ok(val) = val.get().try_into() else {
119                tcx.dcx().emit_err(errors::OutOfRangeInteger { span: name_value_lit.span });
120                continue;
121            };
122
123            *attrib_to_write = Some(val);
124        }
125
126        if let (None, None) = (prefix, entry) {
127            tcx.dcx().span_err(attr.span(), "must specify at least one parameter");
128        }
129
130        Some(PatchableFunctionEntry::from_prefix_and_entry(prefix.unwrap_or(0), entry.unwrap_or(0)))
131    })
132}
133
134/// Spans that are collected when processing built-in attributes,
135/// that are useful for emitting diagnostics later.
136#[derive(Default)]
137struct InterestingAttributeDiagnosticSpans {
138    link_ordinal: Option<Span>,
139    sanitize: Option<Span>,
140    inline: Option<Span>,
141    no_mangle: Option<Span>,
142}
143
144/// Process the builtin attrs ([`hir::Attribute`]) on the item.
145/// Many of them directly translate to codegen attrs.
146fn process_builtin_attrs(
147    tcx: TyCtxt<'_>,
148    did: LocalDefId,
149    attrs: &[Attribute],
150    codegen_fn_attrs: &mut CodegenFnAttrs,
151) -> InterestingAttributeDiagnosticSpans {
152    let mut interesting_spans = InterestingAttributeDiagnosticSpans::default();
153    let rust_target_features = tcx.rust_target_features(LOCAL_CRATE);
154
155    for attr in attrs.iter() {
156        if let hir::Attribute::Parsed(p) = attr {
157            match p {
158                AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
159                AttributeKind::ExportName { name, .. } => {
160                    codegen_fn_attrs.symbol_name = Some(*name)
161                }
162                AttributeKind::Inline(inline, span) => {
163                    codegen_fn_attrs.inline = *inline;
164                    interesting_spans.inline = Some(*span);
165                }
166                AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
167                AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
168                AttributeKind::LinkName { name, .. } => {
169                    // FIXME Remove check for foreign functions once #[link_name] on non-foreign
170                    // functions is a hard error
171                    if tcx.is_foreign_item(did) {
172                        codegen_fn_attrs.symbol_name = Some(*name);
173                    }
174                }
175                AttributeKind::LinkOrdinal { ordinal, span } => {
176                    codegen_fn_attrs.link_ordinal = Some(*ordinal);
177                    interesting_spans.link_ordinal = Some(*span);
178                }
179                AttributeKind::LinkSection { name, .. } => {
180                    codegen_fn_attrs.link_section = Some(*name)
181                }
182                AttributeKind::NoMangle(attr_span) => {
183                    interesting_spans.no_mangle = Some(*attr_span);
184                    if tcx.opt_item_name(did.to_def_id()).is_some() {
185                        codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
186                    } else {
187                        tcx.dcx().span_delayed_bug(
188                            *attr_span,
189                            "no_mangle should be on a named function",
190                        );
191                    }
192                }
193                AttributeKind::Optimize(optimize, _) => codegen_fn_attrs.optimize = *optimize,
194                AttributeKind::TargetFeature { features, attr_span, was_forced } => {
195                    let Some(sig) = tcx.hir_node_by_def_id(did).fn_sig() else {
196                        tcx.dcx().span_delayed_bug(*attr_span, "target_feature applied to non-fn");
197                        continue;
198                    };
199                    let safe_target_features =
200                        matches!(sig.header.safety, hir::HeaderSafety::SafeTargetFeatures);
201                    codegen_fn_attrs.safe_target_features = safe_target_features;
202                    if safe_target_features && !was_forced {
203                        if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
204                            // The `#[target_feature]` attribute is allowed on
205                            // WebAssembly targets on all functions. Prior to stabilizing
206                            // the `target_feature_11` feature, `#[target_feature]` was
207                            // only permitted on unsafe functions because on most targets
208                            // execution of instructions that are not supported is
209                            // considered undefined behavior. For WebAssembly which is a
210                            // 100% safe target at execution time it's not possible to
211                            // execute undefined instructions, and even if a future
212                            // feature was added in some form for this it would be a
213                            // deterministic trap. There is no undefined behavior when
214                            // executing WebAssembly so `#[target_feature]` is allowed
215                            // on safe functions (but again, only for WebAssembly)
216                            //
217                            // Note that this is also allowed if `actually_rustdoc` so
218                            // if a target is documenting some wasm-specific code then
219                            // it's not spuriously denied.
220                            //
221                            // Now that `#[target_feature]` is permitted on safe functions,
222                            // this exception must still exist for allowing the attribute on
223                            // `main`, `start`, and other functions that are not usually
224                            // allowed.
225                        } else {
226                            check_target_feature_trait_unsafe(tcx, did, *attr_span);
227                        }
228                    }
229                    from_target_feature_attr(
230                        tcx,
231                        did,
232                        features,
233                        *was_forced,
234                        rust_target_features,
235                        &mut codegen_fn_attrs.target_features,
236                    );
237                }
238                AttributeKind::TrackCaller(attr_span) => {
239                    let is_closure = tcx.is_closure_like(did.to_def_id());
240
241                    if !is_closure
242                        && let Some(fn_sig) = try_fn_sig(tcx, did, *attr_span)
243                        && fn_sig.skip_binder().abi() != ExternAbi::Rust
244                    {
245                        tcx.dcx().emit_err(errors::RequiresRustAbi { span: *attr_span });
246                    }
247                    if is_closure
248                        && !tcx.features().closure_track_caller()
249                        && !attr_span.allows_unstable(sym::closure_track_caller)
250                    {
251                        feature_err(
252                            &tcx.sess,
253                            sym::closure_track_caller,
254                            *attr_span,
255                            "`#[track_caller]` on closures is currently unstable",
256                        )
257                        .emit();
258                    }
259                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
260                }
261                AttributeKind::Used { used_by, .. } => match used_by {
262                    UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
263                    UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
264                    UsedBy::Default => {
265                        let used_form = if tcx.sess.target.os == Os::Illumos {
266                            // illumos' `ld` doesn't support a section header that would represent
267                            // `#[used(linker)]`, see
268                            // https://github.com/rust-lang/rust/issues/146169. For that target,
269                            // downgrade as if `#[used(compiler)]` was requested and hope for the
270                            // best.
271                            CodegenFnAttrFlags::USED_COMPILER
272                        } else {
273                            CodegenFnAttrFlags::USED_LINKER
274                        };
275                        codegen_fn_attrs.flags |= used_form;
276                    }
277                },
278                AttributeKind::FfiConst(_) => {
279                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST
280                }
281                AttributeKind::FfiPure(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
282                AttributeKind::StdInternalSymbol(_) => {
283                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
284                }
285                AttributeKind::Linkage(linkage, span) => {
286                    let linkage = Some(*linkage);
287
288                    if tcx.is_foreign_item(did) {
289                        codegen_fn_attrs.import_linkage = linkage;
290
291                        if tcx.is_mutable_static(did.into()) {
292                            let mut diag = tcx.dcx().struct_span_err(
293                                *span,
294                                "extern mutable statics are not allowed with `#[linkage]`",
295                            );
296                            diag.note(
297                                "marking the extern static mutable would allow changing which \
298                                symbol the static references rather than make the target of the \
299                                symbol mutable",
300                            );
301                            diag.emit();
302                        }
303                    } else {
304                        codegen_fn_attrs.linkage = linkage;
305                    }
306                }
307                AttributeKind::Sanitize { span, .. } => {
308                    interesting_spans.sanitize = Some(*span);
309                }
310                AttributeKind::ObjcClass { classname, .. } => {
311                    codegen_fn_attrs.objc_class = Some(*classname);
312                }
313                AttributeKind::ObjcSelector { methname, .. } => {
314                    codegen_fn_attrs.objc_selector = Some(*methname);
315                }
316                AttributeKind::EiiExternItem => {
317                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
318                }
319                AttributeKind::EiiImpls(impls) => {
320                    for i in impls {
321                        let extern_item = find_attr!(
322                            tcx.get_all_attrs(i.eii_macro),
323                            AttributeKind::EiiExternTarget(target) => target.eii_extern_target
324                        )
325                        .expect("eii should have declaration macro with extern target attribute");
326
327                        let symbol_name = tcx.symbol_name(Instance::mono(tcx, extern_item));
328
329                        // this is to prevent a bug where a single crate defines both the default and explicit implementation
330                        // for an EII. In that case, both of them may be part of the same final object file. I'm not 100% sure
331                        // what happens, either rustc deduplicates the symbol or llvm, or it's random/order-dependent.
332                        // However, the fact that the default one of has weak linkage isn't considered and you sometimes get that
333                        // the default implementation is used while an explicit implementation is given.
334                        if
335                        // if this is a default impl
336                        i.is_default
337                            // iterate over all implementations *in the current crate*
338                            // (this is ok since we generate codegen fn attrs in the local crate)
339                            // if any of them is *not default* then don't emit the alias.
340                            && tcx.externally_implementable_items(LOCAL_CRATE).get(&i.eii_macro).expect("at least one").1.iter().any(|(_, imp)| !imp.is_default)
341                        {
342                            continue;
343                        }
344
345                        codegen_fn_attrs.foreign_item_symbol_aliases.push((
346                            Symbol::intern(symbol_name.name),
347                            if i.is_default { Linkage::LinkOnceAny } else { Linkage::External },
348                            Visibility::Default,
349                        ));
350                        codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
351                    }
352                }
353                AttributeKind::ThreadLocal => {
354                    codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL
355                }
356                _ => {}
357            }
358        }
359
360        let Some(Ident { name, .. }) = attr.ident() else {
361            continue;
362        };
363
364        match name {
365            sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
366            sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
367            sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
368            sym::rustc_deallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR,
369            sym::rustc_allocator_zeroed => {
370                codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
371            }
372            sym::instruction_set => {
373                codegen_fn_attrs.instruction_set = parse_instruction_set_attr(tcx, attr)
374            }
375            sym::patchable_function_entry => {
376                codegen_fn_attrs.patchable_function_entry =
377                    parse_patchable_function_entry(tcx, attr);
378            }
379            sym::rustc_offload_kernel => {
380                codegen_fn_attrs.flags |= CodegenFnAttrFlags::OFFLOAD_KERNEL
381            }
382            _ => {}
383        }
384    }
385
386    interesting_spans
387}
388
389/// Applies overrides for codegen fn attrs. These often have a specific reason why they're necessary.
390/// Please comment why when adding a new one!
391fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut CodegenFnAttrs) {
392    // Apply the minimum function alignment here. This ensures that a function's alignment is
393    // determined by the `-C` flags of the crate it is defined in, not the `-C` flags of the crate
394    // it happens to be codegen'd (or const-eval'd) in.
395    codegen_fn_attrs.alignment =
396        Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
397
398    // Passed in sanitizer settings are always the default.
399    assert!(codegen_fn_attrs.sanitizers == SanitizerFnAttrs::default());
400    // Replace with #[sanitize] value
401    codegen_fn_attrs.sanitizers = tcx.sanitizer_settings_for(did);
402    // On trait methods, inherit the `#[align]` of the trait's method prototype.
403    codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
404
405    // naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
406    // but not for the code generation backend because at that point the naked function will just be
407    // a declaration, with a definition provided in global assembly.
408    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
409        codegen_fn_attrs.inline = InlineAttr::Never;
410    }
411
412    // #73631: closures inherit `#[target_feature]` annotations
413    //
414    // If this closure is marked `#[inline(always)]`, simply skip adding `#[target_feature]`.
415    //
416    // At this point, `unsafe` has already been checked and `#[target_feature]` only affects codegen.
417    // Due to LLVM limitations, emitting both `#[inline(always)]` and `#[target_feature]` is *unsound*:
418    // the function may be inlined into a caller with fewer target features. Also see
419    // <https://github.com/rust-lang/rust/issues/116573>.
420    //
421    // Using `#[inline(always)]` implies that this closure will most likely be inlined into
422    // its parent function, which effectively inherits the features anyway. Boxing this closure
423    // would result in this closure being compiled without the inherited target features, but this
424    // is probably a poor usage of `#[inline(always)]` and easily avoided by not using the attribute.
425    if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
426        let owner_id = tcx.parent(did.to_def_id());
427        if tcx.def_kind(owner_id).has_codegen_attrs() {
428            codegen_fn_attrs
429                .target_features
430                .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
431        }
432    }
433
434    // When `no_builtins` is applied at the crate level, we should add the
435    // `no-builtins` attribute to each function to ensure it takes effect in LTO.
436    let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
437    let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
438    if no_builtins {
439        codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
440    }
441
442    // inherit track-caller properly
443    if tcx.should_inherit_track_caller(did) {
444        codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
445    }
446
447    // Foreign items by default use no mangling for their symbol name.
448    if tcx.is_foreign_item(did) {
449        codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
450
451        // There's a few exceptions to this rule though:
452        if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
453            // * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
454            //   both for exports and imports through foreign items. This is handled further,
455            //   during symbol mangling logic.
456        } else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM)
457        {
458            // * externally implementable items keep their mangled symbol name.
459            //   multiple EIIs can have the same name, so not mangling them would be a bug.
460            //   Implementing an EII does the appropriate name resolution to make sure the implementations
461            //   get the same symbol name as the *mangled* foreign item they refer to so that's all good.
462        } else if codegen_fn_attrs.symbol_name.is_some() {
463            // * This can be overridden with the `#[link_name]` attribute
464        } else {
465            // NOTE: there's one more exception that we cannot apply here. On wasm,
466            // some items cannot be `no_mangle`.
467            // However, we don't have enough information here to determine that.
468            // As such, no_mangle foreign items on wasm that have the same defid as some
469            // import will *still* be mangled despite this.
470            //
471            // if none of the exceptions apply; apply no_mangle
472            codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
473        }
474    }
475}
476
477fn check_result(
478    tcx: TyCtxt<'_>,
479    did: LocalDefId,
480    interesting_spans: InterestingAttributeDiagnosticSpans,
481    codegen_fn_attrs: &CodegenFnAttrs,
482) {
483    // If a function uses `#[target_feature]` it can't be inlined into general
484    // purpose functions as they wouldn't have the right target features
485    // enabled. For that reason we also forbid `#[inline(always)]` as it can't be
486    // respected.
487    //
488    // `#[rustc_force_inline]` doesn't need to be prohibited here, only
489    // `#[inline(always)]`, as forced inlining is implemented entirely within
490    // rustc (and so the MIR inliner can do any necessary checks for compatible target
491    // features).
492    //
493    // This sidesteps the LLVM blockers in enabling `target_features` +
494    // `inline(always)` to be used together (see rust-lang/rust#116573 and
495    // llvm/llvm-project#70563).
496    if !codegen_fn_attrs.target_features.is_empty()
497        && matches!(codegen_fn_attrs.inline, InlineAttr::Always)
498        && !tcx.features().target_feature_inline_always()
499        && let Some(span) = interesting_spans.inline
500    {
501        feature_err(
502            tcx.sess,
503            sym::target_feature_inline_always,
504            span,
505            "cannot use `#[inline(always)]` with `#[target_feature]`",
506        )
507        .emit();
508    }
509
510    // warn that inline has no effect when no_sanitize is present
511    if codegen_fn_attrs.sanitizers != SanitizerFnAttrs::default()
512        && codegen_fn_attrs.inline.always()
513        && let (Some(sanitize_span), Some(inline_span)) =
514            (interesting_spans.sanitize, interesting_spans.inline)
515    {
516        let hir_id = tcx.local_def_id_to_hir_id(did);
517        tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, sanitize_span, |lint| {
518            lint.primary_message("non-default `sanitize` will have no effect after inlining");
519            lint.span_note(inline_span, "inlining requested here");
520        })
521    }
522
523    // warn for nonblocking async functions, blocks and closures.
524    // This doesn't behave as expected, because the executor can run blocking code without the sanitizer noticing.
525    if codegen_fn_attrs.sanitizers.rtsan_setting == RtsanSetting::Nonblocking
526        && let Some(sanitize_span) = interesting_spans.sanitize
527        // async fn
528        && (tcx.asyncness(did).is_async()
529            // async block
530            || tcx.is_coroutine(did.into())
531            // async closure
532            || (tcx.is_closure_like(did.into())
533                && tcx.hir_node_by_def_id(did).expect_closure().kind
534                    != rustc_hir::ClosureKind::Closure))
535    {
536        let hir_id = tcx.local_def_id_to_hir_id(did);
537        tcx.node_span_lint(
538            lint::builtin::RTSAN_NONBLOCKING_ASYNC,
539            hir_id,
540            sanitize_span,
541            |lint| {
542                lint.primary_message(r#"the async executor can run blocking code, without realtime sanitizer catching it"#);
543            }
544        );
545    }
546
547    // error when specifying link_name together with link_ordinal
548    if let Some(_) = codegen_fn_attrs.symbol_name
549        && let Some(_) = codegen_fn_attrs.link_ordinal
550    {
551        let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
552        if let Some(span) = interesting_spans.link_ordinal {
553            tcx.dcx().span_err(span, msg);
554        } else {
555            tcx.dcx().err(msg);
556        }
557    }
558
559    if let Some(features) = check_tied_features(
560        tcx.sess,
561        &codegen_fn_attrs
562            .target_features
563            .iter()
564            .map(|features| (features.name.as_str(), true))
565            .collect(),
566    ) {
567        let span =
568            find_attr!(tcx.get_all_attrs(did), AttributeKind::TargetFeature{attr_span: span, ..} => *span)
569                .unwrap_or_else(|| tcx.def_span(did));
570
571        tcx.dcx()
572            .create_err(errors::TargetFeatureDisableOrEnable {
573                features,
574                span: Some(span),
575                missing_features: Some(errors::MissingFeatures),
576            })
577            .emit();
578    }
579}
580
581fn handle_lang_items(
582    tcx: TyCtxt<'_>,
583    did: LocalDefId,
584    interesting_spans: &InterestingAttributeDiagnosticSpans,
585    attrs: &[Attribute],
586    codegen_fn_attrs: &mut CodegenFnAttrs,
587) {
588    let lang_item = lang_items::extract(attrs).and_then(|(name, _)| LangItem::from_name(name));
589
590    // Weak lang items have the same semantics as "std internal" symbols in the
591    // sense that they're preserved through all our LTO passes and only
592    // strippable by the linker.
593    //
594    // Additionally weak lang items have predetermined symbol names.
595    if let Some(lang_item) = lang_item
596        && let Some(link_name) = lang_item.link_name()
597    {
598        codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
599        codegen_fn_attrs.symbol_name = Some(link_name);
600    }
601
602    // error when using no_mangle on a lang item item
603    if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
604        && codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
605    {
606        let mut err = tcx
607            .dcx()
608            .struct_span_err(
609                interesting_spans.no_mangle.unwrap_or_default(),
610                "`#[no_mangle]` cannot be used on internal language items",
611            )
612            .with_note("Rustc requires this item to have a specific mangled name.")
613            .with_span_label(tcx.def_span(did), "should be the internal language item");
614        if let Some(lang_item) = lang_item
615            && let Some(link_name) = lang_item.link_name()
616        {
617            err = err
618                .with_note("If you are trying to prevent mangling to ease debugging, many")
619                .with_note(format!("debuggers support a command such as `rbreak {link_name}` to"))
620                .with_note(format!(
621                    "match `.*{link_name}.*` instead of `break {link_name}` on a specific name"
622                ))
623        }
624        err.emit();
625    }
626}
627
628/// Generate the [`CodegenFnAttrs`] for an item (identified by the [`LocalDefId`]).
629///
630/// This happens in 4 stages:
631/// - apply built-in attributes that directly translate to codegen attributes.
632/// - handle lang items. These have special codegen attrs applied to them.
633/// - apply overrides, like minimum requirements for alignment and other settings that don't rely directly the built-in attrs on the item.
634///   overrides come after applying built-in attributes since they may only apply when certain attributes were already set in the stage before.
635/// - check that the result is valid. There's various ways in which this may not be the case, such as certain combinations of attrs.
636fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
637    if cfg!(debug_assertions) {
638        let def_kind = tcx.def_kind(did);
639        assert!(
640            def_kind.has_codegen_attrs(),
641            "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
642        );
643    }
644
645    let mut codegen_fn_attrs = CodegenFnAttrs::new();
646    let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(did));
647
648    let interesting_spans = process_builtin_attrs(tcx, did, attrs, &mut codegen_fn_attrs);
649    handle_lang_items(tcx, did, &interesting_spans, attrs, &mut codegen_fn_attrs);
650    apply_overrides(tcx, did, &mut codegen_fn_attrs);
651    check_result(tcx, did, interesting_spans, &codegen_fn_attrs);
652
653    codegen_fn_attrs
654}
655
656fn sanitizer_settings_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerFnAttrs {
657    // Backtrack to the crate root.
658    let mut settings = match tcx.opt_local_parent(did) {
659        // Check the parent (recursively).
660        Some(parent) => tcx.sanitizer_settings_for(parent),
661        // We reached the crate root without seeing an attribute, so
662        // there is no sanitizers to exclude.
663        None => SanitizerFnAttrs::default(),
664    };
665
666    // Check for a sanitize annotation directly on this def.
667    if let Some((on_set, off_set, rtsan)) = find_attr!(tcx.get_all_attrs(did), AttributeKind::Sanitize {on_set, off_set, rtsan, ..} => (on_set, off_set, rtsan))
668    {
669        // the on set is the set of sanitizers explicitly enabled.
670        // we mask those out since we want the set of disabled sanitizers here
671        settings.disabled &= !*on_set;
672        // the off set is the set of sanitizers explicitly disabled.
673        // we or those in here.
674        settings.disabled |= *off_set;
675        // the on set and off set are distjoint since there's a third option: unset.
676        // a node may not set the sanitizer setting in which case it inherits from parents.
677        // the code above in this function does this backtracking
678
679        // if rtsan was specified here override the parent
680        if let Some(rtsan) = rtsan {
681            settings.rtsan_setting = *rtsan;
682        }
683    }
684    settings
685}
686
687/// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
688/// applied to the method prototype.
689fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
690    tcx.trait_item_of(def_id).is_some_and(|id| {
691        tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
692    })
693}
694
695/// If the provided DefId is a method in a trait impl, return the value of the `#[align]`
696/// attribute on the method prototype (if any).
697fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
698    tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
699}
700
701/// We now check the #\[rustc_autodiff\] attributes which we generated from the #[autodiff(...)]
702/// macros. There are two forms. The pure one without args to mark primal functions (the functions
703/// being differentiated). The other form is #[rustc_autodiff(Mode, ActivityList)] on top of the
704/// placeholder functions. We wrote the rustc_autodiff attributes ourself, so this should never
705/// panic, unless we introduced a bug when parsing the autodiff macro.
706//FIXME(jdonszelmann): put in the main loop. No need to have two..... :/ Let's do that when we make autodiff parsed.
707pub fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
708    let attrs = tcx.get_attrs(id, sym::rustc_autodiff);
709
710    let attrs = attrs.filter(|attr| attr.has_name(sym::rustc_autodiff)).collect::<Vec<_>>();
711
712    // check for exactly one autodiff attribute on placeholder functions.
713    // There should only be one, since we generate a new placeholder per ad macro.
714    let attr = match &attrs[..] {
715        [] => return None,
716        [attr] => attr,
717        _ => {
718            span_bug!(attrs[1].span(), "cg_ssa: rustc_autodiff should only exist once per source");
719        }
720    };
721
722    let list = attr.meta_item_list().unwrap_or_default();
723
724    // empty autodiff attribute macros (i.e. `#[autodiff]`) are used to mark source functions
725    if list.is_empty() {
726        return Some(AutoDiffAttrs::source());
727    }
728
729    let [mode, width_meta, input_activities @ .., ret_activity] = &list[..] else {
730        span_bug!(attr.span(), "rustc_autodiff attribute must contain mode, width and activities");
731    };
732    let mode = if let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = mode {
733        p1.segments.first().unwrap().ident
734    } else {
735        span_bug!(attr.span(), "rustc_autodiff attribute must contain mode");
736    };
737
738    // parse mode
739    let mode = match mode.as_str() {
740        "Forward" => DiffMode::Forward,
741        "Reverse" => DiffMode::Reverse,
742        _ => {
743            span_bug!(mode.span, "rustc_autodiff attribute contains invalid mode");
744        }
745    };
746
747    let width: u32 = match width_meta {
748        MetaItemInner::MetaItem(MetaItem { path: p1, .. }) => {
749            let w = p1.segments.first().unwrap().ident;
750            match w.as_str().parse() {
751                Ok(val) => val,
752                Err(_) => {
753                    span_bug!(w.span, "rustc_autodiff width should fit u32");
754                }
755            }
756        }
757        MetaItemInner::Lit(lit) => {
758            if let LitKind::Int(val, _) = lit.kind {
759                match val.get().try_into() {
760                    Ok(val) => val,
761                    Err(_) => {
762                        span_bug!(lit.span, "rustc_autodiff width should fit u32");
763                    }
764                }
765            } else {
766                span_bug!(lit.span, "rustc_autodiff width should be an integer");
767            }
768        }
769    };
770
771    // First read the ret symbol from the attribute
772    let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = ret_activity else {
773        span_bug!(attr.span(), "rustc_autodiff attribute must contain the return activity");
774    };
775    let ret_symbol = p1.segments.first().unwrap().ident;
776
777    // Then parse it into an actual DiffActivity
778    let Ok(ret_activity) = DiffActivity::from_str(ret_symbol.as_str()) else {
779        span_bug!(ret_symbol.span, "invalid return activity");
780    };
781
782    // Now parse all the intermediate (input) activities
783    let mut arg_activities: Vec<DiffActivity> = vec![];
784    for arg in input_activities {
785        let arg_symbol = if let MetaItemInner::MetaItem(MetaItem { path: p2, .. }) = arg {
786            match p2.segments.first() {
787                Some(x) => x.ident,
788                None => {
789                    span_bug!(
790                        arg.span(),
791                        "rustc_autodiff attribute must contain the input activity"
792                    );
793                }
794            }
795        } else {
796            span_bug!(arg.span(), "rustc_autodiff attribute must contain the input activity");
797        };
798
799        match DiffActivity::from_str(arg_symbol.as_str()) {
800            Ok(arg_activity) => arg_activities.push(arg_activity),
801            Err(_) => {
802                span_bug!(arg_symbol.span, "invalid input activity");
803            }
804        }
805    }
806
807    Some(AutoDiffAttrs { mode, width, ret_activity, input_activity: arg_activities })
808}
809
810pub(crate) fn provide(providers: &mut Providers) {
811    *providers = Providers {
812        codegen_fn_attrs,
813        should_inherit_track_caller,
814        inherited_align,
815        sanitizer_settings_for,
816        ..*providers
817    };
818}