rustc_codegen_ssa/
target_features.rs1use rustc_attr_parsing::InstructionSetAttr;
2use rustc_data_structures::fx::FxIndexSet;
3use rustc_data_structures::unord::{UnordMap, UnordSet};
4use rustc_errors::Applicability;
5use rustc_hir as hir;
6use rustc_hir::def::DefKind;
7use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
8use rustc_middle::middle::codegen_fn_attrs::TargetFeature;
9use rustc_middle::query::Providers;
10use rustc_middle::ty::TyCtxt;
11use rustc_session::parse::feature_err;
12use rustc_span::{Span, Symbol, sym};
13use rustc_target::target_features;
14
15use crate::errors;
16
17pub(crate) fn from_target_feature_attr(
20 tcx: TyCtxt<'_>,
21 attr: &hir::Attribute,
22 rust_target_features: &UnordMap<String, target_features::Stability>,
23 target_features: &mut Vec<TargetFeature>,
24) {
25 let Some(list) = attr.meta_item_list() else { return };
26 let bad_item = |span| {
27 let msg = "malformed `target_feature` attribute input";
28 let code = "enable = \"..\"";
29 tcx.dcx()
30 .struct_span_err(span, msg)
31 .with_span_suggestion(span, "must be of the form", code, Applicability::HasPlaceholders)
32 .emit();
33 };
34 let rust_features = tcx.features();
35 let abi_feature_constraints = tcx.sess.target.abi_required_features();
36 for item in list {
37 if !item.has_name(sym::enable) {
39 bad_item(item.span());
40 continue;
41 }
42
43 let Some(value) = item.value_str() else {
45 bad_item(item.span());
46 continue;
47 };
48
49 for feature in value.as_str().split(',') {
51 let Some(stability) = rust_target_features.get(feature) else {
52 let msg = format!("the feature named `{feature}` is not valid for this target");
53 let mut err = tcx.dcx().struct_span_err(item.span(), msg);
54 err.span_label(item.span(), format!("`{feature}` is not valid for this target"));
55 if let Some(stripped) = feature.strip_prefix('+') {
56 let valid = rust_target_features.contains_key(stripped);
57 if valid {
58 err.help("consider removing the leading `+` in the feature name");
59 }
60 }
61 err.emit();
62 continue;
63 };
64
65 if let Err(reason) = stability.toggle_allowed() {
68 tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
69 span: item.span(),
70 feature,
71 reason,
72 });
73 } else if let Some(nightly_feature) = stability.requires_nightly()
74 && !rust_features.enabled(nightly_feature)
75 {
76 feature_err(
77 &tcx.sess,
78 nightly_feature,
79 item.span(),
80 format!("the target feature `{feature}` is currently unstable"),
81 )
82 .emit();
83 } else {
84 let feature_sym = Symbol::intern(feature);
86 for &name in tcx.implied_target_features(feature_sym) {
87 if abi_feature_constraints.incompatible.contains(&name.as_str()) {
91 tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
92 span: item.span(),
93 feature: name.as_str(),
94 reason: "this feature is incompatible with the target ABI",
95 });
96 }
97 target_features.push(TargetFeature { name, implied: name != feature_sym })
98 }
99 }
100 }
101 }
102}
103
104fn asm_target_features(tcx: TyCtxt<'_>, did: DefId) -> &FxIndexSet<Symbol> {
107 let mut target_features = tcx.sess.unstable_target_features.clone();
108 if tcx.def_kind(did).has_codegen_attrs() {
109 let attrs = tcx.codegen_fn_attrs(did);
110 target_features.extend(attrs.target_features.iter().map(|feature| feature.name));
111 match attrs.instruction_set {
112 None => {}
113 Some(InstructionSetAttr::ArmA32) => {
114 target_features.swap_remove(&sym::thumb_mode);
116 }
117 Some(InstructionSetAttr::ArmT32) => {
118 target_features.insert(sym::thumb_mode);
119 }
120 }
121 }
122
123 tcx.arena.alloc(target_features)
124}
125
126pub(crate) fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
129 if let DefKind::AssocFn = tcx.def_kind(id) {
130 let parent_id = tcx.local_parent(id);
131 if let DefKind::Trait | DefKind::Impl { of_trait: true } = tcx.def_kind(parent_id) {
132 tcx.dcx().emit_err(errors::TargetFeatureSafeTrait {
133 span: attr_span,
134 def: tcx.def_span(id),
135 });
136 }
137 }
138}
139
140pub(crate) fn provide(providers: &mut Providers) {
141 *providers = Providers {
142 rust_target_features: |tcx, cnum| {
143 assert_eq!(cnum, LOCAL_CRATE);
144 if tcx.sess.opts.actually_rustdoc {
145 rustc_target::target_features::all_rust_features()
148 .map(|(a, b)| (a.to_string(), b))
149 .collect()
150 } else {
151 tcx.sess
152 .target
153 .rust_target_features()
154 .iter()
155 .map(|(a, b, _)| (a.to_string(), *b))
156 .collect()
157 }
158 },
159 implied_target_features: |tcx, feature: Symbol| {
160 let feature = feature.as_str();
161 UnordSet::from(tcx.sess.target.implied_target_features(std::iter::once(feature)))
162 .into_sorted_stable_ord()
163 .into_iter()
164 .map(|s| Symbol::intern(s))
165 .collect()
166 },
167 asm_target_features,
168 ..*providers
169 }
170}