Skip to content

Commit e1efa32

Browse files
committed
Add help for target CPUs, features, relocation and code models.
1 parent d119362 commit e1efa32

File tree

6 files changed

+95
-4
lines changed

6 files changed

+95
-4
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[submodule "src/llvm"]
22
path = src/llvm
3-
url = https://github.com/rust-lang/llvm.git
3+
url = https://github.com/bitshifter/llvm.git
44
branch = master
55
[submodule "src/compiler-rt"]
66
path = src/compiler-rt

src/librustc/session/config.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ pub enum PrintRequest {
165165
CrateName,
166166
Cfg,
167167
TargetList,
168+
TargetCPUs,
169+
TargetFeatures,
170+
RelocationModels,
171+
CodeModels,
168172
}
169173

170174
pub enum Input {
@@ -1197,6 +1201,24 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
11971201
early_error(error_format, "Value for codegen units must be a positive nonzero integer");
11981202
}
11991203

1204+
let mut prints = Vec::<PrintRequest>::new();
1205+
if cg.target_cpu.as_ref().map_or(false, |s| s == "help") {
1206+
prints.push(PrintRequest::TargetCPUs);
1207+
cg.target_cpu = None;
1208+
};
1209+
if cg.target_feature == "help" {
1210+
prints.push(PrintRequest::TargetFeatures);
1211+
cg.target_feature = "".to_string();
1212+
}
1213+
if cg.relocation_model.as_ref().map_or(false, |s| s == "help") {
1214+
prints.push(PrintRequest::RelocationModels);
1215+
cg.relocation_model = None;
1216+
}
1217+
if cg.code_model.as_ref().map_or(false, |s| s == "help") {
1218+
prints.push(PrintRequest::CodeModels);
1219+
cg.code_model = None;
1220+
}
1221+
12001222
let cg = cg;
12011223

12021224
let sysroot_opt = matches.opt_str("sysroot").map(|m| PathBuf::from(&m));
@@ -1274,18 +1296,22 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
12741296
let cfg = parse_cfgspecs(matches.opt_strs("cfg"));
12751297
let test = matches.opt_present("test");
12761298

1277-
let prints = matches.opt_strs("print").into_iter().map(|s| {
1299+
prints.extend(matches.opt_strs("print").into_iter().map(|s| {
12781300
match &*s {
12791301
"crate-name" => PrintRequest::CrateName,
12801302
"file-names" => PrintRequest::FileNames,
12811303
"sysroot" => PrintRequest::Sysroot,
12821304
"cfg" => PrintRequest::Cfg,
12831305
"target-list" => PrintRequest::TargetList,
1306+
"target-cpus" => PrintRequest::TargetCPUs,
1307+
"target-features" => PrintRequest::TargetFeatures,
1308+
"relocation-models" => PrintRequest::RelocationModels,
1309+
"code-models" => PrintRequest::CodeModels,
12841310
req => {
12851311
early_error(error_format, &format!("unknown print request `{}`", req))
12861312
}
12871313
}
1288-
}).collect::<Vec<_>>();
1314+
}));
12891315

12901316
if !cg.remark.is_empty() && debuginfo == NoDebugInfo {
12911317
early_warn(error_format, "-C remark will not show source locations without \

src/librustc_driver/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use pretty::{PpMode, UserIdentifiedItem};
6969
use rustc_resolve as resolve;
7070
use rustc_save_analysis as save;
7171
use rustc_trans::back::link;
72+
use rustc_trans::back::write::create_target_machine;
7273
use rustc::dep_graph::DepGraph;
7374
use rustc::session::{self, config, Session, build_session, CompileResult};
7475
use rustc::session::config::{Input, PrintRequest, OutputType, ErrorOutputType};
@@ -657,6 +658,29 @@ impl RustcDefaultCalls {
657658
}
658659
}
659660
}
661+
PrintRequest::TargetCPUs => {
662+
let tm = create_target_machine(sess);
663+
unsafe { llvm::LLVMRustPrintTargetCPUs(tm); }
664+
}
665+
PrintRequest::TargetFeatures => {
666+
let tm = create_target_machine(sess);
667+
unsafe { llvm::LLVMRustPrintTargetFeatures(tm); }
668+
}
669+
PrintRequest::RelocationModels => {
670+
println!("Available relocation models:\n");
671+
println!(" pic");
672+
println!(" static");
673+
println!(" default");
674+
println!(" dynamic-no-pic\n");
675+
}
676+
PrintRequest::CodeModels => {
677+
println!("Available code models:\n");
678+
println!(" default");
679+
println!(" small");
680+
println!(" kernel");
681+
println!(" medium");
682+
println!(" large\n");
683+
}
660684
}
661685
}
662686
return Compilation::Stop;

src/librustc_llvm/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,9 @@ extern {
20402040
pub fn LLVMRustHasFeature(T: TargetMachineRef,
20412041
s: *const c_char) -> bool;
20422042

2043+
pub fn LLVMRustPrintTargetCPUs(T: TargetMachineRef);
2044+
pub fn LLVMRustPrintTargetFeatures(T: TargetMachineRef);
2045+
20432046
pub fn LLVMRustCreateTargetMachine(Triple: *const c_char,
20442047
CPU: *const c_char,
20452048
Features: *const c_char,

src/llvm

src/rustllvm/PassWrapper.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,44 @@ LLVMRustHasFeature(LLVMTargetMachineRef TM,
162162
return (Bits & FeatureEntry->Value) == FeatureEntry->Value;
163163
}
164164

165+
/// getLongestEntryLength - Return the length of the longest entry in the table.
166+
///
167+
static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
168+
size_t MaxLen = 0;
169+
for (auto &I : Table)
170+
MaxLen = std::max(MaxLen, std::strlen(I.Key));
171+
return MaxLen;
172+
}
173+
174+
extern "C" void
175+
LLVMRustPrintTargetCPUs(LLVMTargetMachineRef TM) {
176+
const TargetMachine *Target = unwrap(TM);
177+
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
178+
const ArrayRef<SubtargetFeatureKV> CPUTable = MCInfo->getCPUTable();
179+
unsigned MaxCPULen = getLongestEntryLength(CPUTable);
180+
181+
printf("Available CPUs for this target:\n\n");
182+
for (auto &CPU : CPUTable)
183+
printf(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
184+
printf("\n");
185+
}
186+
187+
extern "C" void
188+
LLVMRustPrintTargetFeatures(LLVMTargetMachineRef TM) {
189+
const TargetMachine *Target = unwrap(TM);
190+
const MCSubtargetInfo *MCInfo = Target->getMCSubtargetInfo();
191+
const ArrayRef<SubtargetFeatureKV> FeatTable = MCInfo->getFeatureTable();
192+
unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
193+
194+
printf("Available features for this target:\n\n");
195+
for (auto &Feature : FeatTable)
196+
printf(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
197+
printf("\n");
198+
199+
printf("Use +feature to enable a feature, or -feature to disable it.\n"
200+
"For example, rustc -C -target-cpu=mycpu -C target-feature=+feature1,-feature2\n");
201+
}
202+
165203
extern "C" LLVMTargetMachineRef
166204
LLVMRustCreateTargetMachine(const char *triple,
167205
const char *cpu,

0 commit comments

Comments
 (0)