Skip to content

Commit 84177a3

Browse files
committed
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 wasmerio#959
1 parent 7286493 commit 84177a3

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

lib/clif-backend/src/code.rs

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ impl ModuleCodeGenerator<CraneliftFunctionCodeGenerator, Caller, CodegenError>
5454
}
5555
}
5656

57+
fn new_with_target(_: Option<String>, _: Option<String>, _: Option<String>) -> Self {
58+
unimplemented!()
59+
}
60+
5761
fn backend_id() -> Backend {
5862
Backend::Cranelift
5963
}

lib/llvm-backend/src/code.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -7969,24 +7969,37 @@ impl ModuleCodeGenerator<LLVMFunctionCodeGenerator, LLVMBackend, CodegenError>
79697969
for LLVMModuleCodeGenerator
79707970
{
79717971
fn new() -> LLVMModuleCodeGenerator {
7972+
Self::new_with_target(None, None, None)
7973+
}
7974+
7975+
fn new_with_target(
7976+
triple: Option<String>,
7977+
cpu_name: Option<String>,
7978+
cpu_features: Option<String>,
7979+
) -> LLVMModuleCodeGenerator {
79727980
let context = Context::create();
79737981
let module = context.create_module("module");
79747982

7975-
Target::initialize_x86(&InitializationConfig {
7976-
asm_parser: true,
7977-
asm_printer: true,
7978-
base: true,
7979-
disassembler: true,
7980-
info: true,
7981-
machine_code: true,
7982-
});
7983-
let triple = TargetMachine::get_default_triple().to_string();
7983+
let triple = triple.unwrap_or(TargetMachine::get_default_triple().to_string());
7984+
7985+
match triple {
7986+
_ if triple.starts_with("x86") => Target::initialize_x86(&InitializationConfig {
7987+
asm_parser: true,
7988+
asm_printer: true,
7989+
base: true,
7990+
disassembler: true,
7991+
info: true,
7992+
machine_code: true,
7993+
}),
7994+
_ => unimplemented!(),
7995+
}
7996+
79847997
let target = Target::from_triple(&triple).unwrap();
79857998
let target_machine = target
79867999
.create_target_machine(
79878000
&triple,
7988-
&TargetMachine::get_host_cpu_name().to_string(),
7989-
&TargetMachine::get_host_cpu_features().to_string(),
8001+
&cpu_name.unwrap_or(TargetMachine::get_host_cpu_name().to_string()),
8002+
&cpu_features.unwrap_or(TargetMachine::get_host_cpu_features().to_string()),
79908003
OptimizationLevel::Aggressive,
79918004
RelocMode::Static,
79928005
CodeModel::Large,

lib/runtime-core/src/backend.rs

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ pub struct CompilerConfig {
129129
pub enforce_stack_check: bool,
130130
pub track_state: bool,
131131
pub features: Features,
132+
133+
// target info used by LLVM
134+
pub triple: Option<String>,
135+
pub cpu_name: Option<String>,
136+
pub cpu_features: Option<String>,
132137
}
133138

134139
pub trait Compiler {

lib/runtime-core/src/codegen.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ pub trait ModuleCodeGenerator<FCG: FunctionCodeGenerator<E>, RM: RunnableModule,
7474
/// Creates a new module code generator.
7575
fn new() -> Self;
7676

77+
/// Creates a new module code generator for specified target.
78+
fn new_with_target(
79+
triple: Option<String>,
80+
cpu_name: Option<String>,
81+
cpu_features: Option<String>,
82+
) -> Self;
83+
7784
/// Returns the backend id associated with this MCG.
7885
fn backend_id() -> Backend;
7986

@@ -206,7 +213,14 @@ impl<
206213
validate_with_features(wasm, &compiler_config.features)?;
207214
}
208215

209-
let mut mcg = MCG::new();
216+
let mut mcg = match MCG::backend_id() {
217+
Backend::LLVM => MCG::new_with_target(
218+
compiler_config.triple.clone(),
219+
compiler_config.cpu_name.clone(),
220+
compiler_config.cpu_features.clone(),
221+
),
222+
_ => MCG::new(),
223+
};
210224
let mut chain = (self.middleware_chain_generator)();
211225
let info = crate::parse::read_module(
212226
wasm,

lib/singlepass-backend/src/codegen_x64.rs

+4
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
370370
}
371371
}
372372

373+
fn new_with_target(_: Option<String>, _: Option<String>, _: Option<String>) -> Self {
374+
unimplemented!()
375+
}
376+
373377
fn backend_id() -> Backend {
374378
Backend::Singlepass
375379
}

src/bin/wasmer.rs

+3
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,9 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
443443
simd: options.features.simd || options.features.all,
444444
threads: options.features.threads || options.features.all,
445445
},
446+
triple: None,
447+
cpu_name: None,
448+
cpu_features: None,
446449
},
447450
&*compiler,
448451
)

0 commit comments

Comments
 (0)