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