From 3861a867532be4c7f3e039fa125a66b1b39a823e Mon Sep 17 00:00:00 2001 From: firestar99 Date: Wed, 18 Nov 2026 12:26:20 +0100 Subject: [PATCH 1/2] sextend: difftest for sign extensions --- tests/difftests/tests/Cargo.lock | 17 ++++++ tests/difftests/tests/Cargo.toml | 2 + .../lang/core/ops/casts/casts-cpu/Cargo.toml | 10 ++++ .../lang/core/ops/casts/casts-cpu/src/main.rs | 13 +++++ .../lang/core/ops/casts/casts-gpu/Cargo.toml | 15 +++++ .../lang/core/ops/casts/casts-gpu/src/lib.rs | 56 ++++++++++++++++++ .../lang/core/ops/casts/casts-gpu/src/main.rs | 58 +++++++++++++++++++ 7 files changed, 171 insertions(+) create mode 100644 tests/difftests/tests/lang/core/ops/casts/casts-cpu/Cargo.toml create mode 100644 tests/difftests/tests/lang/core/ops/casts/casts-cpu/src/main.rs create mode 100644 tests/difftests/tests/lang/core/ops/casts/casts-gpu/Cargo.toml create mode 100644 tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/lib.rs create mode 100644 tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/main.rs diff --git a/tests/difftests/tests/Cargo.lock b/tests/difftests/tests/Cargo.lock index bb788df52f8..b25543635b9 100644 --- a/tests/difftests/tests/Cargo.lock +++ b/tests/difftests/tests/Cargo.lock @@ -325,6 +325,23 @@ dependencies = [ "thiserror", ] +[[package]] +name = "casts-cpu" +version = "0.0.0" +dependencies = [ + "casts-gpu", + "difftest", +] + +[[package]] +name = "casts-gpu" +version = "0.0.0" +dependencies = [ + "bytemuck", + "difftest", + "spirv-std", +] + [[package]] name = "cc" version = "1.5.1" diff --git a/tests/difftests/tests/Cargo.toml b/tests/difftests/tests/Cargo.toml index 14295887b04..7435155d325 100644 --- a/tests/difftests/tests/Cargo.toml +++ b/tests/difftests/tests/Cargo.toml @@ -51,6 +51,8 @@ members = [ "lang/core/intrinsics/bit_ops/bit-ops-cpu", "lang/core/intrinsics/rotate/rotate-rust", "lang/core/intrinsics/rotate/rotate-cpu", + "lang/core/ops/casts/casts-cpu", + "lang/core/ops/casts/casts-gpu", ] [workspace.package] diff --git a/tests/difftests/tests/lang/core/ops/casts/casts-cpu/Cargo.toml b/tests/difftests/tests/lang/core/ops/casts/casts-cpu/Cargo.toml new file mode 100644 index 00000000000..5a9f1f90ca3 --- /dev/null +++ b/tests/difftests/tests/lang/core/ops/casts/casts-cpu/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "casts-cpu" +edition.workspace = true + +[lints] +workspace = true + +[dependencies] +difftest.workspace = true +casts-gpu = { path = "../casts-gpu" } diff --git a/tests/difftests/tests/lang/core/ops/casts/casts-cpu/src/main.rs b/tests/difftests/tests/lang/core/ops/casts/casts-cpu/src/main.rs new file mode 100644 index 00000000000..03a5096b9c0 --- /dev/null +++ b/tests/difftests/tests/lang/core/ops/casts/casts-cpu/src/main.rs @@ -0,0 +1,13 @@ +use casts_gpu::{TEST_DATA, test}; +use difftest::config::{Config, TestMetadata}; + +fn main() { + let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); + + let output: Vec = TEST_DATA.iter().flat_map(|&value| test(value)).collect(); + + config.write_result(&output).unwrap(); + config + .write_metadata(&TestMetadata::u32()) + .expect("Failed to write metadata"); +} diff --git a/tests/difftests/tests/lang/core/ops/casts/casts-gpu/Cargo.toml b/tests/difftests/tests/lang/core/ops/casts/casts-gpu/Cargo.toml new file mode 100644 index 00000000000..4e8f04b2027 --- /dev/null +++ b/tests/difftests/tests/lang/core/ops/casts/casts-gpu/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "casts-gpu" +edition.workspace = true + +[lints] +workspace = true + +# GPU deps +[dependencies] +spirv-std.workspace = true + +# CPU deps (for the test harness) +[target.'cfg(not(target_arch = "spirv"))'.dependencies] +difftest.workspace = true +bytemuck.workspace = true diff --git a/tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/lib.rs b/tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/lib.rs new file mode 100644 index 00000000000..3cc0e79f756 --- /dev/null +++ b/tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/lib.rs @@ -0,0 +1,56 @@ +#![no_std] + +use spirv_std::spirv; + +pub const WORDS_PER_CASE: usize = 10; + +pub const TEST_DATA: [u64; 16] = [ + 0x0000000000000000, + 0x0000000000000001, + 0x0000000000000010, + 0x0000000000000080, + 0x00000000000000FF, + 0x0000000000000100, + 0x0000000000008000, + 0x000000000000FFFF, + 0x0000000080000000, + 0x00000000FFFFFFFF, + 0x0000000100000000, + 0x0010000000000000, + 0x8000000000000000, + 0x8000000000000001, + 0x9ABCDEF012345678, + 0xFFFFFFFFFFFFFFFF, +]; + +pub fn test(value: u64) -> [u32; WORDS_PER_CASE] { + let (i32, i16, i8) = (value as i32, value as i16, value as i8); + [ + i8 as i32 as u32, + i8 as u32, + i8 as i16 as u32, + i8 as u16 as u32, + i16 as i32 as u32, + i16 as u32, + (i32 as i64) as u32, + (i32 as i64 >> 32) as u32, + (i32 as u64) as u32, + (i32 as u64 >> 32) as u32, + ] +} + +#[spirv(compute(threads(1)))] +pub fn main_cs( + #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] input: &[u64], + #[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut [u32], + #[spirv(global_invocation_id)] global_id: spirv_std::glam::UVec3, +) { + let tid = global_id.x as usize; + + if tid < input.len() { + let words = test(input[tid]); + for i in 0..WORDS_PER_CASE { + output[tid * WORDS_PER_CASE + i] = words[i]; + } + } +} diff --git a/tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/main.rs b/tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/main.rs new file mode 100644 index 00000000000..69f3465260d --- /dev/null +++ b/tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/main.rs @@ -0,0 +1,58 @@ +#[cfg(not(target_arch = "spirv"))] +fn main() { + use casts_gpu::{TEST_DATA, WORDS_PER_CASE}; + use difftest::config::{Config, TestMetadata}; + use difftest::scaffold::compute::{ + AshBackend, BufferConfig, BufferUsage, ComputeShaderTest, RustComputeShader, + }; + use difftest::spirv_builder::Capability; + + let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap(); + + // Skip on macOS due to Vulkan/MoltenVK configuration issues + #[cfg(target_os = "macos")] + { + use difftest::scaffold::Skip; + + let skip = Skip::new("Ash tests are skipped on macOS due to MoltenVK configuration issues"); + skip.run_test(&config).unwrap(); + return; + } + + #[cfg(not(target_os = "macos"))] + { + let input_bytes: Vec = bytemuck::cast_slice(&TEST_DATA).to_vec(); + let output_size = (TEST_DATA.len() * WORDS_PER_CASE * std::mem::size_of::()) as u64; + + let buffers = vec![ + BufferConfig { + size: input_bytes.len() as u64, + usage: BufferUsage::StorageReadOnly, + initial_data: Some(input_bytes), + }, + BufferConfig { + size: output_size, + usage: BufferUsage::Storage, + initial_data: None, + }, + ]; + + // Use Ash backend since wgpu/naga doesn't support Int8, Int16 and Int64 + let shader = RustComputeShader::default() + .with_capability(Capability::Int8) + .with_capability(Capability::Int16) + .with_capability(Capability::Int64); + let num_workgroups = TEST_DATA.len() as u32; + let test = ComputeShaderTest::::new(shader, [num_workgroups, 1, 1], buffers) + .unwrap(); + + config + .write_metadata(&TestMetadata::u32()) + .expect("Failed to write metadata"); + + test.run_test(&config).unwrap(); + } +} + +#[cfg(target_arch = "spirv")] +fn main() {} From 89a976614ada7c629a7250c6678bd03db3cbacb8 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Wed, 18 Nov 2026 08:21:57 +0000 Subject: [PATCH 2/2] sextend: fix integer casts extending based on the signedness of the destination `intcast` chose between `OpSConvert` and `OpUConvert` by the signedness of the destination type and ignored `is_signed`, which describes the source. So `i8 as u32` zero-extended and `u8 as i32` sign-extended. Extend based on the source signedness instead. `OpUConvert` requires an unsigned result type, so zero extension to a signed type converts to the unsigned type of the same width and bitcasts. --- .../src/builder/builder_methods.rs | 32 +++++++--- .../compiletests/ui/dis/int-cast-extension.rs | 38 ++++++++++++ .../ui/dis/int-cast-extension.stderr | 58 +++++++++++++++++++ 3 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 tests/compiletests/ui/dis/int-cast-extension.rs create mode 100644 tests/compiletests/ui/dis/int-cast-extension.stderr diff --git a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs index 10927fccbd0..53b7ee1f215 100644 --- a/crates/rustc_codegen_spirv/src/builder/builder_methods.rs +++ b/crates/rustc_codegen_spirv/src/builder/builder_methods.rs @@ -2383,16 +2383,32 @@ impl<'a, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'tcx> { .unwrap() .with_type(dest_ty), // width change, and optional sign change - (SpirvType::Integer(_, _), SpirvType::Integer(_, dest_signedness)) => { - // spir-v spec doesn't seem to say that signedness needs to match the operands, only that the signedness - // of the destination type must match the instruction's signedness. - if dest_signedness { - self.emit().s_convert(dest_ty, None, val.def(self)) + ( + SpirvType::Integer(val_width, _), + SpirvType::Integer(dest_width, dest_signedness), + ) => { + // Extension depends on the signedness of the source (`is_signed`), while truncation + // works the same either way. `OpUConvert` requires an unsigned result type, unlike + // `OpSConvert`, so zero extension to a signed type goes through a bitcast. + if is_signed || (dest_signedness && val_width > dest_width) { + self.emit() + .s_convert(dest_ty, None, val.def(self)) + .unwrap() + .with_type(dest_ty) + } else if !dest_signedness { + self.emit() + .u_convert(dest_ty, None, val.def(self)) + .unwrap() + .with_type(dest_ty) } else { - self.emit().u_convert(dest_ty, None, val.def(self)) + let unsigned_ty = SpirvType::Integer(dest_width, false).def(self.span(), self); + let extended = self + .emit() + .u_convert(unsigned_ty, None, val.def(self)) + .unwrap() + .with_type(unsigned_ty); + self.bitcast(extended, dest_ty) } - .unwrap() - .with_type(dest_ty) } // bools are ints in llvm, so we have to implement this here (SpirvType::Bool, SpirvType::Integer(_, _)) => { diff --git a/tests/compiletests/ui/dis/int-cast-extension.rs b/tests/compiletests/ui/dis/int-cast-extension.rs new file mode 100644 index 00000000000..b3c2d2e737d --- /dev/null +++ b/tests/compiletests/ui/dis/int-cast-extension.rs @@ -0,0 +1,38 @@ +// Test that widening integer casts extend based on the signedness of the source type, +// not the destination type. + +// build-pass +// compile-flags: -C target-feature=+Int8,+Int64 +// compile-flags: -C llvm-args=--disassemble-fn=int_cast_extension::casts +// compile-flags: -C llvm-args=--disassemble-globals +// normalize-stderr-test "\n\W*OpCapability VulkanMemoryModel" -> "" +// normalize-stderr-test "\n\W*OpSource .*" -> "" +// normalize-stderr-test "\n\W*OpExtension .SPV_KHR_vulkan_memory_model." -> "" +// normalize-stderr-test "OpMemoryModel Logical Vulkan" -> "OpMemoryModel Logical Simple" +// normalize-stderr-test "OpString .*" -> "OpString" + +use spirv_std::spirv; + +const MINUS_ONE: i8 = -1; +const MINUS_TWO: i8 = -2; + +#[inline(never)] +fn casts(a: i8, b: u8, c: i32, d: u32) -> [u64; 6] { + [ + // sign extension to an unsigned type + a as u32 as u64, + // zero extension to a signed type + b as i32 as u64, + c as u64, + d as i64 as u64, + // constant sign extension to an unsigned type + MINUS_ONE as u64, + // constant sign extension through `From` + i64::from(MINUS_TWO) as u64, + ] +} + +#[spirv(fragment)] +pub fn main(#[spirv(flat)] x: u32, out: &mut [u64; 6]) { + *out = casts(x as i8, x as u8, x as i32, x); +} diff --git a/tests/compiletests/ui/dis/int-cast-extension.stderr b/tests/compiletests/ui/dis/int-cast-extension.stderr new file mode 100644 index 00000000000..b3e24a1b703 --- /dev/null +++ b/tests/compiletests/ui/dis/int-cast-extension.stderr @@ -0,0 +1,58 @@ + %1 = OpFunction %2 DontInline %3 + %4 = OpFunctionParameter %5 + %6 = OpFunctionParameter %7 + %8 = OpFunctionParameter %9 + %10 = OpFunctionParameter %11 + %12 = OpLabel + OpLine %13 23 8 + %14 = OpSConvert %11 %4 + %15 = OpUConvert %16 %14 + OpLine %13 25 8 + %17 = OpUConvert %11 %6 + %18 = OpBitcast %9 %17 + %19 = OpSConvert %16 %18 + OpLine %13 26 8 + %20 = OpSConvert %16 %8 + OpLine %13 27 8 + %21 = OpUConvert %16 %10 + %22 = OpBitcast %23 %21 + %24 = OpBitcast %16 %22 + OpLine %13 31 8 + %25 = OpBitcast %16 %26 + OpLine %13 21 4 + %27 = OpCompositeConstruct %2 %15 %19 %20 %24 %28 %25 + OpNoLine + OpReturnValue %27 + OpFunctionEnd + OpCapability Shader + OpCapability Int64 + OpCapability Int8 + OpMemoryModel Logical Simple + OpEntryPoint Fragment %1 "main" %x %out + OpExecutionMode %1 OriginUpperLeft + %4 = OpString + OpName %x "x" + OpName %out "out" + OpName %int_cast_extension__casts "int_cast_extension::casts" + OpDecorate %x Flat + OpDecorate %x Location 0 + OpDecorate %6 ArrayStride 8 + OpDecorate %out Location 0 + %u32 = OpTypeInt 32 0 +%_ptr_Input_u32 = OpTypePointer Input %u32 + %u64 = OpTypeInt 64 0 + %u32_6 = OpConstant %u32 6 + %11 = OpTypeArray %u64 %u32_6 +%_ptr_Output_11 = OpTypePointer Output %11 + %void = OpTypeVoid + %14 = OpTypeFunction %void + %x = OpVariable %_ptr_Input_u32 Input + %i8 = OpTypeInt 8 1 + %u8 = OpTypeInt 8 0 + %i32 = OpTypeInt 32 1 + %6 = OpTypeArray %u64 %u32_6 + %18 = OpTypeFunction %6 %i8 %u8 %i32 %u32 + %i64 = OpTypeInt 64 1 + %i64_n2 = OpConstant %i64 -2 +%u64_18446744073709551615 = OpConstant %u64 18446744073709551615 + %out = OpVariable %_ptr_Output_11 Output