Skip to content

Commit 66b7eb3

Browse files
support memset on 128-bit integers
Co-authored-by: firestar99 <firestar99@vectorware.com>
1 parent c904e5c commit 66b7eb3

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

crates/rustc_codegen_spirv/src/builder/builder_methods.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ fn memset_fill_u64(b: u8) -> u64 {
308308
| ((b as u64) << 56)
309309
}
310310

311+
fn memset_fill_u128(b: u8) -> u128 {
312+
let b64 = memset_fill_u64(b) as u128;
313+
b64 | b64 >> 64
314+
}
315+
311316
fn memset_dynamic_scalar(
312317
builder: &mut Builder<'_, '_>,
313318
fill_var: Word,
@@ -387,6 +392,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
387392
64 => self
388393
.constant_u64(self.span(), memset_fill_u64(fill_byte))
389394
.def(self),
395+
128 => self
396+
.constant_u128(self.span(), memset_fill_u128(fill_byte))
397+
.def(self),
390398
_ => self.fatal(format!(
391399
"memset on integer width {width} not implemented yet"
392400
)),
@@ -402,6 +410,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
402410
64 => self
403411
.constant_i64(self.span(), memset_fill_u64(fill_byte) as i64)
404412
.def(self),
413+
128 => self
414+
.constant_u128(self.span(), memset_fill_u128(fill_byte))
415+
.def(self),
405416
_ => self.fatal(format!(
406417
"memset on integer width {width} not implemented yet"
407418
)),

crates/rustc_codegen_spirv/src/codegen_cx/constant.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ impl<'tcx> CodegenCx<'tcx> {
5050
self.constant_int_from_native_unsigned(span, val)
5151
}
5252

53+
pub fn constant_u128(&self, span: Span, val: u128) -> SpirvValue {
54+
self.constant_int_from_native_unsigned(span, val)
55+
}
56+
5357
fn constant_int_from_native_unsigned(&self, span: Span, val: impl Into<u128>) -> SpirvValue {
5458
let size = Size::from_bytes(std::mem::size_of_val(&val));
5559
let ty = SpirvType::Integer(size.bits() as u32, false).def(span, self);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// build-pass
2+
3+
use spirv_std::spirv;
4+
5+
pub fn u128_constant() -> [u128; 4] {
6+
return [0x123456789ABCDEF0123456789ABCDEF; 4];
7+
}
8+
9+
pub fn i128_constant() -> [i128; 4] {
10+
return [0x123456789ABCDEF0123456789ABCDEF; 4];
11+
}
12+
13+
#[spirv(fragment)]
14+
pub fn main() {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// build-pass
2+
3+
use spirv_std::spirv;
4+
5+
/// `crypto-common` has a proc macro to implement code for u8, u16, u32, u16 and u128,
6+
/// which means we need our codegen to handle the u128 case for the crate to even compile.
7+
/// Specifically, this calls `memset_const_pattern` with u128.
8+
///
9+
/// <https://github.com/RustCrypto/traits/blob/d9067c494b4ae8f1ecbc9ae72b683e8a65dd53ce/crypto-common/src/hazmat.rs#L347-L363>
10+
///
11+
/// Note that the u128 path is unusable anyway as 128-bit integer are not supported in SPIR-V.
12+
/// So if this is used nowhere in any entry point, our post-link DCE will remove the u128 code,
13+
/// and the SPIR-V will be valid.
14+
pub fn u128_zero_fill() -> [u128; 4] {
15+
return [0; 4];
16+
}
17+
18+
pub fn i128_zero_fill() -> [i128; 4] {
19+
return [0; 4];
20+
}
21+
22+
#[spirv(fragment)]
23+
pub fn main() {}

0 commit comments

Comments
 (0)