Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable compilation for specific target #964

Merged
merged 1 commit into from
Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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