Skip to content

Commit

Permalink
Merge #964
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and xofyarg authored Nov 16, 2019
2 parents ab59a41 + 3b2d751 commit 5582a89
Show file tree
Hide file tree
Showing 7 changed files with 54 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!("cross compilation is not available for clif backend")
}

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 @@ -8075,24 +8075,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!("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,
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!("cross compilation is not available for singlepass backend")
}

fn backend_id() -> Backend {
Backend::Singlepass
}
Expand Down
1 change: 1 addition & 0 deletions src/bin/kwasmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn handle_client(mut stream: UnixStream) {
enforce_stack_check: true,
track_state: false,
features: Default::default(),
..Default::default()
},
&SinglePassCompiler::new(),
)
Expand Down
1 change: 1 addition & 0 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down

0 comments on commit 5582a89

Please sign in to comment.