standalone: Add command line flags for target properties

For testing codegen for different platforms on the host system.
This commit is contained in:
David Mak 2023-09-12 14:41:34 +08:00 committed by David Mak
parent 5722f3397e
commit c86b6e22d6
1 changed files with 30 additions and 3 deletions

View File

@ -53,6 +53,18 @@ struct CommandLineArgs {
/// Whether to use LLVM's Legacy Pass Manager for optimizations. /// Whether to use LLVM's Legacy Pass Manager for optimizations.
#[arg(long, default_value_t = false)] #[arg(long, default_value_t = false)]
legacy_pm: bool, legacy_pm: bool,
/// The target triple to compile for.
#[arg(long)]
triple: Option<String>,
/// The target CPU to compile for.
#[arg(long)]
mcpu: Option<String>,
/// Additional target features to enable/disable, specified using the `+`/`-` prefixes.
#[arg(long)]
target_features: Option<String>,
} }
fn handle_typevar_definition( fn handle_typevar_definition(
@ -187,7 +199,19 @@ fn main() {
opt_level, opt_level,
emit_llvm, emit_llvm,
legacy_pm, legacy_pm,
triple,
mcpu,
target_features,
} = cli; } = cli;
Target::initialize_all(&InitializationConfig::default());
let host_target_machine = CodeGenTargetMachineOptions::from_host();
let triple = triple.unwrap_or(host_target_machine.triple.clone());
let mcpu = mcpu
.map(|arg| if arg == "native" { host_target_machine.cpu.clone() } else { arg })
.unwrap_or_default();
let target_features = target_features.unwrap_or_default();
let opt_level = match opt_level { let opt_level = match opt_level {
0 => OptimizationLevel::None, 0 => OptimizationLevel::None,
1 => OptimizationLevel::Less, 1 => OptimizationLevel::Less,
@ -196,8 +220,6 @@ fn main() {
_ => OptimizationLevel::Aggressive, _ => OptimizationLevel::Aggressive,
}; };
Target::initialize_all(&InitializationConfig::default());
let program = match fs::read_to_string(file_name.clone()) { let program = match fs::read_to_string(file_name.clone()) {
Ok(program) => program, Ok(program) => program,
Err(err) => { Err(err) => {
@ -283,7 +305,12 @@ fn main() {
let llvm_options = CodeGenLLVMOptions { let llvm_options = CodeGenLLVMOptions {
opt_level, opt_level,
legacy_pm, legacy_pm,
target: CodeGenTargetMachineOptions::from_host_triple(), target: CodeGenTargetMachineOptions {
triple,
cpu: mcpu,
features: target_features,
..host_target_machine
},
emit_llvm, emit_llvm,
}; };