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