Skip to content

Commit

Permalink
Enable compilation for specific target
Browse files Browse the repository at this point in the history
By exposing the target information through `CompilerConfig`,
compiler(only LLVM at the moment) could create a machine with
different CPU feature flags other than current host, which makes it
capable to "cross compile" to some degree.

Update #959
  • Loading branch information
xofyarg committed Nov 13, 2019
1 parent 7286493 commit 84177a3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
4 changes: 4 additions & 0 deletions lib/clif-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
}
}

fn new_with_target(_: Option<String>, _: Option<String>, _: Option<String>) -> Self {
unimplemented!()
}

fn backend_id() -> Backend {
Backend::Cranelift
}
Expand Down
35 changes: 24 additions & 11 deletions lib/llvm-backend/src/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7969,24 +7969,37 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
for LLVMModuleCodeGenerator
{
fn new() -> LLVMModuleCodeGenerator {
Self::new_with_target(None, None, None)
}

fn new_with_target(
triple: Option<String>,
cpu_name: Option<String>,
cpu_features: Option<String>,
) -> LLVMModuleCodeGenerator {
let context = Context::create();
let module = context.create_module("module");

Target::initialize_x86(&InitializationConfig {
asm_parser: true,
asm_printer: true,
base: true,
disassembler: true,
info: true,
machine_code: true,
});
let triple = TargetMachine::get_default_triple().to_string();
let triple = triple.unwrap_or(TargetMachine::get_default_triple().to_string());

match triple {
_ if triple.starts_with("x86") => Target::initialize_x86(&InitializationConfig {
asm_parser: true,
asm_printer: true,
base: true,
disassembler: true,
info: true,
machine_code: true,
}),
_ => unimplemented!(),
}

let target = Target::from_triple(&triple).unwrap();
let target_machine = target
.create_target_machine(
&triple,
&TargetMachine::get_host_cpu_name().to_string(),
&TargetMachine::get_host_cpu_features().to_string(),
&cpu_name.unwrap_or(TargetMachine::get_host_cpu_name().to_string()),
&cpu_features.unwrap_or(TargetMachine::get_host_cpu_features().to_string()),
OptimizationLevel::Aggressive,
RelocMode::Static,
CodeModel::Large,
Expand Down
5 changes: 5 additions & 0 deletions lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ pub struct CompilerConfig {
pub enforce_stack_check: bool,
pub track_state: bool,
pub features: Features,

// target info used by LLVM
pub triple: Option<String>,
pub cpu_name: Option<String>,
pub cpu_features: Option<String>,
}

pub trait Compiler {
Expand Down
16 changes: 15 additions & 1 deletion lib/runtime-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,
/// Creates a new module code generator.
fn new() -> Self;

/// Creates a new module code generator for specified target.
fn new_with_target(
triple: Option<String>,
cpu_name: Option<String>,
cpu_features: Option<String>,
) -> Self;

/// Returns the backend id associated with this MCG.
fn backend_id() -> Backend;

Expand Down Expand Up @@ -206,7 +213,14 @@ impl<
validate_with_features(wasm, &compiler_config.features)?;
}

let mut mcg = MCG::new();
let mut mcg = match MCG::backend_id() {
Backend::LLVM => MCG::new_with_target(
compiler_config.triple.clone(),
compiler_config.cpu_name.clone(),
compiler_config.cpu_features.clone(),
),
_ => MCG::new(),
};
let mut chain = (self.middleware_chain_generator)();
let info = crate::parse::read_module(
wasm,
Expand Down
4 changes: 4 additions & 0 deletions lib/singlepass-backend/src/codegen_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
}
}

fn new_with_target(_: Option<String>, _: Option<String>, _: Option<String>) -> Self {
unimplemented!()
}

fn backend_id() -> Backend {
Backend::Singlepass
}
Expand Down
3 changes: 3 additions & 0 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
simd: options.features.simd || options.features.all,
threads: options.features.threads || options.features.all,
},
triple: None,
cpu_name: None,
cpu_features: None,
},
&*compiler,
)
Expand Down

0 comments on commit 84177a3

Please sign in to comment.