Skip to content

Commit caef959

Browse files
committed
s390x: Implement full SIMD support
This adds full support for all Cranelift SIMD instructions to the s390x target. Everything is matched fully via ISLE. In addition to adding support for many new instructions, and the lower.isle code to match all SIMD IR patterns, this patch also adds ABI support for vector types. In particular, we now need to handle the fact that vector registers 8 .. 15 are partially callee-saved, i.e. the high parts of those registers (which correspond to the old floating-poing registers) are callee-saved, but the low parts are not. This is the exact same situation that we already have on AArch64, and so this patch uses the same solution (the is_included_in_clobbers callback). The bulk of the changes are platform-specific, but there are a few exceptions: - Added ISLE extractors for the Immediate and Constant types, to enable matching the vconst and swizzle instructions. - Added a missing accessor for call_conv to ABISig. - Fixed endian conversion for vector types in data_value.rs to enable their use in runtests on the big-endian platforms. - Enabled (nearly) all SIMD runtests on s390x. [ Two test cases remain disabled due to vector shift count semantics, see below. ] - Enabled all Wasmtime SIMD tests on s390x. There are three minor issues, called out via FIXMEs below, which should be addressed in the future, but should not be blockers to getting this patch merged. I've opened the following issues to track them: - Vector shift count semantics #4424 - is_included_in_clobbers vs. link register #4425 - gen_constant callback #4426 All tests, including all newly enabled SIMD tests, pass on both z14 and z15 architectures.
1 parent 6c70428 commit caef959

75 files changed

Lines changed: 17187 additions & 1129 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,10 @@ fn write_testsuite_tests(
171171
fn ignore(testsuite: &str, testname: &str, strategy: &str) -> bool {
172172
match strategy {
173173
"Cranelift" => match (testsuite, testname) {
174-
// No simd support yet for s390x.
175-
("simd", _) if platform_is_s390x() => return true,
176-
_ if platform_is_s390x() && testname.starts_with("simd") => return true,
177174
_ => {}
178175
},
179176
_ => panic!("unrecognized strategy"),
180177
}
181178

182179
false
183180
}
184-
185-
fn platform_is_s390x() -> bool {
186-
env::var("CARGO_CFG_TARGET_ARCH").unwrap() == "s390x"
187-
}

cranelift/codegen/src/data_value.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl DataValue {
8989
DataValue::I128(i) => dst[..16].copy_from_slice(&i.to_ne_bytes()[..]),
9090
DataValue::F32(f) => dst[..4].copy_from_slice(&f.bits().to_ne_bytes()[..]),
9191
DataValue::F64(f) => dst[..8].copy_from_slice(&f.bits().to_ne_bytes()[..]),
92-
DataValue::V128(v) => dst[..16].copy_from_slice(&v[..]),
92+
DataValue::V128(v) => dst[..16].copy_from_slice(&u128::from_le_bytes(*v).to_ne_bytes()),
9393
_ => unimplemented!(),
9494
};
9595
}
@@ -120,7 +120,7 @@ impl DataValue {
120120
DataValue::B(src[..size].iter().any(|&i| i != 0))
121121
}
122122
_ if ty.is_vector() && ty.bytes() == 16 => {
123-
DataValue::V128(src[..16].try_into().unwrap())
123+
DataValue::V128(u128::from_ne_bytes(src[..16].try_into().unwrap()).to_le_bytes())
124124
}
125125
_ => unimplemented!(),
126126
}

cranelift/codegen/src/isa/s390x/abi.rs

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ fn in_flt_reg(ty: Type) -> bool {
9797
}
9898
}
9999

100+
fn in_vec_reg(ty: Type) -> bool {
101+
ty.is_vector() && ty.bits() == 128
102+
}
103+
100104
fn get_intreg_for_arg(idx: usize) -> Option<Reg> {
101105
match idx {
102106
0 => Some(regs::gpr(2)),
@@ -118,6 +122,20 @@ fn get_fltreg_for_arg(idx: usize) -> Option<Reg> {
118122
}
119123
}
120124

125+
fn get_vecreg_for_arg(idx: usize) -> Option<Reg> {
126+
match idx {
127+
0 => Some(regs::vr(24)),
128+
1 => Some(regs::vr(25)),
129+
2 => Some(regs::vr(26)),
130+
3 => Some(regs::vr(27)),
131+
4 => Some(regs::vr(28)),
132+
5 => Some(regs::vr(29)),
133+
6 => Some(regs::vr(30)),
134+
7 => Some(regs::vr(31)),
135+
_ => None,
136+
}
137+
}
138+
121139
fn get_intreg_for_ret(idx: usize) -> Option<Reg> {
122140
match idx {
123141
0 => Some(regs::gpr(2)),
@@ -140,6 +158,21 @@ fn get_fltreg_for_ret(idx: usize) -> Option<Reg> {
140158
}
141159
}
142160

161+
fn get_vecreg_for_ret(idx: usize) -> Option<Reg> {
162+
match idx {
163+
0 => Some(regs::vr(24)),
164+
// ABI extension to support multi-value returns:
165+
1 => Some(regs::vr(25)),
166+
2 => Some(regs::vr(26)),
167+
3 => Some(regs::vr(27)),
168+
4 => Some(regs::vr(28)),
169+
5 => Some(regs::vr(29)),
170+
6 => Some(regs::vr(30)),
171+
7 => Some(regs::vr(31)),
172+
_ => None,
173+
}
174+
}
175+
143176
/// This is the limit for the size of argument and return-value areas on the
144177
/// stack. We place a reasonable limit here to avoid integer overflow issues
145178
/// with 32-bit arithmetic: for now, 128 MB.
@@ -182,6 +215,7 @@ impl ABIMachineSpec for S390xMachineDeps {
182215
) -> CodegenResult<(Vec<ABIArg>, i64, Option<usize>)> {
183216
let mut next_gpr = 0;
184217
let mut next_fpr = 0;
218+
let mut next_vr = 0;
185219
let mut next_stack: u64 = 0;
186220
let mut ret = vec![];
187221

@@ -206,21 +240,27 @@ impl ABIMachineSpec for S390xMachineDeps {
206240

207241
let intreg = in_int_reg(param.value_type);
208242
let fltreg = in_flt_reg(param.value_type);
209-
debug_assert!(intreg || fltreg);
210-
debug_assert!(!(intreg && fltreg));
243+
let vecreg = in_vec_reg(param.value_type);
244+
debug_assert!(intreg as i32 + fltreg as i32 + vecreg as i32 == 1);
211245

212246
let (next_reg, candidate) = if intreg {
213247
let candidate = match args_or_rets {
214248
ArgsOrRets::Args => get_intreg_for_arg(next_gpr),
215249
ArgsOrRets::Rets => get_intreg_for_ret(next_gpr),
216250
};
217251
(&mut next_gpr, candidate)
218-
} else {
252+
} else if fltreg {
219253
let candidate = match args_or_rets {
220254
ArgsOrRets::Args => get_fltreg_for_arg(next_fpr),
221255
ArgsOrRets::Rets => get_fltreg_for_ret(next_fpr),
222256
};
223257
(&mut next_fpr, candidate)
258+
} else {
259+
let candidate = match args_or_rets {
260+
ArgsOrRets::Args => get_vecreg_for_arg(next_vr),
261+
ArgsOrRets::Rets => get_vecreg_for_ret(next_vr),
262+
};
263+
(&mut next_vr, candidate)
224264
};
225265

226266
// In the Wasmtime ABI only the first return value can be in a register.
@@ -252,7 +292,8 @@ impl ABIMachineSpec for S390xMachineDeps {
252292

253293
// Align the stack slot.
254294
debug_assert!(slot_size.is_power_of_two());
255-
next_stack = align_to(next_stack, slot_size);
295+
let slot_align = std::cmp::min(slot_size, 8);
296+
next_stack = align_to(next_stack, slot_align);
256297

257298
// If the type is actually of smaller size (and the argument
258299
// was not extended), it is passed right-aligned.
@@ -477,6 +518,13 @@ impl ABIMachineSpec for S390xMachineDeps {
477518
RegClass::Float => clobbered_fpr.push(reg),
478519
}
479520
}
521+
// We need to save the link register in non-leaf functions.
522+
// FIXME: This should be included in the clobber list to begin with,
523+
// but isn't because we have have excluded call instructions via the
524+
// is_included_in_clobbers callback.
525+
if outgoing_args_size > 0 {
526+
clobbered_gpr.push(Writable::from_reg(RealReg::from(gpr_preg(14))));
527+
}
480528

481529
let mut first_clobbered_gpr = 16;
482530
for reg in clobbered_gpr {
@@ -534,13 +582,15 @@ impl ABIMachineSpec for S390xMachineDeps {
534582

535583
// Save FPRs.
536584
for (i, reg) in clobbered_fpr.iter().enumerate() {
537-
insts.push(Inst::FpuStore64 {
585+
insts.push(Inst::VecStoreLane {
586+
size: 64,
538587
rd: reg.to_reg().into(),
539588
mem: MemArg::reg_plus_off(
540589
stack_reg(),
541590
(i * 8) as i64 + outgoing_args_size as i64 + fixed_frame_storage_size as i64,
542591
MemFlags::trusted(),
543592
),
593+
lane_imm: 0,
544594
});
545595
if flags.unwind_info() {
546596
insts.push(Inst::Unwind {
@@ -566,7 +616,14 @@ impl ABIMachineSpec for S390xMachineDeps {
566616
let mut insts = SmallVec::new();
567617

568618
// Collect clobbered registers.
569-
let (clobbered_gpr, clobbered_fpr) = get_regs_saved_in_prologue(call_conv, clobbers);
619+
let (mut clobbered_gpr, clobbered_fpr) = get_regs_saved_in_prologue(call_conv, clobbers);
620+
// We need to restore the link register in non-leaf functions.
621+
// FIXME: This should be included in the clobber list to begin with,
622+
// but isn't because we have have excluded call instructions via the
623+
// is_included_in_clobbers callback.
624+
if outgoing_args_size > 0 {
625+
clobbered_gpr.push(Writable::from_reg(RealReg::from(gpr_preg(14))));
626+
}
570627
let mut first_clobbered_gpr = 16;
571628
for reg in clobbered_gpr {
572629
let enc = reg.to_reg().hw_enc();
@@ -578,13 +635,15 @@ impl ABIMachineSpec for S390xMachineDeps {
578635

579636
// Restore FPRs.
580637
for (i, reg) in clobbered_fpr.iter().enumerate() {
581-
insts.push(Inst::FpuLoad64 {
638+
insts.push(Inst::VecLoadLaneUndef {
639+
size: 64,
582640
rd: Writable::from_reg(reg.to_reg().into()),
583641
mem: MemArg::reg_plus_off(
584642
stack_reg(),
585643
(i * 8) as i64 + outgoing_args_size as i64 + fixed_frame_storage_size as i64,
586644
MemFlags::trusted(),
587645
),
646+
lane_imm: 0,
588647
});
589648
}
590649

@@ -639,7 +698,7 @@ impl ABIMachineSpec for S390xMachineDeps {
639698
// We allocate in terms of 8-byte slots.
640699
match rc {
641700
RegClass::Int => 1,
642-
RegClass::Float => 1,
701+
RegClass::Float => 2,
643702
}
644703
}
645704

@@ -739,6 +798,21 @@ const fn clobbers() -> PRegSet {
739798
.with(gpr_preg(3))
740799
.with(gpr_preg(4))
741800
.with(gpr_preg(5))
801+
// v0 - v7 inclusive and v16 - v31 inclusive are
802+
// caller-saves. The upper 64 bits of v8 - v15 inclusive are
803+
// also caller-saves. However, because we cannot currently
804+
// represent partial registers to regalloc2, we indicate here
805+
// that every vector register is caller-save. Because this
806+
// function is used at *callsites*, approximating in this
807+
// direction (save more than necessary) is conservative and
808+
// thus safe.
809+
//
810+
// Note that we exclude clobbers from a call instruction when
811+
// a call instruction's callee has the same ABI as the caller
812+
// (the current function body); this is safe (anything
813+
// clobbered by callee can be clobbered by caller as well) and
814+
// avoids unnecessary saves of v8-v15 in the prologue even
815+
// though we include them as defs here.
742816
.with(vr_preg(0))
743817
.with(vr_preg(1))
744818
.with(vr_preg(2))
@@ -747,6 +821,14 @@ const fn clobbers() -> PRegSet {
747821
.with(vr_preg(5))
748822
.with(vr_preg(6))
749823
.with(vr_preg(7))
824+
.with(vr_preg(8))
825+
.with(vr_preg(9))
826+
.with(vr_preg(10))
827+
.with(vr_preg(11))
828+
.with(vr_preg(12))
829+
.with(vr_preg(13))
830+
.with(vr_preg(14))
831+
.with(vr_preg(15))
750832
.with(vr_preg(16))
751833
.with(vr_preg(17))
752834
.with(vr_preg(18))

0 commit comments

Comments
 (0)