Skip to content

Commit 1785841

Browse files
committed
auto merge of #8410 : luqmana/rust/mcpu, r=sanxiyn
Adds `--target-cpu` flag which lets you choose a more specific target cpu instead of just passing the default, `generic`. It's more or less akin to `-mcpu`/`-mtune` in clang/gcc.
2 parents 0679436 + fcfd6e7 commit 1785841

File tree

6 files changed

+36
-28
lines changed

6 files changed

+36
-28
lines changed

src/etc/zsh/_rust

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ _rustc_opts_switches=(
2727
--sysroot'[Override the system root]'
2828
--test'[Build a test harness]'
2929
--target'[Target triple cpu-manufacturer-kernel\[-os\] to compile]'
30-
--target-feature'[Target specific attributes (llc -mattr=help for detail)]'
30+
--target-cpu'[Select target processor (llc -mcpu=help for details)]'
31+
--target-feature'[Target specific attributes (llc -mattr=help for details)]'
3132
--android-cross-path'[The path to the Android NDK]'
3233
{-v,--version}'[Print version info and exit]'
3334
)

src/librustc/back/link.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub fn llvm_err(sess: Session, msg: ~str) -> ! {
6969
pub fn WriteOutputFile(sess: Session,
7070
PM: lib::llvm::PassManagerRef, M: ModuleRef,
7171
Triple: &str,
72+
Cpu: &str,
7273
Feature: &str,
7374
Output: &str,
7475
// FIXME: When #2334 is fixed, change
@@ -78,19 +79,22 @@ pub fn WriteOutputFile(sess: Session,
7879
EnableSegmentedStacks: bool) {
7980
unsafe {
8081
do Triple.to_c_str().with_ref |Triple| {
81-
do Feature.to_c_str().with_ref |Feature| {
82-
do Output.to_c_str().with_ref |Output| {
83-
let result = llvm::LLVMRustWriteOutputFile(
84-
PM,
85-
M,
86-
Triple,
87-
Feature,
88-
Output,
89-
FileType,
90-
OptLevel,
91-
EnableSegmentedStacks);
92-
if (!result) {
93-
llvm_err(sess, ~"Could not write output");
82+
do Cpu.to_c_str().with_ref |Cpu| {
83+
do Feature.to_c_str().with_ref |Feature| {
84+
do Output.to_c_str().with_ref |Output| {
85+
let result = llvm::LLVMRustWriteOutputFile(
86+
PM,
87+
M,
88+
Triple,
89+
Cpu,
90+
Feature,
91+
Output,
92+
FileType,
93+
OptLevel,
94+
EnableSegmentedStacks);
95+
if (!result) {
96+
llvm_err(sess, ~"Could not write output");
97+
}
9498
}
9599
}
96100
}
@@ -346,6 +350,7 @@ pub mod write {
346350
pm.llpm,
347351
llmod,
348352
sess.targ_cfg.target_strs.target_triple,
353+
opts.target_cpu,
349354
opts.target_feature,
350355
output.to_str(),
351356
lib::llvm::AssemblyFile as c_uint,
@@ -362,6 +367,7 @@ pub mod write {
362367
pm.llpm,
363368
llmod,
364369
sess.targ_cfg.target_strs.target_triple,
370+
opts.target_cpu,
365371
opts.target_feature,
366372
output.to_str(),
367373
lib::llvm::ObjectFile as c_uint,
@@ -376,6 +382,7 @@ pub mod write {
376382
pm.llpm,
377383
llmod,
378384
sess.targ_cfg.target_strs.target_triple,
385+
opts.target_cpu,
379386
opts.target_feature,
380387
output.to_str(),
381388
FileType as c_uint,

src/librustc/driver/driver.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,9 @@ pub fn build_session_options(binary: @str,
684684
link::output_type_bitcode
685685
} else { link::output_type_exe };
686686
let sysroot_opt = getopts::opt_maybe_str(matches, "sysroot").map_move(|m| @Path(m));
687-
let target_opt = getopts::opt_maybe_str(matches, "target");
688-
let target_feature_opt = getopts::opt_maybe_str(matches, "target-feature");
687+
let target = getopts::opt_maybe_str(matches, "target").unwrap_or_default(host_triple());
688+
let target_cpu = getopts::opt_maybe_str(matches, "target-cpu").unwrap_or_default(~"generic");
689+
let target_feature = getopts::opt_maybe_str(matches, "target-feature").unwrap_or_default(~"");
689690
let save_temps = getopts::opt_present(matches, "save-temps");
690691
let opt_level = {
691692
if (debugging_opts & session::no_opt) != 0 {
@@ -713,15 +714,6 @@ pub fn build_session_options(binary: @str,
713714
let debuginfo = debugging_opts & session::debug_info != 0 ||
714715
extra_debuginfo;
715716
let statik = debugging_opts & session::statik != 0;
716-
let target =
717-
match target_opt {
718-
None => host_triple(),
719-
Some(s) => s
720-
};
721-
let target_feature = match target_feature_opt {
722-
None => ~"",
723-
Some(s) => s
724-
};
725717

726718
let addl_lib_search_paths = getopts::opt_strs(matches, "L").map(|s| Path(*s));
727719
let linker = getopts::opt_maybe_str(matches, "linker");
@@ -760,6 +752,7 @@ pub fn build_session_options(binary: @str,
760752
linker_args: linker_args,
761753
maybe_sysroot: sysroot_opt,
762754
target_triple: target,
755+
target_cpu: target_cpu,
763756
target_feature: target_feature,
764757
cfg: cfg,
765758
binary: binary,
@@ -876,10 +869,13 @@ pub fn optgroups() -> ~[getopts::groups::OptGroup] {
876869
optopt("", "target",
877870
"Target triple cpu-manufacturer-kernel[-os]
878871
to compile for (see chapter 3.4 of http://www.sourceware.org/autobook/
879-
for detail)", "TRIPLE"),
872+
for details)", "TRIPLE"),
873+
optopt("", "target-cpu",
874+
"Select target processor (llc -mcpu=help
875+
for details)", "CPU"),
880876
optopt("", "target-feature",
881877
"Target specific attributes (llc -mattr=help
882-
for detail)", "FEATURE"),
878+
for details)", "FEATURE"),
883879
optopt("", "android-cross-path",
884880
"The path to the Android NDK", "PATH"),
885881
optflagopt("W", "warn",

src/librustc/driver/session.rs

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ pub struct options {
153153
linker_args: ~[~str],
154154
maybe_sysroot: Option<@Path>,
155155
target_triple: ~str,
156+
target_cpu: ~str,
156157
target_feature: ~str,
157158
// User-specified cfg meta items. The compiler itself will add additional
158159
// items to the crate config, and during parsing the entire crate config
@@ -340,6 +341,7 @@ pub fn basic_options() -> @options {
340341
linker_args: ~[],
341342
maybe_sysroot: None,
342343
target_triple: host_triple(),
344+
target_cpu: ~"generic",
343345
target_feature: ~"",
344346
cfg: ~[],
345347
binary: @"rustc",

src/librustc/lib/llvm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,7 @@ pub mod llvm {
18111811
pub fn LLVMRustWriteOutputFile(PM: PassManagerRef,
18121812
M: ModuleRef,
18131813
Triple: *c_char,
1814+
Cpu: *c_char,
18141815
Feature: *c_char,
18151816
Output: *c_char,
18161817
// FIXME: When #2334 is fixed,

src/rustllvm/RustWrapper.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ extern "C" bool
372372
LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
373373
LLVMModuleRef M,
374374
const char *triple,
375+
const char *cpu,
375376
const char *feature,
376377
const char *path,
377378
TargetMachine::CodeGenFileType FileType,
@@ -401,7 +402,7 @@ LLVMRustWriteOutputFile(LLVMPassManagerRef PMR,
401402
std::string Err;
402403
std::string Trip(Triple::normalize(triple));
403404
std::string FeaturesStr(feature);
404-
std::string CPUStr("generic");
405+
std::string CPUStr(cpu);
405406
const Target *TheTarget = TargetRegistry::lookupTarget(Trip, Err);
406407
TargetMachine *Target =
407408
TheTarget->createTargetMachine(Trip, CPUStr, FeaturesStr,

0 commit comments

Comments
 (0)