Skip to content

Commit 2e64b53

Browse files
committed
add dedicated type for ABI target feature constraints
1 parent 43ede97 commit 2e64b53

File tree

5 files changed

+45
-31
lines changed

5 files changed

+45
-31
lines changed

compiler/rustc_codegen_gcc/src/gcc_util.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
4747

4848
// Ensure that all ABI-required features are enabled, and the ABI-forbidden ones
4949
// are disabled.
50-
let (abi_enable, abi_disable) = sess.target.abi_required_features();
51-
let abi_enable_set = FxHashSet::from_iter(abi_enable.iter().copied());
52-
let abi_disable_set = FxHashSet::from_iter(abi_disable.iter().copied());
50+
let abi_feature_constraints = sess.target.abi_required_features();
51+
let abi_incompatible_set =
52+
FxHashSet::from_iter(abi_feature_constraints.incompatible.iter().copied());
5353

5454
// Compute implied features
5555
let mut all_rust_features = vec![];
@@ -72,7 +72,7 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
7272
}
7373
}
7474
}
75-
// Remove features that are meant for rustc, not LLVM.
75+
// Remove features that are meant for rustc, not codegen.
7676
all_rust_features.retain(|(_, feature)| {
7777
// Retain if it is not a rustc feature
7878
!RUSTC_SPECIFIC_FEATURES.contains(feature)
@@ -121,7 +121,7 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
121121

122122
// Ensure that the features we enable/disable are compatible with the ABI.
123123
if enable {
124-
if abi_disable_set.contains(feature) {
124+
if abi_incompatible_set.contains(feature) {
125125
sess.dcx().emit_warn(ForbiddenCTargetFeature {
126126
feature,
127127
enabled: "enabled",
@@ -131,8 +131,7 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
131131
} else {
132132
// FIXME: we have to request implied features here since
133133
// negative features do not handle implied features above.
134-
#[allow(rustc::potential_query_instability)] // order does not matter
135-
for &required in abi_enable_set.iter() {
134+
for &required in abi_feature_constraints.required.iter() {
136135
let implied = sess.target.implied_target_features(std::iter::once(required));
137136
if implied.contains(feature) {
138137
sess.dcx().emit_warn(ForbiddenCTargetFeature {
@@ -158,7 +157,11 @@ pub(crate) fn global_gcc_features(sess: &Session, diagnostics: bool) -> Vec<Stri
158157
// still override it... that's unsound, but more compatible with past behavior.
159158
all_rust_features.splice(
160159
0..0,
161-
abi_enable.iter().map(|&f| (true, f)).chain(abi_disable.iter().map(|&f| (false, f))),
160+
abi_feature_constraints
161+
.required
162+
.iter()
163+
.map(|&f| (true, f))
164+
.chain(abi_feature_constraints.incompatible.iter().map(|&f| (false, f))),
162165
);
163166

164167
// Translate this into GCC features.

compiler/rustc_codegen_llvm/src/llvm_util.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,9 @@ pub(crate) fn global_llvm_features(
669669

670670
// Ensure that all ABI-required features are enabled, and the ABI-forbidden ones
671671
// are disabled.
672-
let (abi_enable, abi_disable) = sess.target.abi_required_features();
673-
let abi_enable_set = FxHashSet::from_iter(abi_enable.iter().copied());
674-
let abi_disable_set = FxHashSet::from_iter(abi_disable.iter().copied());
672+
let abi_feature_constraints = sess.target.abi_required_features();
673+
let abi_incompatible_set =
674+
FxHashSet::from_iter(abi_feature_constraints.incompatible.iter().copied());
675675

676676
// Compute implied features
677677
let mut all_rust_features = vec![];
@@ -745,7 +745,7 @@ pub(crate) fn global_llvm_features(
745745

746746
// Ensure that the features we enable/disable are compatible with the ABI.
747747
if enable {
748-
if abi_disable_set.contains(feature) {
748+
if abi_incompatible_set.contains(feature) {
749749
sess.dcx().emit_warn(ForbiddenCTargetFeature {
750750
feature,
751751
enabled: "enabled",
@@ -755,8 +755,7 @@ pub(crate) fn global_llvm_features(
755755
} else {
756756
// FIXME: we have to request implied features here since
757757
// negative features do not handle implied features above.
758-
#[allow(rustc::potential_query_instability)] // order does not matter
759-
for &required in abi_enable_set.iter() {
758+
for &required in abi_feature_constraints.required.iter() {
760759
let implied =
761760
sess.target.implied_target_features(std::iter::once(required));
762761
if implied.contains(feature) {
@@ -783,7 +782,11 @@ pub(crate) fn global_llvm_features(
783782
// still override it... that's unsound, but more compatible with past behavior.
784783
all_rust_features.splice(
785784
0..0,
786-
abi_enable.iter().map(|&f| (true, f)).chain(abi_disable.iter().map(|&f| (false, f))),
785+
abi_feature_constraints
786+
.required
787+
.iter()
788+
.map(|&f| (true, f))
789+
.chain(abi_feature_constraints.incompatible.iter().map(|&f| (false, f))),
787790
);
788791

789792
// Translate this into LLVM features.

compiler/rustc_codegen_ssa/src/target_features.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn from_target_feature_attr(
3232
.emit();
3333
};
3434
let rust_features = tcx.features();
35-
let (_abi_enable, abi_disable) = tcx.sess.target.abi_required_features();
35+
let abi_feature_constraints = tcx.sess.target.abi_required_features();
3636
for item in list {
3737
// Only `enable = ...` is accepted in the meta-item list.
3838
if !item.has_name(sym::enable) {
@@ -87,7 +87,7 @@ pub(crate) fn from_target_feature_attr(
8787
// But ensure the ABI does not forbid enabling this.
8888
// Here we do assume that LLVM doesn't add even more implied features
8989
// we don't know about, at least no features that would have ABI effects!
90-
if abi_disable.contains(&name.as_str()) {
90+
if abi_feature_constraints.incompatible.contains(&name.as_str()) {
9191
tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
9292
span: item.span(),
9393
feature: name.as_str(),

compiler/rustc_target/src/spec/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3242,8 +3242,8 @@ impl Target {
32423242
}
32433243
}
32443244
// Check that we don't mis-set any of the ABI-relevant features.
3245-
let (abi_enable, abi_disable) = self.abi_required_features();
3246-
for feat in abi_enable {
3245+
let abi_feature_constraints = self.abi_required_features();
3246+
for feat in abi_feature_constraints.required {
32473247
// The feature might be enabled by default so we can't *require* it to show up.
32483248
// But it must not be *disabled*.
32493249
if features_disabled.contains(feat) {
@@ -3252,8 +3252,8 @@ impl Target {
32523252
));
32533253
}
32543254
}
3255-
for feat in abi_disable {
3256-
// The feature might be disable by default so we can't *require* it to show up.
3255+
for feat in abi_feature_constraints.incompatible {
3256+
// The feature might be disabled by default so we can't *require* it to show up.
32573257
// But it must not be *enabled*.
32583258
if features_enabled.contains(feat) {
32593259
return Err(format!(

compiler/rustc_target/src/target_features.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,14 @@ const CSKY_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] = &[(
669669
const LOONGARCH_FEATURES_FOR_CORRECT_VECTOR_ABI: &'static [(u64, &'static str)] =
670670
&[(128, "lsx"), (256, "lasx")];
671671

672+
#[derive(Copy, Clone, Debug)]
673+
pub struct FeatureConstraints {
674+
/// Features that must be enabled.
675+
pub required: &'static [&'static str],
676+
/// Features that must be disabled.
677+
pub incompatible: &'static [&'static str],
678+
}
679+
672680
impl Target {
673681
pub fn rust_target_features(&self) -> &'static [(&'static str, Stability, ImpliedFeatures)] {
674682
match &*self.arch {
@@ -749,8 +757,8 @@ impl Target {
749757
/// All features enabled/disabled via `-Ctarget-features` and `#[target_features]` are checked
750758
/// against this. We also check any implied features, based on the information above. If LLVM
751759
/// implicitly enables more implied features than we do, that could bypass this check!
752-
pub fn abi_required_features(&self) -> (&'static [&'static str], &'static [&'static str]) {
753-
const NOTHING: (&'static [&'static str], &'static [&'static str]) = (&[], &[]);
760+
pub fn abi_required_features(&self) -> FeatureConstraints {
761+
const NOTHING: FeatureConstraints = FeatureConstraints { required: &[], incompatible: &[] };
754762
// Some architectures don't have a clean explicit ABI designation; instead, the ABI is
755763
// defined by target features. When that is the case, those target features must be
756764
// "forbidden" in the list above to ensure that there is a consistent answer to the
@@ -763,7 +771,7 @@ impl Target {
763771
NOTHING
764772
} else {
765773
// Hardfloat ABI. x87 must be enabled.
766-
(&["x87"], &[])
774+
FeatureConstraints { required: &["x87"], incompatible: &[] }
767775
}
768776
}
769777
"x86_64" => {
@@ -773,7 +781,7 @@ impl Target {
773781
NOTHING
774782
} else {
775783
// Hardfloat ABI. x87 and SSE2 must be enabled.
776-
(&["x87", "sse2"], &[])
784+
FeatureConstraints { required: &["x87", "sse2"], incompatible: &[] }
777785
}
778786
}
779787
"arm" => {
@@ -786,7 +794,7 @@ impl Target {
786794
}
787795
FloatAbi::Hard => {
788796
// Must have `fpregs` and must not have `soft-float`.
789-
(&["fpregs"], &["soft-float"])
797+
FeatureConstraints { required: &["fpregs"], incompatible: &["soft-float"] }
790798
}
791799
}
792800
}
@@ -803,7 +811,7 @@ impl Target {
803811
_ => {
804812
// Everything else is assumed to use a hardfloat ABI. neon and fp-armv8 must be enabled.
805813
// These are Rust feature names and we use "neon" to control both of them.
806-
(&["neon"], &[])
814+
FeatureConstraints { required: &["neon"], incompatible: &[] }
807815
}
808816
}
809817
}
@@ -813,15 +821,15 @@ impl Target {
813821
match &*self.llvm_abiname {
814822
"ilp32d" | "lp64d" => {
815823
// Requires d (which implies f), incompatible with e.
816-
(&["d"], &["e"])
824+
FeatureConstraints { required: &["d"], incompatible: &["e"] }
817825
}
818826
"ilp32f" | "lp64f" => {
819827
// Requires f, incompatible with e.
820-
(&["f"], &["e"])
828+
FeatureConstraints { required: &["f"], incompatible: &["e"] }
821829
}
822830
"ilp32" | "lp64" => {
823831
// Requires nothing, incompatible with e.
824-
(&[], &["e"])
832+
FeatureConstraints { required: &[], incompatible: &["e"] }
825833
}
826834
"ilp32e" => {
827835
// ilp32e is documented to be incompatible with features that need aligned
@@ -832,7 +840,7 @@ impl Target {
832840
// Note that the `e` feature is not required: the ABI treats the extra
833841
// registers as caller-save, so it is safe to use them only in some parts of
834842
// a program while the rest doesn't know they even exist.
835-
(&[], &["d"])
843+
FeatureConstraints { required: &[], incompatible: &["d"] }
836844
}
837845
"lp64e" => {
838846
// As above, `e` is not required.

0 commit comments

Comments
 (0)