Skip to content

Commit 14e2152

Browse files
expenseskhyperia
andauthored
Add an read_clock_khr function that calls OpReadClockKHR (#757)
* Add support for OpReadClockKHR * Use a const scope * Use full scope import path * Apply some suggestions * Add cfg flags and scopes as raw u32s * Run cargo fmt * Fix comment accuracy * Update crates/rustc_codegen_spirv/src/spirv_type_constraints.rs Co-authored-by: Ashley Hauck <953151+khyperia@users.noreply.github.com> * Run rustfmt * Add the shader clock feature and capability to the compile test rust flags and bless the changed test errors Co-authored-by: Ashley Hauck <953151+khyperia@users.noreply.github.com>
1 parent 4e5f347 commit 14e2152

File tree

9 files changed

+91
-2
lines changed

9 files changed

+91
-2
lines changed

crates/rustc_codegen_spirv/src/spirv_type_constraints.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,10 @@ pub fn instruction_signatures(op: Op) -> Option<&'static [InstSig<'static>]> {
745745
// SPV_AMD_shader_fragment_mask
746746
Op::FragmentMaskFetchAMD | Op::FragmentFetchAMD => reserved!(SPV_AMD_shader_fragment_mask),
747747
// SPV_KHR_shader_clock
748-
Op::ReadClockKHR => reserved!(SPV_KHR_shader_clock),
748+
Op::ReadClockKHR => {
749+
// NOTE(eddyb) we actually use these despite not being in the standard yet.
750+
// reserved!(SPV_KHR_shader_clock)
751+
}
749752
// SPV_NV_mesh_shader
750753
Op::WritePackedPrimitiveIndices4x8NV => reserved!(SPV_NV_mesh_shader),
751754
// SPV_NV_ray_tracing

crates/spirv-std/src/arch.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,53 @@ pub unsafe fn vector_insert_dynamic<T: Scalar, V: Vector<T, N>, const N: usize>(
150150
pub fn kill() -> ! {
151151
unsafe { asm!("OpKill", options(noreturn)) }
152152
}
153+
154+
/// Read from the shader clock with either the `Subgroup` or `Device` scope.
155+
///
156+
/// See:
157+
/// <https://htmlpreview.github.io/?https://github.com/KhronosGroup/SPIRV-Registry/blob/master/extensions/KHR/SPV_KHR_shader_clock.html>
158+
#[cfg(all(
159+
target_feature = "Int64",
160+
target_feature = "ShaderClockKHR",
161+
target_feature = "ext:SPV_KHR_shader_clock"
162+
))]
163+
#[spirv_std_macros::gpu_only]
164+
#[doc(alias = "OpReadClockKHR")]
165+
pub unsafe fn read_clock_khr<const SCOPE: u32>() -> u64 {
166+
let mut result: u64;
167+
168+
asm! {
169+
"%uint = OpTypeInt 32 0",
170+
"%scope = OpConstant %uint {scope}",
171+
"{result} = OpReadClockKHR typeof*{result} %scope",
172+
result = out(reg) result,
173+
scope = const SCOPE,
174+
};
175+
176+
result
177+
}
178+
179+
/// Like `read_clock_khr` but returns a vector to avoid requiring the `Int64`
180+
/// capability. It returns a 'vector of two-components of 32-bit unsigned
181+
/// integer type with the first component containing the 32 least significant
182+
/// bits and the second component containing the 32 most significant bits.'
183+
#[cfg(all(
184+
target_feature = "ShaderClockKHR",
185+
target_feature = "ext:SPV_KHR_shader_clock"
186+
))]
187+
#[spirv_std_macros::gpu_only]
188+
#[doc(alias = "OpReadClockKHR")]
189+
pub unsafe fn read_clock_uvec2_khr<V: Vector<u32, 2>, const SCOPE: u32>() -> V {
190+
let mut result = V::default();
191+
192+
asm! {
193+
"%uint = OpTypeInt 32 0",
194+
"%scope = OpConstant %uint {scope}",
195+
"%result = OpReadClockKHR typeof*{result} %scope",
196+
"OpStore {result} %result",
197+
result = in(reg) &mut result,
198+
scope = const SCOPE,
199+
};
200+
201+
result
202+
}

tests/src/main.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,23 @@ struct TestDeps {
294294
/// The RUSTFLAGS passed to all SPIR-V builds.
295295
// FIXME(eddyb) expose most of these from `spirv-builder`.
296296
fn rust_flags(codegen_backend_path: &Path) -> String {
297+
let target_features = [
298+
"Int8",
299+
"Int16",
300+
"Int64",
301+
"Float64",
302+
// Only needed for `ui/arch/read_clock_khr.rs`.
303+
"ShaderClockKHR",
304+
"ext:SPV_KHR_shader_clock",
305+
];
306+
297307
[
298308
&*format!("-Zcodegen-backend={}", codegen_backend_path.display()),
299309
"-Coverflow-checks=off",
300310
"-Cdebug-assertions=off",
301311
"-Cdebuginfo=2",
302312
"-Cembed-bitcode=no",
303-
"-Ctarget-feature=+Int8,+Int16,+Int64,+Float64",
313+
&format!("-Ctarget-feature=+{}", target_features.join(",+")),
304314
"-Zsymbol-mangling-version=v0",
305315
]
306316
.join(" ")

tests/ui/arch/read_clock_khr.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// build-pass
2+
// compile-flags: -Ctarget-feature=+Int64,+ShaderClockKHR,+ext:SPV_KHR_shader_clock
3+
4+
use glam::UVec2;
5+
use spirv_std::{
6+
arch::{read_clock_khr, read_clock_uvec2_khr},
7+
memory::Scope,
8+
};
9+
10+
#[spirv(fragment)]
11+
pub fn main() {
12+
let clock_time = unsafe { read_clock_khr::<{ Scope::Subgroup as u32 }>() };
13+
14+
let clock_time_uvec2: UVec2 =
15+
unsafe { read_clock_uvec2_khr::<_, { Scope::Subgroup as u32 }>() };
16+
}

tests/ui/dis/asm_op_decorate.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ OpCapability Int16
33
OpCapability Int64
44
OpCapability Int8
55
OpCapability RuntimeDescriptorArray
6+
OpCapability ShaderClockKHR
67
OpCapability Shader
78
OpExtension "SPV_EXT_descriptor_indexing"
9+
OpExtension "SPV_KHR_shader_clock"
810
OpMemoryModel Logical Simple
911
OpEntryPoint Fragment %1 "main"
1012
OpExecutionMode %1 OriginUpperLeft

tests/ui/dis/custom_entry_point.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ OpCapability Float64
22
OpCapability Int16
33
OpCapability Int64
44
OpCapability Int8
5+
OpCapability ShaderClockKHR
56
OpCapability Shader
7+
OpExtension "SPV_KHR_shader_clock"
68
OpMemoryModel Logical Simple
79
OpEntryPoint Fragment %1 "hello_world"
810
OpExecutionMode %1 OriginUpperLeft

tests/ui/dis/generic-fn-op-name.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ OpCapability Float64
22
OpCapability Int16
33
OpCapability Int64
44
OpCapability Int8
5+
OpCapability ShaderClockKHR
56
OpCapability Shader
7+
OpExtension "SPV_KHR_shader_clock"
68
OpMemoryModel Logical Simple
79
OpEntryPoint Fragment %1 "main"
810
OpExecutionMode %1 OriginUpperLeft

tests/ui/dis/issue-723-indirect-input.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ OpCapability Float64
22
OpCapability Int16
33
OpCapability Int64
44
OpCapability Int8
5+
OpCapability ShaderClockKHR
56
OpCapability Shader
7+
OpExtension "SPV_KHR_shader_clock"
68
OpMemoryModel Logical Simple
79
OpEntryPoint Fragment %1 "main" %2
810
OpExecutionMode %1 OriginUpperLeft

tests/ui/dis/issue-723-output.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ OpCapability Float64
22
OpCapability Int16
33
OpCapability Int64
44
OpCapability Int8
5+
OpCapability ShaderClockKHR
56
OpCapability Shader
7+
OpExtension "SPV_KHR_shader_clock"
68
OpMemoryModel Logical Simple
79
OpEntryPoint Fragment %1 "main" %2
810
OpExecutionMode %1 OriginUpperLeft

0 commit comments

Comments
 (0)