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