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
5 changes: 2 additions & 3 deletions crates/rustc_codegen_spirv/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
SpirvType::Pointer { pointee: ty }.def(self.span(), self)
}

// TODO: Definitely add tests to make sure this impl is right.
fn rotate(&mut self, value: SpirvValue, shift: SpirvValue, is_left: bool) -> SpirvValue {
let width = match self.lookup_type(shift.ty) {
let width = match self.lookup_type(value.ty) {
SpirvType::Integer(width, _) => width,
other => self.fatal(format!(
"cannot rotate non-integer type: {}",
other.debug(shift.ty, self)
other.debug(value.ty, self)
)),
};
let int_size = self.constant_int(shift.ty, width.into());
Expand Down
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 @@ -49,6 +49,8 @@ members = [
"lang/core/intrinsics/black_box_noop/without-black-box",
"lang/core/intrinsics/bit_ops/bit-ops-rust",
"lang/core/intrinsics/bit_ops/bit-ops-cpu",
"lang/core/intrinsics/rotate/rotate-rust",
"lang/core/intrinsics/rotate/rotate-cpu",
]

[workspace.package]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "rotate-cpu"
edition.workspace = true

[lints]
workspace = true

[dependencies]
difftest.workspace = true
rotate-rust = { path = "../rotate-rust" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use difftest::config::{Config, TestMetadata};
use rotate_rust::{rotate_case, test_data};

fn main() {
let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap();

let output: Vec<u32> = test_data()
.iter()
.flat_map(|&[value, amount]| rotate_case(value, amount as u32))
.collect();

config.write_result(&output).unwrap();
config
.write_metadata(&TestMetadata::u32())
.expect("Failed to write metadata");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "rotate-rust"
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#![no_std]

use spirv_std::spirv;

pub const WORDS_PER_CASE: usize = 16;

const VALUES: [u64; 2] = [0x9ABC_DEF0_1234_5678, 0x8000_0000_0000_0001];

const AMOUNTS: [u32; 14] = [0, 1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, u32::MAX];

pub const NUM_CASES: usize = VALUES.len() * AMOUNTS.len();

pub fn test_data() -> [[u64; 2]; NUM_CASES] {
let mut data = [[0; 2]; NUM_CASES];
for (i, case) in data.iter_mut().enumerate() {
*case = [VALUES[i / AMOUNTS.len()], AMOUNTS[i % AMOUNTS.len()] as u64];
}
data
}

pub fn rotate_case(value: u64, amount: u32) -> [u32; WORDS_PER_CASE] {
let left_64 = value.rotate_left(amount);
let right_64 = value.rotate_right(amount);
let left_i64 = (value as i64).rotate_left(amount) as u64;
let right_i64 = (value as i64).rotate_right(amount) as u64;
// An amount that is a constant in the shader
let left_64_const = value.rotate_left(40);
[
left_64 as u32,
(left_64 >> 32) as u32,
right_64 as u32,
(right_64 >> 32) as u32,
left_i64 as u32,
(left_i64 >> 32) as u32,
right_i64 as u32,
(right_i64 >> 32) as u32,
(value as u32).rotate_left(amount),
(value as u32).rotate_right(amount),
(value as u16).rotate_left(amount) as u32,
(value as u16).rotate_right(amount) as u32,
(value as u8).rotate_left(amount) as u32,
(value as u8).rotate_right(amount) as u32,
left_64_const as u32,
(left_64_const >> 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 = rotate_case(input[2 * tid], input[2 * tid + 1] as u32);
for i in 0..WORDS_PER_CASE {
output[tid * WORDS_PER_CASE + i] = words[i];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#[cfg(not(target_arch = "spirv"))]
fn main() {
use difftest::config::{Config, TestMetadata};
use difftest::scaffold::compute::{
AshBackend, BufferConfig, BufferUsage, ComputeShaderTest, RustComputeShader,
};
use difftest::spirv_builder::Capability;
use rotate_rust::{NUM_CASES, WORDS_PER_CASE, test_data};

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 = (NUM_CASES * 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 = NUM_CASES 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