rustc_codegen_ssa/
codegen_attrs.rs

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