Skip to content

Commit

Permalink
Added Wait/Notify opcode handling to Sinbglepass, and enable x86_64 t…
Browse files Browse the repository at this point in the history
…hreads::atomic test (for #3158)
  • Loading branch information
ptitSeb committed Nov 22, 2022
1 parent 9bbcc8a commit bfc8d94
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 2 deletions.
167 changes: 167 additions & 0 deletions lib/compiler-singlepass/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6417,6 +6417,173 @@ impl<'a, M: Machine> FuncGen<'a, M> {
[WpType::I32].iter().cloned(),
)?;
}
Operator::MemoryAtomicWait32 { ref memarg } => {
let timeout = self.value_stack.pop().unwrap();
let val = self.value_stack.pop().unwrap();
let dst = self.value_stack.pop().unwrap();
self.release_locations_only_regs(&[timeout, val, dst])?;

let memory_index = MemoryIndex::new(memarg.memory as usize);
let (memory_atomic_wait32, memory_index) =
if self.module.local_memory_index(memory_index).is_some() {
(
VMBuiltinFunctionIndex::get_memory_atomic_wait32_index(),
memory_index,
)
} else {
(
VMBuiltinFunctionIndex::get_imported_memory_atomic_wait32_index(),
memory_index,
)
};

self.machine.move_location(
Size::S64,
Location::Memory(
self.machine.get_vmctx_reg(),
self.vmoffsets.vmctx_builtin_function(memory_atomic_wait32) as i32,
),
Location::GPR(self.machine.get_grp_for_call()),
)?;

// TODO: should this be 3?
self.release_locations_only_osr_state(1)?;

self.emit_call_native(
|this| {
this.machine
.emit_call_register(this.machine.get_grp_for_call())
},
// [vmctx, memory_index, dst, src, timeout]
[
Location::Imm32(memory_index.index() as u32),
dst,
val,
timeout,
]
.iter()
.cloned(),
[WpType::I32, WpType::I32, WpType::I32, WpType::I64]
.iter()
.cloned(),
)?;
self.release_locations_only_stack(&[dst, val, timeout])?;
let ret = self.acquire_locations(
&[(WpType::I32, MachineValue::WasmStack(self.value_stack.len()))],
false,
)?[0];
self.value_stack.push(ret);
self.machine.move_location(
Size::S32,
Location::GPR(self.machine.get_gpr_for_ret()),
ret,
)?;
}
Operator::MemoryAtomicWait64 { ref memarg } => {
let timeout = self.value_stack.pop().unwrap();
let val = self.value_stack.pop().unwrap();
let dst = self.value_stack.pop().unwrap();
self.release_locations_only_regs(&[timeout, val, dst])?;

let memory_index = MemoryIndex::new(memarg.memory as usize);
let (memory_atomic_wait64, memory_index) =
if self.module.local_memory_index(memory_index).is_some() {
(
VMBuiltinFunctionIndex::get_memory_atomic_wait64_index(),
memory_index,
)
} else {
(
VMBuiltinFunctionIndex::get_imported_memory_atomic_wait64_index(),
memory_index,
)
};

self.machine.move_location(
Size::S64,
Location::Memory(
self.machine.get_vmctx_reg(),
self.vmoffsets.vmctx_builtin_function(memory_atomic_wait64) as i32,
),
Location::GPR(self.machine.get_grp_for_call()),
)?;

// TODO: should this be 3?
self.release_locations_only_osr_state(1)?;

self.emit_call_native(
|this| {
this.machine
.emit_call_register(this.machine.get_grp_for_call())
},
// [vmctx, memory_index, dst, src, timeout]
[
Location::Imm32(memory_index.index() as u32),
dst,
val,
timeout,
]
.iter()
.cloned(),
[WpType::I32, WpType::I32, WpType::I64, WpType::I64]
.iter()
.cloned(),
)?;
self.release_locations_only_stack(&[dst, val, timeout])?;
let ret = self.acquire_locations(
&[(WpType::I32, MachineValue::WasmStack(self.value_stack.len()))],
false,
)?[0];
self.value_stack.push(ret);
self.machine.move_location(
Size::S32,
Location::GPR(self.machine.get_gpr_for_ret()),
ret,
)?;
}
Operator::MemoryAtomicNotify { ref memarg } => {
let dst = self.value_stack.pop().unwrap();
self.release_locations_only_regs(&[dst])?;

let memory_index = MemoryIndex::new(memarg.memory as usize);
let (memory_atomic_wait32, memory_index) =
if self.module.local_memory_index(memory_index).is_some() {
(
VMBuiltinFunctionIndex::get_memory_atomic_wait32_index(),
memory_index,
)
} else {
(
VMBuiltinFunctionIndex::get_imported_memory_atomic_wait32_index(),
memory_index,
)
};

self.machine.move_location(
Size::S64,
Location::Memory(
self.machine.get_vmctx_reg(),
self.vmoffsets.vmctx_builtin_function(memory_atomic_wait32) as i32,
),
Location::GPR(self.machine.get_grp_for_call()),
)?;

// TODO: should this be 3?
self.release_locations_only_osr_state(1)?;

self.emit_call_native(
|this| {
this.machine
.emit_call_register(this.machine.get_grp_for_call())
},
// [vmctx, memory_index, dst, src, timeout]
[Location::Imm32(memory_index.index() as u32), dst]
.iter()
.cloned(),
[WpType::I32, WpType::I32].iter().cloned(),
)?;
self.release_locations_only_stack(&[dst])?;
}
_ => {
return Err(CompileError::Codegen(format!(
"not yet implemented: {:?}",
Expand Down
3 changes: 3 additions & 0 deletions lib/compiler-singlepass/src/emitter_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,9 @@ impl EmitterX64 for AssemblerX64 {
(Size::S32, Location::Imm64(imm), Size::S64, Location::GPR(dst)) => {
dynasm!(self ; mov Rq(dst as u8), imm as i32);
}
(Size::S16, Location::Imm64(imm), Size::S64, Location::GPR(dst)) => {
dynasm!(self ; mov Rq(dst as u8), imm as i32);
}
_ => {
codegen_error!(
"singlepass can't emit MOVZX {:?} {:?} {:?} {:?}",
Expand Down
3 changes: 2 additions & 1 deletion lib/compiler-singlepass/src/machine_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,7 +2415,8 @@ impl Machine for MachineX86_64 {
Location::GPR(_)
| Location::Memory(_, _)
| Location::Memory2(_, _, _, _)
| Location::Imm32(_) => match size_val {
| Location::Imm32(_)
| Location::Imm64(_) => match size_val {
Size::S32 | Size::S64 => self.assembler.emit_mov(size_val, source, dst),
Size::S16 | Size::S8 => {
if signed {
Expand Down
2 changes: 1 addition & 1 deletion tests/ignores.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ llvm traps::start_trap_pretty
cranelift+aarch64+macos traps::start_trap_pretty

# Atomics (WIP)
singlepass spec::threads::atomic
singlepass+aarch64 spec::threads::atomic
singlepass spec::threads::imports
cranelift spec::threads::atomic
cranelift spec::threads::imports
Expand Down

0 comments on commit bfc8d94

Please sign in to comment.