Implement Wasm Atomics for Cranelift/newBE/aarch64.
The implementation is pretty straightforward. Wasm atomic instructions fall
into 5 groups
* atomic read-modify-write
* atomic compare-and-swap
* atomic loads
* atomic stores
* fences
and the implementation mirrors that structure, at both the CLIF and AArch64
levels.
At the CLIF level, there are five new instructions, one for each group. Some
comments about these:
* for those that take addresses (all except fences), the address is contained
entirely in a single `Value`; there is no offset field as there is with
normal loads and stores. Wasm atomics require alignment checks, and
removing the offset makes implementation of those checks a bit simpler.
* atomic loads and stores get their own instructions, rather than reusing the
existing load and store instructions, for two reasons:
- per above comment, makes alignment checking simpler
- reuse of existing loads and stores would require extension of `MemFlags`
to indicate atomicity, which sounds semantically unclean. For example,
then *any* instruction carrying `MemFlags` could be marked as atomic, even
in cases where it is meaningless or ambiguous.
* I tried to specify, in comments, the behaviour of these instructions as
tightly as I could. Unfortunately there is no way (per my limited CLIF
knowledge) to enforce the constraint that they may only be used on I8, I16,
I32 and I64 types, and in particular not on floating point or vector types.
The translation from Wasm to CLIF, in `code_translator.rs` is unremarkable.
At the AArch64 level, there are also five new instructions, one for each
group. All of them except `::Fence` contain multiple real machine
instructions. Atomic r-m-w and atomic c-a-s are emitted as the usual
load-linked store-conditional loops, guarded at both ends by memory fences.
Atomic loads and stores are emitted as a load preceded by a fence, and a store
followed by a fence, respectively. The amount of fencing may be overkill, but
it reflects exactly what the SM Wasm baseline compiler for AArch64 does.
One reason to implement r-m-w and c-a-s as a single insn which is expanded
only at emission time is that we must be very careful what instructions we
allow in between the load-linked and store-conditional. In particular, we
cannot allow *any* extra memory transactions in there, since -- particularly
on low-end hardware -- that might cause the transaction to fail, hence
deadlocking the generated code. That implies that we can't present the LL/SC
loop to the register allocator as its constituent instructions, since it might
insert spills anywhere. Hence we must present it as a single indivisible
unit, as we do here. It also has the benefit of reducing the total amount of
work the RA has to do.
The only other notable feature of the r-m-w and c-a-s translations into
AArch64 code, is that they both need a scratch register internally. Rather
than faking one up by claiming, in `get_regs` that it modifies an extra
scratch register, and having to have a dummy initialisation of it, these new
instructions (`::LLSC` and `::CAS`) simply use fixed registers in the range
x24-x28. We rely on the RA's ability to coalesce V<-->R copies to make the
cost of the resulting extra copies zero or almost zero. x24-x28 are chosen so
as to be call-clobbered, hence their use is less likely to interfere with long
live ranges that span calls.
One subtlety regarding the use of completely fixed input and output registers
is that we must be careful how the surrounding copy from/to of the arg/result
registers is done. In particular, it is not safe to simply emit copies in
some arbitrary order if one of the arg registers is a real reg. For that
reason, the arguments are first moved into virtual regs if they are not
already there, using a new method `<LowerCtx for Lower>::ensure_in_vreg`.
Again, we rely on coalescing to turn them into no-ops in the common case.
There is also a ridealong fix for the AArch64 lowering case for
`Opcode::Trapif | Opcode::Trapff`, which removes a bug in which two trap insns
in a row were generated.
In the patch as submitted there are 6 "FIXME JRS" comments, which mark things
which I believe to be correct, but for which I would appreciate a second
opinion. Unless otherwise directed, I will remove them for the final commit
but leave the associated code/comments unchanged.
This commit is contained in:
committed by
julian-seward1
parent
ef122a72d2
commit
25e31739a6
@@ -606,6 +606,68 @@ pub enum Inst {
|
||||
cond: Cond,
|
||||
},
|
||||
|
||||
/// A synthetic insn, which is a load-linked store-conditional loop, that has the overall
|
||||
/// effect of atomically modifying a memory location in a particular way. Because we have
|
||||
/// no way to explain to the regalloc about earlyclobber registers, this instruction has
|
||||
/// completely fixed operand registers, and we rely on the RA's coalescing to remove copies
|
||||
/// in the surrounding code to the extent it can. The sequence is both preceded and
|
||||
/// followed by a fence which is at least as comprehensive as that of the `Fence`
|
||||
/// instruction below. This instruction is sequentially consistent. The operand
|
||||
/// conventions are:
|
||||
///
|
||||
/// x25 (rd) address
|
||||
/// x26 (rd) second operand for `op`
|
||||
/// x27 (wr) old value
|
||||
/// x24 (wr) scratch reg; value afterwards has no meaning
|
||||
/// x28 (wr) scratch reg; value afterwards has no meaning
|
||||
AtomicRMW {
|
||||
ty: Type, // I8, I16, I32 or I64
|
||||
op: AtomicRMWOp,
|
||||
srcloc: Option<SourceLoc>,
|
||||
},
|
||||
|
||||
/// Similar to AtomicRMW, a compare-and-swap operation implemented using a load-linked
|
||||
/// store-conditional loop. (Although we could possibly implement it more directly using
|
||||
/// CAS insns that are available in some revisions of AArch64 above 8.0). The sequence is
|
||||
/// both preceded and followed by a fence which is at least as comprehensive as that of the
|
||||
/// `Fence` instruction below. This instruction is sequentially consistent. Note that the
|
||||
/// operand conventions, although very similar to AtomicRMW, are different:
|
||||
///
|
||||
/// x25 (rd) address
|
||||
/// x26 (rd) expected value
|
||||
/// x28 (rd) replacement value
|
||||
/// x27 (wr) old value
|
||||
/// x24 (wr) scratch reg; value afterwards has no meaning
|
||||
AtomicCAS {
|
||||
ty: Type, // I8, I16, I32 or I64
|
||||
srcloc: Option<SourceLoc>,
|
||||
},
|
||||
|
||||
/// Read `ty` bits from address `r_addr`, zero extend the loaded value to 64 bits and put it
|
||||
/// in `r_data`. The load instruction is preceded by a fence at least as comprehensive as
|
||||
/// that of the `Fence` instruction below. This instruction is sequentially consistent.
|
||||
AtomicLoad {
|
||||
ty: Type, // I8, I16, I32 or I64
|
||||
r_data: Writable<Reg>,
|
||||
r_addr: Reg,
|
||||
srcloc: Option<SourceLoc>,
|
||||
},
|
||||
|
||||
/// Write the lowest `ty` bits of `r_data` to address `r_addr`, with a memory fence
|
||||
/// instruction following the store. The fence is at least as comprehensive as that of the
|
||||
/// `Fence` instruction below. This instruction is sequentially consistent.
|
||||
AtomicStore {
|
||||
ty: Type, // I8, I16, I32 or I64
|
||||
r_data: Reg,
|
||||
r_addr: Reg,
|
||||
srcloc: Option<SourceLoc>,
|
||||
},
|
||||
|
||||
/// A memory fence. This must provide ordering to ensure that, at a minimum, neither loads
|
||||
/// nor stores may move forwards or backwards across the fence. Currently emitted as "dmb
|
||||
/// ish". This instruction is sequentially consistent.
|
||||
Fence,
|
||||
|
||||
/// FPU move. Note that this is distinct from a vector-register
|
||||
/// move; moving just 64 bits seems to be significantly faster.
|
||||
FpuMove64 {
|
||||
@@ -1249,6 +1311,29 @@ fn aarch64_get_regs(inst: &Inst, collector: &mut RegUsageCollector) {
|
||||
&Inst::CCmpImm { rn, .. } => {
|
||||
collector.add_use(rn);
|
||||
}
|
||||
&Inst::AtomicRMW { .. } => {
|
||||
collector.add_use(xreg(25));
|
||||
collector.add_use(xreg(26));
|
||||
collector.add_def(writable_xreg(24));
|
||||
collector.add_def(writable_xreg(27));
|
||||
collector.add_def(writable_xreg(28));
|
||||
}
|
||||
&Inst::AtomicCAS { .. } => {
|
||||
collector.add_use(xreg(25));
|
||||
collector.add_use(xreg(26));
|
||||
collector.add_use(xreg(28));
|
||||
collector.add_def(writable_xreg(24));
|
||||
collector.add_def(writable_xreg(27));
|
||||
}
|
||||
&Inst::AtomicLoad { r_data, r_addr, .. } => {
|
||||
collector.add_use(r_addr);
|
||||
collector.add_def(r_data);
|
||||
}
|
||||
&Inst::AtomicStore { r_data, r_addr, .. } => {
|
||||
collector.add_use(r_addr);
|
||||
collector.add_use(r_data);
|
||||
}
|
||||
&Inst::Fence {} => {}
|
||||
&Inst::FpuMove64 { rd, rn } => {
|
||||
collector.add_def(rd);
|
||||
collector.add_use(rn);
|
||||
@@ -1721,6 +1806,29 @@ fn aarch64_map_regs<RUM: RegUsageMapper>(inst: &mut Inst, mapper: &RUM) {
|
||||
&mut Inst::CCmpImm { ref mut rn, .. } => {
|
||||
map_use(mapper, rn);
|
||||
}
|
||||
&mut Inst::AtomicRMW { .. } => {
|
||||
// There are no vregs to map in this insn.
|
||||
}
|
||||
&mut Inst::AtomicCAS { .. } => {
|
||||
// There are no vregs to map in this insn.
|
||||
}
|
||||
&mut Inst::AtomicLoad {
|
||||
ref mut r_data,
|
||||
ref mut r_addr,
|
||||
..
|
||||
} => {
|
||||
map_def(mapper, r_data);
|
||||
map_use(mapper, r_addr);
|
||||
}
|
||||
&mut Inst::AtomicStore {
|
||||
ref mut r_data,
|
||||
ref mut r_addr,
|
||||
..
|
||||
} => {
|
||||
map_use(mapper, r_data);
|
||||
map_use(mapper, r_addr);
|
||||
}
|
||||
&mut Inst::Fence {} => {}
|
||||
&mut Inst::FpuMove64 {
|
||||
ref mut rd,
|
||||
ref mut rn,
|
||||
@@ -2534,6 +2642,28 @@ impl Inst {
|
||||
let cond = cond.show_rru(mb_rru);
|
||||
format!("ccmp {}, {}, {}, {}", rn, imm, nzcv, cond)
|
||||
}
|
||||
&Inst::AtomicRMW { ty, op, .. } => {
|
||||
format!(
|
||||
"atomically {{ {}_bits_at_[x25]) {:?}= x26 ; x27 = old_value_at_[x25]; x24,x28 = trash }}",
|
||||
ty.bits(), op)
|
||||
}
|
||||
&Inst::AtomicCAS { ty, .. } => {
|
||||
format!(
|
||||
"atomically {{ compare-and-swap({}_bits_at_[x25], x26 -> x28), x27 = old_value_at_[x25]; x24 = trash }}",
|
||||
ty.bits())
|
||||
}
|
||||
&Inst::AtomicLoad { ty, r_data, r_addr, .. } => {
|
||||
format!(
|
||||
"atomically {{ {} = zero_extend_{}_bits_at[{}] }}",
|
||||
r_data.show_rru(mb_rru), ty.bits(), r_addr.show_rru(mb_rru))
|
||||
}
|
||||
&Inst::AtomicStore { ty, r_data, r_addr, .. } => {
|
||||
format!(
|
||||
"atomically {{ {}_bits_at[{}] = {} }}", ty.bits(), r_addr.show_rru(mb_rru), r_data.show_rru(mb_rru))
|
||||
}
|
||||
&Inst::Fence {} => {
|
||||
format!("dmb ish")
|
||||
}
|
||||
&Inst::FpuMove64 { rd, rn } => {
|
||||
let rd = rd.to_reg().show_rru(mb_rru);
|
||||
let rn = rn.show_rru(mb_rru);
|
||||
|
||||
Reference in New Issue
Block a user