Remove target from CompilerConfig

This commit is contained in:
Syrus
2020-06-17 20:26:50 -07:00
parent e062e87d8b
commit 8649f2eb79
17 changed files with 118 additions and 138 deletions

View File

@@ -5,7 +5,7 @@ use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use wasm_common::entity::{EntityRef, PrimaryMap, SecondaryMap};
use wasm_common::LocalFunctionIndex;
use wasmer_compiler::{
Compilation, CompileError, CompileModuleInfo, Compiler, CompilerConfig, FunctionBodyData,
Compilation, CompileError, CompileModuleInfo, Compiler, FunctionBodyData,
ModuleTranslationState, RelocationTarget, SectionIndex, Target,
};
@@ -32,15 +32,11 @@ impl LLVMCompiler {
}
impl Compiler for LLVMCompiler {
/// Gets the target associated to this Compiler.
fn target(&self) -> &Target {
self.config.target()
}
/// Compile the module using LLVM, producing a compilation result with
/// associated relocations.
fn compile_module<'data, 'module>(
&self,
target: &Target,
compile_info: &'module CompileModuleInfo,
module_translation: &ModuleTranslationState,
function_body_inputs: PrimaryMap<LocalFunctionIndex, FunctionBodyData<'data>>,
@@ -65,20 +61,26 @@ impl Compiler for LLVMCompiler {
.into_iter()
.collect::<Vec<(LocalFunctionIndex, &FunctionBodyData<'_>)>>()
.par_iter()
.map_init(FuncTranslator::new, |func_translator, (i, input)| {
// TODO: remove (to serialize)
//let mut data = data.lock().unwrap();
func_translator.translate(
&module,
module_translation,
i,
input,
self.config(),
&memory_plans,
&table_plans,
&func_names,
)
})
.map_init(
|| {
let target_machine = self.config().target_machine(target);
FuncTranslator::new(target_machine)
},
|func_translator, (i, input)| {
// TODO: remove (to serialize)
//let mut data = data.lock().unwrap();
func_translator.translate(
&module,
module_translation,
i,
input,
self.config(),
&memory_plans,
&table_plans,
&func_names,
)
},
)
.collect::<Result<Vec<_>, CompileError>>()?
.into_iter()
.map(|(mut compiled_function, function_custom_sections)| {
@@ -111,9 +113,13 @@ impl Compiler for LLVMCompiler {
.values()
.collect::<Vec<_>>()
.par_iter()
.map_init(FuncTrampoline::new, |func_trampoline, sig| {
func_trampoline.trampoline(sig, self.config())
})
.map_init(
|| {
let target_machine = self.config().target_machine(target);
FuncTrampoline::new(target_machine)
},
|func_trampoline, sig| func_trampoline.trampoline(sig, self.config()),
)
.collect::<Vec<_>>()
.into_iter()
.collect::<Result<PrimaryMap<_, _>, CompileError>>()?;
@@ -122,9 +128,15 @@ impl Compiler for LLVMCompiler {
.imported_function_types()
.collect::<Vec<_>>()
.par_iter()
.map_init(FuncTrampoline::new, |func_trampoline, func_type| {
func_trampoline.dynamic_trampoline(&func_type, self.config())
})
.map_init(
|| {
let target_machine = self.config().target_machine(target);
FuncTrampoline::new(target_machine)
},
|func_trampoline, func_type| {
func_trampoline.dynamic_trampoline(&func_type, self.config())
},
)
.collect::<Result<Vec<_>, CompileError>>()?
.into_iter()
.collect::<PrimaryMap<_, _>>();

View File

@@ -63,20 +63,17 @@ pub struct LLVMConfig {
/// The middleware chain.
pub(crate) middlewares: Vec<Arc<dyn FunctionMiddlewareGenerator>>,
target: Target,
}
impl LLVMConfig {
/// Creates a new configuration object with the default configuration
/// specified.
pub fn new(target: Target) -> Self {
pub fn new() -> Self {
Self {
enable_nan_canonicalization: true,
enable_verifier: false,
opt_level: OptimizationLevel::Aggressive,
is_pic: false,
target,
callbacks: None,
middlewares: vec![],
}
@@ -94,8 +91,7 @@ impl LLVMConfig {
CodeModel::Large
}
pub fn target_triple(&self) -> TargetTriple {
let target = self.target();
fn target_triple(&self, target: &Target) -> TargetTriple {
let operating_system =
if target.triple().operating_system == wasmer_compiler::OperatingSystem::Darwin {
// LLVM detects static relocation + darwin + 64-bit and
@@ -119,8 +115,7 @@ impl LLVMConfig {
}
/// Generates the target machine for the current target
pub fn target_machine(&self) -> TargetMachine {
let target = self.target();
pub fn target_machine(&self, target: &Target) -> TargetMachine {
let triple = target.triple();
let cpu_features = &target.cpu_features();
@@ -152,7 +147,7 @@ impl LLVMConfig {
.map(|feature| format!("+{}", feature.to_string()))
.join(",");
let target_triple = self.target_triple();
let target_triple = self.target_triple(&target);
let llvm_target = InkwellTarget::from_triple(&target_triple).unwrap();
llvm_target
.create_target_machine(
@@ -175,12 +170,6 @@ impl CompilerConfig for LLVMConfig {
self.is_pic = true;
}
/// Gets the target that we will use for compiling.
/// the WebAssembly module
fn target(&self) -> &Target {
&self.target
}
/// Transform it into the compiler.
fn compiler(&self) -> Box<dyn Compiler + Send> {
Box::new(LLVMCompiler::new(&self))
@@ -194,6 +183,6 @@ impl CompilerConfig for LLVMConfig {
impl Default for LLVMConfig {
fn default() -> LLVMConfig {
Self::new(Default::default())
Self::new()
}
}

View File

@@ -10,7 +10,7 @@ use inkwell::{
context::Context,
module::Linkage,
passes::PassManager,
targets::FileType,
targets::{FileType, TargetMachine},
types::BasicType,
values::{BasicValue, FunctionValue},
AddressSpace,
@@ -22,14 +22,16 @@ use wasmer_compiler::{CompileError, FunctionBody};
pub struct FuncTrampoline {
ctx: Context,
target_machine: TargetMachine,
}
const FUNCTION_SECTION: &str = ".wasmer_trampoline";
impl FuncTrampoline {
pub fn new() -> Self {
pub fn new(target_machine: TargetMachine) -> Self {
Self {
ctx: Context::create(),
target_machine,
}
}
@@ -41,8 +43,8 @@ impl FuncTrampoline {
// The function type, used for the callbacks.
let function = CompiledFunctionKind::FunctionCallTrampoline(ty.clone());
let module = self.ctx.create_module("");
let target_triple = config.target_triple();
let target_machine = config.target_machine();
let target_machine = &self.target_machine;
let target_triple = target_machine.get_triple();
module.set_triple(&target_triple);
module.set_data_layout(&target_machine.get_target_data().get_data_layout());
let intrinsics = Intrinsics::declare(&module, &self.ctx);
@@ -130,8 +132,8 @@ impl FuncTrampoline {
// The function type, used for the callbacks
let function = CompiledFunctionKind::DynamicFunctionTrampoline(ty.clone());
let module = self.ctx.create_module("");
let target_triple = config.target_triple();
let target_machine = config.target_machine();
let target_machine = &self.target_machine;
let target_triple = target_machine.get_triple();
module.set_triple(&target_triple);
module.set_data_layout(&target_machine.get_target_data().get_data_layout());
let intrinsics = Intrinsics::declare(&module, &self.ctx);

View File

@@ -12,7 +12,7 @@ use inkwell::{
context::Context,
module::{Linkage, Module},
passes::PassManager,
targets::FileType,
targets::{FileType, TargetMachine},
types::{BasicType, BasicTypeEnum, FloatMathType, IntType, PointerType, VectorType},
values::{
BasicValue, BasicValueEnum, FloatValue, FunctionValue, InstructionOpcode, InstructionValue,
@@ -55,12 +55,14 @@ fn const_zero(ty: BasicTypeEnum) -> BasicValueEnum {
pub struct FuncTranslator {
ctx: Context,
target_machine: TargetMachine,
}
impl FuncTranslator {
pub fn new() -> Self {
pub fn new(target_machine: TargetMachine) -> Self {
Self {
ctx: Context::create(),
target_machine,
}
}
@@ -85,8 +87,8 @@ impl FuncTranslator {
};
let module = self.ctx.create_module(module_name.as_str());
let target_triple = config.target_triple();
let target_machine = config.target_machine();
let target_machine = &self.target_machine;
let target_triple = target_machine.get_triple();
module.set_triple(&target_triple);
module.set_data_layout(&target_machine.get_target_data().get_data_layout());
let wasm_fn_type = wasm_module