From 3b2d751c5595126fe90aabab8d1cdf8dd871f41f Mon Sep 17 00:00:00 2001 From: anb Date: Wed, 13 Nov 2019 12:31:50 -0800 Subject: [PATCH] Enable compilation for specific target 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 --- lib/clif-backend/src/code.rs | 4 +++ lib/llvm-backend/src/code.rs | 35 ++++++++++++++++------- lib/runtime-core/src/backend.rs | 5 ++++ lib/runtime-core/src/codegen.rs | 16 ++++++++++- lib/singlepass-backend/src/codegen_x64.rs | 4 +++ src/bin/kwasmd.rs | 1 + src/bin/wasmer.rs | 1 + 7 files changed, 54 insertions(+), 12 deletions(-) diff --git a/lib/clif-backend/src/code.rs b/lib/clif-backend/src/code.rs index 1bd1958e3de..d86d19b4f0f 100644 --- a/lib/clif-backend/src/code.rs +++ b/lib/clif-backend/src/code.rs @@ -54,6 +54,10 @@ impl ModuleCodeGenerator } } + fn new_with_target(_: Option, _: Option, _: Option) -> Self { + unimplemented!("cross compilation is not available for clif backend") + } + fn backend_id() -> Backend { Backend::Cranelift } diff --git a/lib/llvm-backend/src/code.rs b/lib/llvm-backend/src/code.rs index ac7ac92bbc3..0aca846505c 100644 --- a/lib/llvm-backend/src/code.rs +++ b/lib/llvm-backend/src/code.rs @@ -8075,24 +8075,37 @@ impl ModuleCodeGenerator for LLVMModuleCodeGenerator { fn new() -> LLVMModuleCodeGenerator { + Self::new_with_target(None, None, None) + } + + fn new_with_target( + triple: Option, + cpu_name: Option, + cpu_features: Option, + ) -> 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!("compile to target other than x86-64 is not supported"), + } + 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, diff --git a/lib/runtime-core/src/backend.rs b/lib/runtime-core/src/backend.rs index f1086de0d68..893874ed934 100644 --- a/lib/runtime-core/src/backend.rs +++ b/lib/runtime-core/src/backend.rs @@ -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, + pub cpu_name: Option, + pub cpu_features: Option, } pub trait Compiler { diff --git a/lib/runtime-core/src/codegen.rs b/lib/runtime-core/src/codegen.rs index e96f419e6b4..9e42d80490a 100644 --- a/lib/runtime-core/src/codegen.rs +++ b/lib/runtime-core/src/codegen.rs @@ -74,6 +74,13 @@ pub trait ModuleCodeGenerator, 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, + cpu_name: Option, + cpu_features: Option, + ) -> Self; + /// Returns the backend id associated with this MCG. fn backend_id() -> Backend; @@ -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, diff --git a/lib/singlepass-backend/src/codegen_x64.rs b/lib/singlepass-backend/src/codegen_x64.rs index 7a71a289fe2..e4934e3c2ba 100644 --- a/lib/singlepass-backend/src/codegen_x64.rs +++ b/lib/singlepass-backend/src/codegen_x64.rs @@ -370,6 +370,10 @@ impl ModuleCodeGenerator } } + fn new_with_target(_: Option, _: Option, _: Option) -> Self { + unimplemented!("cross compilation is not available for singlepass backend") + } + fn backend_id() -> Backend { Backend::Singlepass } diff --git a/src/bin/kwasmd.rs b/src/bin/kwasmd.rs index 62e22b731a7..79949417b10 100644 --- a/src/bin/kwasmd.rs +++ b/src/bin/kwasmd.rs @@ -63,6 +63,7 @@ fn handle_client(mut stream: UnixStream) { enforce_stack_check: true, track_state: false, features: Default::default(), + ..Default::default() }, &SinglePassCompiler::new(), ) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 7af5df3e258..ad9afa97211 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -447,6 +447,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> { simd: options.features.simd || options.features.all, threads: options.features.threads || options.features.all, }, + ..Default::default() }, &*compiler, )