964: Enable compilation for specific target r=MarkMcCaskey a=xofyarg


<!-- 
Prior to submitting a PR, review the CONTRIBUTING.md document for recommendations on how to test:
https://github.com/wasmerio/wasmer/blob/master/CONTRIBUTING.md#pull-requests

-->

# Description
<!-- 
Provide details regarding the change including motivation,
links to related issues, and the context of the PR.
-->
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

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: anb <anb@dev.null>
This commit is contained in:
bors[bot] 2019-11-16 03:20:37 +00:00 committed by GitHub
commit 5582a89e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 12 deletions

View File

@ -54,6 +54,10 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
} }
} }
fn new_with_target(_: Option<String>, _: Option<String>, _: Option<String>) -> Self {
unimplemented!("cross compilation is not available for clif backend")
}
fn backend_id() -> Backend { fn backend_id() -> Backend {
Backend::Cranelift Backend::Cranelift
} }

View File

@ -8075,24 +8075,37 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
for LLVMModuleCodeGenerator for LLVMModuleCodeGenerator
{ {
fn new() -> 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 context = Context::create();
let module = context.create_module("module"); let module = context.create_module("module");
Target::initialize_x86(&InitializationConfig { 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_parser: true,
asm_printer: true, asm_printer: true,
base: true, base: true,
disassembler: true, disassembler: true,
info: true, info: true,
machine_code: true, machine_code: true,
}); }),
let triple = TargetMachine::get_default_triple().to_string(); _ => unimplemented!("compile to target other than x86-64 is not supported"),
}
let target = Target::from_triple(&triple).unwrap(); let target = Target::from_triple(&triple).unwrap();
let target_machine = target let target_machine = target
.create_target_machine( .create_target_machine(
&triple, &triple,
&TargetMachine::get_host_cpu_name().to_string(), &cpu_name.unwrap_or(TargetMachine::get_host_cpu_name().to_string()),
&TargetMachine::get_host_cpu_features().to_string(), &cpu_features.unwrap_or(TargetMachine::get_host_cpu_features().to_string()),
OptimizationLevel::Aggressive, OptimizationLevel::Aggressive,
RelocMode::Static, RelocMode::Static,
CodeModel::Large, CodeModel::Large,

View File

@ -129,6 +129,11 @@ pub struct CompilerConfig {
pub enforce_stack_check: bool, pub enforce_stack_check: bool,
pub track_state: bool, pub track_state: bool,
pub features: Features, 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 { pub trait Compiler {

View File

@ -74,6 +74,13 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,
/// Creates a new module code generator. /// Creates a new module code generator.
fn new() -> Self; 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. /// Returns the backend id associated with this MCG.
fn backend_id() -> Backend; fn backend_id() -> Backend;
@ -206,7 +213,14 @@ impl<
validate_with_features(wasm, &compiler_config.features)?; 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 mut chain = (self.middleware_chain_generator)();
let info = crate::parse::read_module( let info = crate::parse::read_module(
wasm, wasm,

View File

@ -370,6 +370,10 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
} }
} }
fn new_with_target(_: Option<String>, _: Option<String>, _: Option<String>) -> Self {
unimplemented!("cross compilation is not available for singlepass backend")
}
fn backend_id() -> Backend { fn backend_id() -> Backend {
Backend::Singlepass Backend::Singlepass
} }

View File

@ -63,6 +63,7 @@ fn handle_client(mut stream: UnixStream) {
enforce_stack_check: true, enforce_stack_check: true,
track_state: false, track_state: false,
features: Default::default(), features: Default::default(),
..Default::default()
}, },
&SinglePassCompiler::new(), &SinglePassCompiler::new(),
) )

View File

@ -447,6 +447,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
simd: options.features.simd || options.features.all, simd: options.features.simd || options.features.all,
threads: options.features.threads || options.features.all, threads: options.features.threads || options.features.all,
}, },
..Default::default()
}, },
&*compiler, &*compiler,
) )