1use std::str::FromStr;
2
3use rustc_abi::{Align, ExternAbi};
4use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
5use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
6use rustc_hir::attrs::{AttributeKind, InlineAttr, InstructionSetAttr, UsedBy};
7use rustc_hir::def::DefKind;
8use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
9use rustc_hir::{self as hir, Attribute, LangItem, find_attr, lang_items};
10use rustc_middle::middle::codegen_fn_attrs::{
11 CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry,
12};
13use rustc_middle::query::Providers;
14use rustc_middle::span_bug;
15use rustc_middle::ty::{self as ty, TyCtxt};
16use rustc_session::lint;
17use rustc_session::parse::feature_err;
18use rustc_span::{Ident, Span, sym};
19use rustc_target::spec::SanitizerSet;
20
21use crate::errors;
22use crate::target_features::{
23 check_target_feature_trait_unsafe, check_tied_features, from_target_feature_attr,
24};
25
26fn try_fn_sig<'tcx>(
32 tcx: TyCtxt<'tcx>,
33 did: LocalDefId,
34 attr_span: Span,
35) -> Option<ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>>> {
36 use DefKind::*;
37
38 let def_kind = tcx.def_kind(did);
39 if let Fn | AssocFn | Variant | Ctor(..) = def_kind {
40 Some(tcx.fn_sig(did))
41 } else {
42 tcx.dcx().span_delayed_bug(attr_span, "this attribute can only be applied to functions");
43 None
44 }
45}
46
47fn parse_instruction_set_attr(tcx: TyCtxt<'_>, attr: &Attribute) -> Option<InstructionSetAttr> {
49 let list = attr.meta_item_list()?;
50
51 match &list[..] {
52 [MetaItemInner::MetaItem(set)] => {
53 let segments = set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
54 match segments.as_slice() {
55 [sym::arm, sym::a32 | sym::t32] if !tcx.sess.target.has_thumb_interworking => {
56 tcx.dcx().emit_err(errors::UnsupportedInstructionSet { span: attr.span() });
57 None
58 }
59 [sym::arm, sym::a32] => Some(InstructionSetAttr::ArmA32),
60 [sym::arm, sym::t32] => Some(InstructionSetAttr::ArmT32),
61 _ => {
62 tcx.dcx().emit_err(errors::InvalidInstructionSet { span: attr.span() });
63 None
64 }
65 }
66 }
67 [] => {
68 tcx.dcx().emit_err(errors::BareInstructionSet { span: attr.span() });
69 None
70 }
71 _ => {
72 tcx.dcx().emit_err(errors::MultipleInstructionSet { span: attr.span() });
73 None
74 }
75 }
76}
77
78fn parse_patchable_function_entry(
80 tcx: TyCtxt<'_>,
81 attr: &Attribute,
82) -> Option<PatchableFunctionEntry> {
83 attr.meta_item_list().and_then(|l| {
84 let mut prefix = None;
85 let mut entry = None;
86 for item in l {
87 let Some(meta_item) = item.meta_item() else {
88 tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
89 continue;
90 };
91
92 let Some(name_value_lit) = meta_item.name_value_literal() else {
93 tcx.dcx().emit_err(errors::ExpectedNameValuePair { span: item.span() });
94 continue;
95 };
96
97 let attrib_to_write = match meta_item.name() {
98 Some(sym::prefix_nops) => &mut prefix,
99 Some(sym::entry_nops) => &mut entry,
100 _ => {
101 tcx.dcx().emit_err(errors::UnexpectedParameterName {
102 span: item.span(),
103 prefix_nops: sym::prefix_nops,
104 entry_nops: sym::entry_nops,
105 });
106 continue;
107 }
108 };
109
110 let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else {
111 tcx.dcx().emit_err(errors::InvalidLiteralValue { span: name_value_lit.span });
112 continue;
113 };
114
115 let Ok(val) = val.get().try_into() else {
116 tcx.dcx().emit_err(errors::OutOfRangeInteger { span: name_value_lit.span });
117 continue;
118 };
119
120 *attrib_to_write = Some(val);
121 }
122
123 if let (None, None) = (prefix, entry) {
124 tcx.dcx().span_err(attr.span(), "must specify at least one parameter");
125 }
126
127 Some(PatchableFunctionEntry::from_prefix_and_entry(prefix.unwrap_or(0), entry.unwrap_or(0)))
128 })
129}
130
131#[derive(Default)]
134struct InterestingAttributeDiagnosticSpans {
135 link_ordinal: Option<Span>,
136 sanitize: Option<Span>,
137 inline: Option<Span>,
138 no_mangle: Option<Span>,
139}
140
141fn process_builtin_attrs(
144 tcx: TyCtxt<'_>,
145 did: LocalDefId,
146 attrs: &[Attribute],
147 codegen_fn_attrs: &mut CodegenFnAttrs,
148) -> InterestingAttributeDiagnosticSpans {
149 let mut interesting_spans = InterestingAttributeDiagnosticSpans::default();
150 let rust_target_features = tcx.rust_target_features(LOCAL_CRATE);
151
152 for attr in attrs.iter() {
153 if let hir::Attribute::Parsed(p) = attr {
154 match p {
155 AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
156 AttributeKind::ExportName { name, .. } => {
157 codegen_fn_attrs.symbol_name = Some(*name)
158 }
159 AttributeKind::Inline(inline, span) => {
160 codegen_fn_attrs.inline = *inline;
161 interesting_spans.inline = Some(*span);
162 }
163 AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
164 AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
165 AttributeKind::LinkName { name, .. } => {
166 if tcx.is_foreign_item(did) {
169 codegen_fn_attrs.symbol_name = Some(*name);
170 }
171 }
172 AttributeKind::LinkOrdinal { ordinal, span } => {
173 codegen_fn_attrs.link_ordinal = Some(*ordinal);
174 interesting_spans.link_ordinal = Some(*span);
175 }
176 AttributeKind::LinkSection { name, .. } => {
177 codegen_fn_attrs.link_section = Some(*name)
178 }
179 AttributeKind::NoMangle(attr_span) => {
180 interesting_spans.no_mangle = Some(*attr_span);
181 if tcx.opt_item_name(did.to_def_id()).is_some() {
182 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
183 } else {
184 tcx.dcx().span_delayed_bug(
185 *attr_span,
186 "no_mangle should be on a named function",
187 );
188 }
189 }
190 AttributeKind::Optimize(optimize, _) => codegen_fn_attrs.optimize = *optimize,
191 AttributeKind::TargetFeature { features, attr_span, was_forced } => {
192 let Some(sig) = tcx.hir_node_by_def_id(did).fn_sig() else {
193 tcx.dcx().span_delayed_bug(*attr_span, "target_feature applied to non-fn");
194 continue;
195 };
196 let safe_target_features =
197 matches!(sig.header.safety, hir::HeaderSafety::SafeTargetFeatures);
198 codegen_fn_attrs.safe_target_features = safe_target_features;
199 if safe_target_features && !was_forced {
200 if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
201 } else {
223 check_target_feature_trait_unsafe(tcx, did, *attr_span);
224 }
225 }
226 from_target_feature_attr(
227 tcx,
228 did,
229 features,
230 *was_forced,
231 rust_target_features,
232 &mut codegen_fn_attrs.target_features,
233 );
234 }
235 AttributeKind::TrackCaller(attr_span) => {
236 let is_closure = tcx.is_closure_like(did.to_def_id());
237
238 if !is_closure
239 && let Some(fn_sig) = try_fn_sig(tcx, did, *attr_span)
240 && fn_sig.skip_binder().abi() != ExternAbi::Rust
241 {
242 tcx.dcx().emit_err(errors::RequiresRustAbi { span: *attr_span });
243 }
244 if is_closure
245 && !tcx.features().closure_track_caller()
246 && !attr_span.allows_unstable(sym::closure_track_caller)
247 {
248 feature_err(
249 &tcx.sess,
250 sym::closure_track_caller,
251 *attr_span,
252 "`#[track_caller]` on closures is currently unstable",
253 )
254 .emit();
255 }
256 codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER
257 }
258 AttributeKind::Used { used_by, .. } => match used_by {
259 UsedBy::Compiler => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_COMPILER,
260 UsedBy::Linker => codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER,
261 UsedBy::Default => {
262 let used_form = if tcx.sess.target.os == "illumos" {
263 CodegenFnAttrFlags::USED_COMPILER
269 } else {
270 CodegenFnAttrFlags::USED_LINKER
271 };
272 codegen_fn_attrs.flags |= used_form;
273 }
274 },
275 AttributeKind::FfiConst(_) => {
276 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST
277 }
278 AttributeKind::FfiPure(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE,
279 AttributeKind::StdInternalSymbol(_) => {
280 codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL
281 }
282 AttributeKind::Linkage(linkage, span) => {
283 let linkage = Some(*linkage);
284
285 if tcx.is_foreign_item(did) {
286 codegen_fn_attrs.import_linkage = linkage;
287
288 if tcx.is_mutable_static(did.into()) {
289 let mut diag = tcx.dcx().struct_span_err(
290 *span,
291 "extern mutable statics are not allowed with `#[linkage]`",
292 );
293 diag.note(
294 "marking the extern static mutable would allow changing which \
295 symbol the static references rather than make the target of the \
296 symbol mutable",
297 );
298 diag.emit();
299 }
300 } else {
301 codegen_fn_attrs.linkage = linkage;
302 }
303 }
304 AttributeKind::Sanitize { span, .. } => {
305 interesting_spans.sanitize = Some(*span);
306 }
307 AttributeKind::ObjcClass { classname, .. } => {
308 codegen_fn_attrs.objc_class = Some(*classname);
309 }
310 AttributeKind::ObjcSelector { methname, .. } => {
311 codegen_fn_attrs.objc_selector = Some(*methname);
312 }
313 _ => {}
314 }
315 }
316
317 let Some(Ident { name, .. }) = attr.ident() else {
318 continue;
319 };
320
321 match name {
322 sym::rustc_allocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR,
323 sym::rustc_nounwind => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND,
324 sym::rustc_reallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR,
325 sym::rustc_deallocator => codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR,
326 sym::rustc_allocator_zeroed => {
327 codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
328 }
329 sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
330 sym::instruction_set => {
331 codegen_fn_attrs.instruction_set = parse_instruction_set_attr(tcx, attr)
332 }
333 sym::patchable_function_entry => {
334 codegen_fn_attrs.patchable_function_entry =
335 parse_patchable_function_entry(tcx, attr);
336 }
337 _ => {}
338 }
339 }
340
341 interesting_spans
342}
343
344fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut CodegenFnAttrs) {
347 codegen_fn_attrs.alignment =
351 Ord::max(codegen_fn_attrs.alignment, tcx.sess.opts.unstable_opts.min_function_alignment);
352
353 codegen_fn_attrs.no_sanitize |= tcx.disabled_sanitizers_for(did);
355 codegen_fn_attrs.alignment = Ord::max(codegen_fn_attrs.alignment, tcx.inherited_align(did));
357
358 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
362 codegen_fn_attrs.inline = InlineAttr::Never;
363 }
364
365 if tcx.is_closure_like(did.to_def_id()) && codegen_fn_attrs.inline != InlineAttr::Always {
379 let owner_id = tcx.parent(did.to_def_id());
380 if tcx.def_kind(owner_id).has_codegen_attrs() {
381 codegen_fn_attrs
382 .target_features
383 .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
384 }
385 }
386
387 let crate_attrs = tcx.hir_attrs(rustc_hir::CRATE_HIR_ID);
390 let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
391 if no_builtins {
392 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_BUILTINS;
393 }
394
395 if tcx.should_inherit_track_caller(did) {
397 codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
398 }
399
400 if tcx.is_foreign_item(did) {
402 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FOREIGN_ITEM;
403
404 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
406 } else if codegen_fn_attrs.symbol_name.is_some() {
410 } else {
412 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
420 }
421 }
422}
423
424fn check_result(
425 tcx: TyCtxt<'_>,
426 did: LocalDefId,
427 interesting_spans: InterestingAttributeDiagnosticSpans,
428 codegen_fn_attrs: &CodegenFnAttrs,
429) {
430 if !codegen_fn_attrs.target_features.is_empty()
444 && matches!(codegen_fn_attrs.inline, InlineAttr::Always)
445 && !tcx.features().target_feature_inline_always()
446 && let Some(span) = interesting_spans.inline
447 {
448 feature_err(
449 tcx.sess,
450 sym::target_feature_inline_always,
451 span,
452 "cannot use `#[inline(always)]` with `#[target_feature]`",
453 )
454 .emit();
455 }
456
457 if !codegen_fn_attrs.no_sanitize.is_empty()
459 && codegen_fn_attrs.inline.always()
460 && let (Some(no_sanitize_span), Some(inline_span)) =
461 (interesting_spans.sanitize, interesting_spans.inline)
462 {
463 let hir_id = tcx.local_def_id_to_hir_id(did);
464 tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| {
465 lint.primary_message("setting `sanitize` off will have no effect after inlining");
466 lint.span_note(inline_span, "inlining requested here");
467 })
468 }
469
470 if let Some(_) = codegen_fn_attrs.symbol_name
472 && let Some(_) = codegen_fn_attrs.link_ordinal
473 {
474 let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
475 if let Some(span) = interesting_spans.link_ordinal {
476 tcx.dcx().span_err(span, msg);
477 } else {
478 tcx.dcx().err(msg);
479 }
480 }
481
482 if let Some(features) = check_tied_features(
483 tcx.sess,
484 &codegen_fn_attrs
485 .target_features
486 .iter()
487 .map(|features| (features.name.as_str(), true))
488 .collect(),
489 ) {
490 let span =
491 find_attr!(tcx.get_all_attrs(did), AttributeKind::TargetFeature{attr_span: span, ..} => *span)
492 .unwrap_or_else(|| tcx.def_span(did));
493
494 tcx.dcx()
495 .create_err(errors::TargetFeatureDisableOrEnable {
496 features,
497 span: Some(span),
498 missing_features: Some(errors::MissingFeatures),
499 })
500 .emit();
501 }
502}
503
504fn handle_lang_items(
505 tcx: TyCtxt<'_>,
506 did: LocalDefId,
507 interesting_spans: &InterestingAttributeDiagnosticSpans,
508 attrs: &[Attribute],
509 codegen_fn_attrs: &mut CodegenFnAttrs,
510) {
511 let lang_item = lang_items::extract(attrs).and_then(|(name, _)| LangItem::from_name(name));
512
513 if let Some(lang_item) = lang_item
519 && let Some(link_name) = lang_item.link_name()
520 {
521 codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
522 codegen_fn_attrs.symbol_name = Some(link_name);
523 }
524
525 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
527 && codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
528 {
529 let mut err = tcx
530 .dcx()
531 .struct_span_err(
532 interesting_spans.no_mangle.unwrap_or_default(),
533 "`#[no_mangle]` cannot be used on internal language items",
534 )
535 .with_note("Rustc requires this item to have a specific mangled name.")
536 .with_span_label(tcx.def_span(did), "should be the internal language item");
537 if let Some(lang_item) = lang_item
538 && let Some(link_name) = lang_item.link_name()
539 {
540 err = err
541 .with_note("If you are trying to prevent mangling to ease debugging, many")
542 .with_note(format!("debuggers support a command such as `rbreak {link_name}` to"))
543 .with_note(format!(
544 "match `.*{link_name}.*` instead of `break {link_name}` on a specific name"
545 ))
546 }
547 err.emit();
548 }
549}
550
551fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
560 if cfg!(debug_assertions) {
561 let def_kind = tcx.def_kind(did);
562 assert!(
563 def_kind.has_codegen_attrs(),
564 "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
565 );
566 }
567
568 let mut codegen_fn_attrs = CodegenFnAttrs::new();
569 let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(did));
570
571 let interesting_spans = process_builtin_attrs(tcx, did, attrs, &mut codegen_fn_attrs);
572 handle_lang_items(tcx, did, &interesting_spans, attrs, &mut codegen_fn_attrs);
573 apply_overrides(tcx, did, &mut codegen_fn_attrs);
574 check_result(tcx, did, interesting_spans, &codegen_fn_attrs);
575
576 codegen_fn_attrs
577}
578
579fn disabled_sanitizers_for(tcx: TyCtxt<'_>, did: LocalDefId) -> SanitizerSet {
580 let mut disabled = match tcx.opt_local_parent(did) {
582 Some(parent) => tcx.disabled_sanitizers_for(parent),
584 None => SanitizerSet::empty(),
587 };
588
589 if let Some((on_set, off_set)) = find_attr!(tcx.get_all_attrs(did), AttributeKind::Sanitize {on_set, off_set, ..} => (on_set, off_set))
591 {
592 disabled &= !*on_set;
595 disabled |= *off_set;
598 }
602 disabled
603}
604
605fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
608 tcx.trait_item_of(def_id).is_some_and(|id| {
609 tcx.codegen_fn_attrs(id).flags.intersects(CodegenFnAttrFlags::TRACK_CALLER)
610 })
611}
612
613fn inherited_align<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Align> {
616 tcx.codegen_fn_attrs(tcx.trait_item_of(def_id)?).alignment
617}
618
619pub fn autodiff_attrs(tcx: TyCtxt<'_>, id: DefId) -> Option<AutoDiffAttrs> {
626 let attrs = tcx.get_attrs(id, sym::rustc_autodiff);
627
628 let attrs = attrs.filter(|attr| attr.has_name(sym::rustc_autodiff)).collect::<Vec<_>>();
629
630 let attr = match &attrs[..] {
633 [] => return None,
634 [attr] => attr,
635 _ => {
636 span_bug!(attrs[1].span(), "cg_ssa: rustc_autodiff should only exist once per source");
637 }
638 };
639
640 let list = attr.meta_item_list().unwrap_or_default();
641
642 if list.is_empty() {
644 return Some(AutoDiffAttrs::source());
645 }
646
647 let [mode, width_meta, input_activities @ .., ret_activity] = &list[..] else {
648 span_bug!(attr.span(), "rustc_autodiff attribute must contain mode, width and activities");
649 };
650 let mode = if let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = mode {
651 p1.segments.first().unwrap().ident
652 } else {
653 span_bug!(attr.span(), "rustc_autodiff attribute must contain mode");
654 };
655
656 let mode = match mode.as_str() {
658 "Forward" => DiffMode::Forward,
659 "Reverse" => DiffMode::Reverse,
660 _ => {
661 span_bug!(mode.span, "rustc_autodiff attribute contains invalid mode");
662 }
663 };
664
665 let width: u32 = match width_meta {
666 MetaItemInner::MetaItem(MetaItem { path: p1, .. }) => {
667 let w = p1.segments.first().unwrap().ident;
668 match w.as_str().parse() {
669 Ok(val) => val,
670 Err(_) => {
671 span_bug!(w.span, "rustc_autodiff width should fit u32");
672 }
673 }
674 }
675 MetaItemInner::Lit(lit) => {
676 if let LitKind::Int(val, _) = lit.kind {
677 match val.get().try_into() {
678 Ok(val) => val,
679 Err(_) => {
680 span_bug!(lit.span, "rustc_autodiff width should fit u32");
681 }
682 }
683 } else {
684 span_bug!(lit.span, "rustc_autodiff width should be an integer");
685 }
686 }
687 };
688
689 let ret_symbol = if let MetaItemInner::MetaItem(MetaItem { path: p1, .. }) = ret_activity {
691 p1.segments.first().unwrap().ident
692 } else {
693 span_bug!(attr.span(), "rustc_autodiff attribute must contain the return activity");
694 };
695
696 let Ok(ret_activity) = DiffActivity::from_str(ret_symbol.as_str()) else {
698 span_bug!(ret_symbol.span, "invalid return activity");
699 };
700
701 let mut arg_activities: Vec<DiffActivity> = vec![];
703 for arg in input_activities {
704 let arg_symbol = if let MetaItemInner::MetaItem(MetaItem { path: p2, .. }) = arg {
705 match p2.segments.first() {
706 Some(x) => x.ident,
707 None => {
708 span_bug!(
709 arg.span(),
710 "rustc_autodiff attribute must contain the input activity"
711 );
712 }
713 }
714 } else {
715 span_bug!(arg.span(), "rustc_autodiff attribute must contain the input activity");
716 };
717
718 match DiffActivity::from_str(arg_symbol.as_str()) {
719 Ok(arg_activity) => arg_activities.push(arg_activity),
720 Err(_) => {
721 span_bug!(arg_symbol.span, "invalid input activity");
722 }
723 }
724 }
725
726 Some(AutoDiffAttrs { mode, width, ret_activity, input_activity: arg_activities })
727}
728
729pub(crate) fn provide(providers: &mut Providers) {
730 *providers = Providers {
731 codegen_fn_attrs,
732 should_inherit_track_caller,
733 inherited_align,
734 disabled_sanitizers_for,
735 ..*providers
736 };
737}