Skip to content

Commit a072716

Browse files
committed
Added tests for 64bit trailing_zeros
1 parent a30bd43 commit a072716

File tree

8 files changed

+153
-3
lines changed

8 files changed

+153
-3
lines changed

crates/rustc_codegen_spirv/src/builder/intrinsics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,8 @@ impl Builder<'_, '_> {
582582

583583
if trailing {
584584
let use_lower = self.emit().i_equal(bool, None, lower, u32_0).unwrap();
585-
let lower_bits = find_xsb(self, lower, 32);
586-
let higher_bits = find_xsb(self, higher, 0);
585+
let lower_bits = find_xsb(self, lower, 0);
586+
let higher_bits = find_xsb(self, higher, 32);
587587
self.emit()
588588
.select(u32, None, use_lower, higher_bits, lower_bits)
589589
.unwrap()

tests/difftests/tests/Cargo.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/difftests/tests/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ members = [
4646
"lang/core/ops/vector_swizzle/vector_swizzle-wgsl",
4747
"lang/core/intrinsics/black_box_noop/with-black-box",
4848
"lang/core/intrinsics/black_box_noop/without-black-box",
49+
"lang/core/intrinsics/trailing_zeros_64/trailing_zeros_64-rust",
50+
"lang/core/intrinsics/trailing_zeros_64/trailing_zeros_64-cpu",
4951
]
5052

5153
[workspace.package]
@@ -55,7 +57,7 @@ publish = false
5557

5658
[workspace.lints.rust]
5759
unexpected_cfgs = { level = "allow", check-cfg = [
58-
'cfg(target_arch, values("spirv"))'
60+
'cfg(target_arch, values("spirv"))',
5961
] }
6062

6163
[workspace.dependencies]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "trailing_zeros_64-cpu"
3+
edition.workspace = true
4+
5+
[lints]
6+
workspace = true
7+
8+
[dependencies]
9+
difftest.workspace = true
10+
trailing_zeros_64-rust = { path = "../trailing_zeros_64-rust" }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use difftest::config::{Config, TestMetadata};
2+
use trailing_zeros_64_rust::TEST_DATA;
3+
4+
fn main() {
5+
let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap();
6+
7+
let output: Vec<u32> = TEST_DATA.iter().map(|v| v.trailing_zeros()).collect();
8+
9+
config.write_result(&output).unwrap();
10+
config
11+
.write_metadata(&TestMetadata::u32())
12+
.expect("Failed to write metadata");
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "trailing_zeros_64-rust"
3+
edition.workspace = true
4+
5+
[lints]
6+
workspace = true
7+
8+
# GPU deps
9+
[dependencies]
10+
spirv-std.workspace = true
11+
12+
# CPU deps (for the test harness)
13+
[target.'cfg(not(target_arch = "spirv"))'.dependencies]
14+
difftest.workspace = true
15+
bytemuck.workspace = true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![no_std]
2+
#![cfg_attr(target_arch = "spirv", feature(asm_experimental_arch))]
3+
4+
use spirv_std::spirv;
5+
6+
/// Test cases for 64-bit trailing_zeros - shared between GPU and CPU tests
7+
#[cfg(not(target_arch = "spirv"))]
8+
pub const TEST_DATA: [u64; 16] = [
9+
0x0000000000000000,
10+
0x0000000000000001,
11+
0x8000000000000000,
12+
0xFFFFFFFFFFFFFFFE,
13+
0x1234000000000000,
14+
0x0000000100000000,
15+
0x0000000000001000,
16+
0x0000000080000000,
17+
0x0000000000000010,
18+
0x0000000000000100,
19+
0x0000000000010000,
20+
0x0001000000000000,
21+
0x0100000000000000,
22+
0xFFFFFFFFFFFFFFFF,
23+
0x8000000000000001,
24+
0x4000000000000000,
25+
];
26+
27+
#[spirv(compute(threads(1)))]
28+
pub fn main_cs(
29+
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] input: &[u64],
30+
#[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut [u32],
31+
#[spirv(global_invocation_id)] global_id: spirv_std::glam::UVec3,
32+
) {
33+
let tid = global_id.x as usize;
34+
35+
if tid < input.len() && tid < output.len() {
36+
output[tid] = input[tid].trailing_zeros();
37+
}
38+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#[cfg(not(target_arch = "spirv"))]
2+
fn main() {
3+
use difftest::config::{Config, TestMetadata};
4+
use difftest::scaffold::compute::{
5+
AshBackend, BufferConfig, BufferUsage, ComputeShaderTest, RustComputeShader,
6+
};
7+
use difftest::spirv_builder::Capability;
8+
use trailing_zeros_64_rust::TEST_DATA;
9+
10+
let config = Config::from_path(std::env::args().nth(1).unwrap()).unwrap();
11+
12+
// Skip on macOS due to Vulkan/MoltenVK configuration issues
13+
#[cfg(target_os = "macos")]
14+
{
15+
use difftest::scaffold::Skip;
16+
17+
let skip = Skip::new("Ash tests are skipped on macOS due to MoltenVK configuration issues");
18+
skip.run_test(&config).unwrap();
19+
return;
20+
}
21+
22+
#[cfg(not(target_os = "macos"))]
23+
{
24+
let input_bytes: Vec<u8> = bytemuck::cast_slice(&TEST_DATA).to_vec();
25+
let output_size = (TEST_DATA.len() * std::mem::size_of::<u32>()) as u64;
26+
27+
let buffers = vec![
28+
BufferConfig {
29+
size: input_bytes.len() as u64,
30+
usage: BufferUsage::StorageReadOnly,
31+
initial_data: Some(input_bytes),
32+
},
33+
BufferConfig {
34+
size: output_size,
35+
usage: BufferUsage::Storage,
36+
initial_data: None,
37+
},
38+
];
39+
40+
// Use Ash backend since wgpu/naga doesn't support Int64
41+
let shader = RustComputeShader::default().with_capability(Capability::Int64);
42+
let num_workgroups = TEST_DATA.len() as u32;
43+
let test = ComputeShaderTest::<AshBackend, _>::new(shader, [num_workgroups, 1, 1], buffers)
44+
.unwrap();
45+
46+
config
47+
.write_metadata(&TestMetadata::u32())
48+
.expect("Failed to write metadata");
49+
50+
test.run_test(&config).unwrap();
51+
}
52+
}
53+
54+
#[cfg(target_arch = "spirv")]
55+
fn main() {}

0 commit comments

Comments
 (0)