Skip to content

Commit 453330b

Browse files
authored
x64: Add rudimentary support for some AVX instructions (#5795)
* x64: Add rudimentary support for some AVX instructions I was poking around Spidermonkey's wasm backend and saw that the various assembler functions used are all `v*`-prefixed which look like they're intended for use with AVX instructions. I looked at Cranelift and it currently doesn't have support for many AVX-based instructions, so I figured I'd take a crack at it! The support added here is a bit of a mishmash when viewed alone, but my general goal was to take a single instruction from the SIMD proposal for WebAssembly and migrate all of its component instructions to AVX. I, by random chance, picked a pretty complicated instruction of `f32x4.min`. This wasm instruction is implemented on x64 with 4 unique SSE instructions and ended up being a pretty good candidate. Further digging about AVX-vs-SSE shows that there should be two major benefits to using AVX over SSE: * Primarily AVX instructions largely use a three-operand form where two input registers are operated with and an output register is also specified. This is in contrast to SSE's predominant one-register-is-input-but-also-output pattern. This should help free up the register allocator a bit and additionally remove the need for movement between registers. * As #4767 notes the memory-based operations of VEX-encoded instructions (aka AVX instructions) do not have strict alignment requirements which means we would be able to sink loads and stores into individual instructions instead of having separate instructions. So I set out on my journey to implement the instructions used by `f32x4.min`. The first few were fairly easy. The machinst backends are already of the shape "take these inputs and compute the output" where the x86 requirement of a register being both input and output is postprocessed in. This means that the `inst.isle` creation helpers for SSE instructions were already of the correct form to use AVX. I chose to add new `rule` branches for the instruction creation helpers, for example `x64_andnps`. The new `rule` conditionally only runs if AVX is enabled and emits an AVX instruction instead of an SSE instruction for achieving the same goal. This means that no lowerings of clif instructions were modified, instead just new instructions are being generated. The VEX encoding was previously not heavily used in Cranelift. The only current user are the FMA-style instructions that Cranelift has at this time. These FMA instructions have one extra operand than `vandnps`, for example, so I split the existing `XmmRmRVex` into a few more variants to fit the shape of the instructions that needed generating for `f32x4.min`. This was accompanied then with more AVX opcode definitions, more emission support, etc. Upon implementing all of this it turned out that the test suite was failing on my machine due to the memory-operand encodings of VEX instructions not being supported. I didn't explicitly add those in myself but some preexisting RIP-relative addressing was leaking into the new instructions with existing tests. I opted to go ahead and fill out the memory addressing modes of VEX encoding to get the tests passing again. All-in-all this PR adds new instructions to the x64 backend for a number of AVX instructions, updates 5 existing instruction producers to use AVX instructions conditionally, implements VEX memory operands, and adds some simple tests for the new output of `f32x4.min`. The existing runtest for `f32x.min` caught a few intermediate bugs along the way and I additionally added a plain `target x86_64` to that runtest to ensure that it executes with and without AVX to test the various lowerings. I'll also note that this, and future support, should be well-fuzzed through Wasmtime's fuzzing which may explicitly disable AVX support despite the machine having access to AVX, so non-AVX lowerings should be well-tested into the future. It's also worth mentioning that I am not an AVX or VEX or x64 expert. Implementing the memory operand part for VEX was the hardest part of this PR and while I think it should be good someone else should definitely double-check me. Additionally I haven't added many instructions to the x64 backend yet so I may have missed obvious places to tests or such, so am happy to follow-up with anything to be more thorough if necessary. Finally I should note that this is just the tip of the iceberg when it comes to AVX. My hope is to get some of the idioms sorted out to make it easier for future PRs to add one-off instruction lowerings or such. * Review feedback
1 parent f8ca67c commit 453330b

File tree

12 files changed

+707
-116
lines changed

12 files changed

+707
-116
lines changed

cranelift/codegen/src/isa/x64/encoding/rex.rs

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -312,18 +312,50 @@ pub(crate) fn emit_std_enc_mem(
312312

313313
prefixes.emit(sink);
314314

315+
// After prefixes, first emit the REX byte depending on the kind of
316+
// addressing mode that's being used.
315317
match *mem_e {
316-
Amode::ImmReg { simm32, base, .. } => {
317-
// First, the REX byte.
318+
Amode::ImmReg { base, .. } => {
318319
let enc_e = int_reg_enc(base);
319320
rex.emit_two_op(sink, enc_g, enc_e);
321+
}
320322

321-
// Now the opcode(s). These include any other prefixes the caller
322-
// hands to us.
323-
while num_opcodes > 0 {
324-
num_opcodes -= 1;
325-
sink.put1(((opcodes >> (num_opcodes << 3)) & 0xFF) as u8);
326-
}
323+
Amode::ImmRegRegShift {
324+
base: reg_base,
325+
index: reg_index,
326+
..
327+
} => {
328+
let enc_base = int_reg_enc(*reg_base);
329+
let enc_index = int_reg_enc(*reg_index);
330+
rex.emit_three_op(sink, enc_g, enc_index, enc_base);
331+
}
332+
333+
Amode::RipRelative { .. } => {
334+
// note REX.B = 0.
335+
rex.emit_two_op(sink, enc_g, 0);
336+
}
337+
}
338+
339+
// Now the opcode(s). These include any other prefixes the caller
340+
// hands to us.
341+
while num_opcodes > 0 {
342+
num_opcodes -= 1;
343+
sink.put1(((opcodes >> (num_opcodes << 3)) & 0xFF) as u8);
344+
}
345+
346+
// And finally encode the mod/rm bytes and all further information.
347+
emit_modrm_sib_disp(sink, enc_g, mem_e, bytes_at_end)
348+
}
349+
350+
pub(crate) fn emit_modrm_sib_disp(
351+
sink: &mut MachBuffer<Inst>,
352+
enc_g: u8,
353+
mem_e: &Amode,
354+
bytes_at_end: u8,
355+
) {
356+
match *mem_e {
357+
Amode::ImmReg { simm32, base, .. } => {
358+
let enc_e = int_reg_enc(base);
327359

328360
// Now the mod/rm and associated immediates. This is
329361
// significantly complicated due to the multiple special cases.
@@ -377,15 +409,6 @@ pub(crate) fn emit_std_enc_mem(
377409
let enc_base = int_reg_enc(*reg_base);
378410
let enc_index = int_reg_enc(*reg_index);
379411

380-
// The rex byte.
381-
rex.emit_three_op(sink, enc_g, enc_index, enc_base);
382-
383-
// All other prefixes and opcodes.
384-
while num_opcodes > 0 {
385-
num_opcodes -= 1;
386-
sink.put1(((opcodes >> (num_opcodes << 3)) & 0xFF) as u8);
387-
}
388-
389412
// modrm, SIB, immediates.
390413
if low8_will_sign_extend_to_32(simm32) && enc_index != regs::ENC_RSP {
391414
sink.put1(encode_modrm(1, enc_g & 7, 4));
@@ -401,16 +424,6 @@ pub(crate) fn emit_std_enc_mem(
401424
}
402425

403426
Amode::RipRelative { ref target } => {
404-
// First, the REX byte, with REX.B = 0.
405-
rex.emit_two_op(sink, enc_g, 0);
406-
407-
// Now the opcode(s). These include any other prefixes the caller
408-
// hands to us.
409-
while num_opcodes > 0 {
410-
num_opcodes -= 1;
411-
sink.put1(((opcodes >> (num_opcodes << 3)) & 0xFF) as u8);
412-
}
413-
414427
// RIP-relative is mod=00, rm=101.
415428
sink.put1(encode_modrm(0, enc_g & 7, 0b101));
416429

0 commit comments

Comments
 (0)