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

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

Merged
merged 4 commits into from
Nov 22, 2022
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
264 changes: 133 additions & 131 deletions lib/compiler-singlepass/src/codegen.rs

Large diffs are not rendered by default.

56 changes: 11 additions & 45 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -144,6 +124,7 @@ impl Compiler for SinglepassCompiler {
target,
calling_convention,
)
.unwrap()
})
.collect::<Vec<_>>()
.into_iter()
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -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!(),
}
Expand All @@ -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())
ptitSeb marked this conversation as resolved.
Show resolved Hide resolved
.collect::<Vec<_>>()
.into_iter()
.collect::<PrimaryMap<_, _>>();
Expand All @@ -244,6 +223,7 @@ impl Compiler for SinglepassCompiler {
target,
calling_convention,
)
.unwrap()
})
.collect::<Vec<_>>()
.into_iter()
Expand Down Expand Up @@ -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;
Expand Down
Loading