Skip to content

Commit

Permalink
Merge #3302
Browse files Browse the repository at this point in the history
3302: Some Refactor of Singlepass compiler to have better error and cpu features handling r=ptitSeb a=ptitSeb

# Description

* Some refactor of Singlepass compiler to have a better handling of Target CPU Feature and Error propagation
* Added support for TZCNT and LZCNT opcode on x86_64 backend (based on near/wasmer#145)
For #3305 

Co-authored-by: ptitSeb <sebastien.chev@gmail.com>
  • Loading branch information
bors[bot] and ptitSeb authored Nov 22, 2022
2 parents c80fc58 + 901457d commit 0928629
Show file tree
Hide file tree
Showing 7 changed files with 1,922 additions and 1,964 deletions.
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())
.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

0 comments on commit 0928629

Please sign in to comment.