Skip to content

Commit 1217d70

Browse files
committed
Separately gate each target_feature feature
Use an explicit whitelist for what features are actually stable and can be enabled.
1 parent 598d836 commit 1217d70

File tree

14 files changed

+220
-72
lines changed

14 files changed

+220
-72
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
url = https://github.com/rust-lang/llvm
5050
[submodule "src/stdsimd"]
5151
path = src/stdsimd
52-
url = https://github.com/alexcrichton/stdsimd
52+
url = https://github.com/rust-lang-nursery/stdsimd
5353
[submodule "src/tools/lld"]
5454
path = src/tools/lld
5555
url = https://github.com/rust-lang/lld.git

src/libcore/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@
9999
#![feature(untagged_unions)]
100100
#![feature(unwind_attributes)]
101101

102+
#![cfg_attr(not(stage0), feature(mmx_target_feature))]
103+
#![cfg_attr(not(stage0), feature(tbm_target_feature))]
104+
#![cfg_attr(not(stage0), feature(sse4a_target_feature))]
105+
#![cfg_attr(not(stage0), feature(arm_target_feature))]
106+
#![cfg_attr(not(stage0), feature(powerpc_target_feature))]
107+
#![cfg_attr(not(stage0), feature(mips_target_feature))]
108+
#![cfg_attr(not(stage0), feature(aarch64_target_feature))]
109+
102110
#![cfg_attr(stage0, feature(target_feature))]
103111
#![cfg_attr(stage0, feature(cfg_target_feature))]
104112

src/librustc/ty/maps/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ define_maps! { <'tcx>
437437
substitute_normalize_and_test_predicates_node((DefId, &'tcx Substs<'tcx>)) -> bool,
438438

439439
[] fn target_features_whitelist:
440-
target_features_whitelist_node(CrateNum) -> Lrc<FxHashSet<String>>,
440+
target_features_whitelist_node(CrateNum) -> Lrc<FxHashMap<String, Option<String>>>,
441441

442442
// Get an estimate of the size of an InstanceDef based on its MIR for CGU partitioning.
443443
[] fn instance_def_size_estimate: instance_def_size_estimate_dep_node(ty::InstanceDef<'tcx>)

src/librustc_trans/attributes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ pub fn provide(providers: &mut Providers) {
174174
// rustdoc needs to be able to document functions that use all the features, so
175175
// whitelist them all
176176
Lrc::new(llvm_util::all_known_features()
177-
.map(|c| c.to_string())
177+
.map(|(a, b)| (a.to_string(), b.map(|s| s.to_string())))
178178
.collect())
179179
} else {
180180
Lrc::new(llvm_util::target_feature_whitelist(tcx.sess)
181181
.iter()
182-
.map(|c| c.to_string())
182+
.map(|&(a, b)| (a.to_string(), b.map(|s| s.to_string())))
183183
.collect())
184184
}
185185
};

src/librustc_trans/llvm_util.rs

+94-29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::session::Session;
1515
use rustc::session::config::PrintRequest;
1616
use libc::c_int;
1717
use std::ffi::CString;
18+
use syntax::feature_gate::UnstableFeatures;
1819

1920
use std::sync::atomic::{AtomicBool, Ordering};
2021
use std::sync::Once;
@@ -82,40 +83,95 @@ unsafe fn configure_llvm(sess: &Session) {
8283
// to LLVM or the feature detection code will walk past the end of the feature
8384
// array, leading to crashes.
8485

85-
const ARM_WHITELIST: &'static [&'static str] = &["neon", "v7", "vfp2", "vfp3", "vfp4"];
86-
87-
const AARCH64_WHITELIST: &'static [&'static str] = &["fp", "neon", "sve", "crc", "crypto",
88-
"ras", "lse", "rdm", "fp16", "rcpc",
89-
"dotprod", "v8.1a", "v8.2a", "v8.3a"];
90-
91-
const X86_WHITELIST: &'static [&'static str] = &["aes", "avx", "avx2", "avx512bw",
92-
"avx512cd", "avx512dq", "avx512er",
93-
"avx512f", "avx512ifma", "avx512pf",
94-
"avx512vbmi", "avx512vl", "avx512vpopcntdq",
95-
"bmi1", "bmi2", "fma", "fxsr",
96-
"lzcnt", "mmx", "pclmulqdq",
97-
"popcnt", "rdrand", "rdseed",
98-
"sha",
99-
"sse", "sse2", "sse3", "sse4.1",
100-
"sse4.2", "sse4a", "ssse3",
101-
"tbm", "xsave", "xsavec",
102-
"xsaveopt", "xsaves"];
103-
104-
const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx", "hvx-double"];
105-
106-
const POWERPC_WHITELIST: &'static [&'static str] = &["altivec",
107-
"power8-altivec", "power9-altivec",
108-
"power8-vector", "power9-vector",
109-
"vsx"];
110-
111-
const MIPS_WHITELIST: &'static [&'static str] = &["fp64", "msa"];
86+
const ARM_WHITELIST: &[(&str, Option<&str>)] = &[
87+
("neon", Some("arm_target_feature")),
88+
("v7", Some("arm_target_feature")),
89+
("vfp2", Some("arm_target_feature")),
90+
("vfp3", Some("arm_target_feature")),
91+
("vfp4", Some("arm_target_feature")),
92+
];
93+
94+
const AARCH64_WHITELIST: &[(&str, Option<&str>)] = &[
95+
("fp", Some("aarch64_target_feature")),
96+
("neon", Some("aarch64_target_feature")),
97+
("sve", Some("aarch64_target_feature")),
98+
("crc", Some("aarch64_target_feature")),
99+
("crypto", Some("aarch64_target_feature")),
100+
("ras", Some("aarch64_target_feature")),
101+
("lse", Some("aarch64_target_feature")),
102+
("rdm", Some("aarch64_target_feature")),
103+
("fp16", Some("aarch64_target_feature")),
104+
("rcpc", Some("aarch64_target_feature")),
105+
("dotprod", Some("aarch64_target_feature")),
106+
("v8.1a", Some("aarch64_target_feature")),
107+
("v8.2a", Some("aarch64_target_feature")),
108+
("v8.3a", Some("aarch64_target_feature")),
109+
];
110+
111+
const X86_WHITELIST: &[(&str, Option<&str>)] = &[
112+
("aes", None),
113+
("avx", None),
114+
("avx2", None),
115+
("avx512bw", Some("avx512_target_feature")),
116+
("avx512cd", Some("avx512_target_feature")),
117+
("avx512dq", Some("avx512_target_feature")),
118+
("avx512er", Some("avx512_target_feature")),
119+
("avx512f", Some("avx512_target_feature")),
120+
("avx512ifma", Some("avx512_target_feature")),
121+
("avx512pf", Some("avx512_target_feature")),
122+
("avx512vbmi", Some("avx512_target_feature")),
123+
("avx512vl", Some("avx512_target_feature")),
124+
("avx512vpopcntdq", Some("avx512_target_feature")),
125+
("bmi1", None),
126+
("bmi2", None),
127+
("fma", None),
128+
("fxsr", None),
129+
("lzcnt", None),
130+
("mmx", Some("mmx_target_feature")),
131+
("pclmulqdq", None),
132+
("popcnt", None),
133+
("rdrand", None),
134+
("rdseed", None),
135+
("sha", None),
136+
("sse", None),
137+
("sse2", None),
138+
("sse3", None),
139+
("sse4.1", None),
140+
("sse4.2", None),
141+
("sse4a", Some("sse4a_target_feature")),
142+
("ssse3", None),
143+
("tbm", Some("tbm_target_feature")),
144+
("xsave", None),
145+
("xsavec", None),
146+
("xsaveopt", None),
147+
("xsaves", None),
148+
];
149+
150+
const HEXAGON_WHITELIST: &[(&str, Option<&str>)] = &[
151+
("hvx", Some("hexagon_target_feature")),
152+
("hvx-double", Some("hexagon_target_feature")),
153+
];
154+
155+
const POWERPC_WHITELIST: &[(&str, Option<&str>)] = &[
156+
("altivec", Some("powerpc_target_feature")),
157+
("power8-altivec", Some("powerpc_target_feature")),
158+
("power9-altivec", Some("powerpc_target_feature")),
159+
("power8-vector", Some("powerpc_target_feature")),
160+
("power9-vector", Some("powerpc_target_feature")),
161+
("vsx", Some("powerpc_target_feature")),
162+
];
163+
164+
const MIPS_WHITELIST: &[(&str, Option<&str>)] = &[
165+
("fp64", Some("mips_target_feature")),
166+
("msa", Some("mips_target_feature")),
167+
];
112168

113169
/// When rustdoc is running, provide a list of all known features so that all their respective
114170
/// primtives may be documented.
115171
///
116172
/// IMPORTANT: If you're adding another whitelist to the above lists, make sure to add it to this
117173
/// iterator!
118-
pub fn all_known_features() -> impl Iterator<Item=&'static str> {
174+
pub fn all_known_features() -> impl Iterator<Item=(&'static str, Option<&'static str>)> {
119175
ARM_WHITELIST.iter().cloned()
120176
.chain(AARCH64_WHITELIST.iter().cloned())
121177
.chain(X86_WHITELIST.iter().cloned())
@@ -144,6 +200,13 @@ pub fn target_features(sess: &Session) -> Vec<Symbol> {
144200
let target_machine = create_target_machine(sess, true);
145201
target_feature_whitelist(sess)
146202
.iter()
203+
.filter_map(|&(feature, gate)| {
204+
if UnstableFeatures::from_environment().is_nightly_build() || gate.is_none() {
205+
Some(feature)
206+
} else {
207+
None
208+
}
209+
})
147210
.filter(|feature| {
148211
let llvm_feature = to_llvm_feature(sess, feature);
149212
let cstr = CString::new(llvm_feature).unwrap();
@@ -152,7 +215,9 @@ pub fn target_features(sess: &Session) -> Vec<Symbol> {
152215
.map(|feature| Symbol::intern(feature)).collect()
153216
}
154217

155-
pub fn target_feature_whitelist(sess: &Session) -> &'static [&'static str] {
218+
pub fn target_feature_whitelist(sess: &Session)
219+
-> &'static [(&'static str, Option<&'static str>)]
220+
{
156221
match &*sess.target.target.arch {
157222
"arm" => ARM_WHITELIST,
158223
"aarch64" => AARCH64_WHITELIST,

src/librustc_trans_utils/trans_crate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use rustc::middle::cstore::EncodedMetadata;
4444
use rustc::middle::cstore::MetadataLoader;
4545
use rustc::dep_graph::DepGraph;
4646
use rustc_back::target::Target;
47-
use rustc_data_structures::fx::FxHashSet;
47+
use rustc_data_structures::fx::FxHashMap;
4848
use rustc_mir::monomorphize::collector;
4949
use link::{build_link_meta, out_filename};
5050

@@ -203,7 +203,7 @@ impl TransCrate for MetadataOnlyTransCrate {
203203
::symbol_names::provide(providers);
204204

205205
providers.target_features_whitelist = |_tcx, _cnum| {
206-
Lrc::new(FxHashSet()) // Just a dummy
206+
Lrc::new(FxHashMap()) // Just a dummy
207207
};
208208
}
209209
fn provide_extern(&self, _providers: &mut Providers) {}

src/librustc_typeck/collect.rs

+54-33
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ use rustc::ty::maps::Providers;
3737
use rustc::ty::util::IntTypeExt;
3838
use rustc::ty::util::Discr;
3939
use rustc::util::captures::Captures;
40-
use rustc::util::nodemap::{FxHashSet, FxHashMap};
40+
use rustc::util::nodemap::FxHashMap;
4141

4242
use syntax::{abi, ast};
4343
use syntax::ast::MetaItemKind;
4444
use syntax::attr::{InlineAttr, list_contains_name, mark_used};
4545
use syntax::codemap::Spanned;
4646
use syntax::symbol::{Symbol, keywords};
47+
use syntax::feature_gate;
4748
use syntax_pos::{Span, DUMMY_SP};
4849

4950
use rustc::hir::{self, map as hir_map, TransFnAttrs, TransFnAttrFlags, Unsafety};
@@ -1682,7 +1683,7 @@ fn is_foreign_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
16821683
fn from_target_feature(
16831684
tcx: TyCtxt,
16841685
attr: &ast::Attribute,
1685-
whitelist: &FxHashSet<String>,
1686+
whitelist: &FxHashMap<String, Option<String>>,
16861687
target_features: &mut Vec<Symbol>,
16871688
) {
16881689
let list = match attr.meta_item_list() {
@@ -1694,41 +1695,75 @@ fn from_target_feature(
16941695
return
16951696
}
16961697
};
1697-
1698+
let rust_features = tcx.features();
16981699
for item in list {
1700+
// Only `enable = ...` is accepted in the meta item list
16991701
if !item.check_name("enable") {
17001702
let msg = "#[target_feature(..)] only accepts sub-keys of `enable` \
17011703
currently";
17021704
tcx.sess.span_err(item.span, &msg);
17031705
continue
17041706
}
1707+
1708+
// Must be of the form `enable = "..."` ( a string)
17051709
let value = match item.value_str() {
1706-
Some(list) => list,
1710+
Some(value) => value,
17071711
None => {
17081712
let msg = "#[target_feature] attribute must be of the form \
17091713
#[target_feature(enable = \"..\")]";
17101714
tcx.sess.span_err(item.span, &msg);
17111715
continue
17121716
}
17131717
};
1714-
let value = value.as_str();
1715-
for feature in value.split(',') {
1716-
if whitelist.contains(feature) {
1717-
target_features.push(Symbol::intern(feature));
1718-
continue
1719-
}
1720-
1721-
let msg = format!("the feature named `{}` is not valid for \
1722-
this target", feature);
1723-
let mut err = tcx.sess.struct_span_err(item.span, &msg);
17241718

1725-
if feature.starts_with("+") {
1726-
let valid = whitelist.contains(&feature[1..]);
1727-
if valid {
1728-
err.help("consider removing the leading `+` in the feature name");
1719+
// We allow comma separation to enable multiple features
1720+
for feature in value.as_str().split(',') {
1721+
1722+
// Only allow whitelisted features per platform
1723+
let feature_gate = match whitelist.get(feature) {
1724+
Some(g) => g,
1725+
None => {
1726+
let msg = format!("the feature named `{}` is not valid for \
1727+
this target", feature);
1728+
let mut err = tcx.sess.struct_span_err(item.span, &msg);
1729+
1730+
if feature.starts_with("+") {
1731+
let valid = whitelist.contains_key(&feature[1..]);
1732+
if valid {
1733+
err.help("consider removing the leading `+` in the feature name");
1734+
}
1735+
}
1736+
err.emit();
1737+
continue
17291738
}
1739+
};
1740+
1741+
// Only allow features whose feature gates have been enabled
1742+
let allowed = match feature_gate.as_ref().map(|s| &**s) {
1743+
Some("arm_target_feature") => rust_features.arm_target_feature,
1744+
Some("aarch64_target_feature") => rust_features.aarch64_target_feature,
1745+
Some("hexagon_target_feature") => rust_features.hexagon_target_feature,
1746+
Some("powerpc_target_feature") => rust_features.powerpc_target_feature,
1747+
Some("mips_target_feature") => rust_features.mips_target_feature,
1748+
Some("avx512_target_feature") => rust_features.avx512_target_feature,
1749+
Some("mmx_target_feature") => rust_features.mmx_target_feature,
1750+
Some("sse4a_target_feature") => rust_features.sse4a_target_feature,
1751+
Some("tbm_target_feature") => rust_features.tbm_target_feature,
1752+
Some(name) => bug!("unknown target feature gate {}", name),
1753+
None => true,
1754+
};
1755+
if !allowed {
1756+
feature_gate::emit_feature_err(
1757+
&tcx.sess.parse_sess,
1758+
feature_gate.as_ref().unwrap(),
1759+
item.span,
1760+
feature_gate::GateIssue::Language,
1761+
&format!("the target feature `{}` is currently unstable",
1762+
feature),
1763+
);
1764+
continue
17301765
}
1731-
err.emit();
1766+
target_features.push(Symbol::intern(feature));
17321767
}
17331768
}
17341769
}
@@ -1835,20 +1870,6 @@ fn trans_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> TransFnAt
18351870
.emit();
18361871
}
18371872
} else if attr.check_name("target_feature") {
1838-
// handle deprecated #[target_feature = "..."]
1839-
if let Some(val) = attr.value_str() {
1840-
for feat in val.as_str().split(",").map(|f| f.trim()) {
1841-
if !feat.is_empty() && !feat.contains('\0') {
1842-
trans_fn_attrs.target_features.push(Symbol::intern(feat));
1843-
}
1844-
}
1845-
let msg = "#[target_feature = \"..\"] is deprecated and will \
1846-
eventually be removed, use \
1847-
#[target_feature(enable = \"..\")] instead";
1848-
tcx.sess.span_warn(attr.span, &msg);
1849-
continue
1850-
}
1851-
18521873
if tcx.fn_sig(id).unsafety() == Unsafety::Normal {
18531874
let msg = "#[target_feature(..)] can only be applied to \
18541875
`unsafe` function";

src/libsyntax/feature_gate.rs

+11
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,17 @@ declare_features! (
446446

447447
// Allows macro invocations in `extern {}` blocks
448448
(active, macros_in_extern, "1.27.0", Some(49476), None),
449+
450+
// unstable #[target_feature] directives
451+
(active, arm_target_feature, "1.27.0", None, None),
452+
(active, aarch64_target_feature, "1.27.0", None, None),
453+
(active, hexagon_target_feature, "1.27.0", None, None),
454+
(active, powerpc_target_feature, "1.27.0", None, None),
455+
(active, mips_target_feature, "1.27.0", None, None),
456+
(active, avx512_target_feature, "1.27.0", None, None),
457+
(active, mmx_target_feature, "1.27.0", None, None),
458+
(active, sse4a_target_feature, "1.27.0", None, None),
459+
(active, tbm_target_feature, "1.27.0", None, None),
449460
);
450461

451462
declare_features! (

src/stdsimd

src/test/run-pass/simd-target-feature-mixup.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// ignore-emscripten
1212

1313
#![feature(repr_simd, target_feature, cfg_target_feature)]
14+
#![feature(avx512_target_feature)]
1415

1516
use std::process::{Command, ExitStatus};
1617
use std::env;

0 commit comments

Comments
 (0)