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