Skip to content

Commit

Permalink
Merge pull request #145 from Ekleog-NEAR/implement-xzcnt
Browse files Browse the repository at this point in the history
implement lzcnt and tzcnt
  • Loading branch information
Ekleog-NEAR authored Oct 14, 2022
2 parents e57b792 + 73d798b commit bd153a6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions lib/compiler-singlepass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ name = "wasmer_compiler_singlepass"
wasmer-compiler = { path = "../compiler", package = "wasmer-compiler-near", version = "=2.4.0", features = ["translator"], default-features = false }
wasmer-vm = { path = "../vm", package = "wasmer-vm-near", version = "=2.4.0" }
wasmer-types = { path = "../types", package = "wasmer-types-near", version = "=2.4.0", default-features = false, features = ["std"] }
rayon = { version = "1.5", optional = true }
hashbrown = { version = "0.11", optional = true }
more-asserts = "0.2"
byteorder = "1.3"
dynasm = "1.0"
dynasmrt = "1.0"
enumset = "1.0"
hashbrown = { version = "0.11", optional = true }
lazy_static = "1.4"
byteorder = "1.3"
smallvec = "1.6"
memoffset = "0.6"
more-asserts = "0.2"
rayon = { version = "1.5", optional = true }
smallvec = "1.6"
tracing = "0.1"

[dev-dependencies]
Expand Down
15 changes: 10 additions & 5 deletions lib/compiler-singlepass/src/codegen_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use wasmer_compiler::{
CallingConvention, CompiledFunction, CompiledFunctionFrameInfo, CustomSection,
CustomSectionProtection, FunctionBody, FunctionBodyData, InstructionAddressMap,
ModuleTranslationState, Relocation, RelocationKind, RelocationTarget, SectionBody,
SectionIndex, SourceLoc,
SectionIndex, SourceLoc, Target,
};
use wasmer_types::{
entity::{EntityRef, PrimaryMap, SecondaryMap},
Expand All @@ -39,6 +39,9 @@ pub(crate) struct FuncGen<'a> {
/// ModuleInfo compilation config.
config: &'a Singlepass,

/// Target to which we compile
target: &'a Target,

/// Offsets of vmctx fields.
vmoffsets: &'a VMOffsets,

Expand Down Expand Up @@ -1900,6 +1903,7 @@ impl<'a> FuncGen<'a> {
module: &'a ModuleInfo,
module_translation_state: &'a ModuleTranslationState,
config: &'a Singlepass,
target: &'a Target,
vmoffsets: &'a VMOffsets,
_table_styles: &'a PrimaryMap<TableIndex, TableStyle>,
local_func_index: LocalFunctionIndex,
Expand All @@ -1926,6 +1930,7 @@ impl<'a> FuncGen<'a> {
module,
module_translation_state,
config,
target,
vmoffsets,
local_types: wasmer_types::partial_sum_map::PartialSumMap::new(),
assembler,
Expand Down Expand Up @@ -2321,7 +2326,7 @@ impl<'a> FuncGen<'a> {
}
};

if self.assembler.arch_has_xzcnt() {
if self.assembler.arch_has_xzcnt(self.target.cpu_features()) {
self.assembler.arch_emit_lzcnt(
Size::S32,
Location::GPR(src),
Expand Down Expand Up @@ -2386,7 +2391,7 @@ impl<'a> FuncGen<'a> {
}
};

if self.assembler.arch_has_xzcnt() {
if self.assembler.arch_has_xzcnt(self.target.cpu_features()) {
self.assembler.arch_emit_tzcnt(
Size::S32,
Location::GPR(src),
Expand Down Expand Up @@ -2552,7 +2557,7 @@ impl<'a> FuncGen<'a> {
}
};

if self.assembler.arch_has_xzcnt() {
if self.assembler.arch_has_xzcnt(self.target.cpu_features()) {
self.assembler.arch_emit_lzcnt(
Size::S64,
Location::GPR(src),
Expand Down Expand Up @@ -2617,7 +2622,7 @@ impl<'a> FuncGen<'a> {
}
};

if self.assembler.arch_has_xzcnt() {
if self.assembler.arch_has_xzcnt(self.target.cpu_features()) {
self.assembler.arch_emit_tzcnt(
Size::S64,
Location::GPR(src),
Expand Down
1 change: 1 addition & 0 deletions lib/compiler-singlepass/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl Compiler for SinglepassCompiler {
module,
module_translation,
&self.config,
&target,
&vmoffsets,
&table_styles,
i,
Expand Down
24 changes: 23 additions & 1 deletion lib/compiler-singlepass/src/emitter_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use dynasm::dynasm;
use dynasmrt::{
x64::X64Relocation, AssemblyOffset, DynamicLabel, DynasmApi, DynasmLabelApi, VecAssembler,
};
use enumset::EnumSet;
use wasmer_compiler::CpuFeature;

type Assembler = VecAssembler<X64Relocation>;

Expand Down Expand Up @@ -287,7 +289,7 @@ pub(crate) trait Emitter {
unimplemented!()
}

fn arch_has_xzcnt(&self) -> bool {
fn arch_has_xzcnt(&self, _cpu: &EnumSet<CpuFeature>) -> bool {
false
}
fn arch_emit_lzcnt(&mut self, _sz: Size, _src: Location, _dst: Location) {
Expand Down Expand Up @@ -1419,6 +1421,26 @@ impl Emitter for Assembler {
self.emit_jmp_location(Location::GPR(target));
}

fn arch_has_xzcnt(&self, cpu: &EnumSet<CpuFeature>) -> bool {
cpu.contains(CpuFeature::BMI1) && cpu.contains(CpuFeature::LZCNT)
}

fn arch_emit_lzcnt(&mut self, sz: Size, src: Location, dst: Location) {
binop_gpr_gpr!(lzcnt, self, sz, src, dst, {
binop_mem_gpr!(lzcnt, self, sz, src, dst, {
panic!("singlepass can't emit LZCNT {:?} {:?} {:?}", sz, src, dst)
})
})
}

fn arch_emit_tzcnt(&mut self, sz: Size, src: Location, dst: Location) {
binop_gpr_gpr!(tzcnt, self, sz, src, dst, {
binop_mem_gpr!(tzcnt, self, sz, src, dst, {
panic!("singlepass can't emit TZCNT {:?} {:?} {:?}", sz, src, dst)
})
})
}

fn arch_mov64_imm_offset(&self) -> usize {
2
}
Expand Down

0 comments on commit bd153a6

Please sign in to comment.