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