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