Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support wasm select instruction with V128-typed operands. #2391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ libc = "0.2.60"
log = "0.4.8"
rayon = "1.2.1"
humantime = "2.0.0"
wasmparser = "0.66"
wasmparser = "0.67"

[dev-dependencies]
env_logger = "0.8.1"
Expand Down
40 changes: 40 additions & 0 deletions cranelift/codegen/src/isa/aarch64/inst/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,46 @@ impl MachInstEmit for Inst {

sink.put4(enc_ldst_vec(q, size, rn, rd));
}
&Inst::VecCSel { rd, rn, rm, cond } => {
/* Emit this:
b.cond else
mov rd, rm
b out
else:
mov rd, rn
out:

Note, we could do better in the cases where rd == rn or rd == rm.
*/
let else_label = sink.get_label();
let out_label = sink.get_label();

// b.cond else
let br_else_offset = sink.cur_offset();
sink.put4(enc_conditional_br(
BranchTarget::Label(else_label),
CondBrKind::Cond(cond),
));
sink.use_label_at_offset(br_else_offset, else_label, LabelUse::Branch19);

// mov rd, rm
sink.put4(enc_vecmov(/* 16b = */ true, rd, rm));

// b out
let b_out_offset = sink.cur_offset();
sink.use_label_at_offset(b_out_offset, out_label, LabelUse::Branch26);
sink.add_uncond_branch(b_out_offset, b_out_offset + 4, out_label);
sink.put4(enc_jump26(0b000101, 0 /* will be fixed up later */));

// else:
sink.bind_label(else_label);

// mov rd, rn
sink.put4(enc_vecmov(/* 16b = */ true, rd, rn));

// out:
sink.bind_label(out_label);
}
&Inst::MovToNZCV { rn } => {
sink.put4(0xd51b4200 | machreg_to_gpr(rn));
}
Expand Down
11 changes: 11 additions & 0 deletions cranelift/codegen/src/isa/aarch64/inst/emit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3879,6 +3879,17 @@ fn test_aarch64_binemit() {
"ld1r { v0.8b }, [x25]",
));

insns.push((
Inst::VecCSel {
rd: writable_vreg(5),
rn: vreg(10),
rm: vreg(19),
cond: Cond::Gt,
},
"6C000054651EB34E02000014451DAA4E",
"vcsel v5.16b, v10.16b, v19.16b, gt (if-then-else diamond)",
));

insns.push((
Inst::Extend {
rd: writable_xreg(1),
Expand Down
31 changes: 31 additions & 0 deletions cranelift/codegen/src/isa/aarch64/inst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,15 @@ pub enum Inst {
size: VectorSize,
},

/// Vector conditional select, 128 bit. A synthetic instruction, which generates a 4-insn
/// control-flow diamond.
VecCSel {
rd: Writable<Reg>,
rn: Reg,
rm: Reg,
cond: Cond,
},

/// Move to the NZCV flags (actually a `MSR NZCV, Xn` insn).
MovToNZCV {
rn: Reg,
Expand Down Expand Up @@ -1732,6 +1741,11 @@ fn aarch64_get_regs(inst: &Inst, collector: &mut RegUsageCollector) {
collector.add_def(rd);
collector.add_use(rn);
}
&Inst::VecCSel { rd, rn, rm, .. } => {
collector.add_def(rd);
collector.add_use(rn);
collector.add_use(rm);
}
&Inst::FpuCmp32 { rn, rm } | &Inst::FpuCmp64 { rn, rm } => {
collector.add_use(rn);
collector.add_use(rm);
Expand Down Expand Up @@ -2343,6 +2357,16 @@ fn aarch64_map_regs<RUM: RegUsageMapper>(inst: &mut Inst, mapper: &RUM) {
map_def(mapper, rd);
map_use(mapper, rn);
}
&mut Inst::VecCSel {
ref mut rd,
ref mut rn,
ref mut rm,
..
} => {
map_def(mapper, rd);
map_use(mapper, rn);
map_use(mapper, rm);
}
&mut Inst::FpuCmp32 {
ref mut rn,
ref mut rm,
Expand Down Expand Up @@ -3591,6 +3615,13 @@ impl Inst {

format!("ld1r {{ {} }}, [{}]", rd, rn)
}
&Inst::VecCSel { rd, rn, rm, cond } => {
let rd = show_vreg_vector(rd.to_reg(), mb_rru, VectorSize::Size8x16);
let rn = show_vreg_vector(rn, mb_rru, VectorSize::Size8x16);
let rm = show_vreg_vector(rm, mb_rru, VectorSize::Size8x16);
let cond = cond.show_rru(mb_rru);
format!("vcsel {}, {}, {}, {} (if-then-else diamond)", rd, rn, rm, cond)
}
&Inst::MovToNZCV { rn } => {
let rn = rn.show_rru(mb_rru);
format!("msr nzcv, {}", rn)
Expand Down
2 changes: 2 additions & 0 deletions cranelift/codegen/src/isa/aarch64/lower_inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,8 @@ pub(crate) fn lower_insn_to_regs<C: LowerCtx<I = Inst>>(
ctx.emit(Inst::FpuCSel32 { cond, rd, rn, rm });
} else if is_float && bits == 64 {
ctx.emit(Inst::FpuCSel64 { cond, rd, rn, rm });
} else if is_float && bits == 128 {
ctx.emit(Inst::VecCSel { cond, rd, rn, rm });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note, it is weird to see that ctx.emit(Inst::CSel ... below did not have assert to prevent float/128 to become csel.

} else {
ctx.emit(Inst::CSel { cond, rd, rn, rm });
}
Expand Down
14 changes: 14 additions & 0 deletions cranelift/filetests/filetests/isa/aarch64/simd.clif
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ block0:
; nextln: mov sp, fp
; nextln: ldp fp, lr, [sp], #16
; nextln: ret

function %f4(i32, i8x16, i8x16) -> i8x16 {
block0(v0: i32, v1: i8x16, v2: i8x16):
v3 = select v0, v1, v2
return v3
}

; check: stp fp, lr, [sp, #-16]!
; nextln: mov fp, sp
; nextln: subs wzr, w0, wzr
; nextln: vcsel v0.16b, v0.16b, v1.16b, ne (if-then-else diamond)
; nextln: mov sp, fp
; nextln: ldp fp, lr, [sp], #16
; nextln: ret
2 changes: 1 addition & 1 deletion cranelift/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["webassembly", "wasm"]
edition = "2018"

[dependencies]
wasmparser = { version = "0.66.0", default-features = false }
wasmparser = { version = "0.67.0", default-features = false }
cranelift-codegen = { path = "../codegen", version = "0.68.0", default-features = false }
cranelift-entity = { path = "../entity", version = "0.68.0" }
cranelift-frontend = { path = "../frontend", version = "0.68.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/debug/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"

[dependencies]
gimli = "0.23.0"
wasmparser = "0.66.0"
wasmparser = "0.67.0"
object = { version = "0.22.0", default-features = false, features = ["read", "write"] }
wasmtime-environ = { path = "../environ", version = "0.21.0" }
target-lexicon = { version = "0.11.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/environ/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ anyhow = "1.0"
cranelift-codegen = { path = "../../cranelift/codegen", version = "0.68.0", features = ["enable-serde"] }
cranelift-entity = { path = "../../cranelift/entity", version = "0.68.0", features = ["enable-serde"] }
cranelift-wasm = { path = "../../cranelift/wasm", version = "0.68.0", features = ["enable-serde"] }
wasmparser = "0.66.0"
wasmparser = "0.67.0"
indexmap = { version = "1.0.2", features = ["serde-1"] }
thiserror = "1.0.4"
serde = { version = "1.0.94", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/fuzzing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ arbitrary = { version = "0.4.1", features = ["derive"] }
env_logger = "0.8.1"
log = "0.4.8"
rayon = "1.2.1"
wasmparser = "0.66.0"
wasmparser = "0.67.0"
wasmprinter = "0.2.13"
wasmtime = { path = "../wasmtime" }
wasmtime-wast = { path = "../wast" }
Expand Down
2 changes: 1 addition & 1 deletion crates/jit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rayon = { version = "1.0", optional = true }
region = "2.1.0"
thiserror = "1.0.4"
target-lexicon = { version = "0.11.0", default-features = false }
wasmparser = "0.66.0"
wasmparser = "0.67.0"
more-asserts = "0.2.1"
anyhow = "1.0"
cfg-if = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/lightbeam/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ more-asserts = "0.2.1"
smallvec = "1.0.0"
thiserror = "1.0.9"
typemap = "0.3"
wasmparser = "0.66.0"
wasmparser = "0.67.0"

[dev-dependencies]
lazy_static = "1.2"
Expand Down
2 changes: 1 addition & 1 deletion crates/lightbeam/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ edition = "2018"

[dependencies]
lightbeam = { path = "..", version = "0.21.0" }
wasmparser = "0.66"
wasmparser = "0.67"
cranelift-codegen = { path = "../../../cranelift/codegen", version = "0.68.0" }
wasmtime-environ = { path = "../../environ", version = "0.21.0" }
2 changes: 1 addition & 1 deletion crates/wasmtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ wasmtime-jit = { path = "../jit", version = "0.21.0" }
wasmtime-cache = { path = "../cache", version = "0.21.0", optional = true }
wasmtime-profiling = { path = "../profiling", version = "0.21.0" }
target-lexicon = { version = "0.11.0", default-features = false }
wasmparser = "0.66.0"
wasmparser = "0.67.0"
anyhow = "1.0.19"
region = "2.2.0"
libc = "0.2"
Expand Down