Skip to content
Open
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
32 changes: 24 additions & 8 deletions crates/rustc_codegen_spirv/src/builder/builder_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_, _)) => {
Expand Down
38 changes: 38 additions & 0 deletions tests/compiletests/ui/dis/int-cast-extension.rs
Original file line number Diff line number Diff line change
@@ -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);
}
58 changes: 58 additions & 0 deletions tests/compiletests/ui/dis/int-cast-extension.stderr
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions tests/difftests/tests/Cargo.lock

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

2 changes: 2 additions & 0 deletions tests/difftests/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions tests/difftests/tests/lang/core/ops/casts/casts-cpu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "casts-cpu"
edition.workspace = true

[lints]
workspace = true

[dependencies]
difftest.workspace = true
casts-gpu = { path = "../casts-gpu" }
13 changes: 13 additions & 0 deletions tests/difftests/tests/lang/core/ops/casts/casts-cpu/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<u32> = TEST_DATA.iter().flat_map(|&value| test(value)).collect();

config.write_result(&output).unwrap();
config
.write_metadata(&TestMetadata::u32())
.expect("Failed to write metadata");
}
15 changes: 15 additions & 0 deletions tests/difftests/tests/lang/core/ops/casts/casts-gpu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
56 changes: 56 additions & 0 deletions tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
58 changes: 58 additions & 0 deletions tests/difftests/tests/lang/core/ops/casts/casts-gpu/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<u8> = bytemuck::cast_slice(&TEST_DATA).to_vec();
let output_size = (TEST_DATA.len() * WORDS_PER_CASE * std::mem::size_of::<u32>()) 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::<AshBackend, _>::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() {}
Loading