Skip to content

Commit 4f44258

Browse files
committed
Add some SIMD target_feature cfg's when appropriate.
NB. this may not be 100% perfect.
1 parent e364f0e commit 4f44258

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

Diff for: src/librustc_driver/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub mod test;
8989

9090
pub mod driver;
9191
pub mod pretty;
92+
pub mod target_features;
9293

9394

9495
const BUG_REPORT_URL: &'static str =
@@ -136,7 +137,8 @@ pub fn run_compiler<'a>(args: &[String],
136137
if sess.unstable_options() {
137138
sess.opts.show_span = matches.opt_str("show-span");
138139
}
139-
let cfg = config::build_configuration(&sess);
140+
let mut cfg = config::build_configuration(&sess);
141+
target_features::add_configuration(&mut cfg, &sess);
140142

141143
do_or_return!(callbacks.late_callback(&matches, &sess, &input, &odir, &ofile));
142144

Diff for: src/librustc_driver/target_features.rs

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use syntax::{ast, attr};
12+
use rustc::session::Session;
13+
use syntax::parse::token::InternedString;
14+
use syntax::parse::token::intern_and_get_ident as intern;
15+
16+
/// Add `target_feature = "..."` cfgs for a variety of platform
17+
/// specific features (SSE, NEON etc.).
18+
///
19+
/// This uses a scheme similar to that employed by clang: reimplement
20+
/// the target feature knowledge. *Theoretically* we could query LLVM
21+
/// since that has perfect knowledge about what things are enabled in
22+
/// code-generation, however, it is extremely non-obvious how to do
23+
/// this successfully. Each platform defines a subclass of a
24+
/// SubtargetInfo, which knows all this information, but the ways to
25+
/// query them do not seem to be public.
26+
pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
27+
let tf = InternedString::new("target_feature");
28+
macro_rules! fillout {
29+
($($func: ident, $name: expr;)*) => {{
30+
$(if $func(sess) {
31+
cfg.push(attr::mk_name_value_item_str(tf.clone(), intern($name)))
32+
})*
33+
}}
34+
}
35+
fillout! {
36+
has_sse, "sse";
37+
has_sse2, "sse2";
38+
has_sse3, "sse3";
39+
has_ssse3, "ssse3";
40+
has_sse41, "sse4.1";
41+
has_sse42, "sse4.2";
42+
has_avx, "avx";
43+
has_avx2, "avx2";
44+
has_neon, "neon";
45+
has_vfp, "vfp";
46+
}
47+
}
48+
49+
50+
fn features_contain(sess: &Session, s: &str) -> bool {
51+
sess.target.target.options.features.contains(s) ||
52+
sess.opts.cg.target_feature.contains(s)
53+
}
54+
55+
pub fn has_sse(sess: &Session) -> bool {
56+
features_contain(sess, "+sse") ||
57+
has_sse2(sess)
58+
}
59+
pub fn has_sse2(sess: &Session) -> bool {
60+
// x86-64 requires at least SSE2 support
61+
sess.target.target.arch == "x86_64" ||
62+
features_contain(sess, "+sse2") ||
63+
has_sse3(sess)
64+
}
65+
pub fn has_sse3(sess: &Session) -> bool {
66+
features_contain(sess, "+sse3") ||
67+
has_ssse3(sess)
68+
}
69+
pub fn has_ssse3(sess: &Session) -> bool {
70+
features_contain(sess, "+ssse3") ||
71+
has_sse41(sess)
72+
}
73+
pub fn has_sse41(sess: &Session) -> bool {
74+
features_contain(sess, "+sse4.1") ||
75+
has_sse42(sess)
76+
}
77+
pub fn has_sse42(sess: &Session) -> bool {
78+
features_contain(sess, "+sse4.2") ||
79+
has_avx(sess)
80+
}
81+
pub fn has_avx(sess: &Session) -> bool {
82+
features_contain(sess, "+avx") ||
83+
has_avx2(sess)
84+
}
85+
pub fn has_avx2(sess: &Session) -> bool {
86+
features_contain(sess, "+avx2")
87+
}
88+
89+
pub fn has_neon(sess: &Session) -> bool {
90+
// AArch64 requires NEON support
91+
sess.target.target.arch == "aarch64" ||
92+
features_contain(sess, "+neon")
93+
}
94+
pub fn has_vfp(sess: &Session) -> bool {
95+
// AArch64 requires VFP support
96+
sess.target.target.arch == "aarch64" ||
97+
features_contain(sess, "+vfp")
98+
}

Diff for: src/librustdoc/core.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pub use self::MaybeTyped::*;
1111

1212
use rustc_lint;
13-
use rustc_driver::driver;
13+
use rustc_driver::{driver, target_features};
1414
use rustc::session::{self, config};
1515
use rustc::middle::{privacy, ty};
1616
use rustc::ast_map;
@@ -119,7 +119,8 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
119119
span_diagnostic_handler);
120120
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
121121

122-
let cfg = config::build_configuration(&sess);
122+
let mut cfg = config::build_configuration(&sess);
123+
target_features::add_configuration(&mut cfg, &sess);
123124

124125
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
125126

0 commit comments

Comments
 (0)