Skip to content

Commit 8be3068

Browse files
committed
added conversion from Rust feature to LLVM feature
1 parent 877272b commit 8be3068

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/librustc_trans/attributes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub fn provide(providers: &mut Providers) {
142142
assert_eq!(cnum, LOCAL_CRATE);
143143
Rc::new(llvm_util::target_feature_whitelist(tcx.sess)
144144
.iter()
145-
.map(|c| c.to_str().unwrap().to_string())
145+
.map(|c| c.to_string())
146146
.collect())
147147
};
148148

@@ -212,7 +212,8 @@ fn from_target_feature(
212212
let value = value.as_str();
213213
for feature in value.split(',') {
214214
if whitelist.contains(feature) {
215-
target_features.push(format!("+{}", feature));
215+
let llvm_feature = llvm_util::to_llvm_feature(feature);
216+
target_features.push(format!("+{}", llvm_feature));
216217
continue
217218
}
218219

src/librustc_trans/llvm_util.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,45 +79,54 @@ unsafe fn configure_llvm(sess: &Session) {
7979
// detection code will walk past the end of the feature array,
8080
// leading to crashes.
8181

82-
const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "v7\0", "vfp2\0", "vfp3\0", "vfp4\0"];
83-
84-
const AARCH64_WHITELIST: &'static [&'static str] = &["neon\0", "v7\0"];
85-
86-
const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0",
87-
"sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0",
88-
"ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0",
89-
"sse4a\0", "rdrnd\0", "rdseed\0", "fma\0",
90-
"xsave\0", "xsaveopt\0", "xsavec\0",
91-
"xsaves\0", "aes\0", "pclmul\0",
92-
"avx512bw\0", "avx512cd\0",
93-
"avx512dq\0", "avx512er\0",
94-
"avx512f\0", "avx512ifma\0",
95-
"avx512pf\0", "avx512vbmi\0",
96-
"avx512vl\0", "avx512vpopcntdq\0",
97-
"mmx\0", "fxsr\0"];
98-
99-
const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"];
100-
101-
const POWERPC_WHITELIST: &'static [&'static str] = &["altivec\0",
102-
"power8-altivec\0", "power9-altivec\0",
103-
"power8-vector\0", "power9-vector\0",
104-
"vsx\0"];
105-
106-
const MIPS_WHITELIST: &'static [&'static str] = &["msa\0"];
82+
const ARM_WHITELIST: &'static [&'static str] = &["neon", "v7", "vfp2", "vfp3", "vfp4"];
83+
84+
const AARCH64_WHITELIST: &'static [&'static str] = &["neon", "v7"];
85+
86+
const X86_WHITELIST: &'static [&'static str] = &["avx", "avx2", "bmi", "bmi2", "sse",
87+
"sse2", "sse3", "sse4.1", "sse4.2",
88+
"ssse3", "tbm", "lzcnt", "popcnt",
89+
"sse4a", "rdrnd", "rdseed", "fma",
90+
"xsave", "xsaveopt", "xsavec",
91+
"xsaves", "aes", "pclmulqdq",
92+
"avx512bw", "avx512cd",
93+
"avx512dq", "avx512er",
94+
"avx512f", "avx512ifma",
95+
"avx512pf", "avx512vbmi",
96+
"avx512vl", "avx512vpopcntdq",
97+
"mmx", "fxsr"];
98+
99+
const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx", "hvx-double"];
100+
101+
const POWERPC_WHITELIST: &'static [&'static str] = &["altivec",
102+
"power8-altivec", "power9-altivec",
103+
"power8-vector", "power9-vector",
104+
"vsx"];
105+
106+
const MIPS_WHITELIST: &'static [&'static str] = &["msa"];
107+
108+
pub fn to_llvm_feature(s: &str) -> &str {
109+
match s {
110+
"pclmulqdq" => "pclmul",
111+
s => s,
112+
}
113+
}
107114

108115
pub fn target_features(sess: &Session) -> Vec<Symbol> {
109116
let whitelist = target_feature_whitelist(sess);
110117
let target_machine = create_target_machine(sess);
111118
let mut features = Vec::new();
112-
for feat in whitelist {
113-
if unsafe { llvm::LLVMRustHasFeature(target_machine, feat.as_ptr()) } {
114-
features.push(Symbol::intern(feat.to_str().unwrap()));
119+
for feature in whitelist {
120+
let llvm_feature = to_llvm_feature(feature);
121+
let ptr = CString::new(llvm_feature).as_ptr();
122+
if unsafe { llvm::LLVMRustHasFeature(target_machine, ptr) } {
123+
features.push(Symbol::intern(feature));
115124
}
116125
}
117126
features
118127
}
119128

120-
pub fn target_feature_whitelist(sess: &Session) -> Vec<&CStr> {
129+
pub fn target_feature_whitelist(sess: &Session) -> &'static [&'static str] {
121130
let whitelist = match &*sess.target.target.arch {
122131
"arm" => ARM_WHITELIST,
123132
"aarch64" => AARCH64_WHITELIST,
@@ -126,10 +135,7 @@ pub fn target_feature_whitelist(sess: &Session) -> Vec<&CStr> {
126135
"mips" | "mips64" => MIPS_WHITELIST,
127136
"powerpc" | "powerpc64" => POWERPC_WHITELIST,
128137
_ => &[],
129-
};
130-
whitelist.iter().map(|m| {
131-
CStr::from_bytes_with_nul(m.as_bytes()).unwrap()
132-
}).collect()
138+
}
133139
}
134140

135141
pub fn print_version() {

0 commit comments

Comments
 (0)