Skip to content

Commit 17d1cbb

Browse files
committed
Move target feature whitelist from cg_llvm to cg_ssa
These target features have to be supported or at least emulated by alternative codegen backends anyway as they are used by common crates. By moving this list to cg_ssa, other codegen backends don't have to copy this code.
1 parent 32cbc65 commit 17d1cbb

File tree

4 files changed

+155
-154
lines changed

4 files changed

+155
-154
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,15 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
349349
}
350350

351351
pub fn provide(providers: &mut Providers) {
352+
use rustc_codegen_ssa::target_features::{all_known_features, supported_target_features};
352353
providers.supported_target_features = |tcx, cnum| {
353354
assert_eq!(cnum, LOCAL_CRATE);
354355
if tcx.sess.opts.actually_rustdoc {
355356
// rustdoc needs to be able to document functions that use all the features, so
356357
// provide them all.
357-
llvm_util::all_known_features().map(|(a, b)| (a.to_string(), b)).collect()
358+
all_known_features().map(|(a, b)| (a.to_string(), b)).collect()
358359
} else {
359-
llvm_util::supported_target_features(tcx.sess)
360-
.iter()
361-
.map(|&(a, b)| (a.to_string(), b))
362-
.collect()
360+
supported_target_features(tcx.sess).iter().map(|&(a, b)| (a.to_string(), b)).collect()
363361
}
364362
};
365363

compiler/rustc_codegen_llvm/src/llvm_util.rs

+1-149
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::back::write::create_informational_target_machine;
22
use crate::llvm;
33
use libc::c_int;
4+
use rustc_codegen_ssa::target_features::supported_target_features;
45
use rustc_data_structures::fx::FxHashSet;
56
use rustc_feature::UnstableFeatures;
67
use rustc_middle::bug;
78
use rustc_session::config::PrintRequest;
89
use rustc_session::Session;
9-
use rustc_span::symbol::sym;
1010
use rustc_span::symbol::Symbol;
1111
use rustc_target::spec::{MergeFunctions, PanicStrategy};
1212
use std::ffi::CString;
@@ -139,140 +139,6 @@ pub fn time_trace_profiler_finish(file_name: &str) {
139139
// WARNING: the features after applying `to_llvm_feature` must be known
140140
// to LLVM or the feature detection code will walk past the end of the feature
141141
// array, leading to crashes.
142-
143-
const ARM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
144-
("aclass", Some(sym::arm_target_feature)),
145-
("mclass", Some(sym::arm_target_feature)),
146-
("rclass", Some(sym::arm_target_feature)),
147-
("dsp", Some(sym::arm_target_feature)),
148-
("neon", Some(sym::arm_target_feature)),
149-
("crc", Some(sym::arm_target_feature)),
150-
("crypto", Some(sym::arm_target_feature)),
151-
("v5te", Some(sym::arm_target_feature)),
152-
("v6", Some(sym::arm_target_feature)),
153-
("v6k", Some(sym::arm_target_feature)),
154-
("v6t2", Some(sym::arm_target_feature)),
155-
("v7", Some(sym::arm_target_feature)),
156-
("v8", Some(sym::arm_target_feature)),
157-
("vfp2", Some(sym::arm_target_feature)),
158-
("vfp3", Some(sym::arm_target_feature)),
159-
("vfp4", Some(sym::arm_target_feature)),
160-
// This is needed for inline assembly, but shouldn't be stabilized as-is
161-
// since it should be enabled per-function using #[instruction_set], not
162-
// #[target_feature].
163-
("thumb-mode", Some(sym::arm_target_feature)),
164-
];
165-
166-
const AARCH64_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
167-
("fp", Some(sym::aarch64_target_feature)),
168-
("neon", Some(sym::aarch64_target_feature)),
169-
("sve", Some(sym::aarch64_target_feature)),
170-
("crc", Some(sym::aarch64_target_feature)),
171-
("crypto", Some(sym::aarch64_target_feature)),
172-
("ras", Some(sym::aarch64_target_feature)),
173-
("lse", Some(sym::aarch64_target_feature)),
174-
("rdm", Some(sym::aarch64_target_feature)),
175-
("fp16", Some(sym::aarch64_target_feature)),
176-
("rcpc", Some(sym::aarch64_target_feature)),
177-
("dotprod", Some(sym::aarch64_target_feature)),
178-
("tme", Some(sym::aarch64_target_feature)),
179-
("v8.1a", Some(sym::aarch64_target_feature)),
180-
("v8.2a", Some(sym::aarch64_target_feature)),
181-
("v8.3a", Some(sym::aarch64_target_feature)),
182-
];
183-
184-
const X86_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
185-
("adx", Some(sym::adx_target_feature)),
186-
("aes", None),
187-
("avx", None),
188-
("avx2", None),
189-
("avx512bw", Some(sym::avx512_target_feature)),
190-
("avx512cd", Some(sym::avx512_target_feature)),
191-
("avx512dq", Some(sym::avx512_target_feature)),
192-
("avx512er", Some(sym::avx512_target_feature)),
193-
("avx512f", Some(sym::avx512_target_feature)),
194-
("avx512ifma", Some(sym::avx512_target_feature)),
195-
("avx512pf", Some(sym::avx512_target_feature)),
196-
("avx512vbmi", Some(sym::avx512_target_feature)),
197-
("avx512vl", Some(sym::avx512_target_feature)),
198-
("avx512vpopcntdq", Some(sym::avx512_target_feature)),
199-
("bmi1", None),
200-
("bmi2", None),
201-
("cmpxchg16b", Some(sym::cmpxchg16b_target_feature)),
202-
("f16c", Some(sym::f16c_target_feature)),
203-
("fma", None),
204-
("fxsr", None),
205-
("lzcnt", None),
206-
("movbe", Some(sym::movbe_target_feature)),
207-
("pclmulqdq", None),
208-
("popcnt", None),
209-
("rdrand", None),
210-
("rdseed", None),
211-
("rtm", Some(sym::rtm_target_feature)),
212-
("sha", None),
213-
("sse", None),
214-
("sse2", None),
215-
("sse3", None),
216-
("sse4.1", None),
217-
("sse4.2", None),
218-
("sse4a", Some(sym::sse4a_target_feature)),
219-
("ssse3", None),
220-
("tbm", Some(sym::tbm_target_feature)),
221-
("xsave", None),
222-
("xsavec", None),
223-
("xsaveopt", None),
224-
("xsaves", None),
225-
];
226-
227-
const HEXAGON_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
228-
("hvx", Some(sym::hexagon_target_feature)),
229-
("hvx-length128b", Some(sym::hexagon_target_feature)),
230-
];
231-
232-
const POWERPC_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
233-
("altivec", Some(sym::powerpc_target_feature)),
234-
("power8-altivec", Some(sym::powerpc_target_feature)),
235-
("power9-altivec", Some(sym::powerpc_target_feature)),
236-
("power8-vector", Some(sym::powerpc_target_feature)),
237-
("power9-vector", Some(sym::powerpc_target_feature)),
238-
("vsx", Some(sym::powerpc_target_feature)),
239-
];
240-
241-
const MIPS_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] =
242-
&[("fp64", Some(sym::mips_target_feature)), ("msa", Some(sym::mips_target_feature))];
243-
244-
const RISCV_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
245-
("m", Some(sym::riscv_target_feature)),
246-
("a", Some(sym::riscv_target_feature)),
247-
("c", Some(sym::riscv_target_feature)),
248-
("f", Some(sym::riscv_target_feature)),
249-
("d", Some(sym::riscv_target_feature)),
250-
("e", Some(sym::riscv_target_feature)),
251-
];
252-
253-
const WASM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
254-
("simd128", Some(sym::wasm_target_feature)),
255-
("atomics", Some(sym::wasm_target_feature)),
256-
("nontrapping-fptoint", Some(sym::wasm_target_feature)),
257-
];
258-
259-
/// When rustdoc is running, provide a list of all known features so that all their respective
260-
/// primitives may be documented.
261-
///
262-
/// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
263-
pub fn all_known_features() -> impl Iterator<Item = (&'static str, Option<Symbol>)> {
264-
std::iter::empty()
265-
.chain(ARM_ALLOWED_FEATURES.iter())
266-
.chain(AARCH64_ALLOWED_FEATURES.iter())
267-
.chain(X86_ALLOWED_FEATURES.iter())
268-
.chain(HEXAGON_ALLOWED_FEATURES.iter())
269-
.chain(POWERPC_ALLOWED_FEATURES.iter())
270-
.chain(MIPS_ALLOWED_FEATURES.iter())
271-
.chain(RISCV_ALLOWED_FEATURES.iter())
272-
.chain(WASM_ALLOWED_FEATURES.iter())
273-
.cloned()
274-
}
275-
276142
pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> &'a str {
277143
let arch = if sess.target.target.arch == "x86_64" { "x86" } else { &*sess.target.target.arch };
278144
match (arch, s) {
@@ -306,20 +172,6 @@ pub fn target_features(sess: &Session) -> Vec<Symbol> {
306172
.collect()
307173
}
308174

309-
pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Option<Symbol>)] {
310-
match &*sess.target.target.arch {
311-
"arm" => ARM_ALLOWED_FEATURES,
312-
"aarch64" => AARCH64_ALLOWED_FEATURES,
313-
"x86" | "x86_64" => X86_ALLOWED_FEATURES,
314-
"hexagon" => HEXAGON_ALLOWED_FEATURES,
315-
"mips" | "mips64" => MIPS_ALLOWED_FEATURES,
316-
"powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
317-
"riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
318-
"wasm32" => WASM_ALLOWED_FEATURES,
319-
_ => &[],
320-
}
321-
}
322-
323175
pub fn print_version() {
324176
// Can be called without initializing LLVM
325177
unsafe {

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod glue;
4242
pub mod meth;
4343
pub mod mir;
4444
pub mod mono_item;
45+
pub mod target_features;
4546
pub mod traits;
4647

4748
pub struct ModuleCodegen<M> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
use rustc_session::Session;
2+
use rustc_span::symbol::sym;
3+
use rustc_span::symbol::Symbol;
4+
5+
const ARM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
6+
("aclass", Some(sym::arm_target_feature)),
7+
("mclass", Some(sym::arm_target_feature)),
8+
("rclass", Some(sym::arm_target_feature)),
9+
("dsp", Some(sym::arm_target_feature)),
10+
("neon", Some(sym::arm_target_feature)),
11+
("crc", Some(sym::arm_target_feature)),
12+
("crypto", Some(sym::arm_target_feature)),
13+
("v5te", Some(sym::arm_target_feature)),
14+
("v6", Some(sym::arm_target_feature)),
15+
("v6k", Some(sym::arm_target_feature)),
16+
("v6t2", Some(sym::arm_target_feature)),
17+
("v7", Some(sym::arm_target_feature)),
18+
("v8", Some(sym::arm_target_feature)),
19+
("vfp2", Some(sym::arm_target_feature)),
20+
("vfp3", Some(sym::arm_target_feature)),
21+
("vfp4", Some(sym::arm_target_feature)),
22+
// This is needed for inline assembly, but shouldn't be stabilized as-is
23+
// since it should be enabled per-function using #[instruction_set], not
24+
// #[target_feature].
25+
("thumb-mode", Some(sym::arm_target_feature)),
26+
];
27+
28+
const AARCH64_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
29+
("fp", Some(sym::aarch64_target_feature)),
30+
("neon", Some(sym::aarch64_target_feature)),
31+
("sve", Some(sym::aarch64_target_feature)),
32+
("crc", Some(sym::aarch64_target_feature)),
33+
("crypto", Some(sym::aarch64_target_feature)),
34+
("ras", Some(sym::aarch64_target_feature)),
35+
("lse", Some(sym::aarch64_target_feature)),
36+
("rdm", Some(sym::aarch64_target_feature)),
37+
("fp16", Some(sym::aarch64_target_feature)),
38+
("rcpc", Some(sym::aarch64_target_feature)),
39+
("dotprod", Some(sym::aarch64_target_feature)),
40+
("tme", Some(sym::aarch64_target_feature)),
41+
("v8.1a", Some(sym::aarch64_target_feature)),
42+
("v8.2a", Some(sym::aarch64_target_feature)),
43+
("v8.3a", Some(sym::aarch64_target_feature)),
44+
];
45+
46+
const X86_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
47+
("adx", Some(sym::adx_target_feature)),
48+
("aes", None),
49+
("avx", None),
50+
("avx2", None),
51+
("avx512bw", Some(sym::avx512_target_feature)),
52+
("avx512cd", Some(sym::avx512_target_feature)),
53+
("avx512dq", Some(sym::avx512_target_feature)),
54+
("avx512er", Some(sym::avx512_target_feature)),
55+
("avx512f", Some(sym::avx512_target_feature)),
56+
("avx512ifma", Some(sym::avx512_target_feature)),
57+
("avx512pf", Some(sym::avx512_target_feature)),
58+
("avx512vbmi", Some(sym::avx512_target_feature)),
59+
("avx512vl", Some(sym::avx512_target_feature)),
60+
("avx512vpopcntdq", Some(sym::avx512_target_feature)),
61+
("bmi1", None),
62+
("bmi2", None),
63+
("cmpxchg16b", Some(sym::cmpxchg16b_target_feature)),
64+
("f16c", Some(sym::f16c_target_feature)),
65+
("fma", None),
66+
("fxsr", None),
67+
("lzcnt", None),
68+
("movbe", Some(sym::movbe_target_feature)),
69+
("pclmulqdq", None),
70+
("popcnt", None),
71+
("rdrand", None),
72+
("rdseed", None),
73+
("rtm", Some(sym::rtm_target_feature)),
74+
("sha", None),
75+
("sse", None),
76+
("sse2", None),
77+
("sse3", None),
78+
("sse4.1", None),
79+
("sse4.2", None),
80+
("sse4a", Some(sym::sse4a_target_feature)),
81+
("ssse3", None),
82+
("tbm", Some(sym::tbm_target_feature)),
83+
("xsave", None),
84+
("xsavec", None),
85+
("xsaveopt", None),
86+
("xsaves", None),
87+
];
88+
89+
const HEXAGON_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
90+
("hvx", Some(sym::hexagon_target_feature)),
91+
("hvx-length128b", Some(sym::hexagon_target_feature)),
92+
];
93+
94+
const POWERPC_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
95+
("altivec", Some(sym::powerpc_target_feature)),
96+
("power8-altivec", Some(sym::powerpc_target_feature)),
97+
("power9-altivec", Some(sym::powerpc_target_feature)),
98+
("power8-vector", Some(sym::powerpc_target_feature)),
99+
("power9-vector", Some(sym::powerpc_target_feature)),
100+
("vsx", Some(sym::powerpc_target_feature)),
101+
];
102+
103+
const MIPS_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] =
104+
&[("fp64", Some(sym::mips_target_feature)), ("msa", Some(sym::mips_target_feature))];
105+
106+
const RISCV_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
107+
("m", Some(sym::riscv_target_feature)),
108+
("a", Some(sym::riscv_target_feature)),
109+
("c", Some(sym::riscv_target_feature)),
110+
("f", Some(sym::riscv_target_feature)),
111+
("d", Some(sym::riscv_target_feature)),
112+
("e", Some(sym::riscv_target_feature)),
113+
];
114+
115+
const WASM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
116+
("simd128", Some(sym::wasm_target_feature)),
117+
("atomics", Some(sym::wasm_target_feature)),
118+
("nontrapping-fptoint", Some(sym::wasm_target_feature)),
119+
];
120+
121+
/// When rustdoc is running, provide a list of all known features so that all their respective
122+
/// primitives may be documented.
123+
///
124+
/// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
125+
pub fn all_known_features() -> impl Iterator<Item = (&'static str, Option<Symbol>)> {
126+
std::iter::empty()
127+
.chain(ARM_ALLOWED_FEATURES.iter())
128+
.chain(AARCH64_ALLOWED_FEATURES.iter())
129+
.chain(X86_ALLOWED_FEATURES.iter())
130+
.chain(HEXAGON_ALLOWED_FEATURES.iter())
131+
.chain(POWERPC_ALLOWED_FEATURES.iter())
132+
.chain(MIPS_ALLOWED_FEATURES.iter())
133+
.chain(RISCV_ALLOWED_FEATURES.iter())
134+
.chain(WASM_ALLOWED_FEATURES.iter())
135+
.cloned()
136+
}
137+
138+
pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Option<Symbol>)] {
139+
match &*sess.target.target.arch {
140+
"arm" => ARM_ALLOWED_FEATURES,
141+
"aarch64" => AARCH64_ALLOWED_FEATURES,
142+
"x86" | "x86_64" => X86_ALLOWED_FEATURES,
143+
"hexagon" => HEXAGON_ALLOWED_FEATURES,
144+
"mips" | "mips64" => MIPS_ALLOWED_FEATURES,
145+
"powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
146+
"riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
147+
"wasm32" => WASM_ALLOWED_FEATURES,
148+
_ => &[],
149+
}
150+
}

0 commit comments

Comments
 (0)