Some Refactor of Singlepass compiler to have better error and cpu features handling

This commit is contained in:
ptitSeb
2022-11-14 13:13:41 +01:00
parent f1a792e5fc
commit 3421e46f6f
7 changed files with 1917 additions and 1959 deletions

View File

@ -8,7 +8,7 @@ use crate::config::Singlepass;
use crate::dwarf::WriterRelocate;
use crate::machine::Machine;
use crate::machine::{
gen_import_call_trampoline, gen_std_dynamic_import_trampoline, gen_std_trampoline, CodegenError,
gen_import_call_trampoline, gen_std_dynamic_import_trampoline, gen_std_trampoline,
};
use crate::machine_arm64::MachineARM64;
use crate::machine_x64::MachineX86_64;
@ -31,12 +31,6 @@ use wasmer_types::{
TrapCode, TrapInformation, VMOffsets,
};
impl From<CodegenError> for CompileError {
fn from(err: CodegenError) -> Self {
Self::Codegen(err.message)
}
}
/// A compiler that compiles a WebAssembly module with Singlepass.
/// It does the compilation in one pass
pub struct SinglepassCompiler {
@ -80,20 +74,6 @@ impl Compiler for SinglepassCompiler {
}
}
let simd_arch = match target.triple().architecture {
Architecture::X86_64 => {
if target.cpu_features().contains(CpuFeature::AVX) {
Some(CpuFeature::AVX)
} else if target.cpu_features().contains(CpuFeature::SSE42) {
Some(CpuFeature::SSE42)
} else {
return Err(CompileError::UnsupportedTarget(
"x86_64 without AVX or SSE 4.2".to_string(),
));
}
}
_ => None,
};
let calling_convention = match target.triple().default_calling_convention() {
Ok(CallingConvention::WindowsFastcall) => CallingConvention::WindowsFastcall,
Ok(CallingConvention::SystemV) => CallingConvention::SystemV,
@ -144,6 +124,7 @@ impl Compiler for SinglepassCompiler {
target,
calling_convention,
)
.unwrap()
})
.collect::<Vec<_>>()
.into_iter()
@ -173,7 +154,7 @@ impl Compiler for SinglepassCompiler {
match target.triple().architecture {
Architecture::X86_64 => {
let machine = MachineX86_64::new(simd_arch);
let machine = MachineX86_64::new(Some(target.clone()))?;
let mut generator = FuncGen::new(
module,
&self.config,
@ -184,15 +165,14 @@ impl Compiler for SinglepassCompiler {
&locals,
machine,
calling_convention,
)
.map_err(to_compile_error)?;
)?;
while generator.has_control_frames() {
generator.set_srcloc(reader.original_position() as u32);
let op = reader.read_operator()?;
generator.feed_operator(op).map_err(to_compile_error)?;
generator.feed_operator(op)?;
}
generator.finalize(input).map_err(to_compile_error)
generator.finalize(input)
}
Architecture::Aarch64(_) => {
let machine = MachineARM64::new();
@ -206,15 +186,14 @@ impl Compiler for SinglepassCompiler {
&locals,
machine,
calling_convention,
)
.map_err(to_compile_error)?;
)?;
while generator.has_control_frames() {
generator.set_srcloc(reader.original_position() as u32);
let op = reader.read_operator()?;
generator.feed_operator(op).map_err(to_compile_error)?;
generator.feed_operator(op)?;
}
generator.finalize(input).map_err(to_compile_error)
generator.finalize(input)
}
_ => unimplemented!(),
}
@ -228,7 +207,7 @@ impl Compiler for SinglepassCompiler {
.values()
.collect::<Vec<_>>()
.into_par_iter_if_rayon()
.map(|func_type| gen_std_trampoline(func_type, target, calling_convention))
.map(|func_type| gen_std_trampoline(func_type, target, calling_convention).unwrap())
.collect::<Vec<_>>()
.into_iter()
.collect::<PrimaryMap<_, _>>();
@ -244,6 +223,7 @@ impl Compiler for SinglepassCompiler {
target,
calling_convention,
)
.unwrap()
})
.collect::<Vec<_>>()
.into_iter()
@ -278,20 +258,6 @@ impl Compiler for SinglepassCompiler {
}
}
trait ToCompileError {
fn to_compile_error(self) -> CompileError;
}
impl ToCompileError for CodegenError {
fn to_compile_error(self) -> CompileError {
CompileError::Codegen(self.message)
}
}
fn to_compile_error<T: ToCompileError>(x: T) -> CompileError {
x.to_compile_error()
}
trait IntoParIterIfRayon {
type Output;
fn into_par_iter_if_rayon(self) -> Self::Output;