From c1b97e9ffd84f5e69b72564e7eb4adcd61fd3cdf Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Mon, 27 Sep 2021 19:33:46 +0200 Subject: [PATCH 01/10] Add support for OpReadClockKHR --- .../src/spirv_type_constraints.rs | 2 +- crates/spirv-std/src/arch.rs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs b/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs index 470f330bc3..ee48675b5f 100644 --- a/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs +++ b/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs @@ -745,7 +745,7 @@ pub fn instruction_signatures(op: Op) -> Option<&'static [InstSig<'static>]> { // SPV_AMD_shader_fragment_mask Op::FragmentMaskFetchAMD | Op::FragmentFetchAMD => reserved!(SPV_AMD_shader_fragment_mask), // SPV_KHR_shader_clock - Op::ReadClockKHR => reserved!(SPV_KHR_shader_clock), + Op::ReadClockKHR => {} // SPV_NV_mesh_shader Op::WritePackedPrimitiveIndices4x8NV => reserved!(SPV_NV_mesh_shader), // SPV_NV_ray_tracing diff --git a/crates/spirv-std/src/arch.rs b/crates/spirv-std/src/arch.rs index 00f0f28b84..680d34689b 100644 --- a/crates/spirv-std/src/arch.rs +++ b/crates/spirv-std/src/arch.rs @@ -150,3 +150,25 @@ pub unsafe fn vector_insert_dynamic, const N: usize>( pub fn kill() -> ! { unsafe { asm!("OpKill", options(noreturn)) } } + +/// Read from the shader clock. +/// +/// Requires the `SPV_KHR_shader_clock` extension and the `ShaderClockKHR` capability. +/// +/// See: +/// +#[spirv_std_macros::gpu_only] +#[doc(alias = "OpReadClockKHR")] +pub unsafe fn read_clock_khr() -> u64 { + let mut result: u64 = 0; + + asm! { + "%uint = OpTypeInt 32 0", + "%uint_3 = OpConstant %uint 3", + "%result = OpReadClockKHR typeof*{result} %uint_3", + "OpStore {result} %result", + result = in(reg) &mut result, + }; + + result +} From ecec0006162ae23638b7e67c1eef8d8d6dcc24cc Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Tue, 28 Sep 2021 12:54:00 +0200 Subject: [PATCH 02/10] Use a const scope --- crates/spirv-std/src/arch.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/spirv-std/src/arch.rs b/crates/spirv-std/src/arch.rs index 680d34689b..2c0d7727b1 100644 --- a/crates/spirv-std/src/arch.rs +++ b/crates/spirv-std/src/arch.rs @@ -164,10 +164,11 @@ pub unsafe fn read_clock_khr() -> u64 { asm! { "%uint = OpTypeInt 32 0", - "%uint_3 = OpConstant %uint 3", - "%result = OpReadClockKHR typeof*{result} %uint_3", + "%scope = OpConstant %uint {scope}", + "%result = OpReadClockKHR typeof*{result} %scope", "OpStore {result} %result", result = in(reg) &mut result, + scope = const Scope::Subgroup as _, }; result From 3230c30b81050e52fd09a904d50505a8638a12da Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Tue, 28 Sep 2021 13:27:56 +0200 Subject: [PATCH 03/10] Use full scope import path --- crates/spirv-std/src/arch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/spirv-std/src/arch.rs b/crates/spirv-std/src/arch.rs index 2c0d7727b1..735d6b0158 100644 --- a/crates/spirv-std/src/arch.rs +++ b/crates/spirv-std/src/arch.rs @@ -168,7 +168,7 @@ pub unsafe fn read_clock_khr() -> u64 { "%result = OpReadClockKHR typeof*{result} %scope", "OpStore {result} %result", result = in(reg) &mut result, - scope = const Scope::Subgroup as _, + scope = const crate::memory::Scope::Subgroup as _, }; result From 5019edc6a450cbe7495280fd7eadf6917d451299 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 30 Sep 2021 15:30:05 +0200 Subject: [PATCH 04/10] Apply some suggestions --- crates/spirv-std/src/arch.rs | 27 ++++++++++++++++++++++++--- tests/ui/arch/read_clock_khr.rs | 16 ++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/ui/arch/read_clock_khr.rs diff --git a/crates/spirv-std/src/arch.rs b/crates/spirv-std/src/arch.rs index 735d6b0158..0a3306d27c 100644 --- a/crates/spirv-std/src/arch.rs +++ b/crates/spirv-std/src/arch.rs @@ -151,16 +151,37 @@ pub fn kill() -> ! { unsafe { asm!("OpKill", options(noreturn)) } } -/// Read from the shader clock. +/// Read from the subgroup shader clock. /// -/// Requires the `SPV_KHR_shader_clock` extension and the `ShaderClockKHR` capability. +/// Requires the `SPV_KHR_shader_clock` extension and both the `Int64` and `ShaderClockKHR` capabilities. /// /// See: /// +#[cfg(target_feature = "Int64")] #[spirv_std_macros::gpu_only] #[doc(alias = "OpReadClockKHR")] pub unsafe fn read_clock_khr() -> u64 { - let mut result: u64 = 0; + let mut result: u64; + + asm! { + "%uint = OpTypeInt 32 0", + "%scope = OpConstant %uint {scope}", + "{result} = OpReadClockKHR typeof*{result} %scope", + result = out(reg) result, + scope = const crate::memory::Scope::Subgroup as _, + }; + + result +} + +/// Like `read_clock_khr` but returns a vector to avoid requiring the `Int64` +/// capability. It returns a 'vector of two-components of 32-bit unsigned +/// integer type with the first component containing the 32 least significant +/// bits and the second component containing the 32 most significant bits.' +#[spirv_std_macros::gpu_only] +#[doc(alias = "OpReadClockKHR")] +pub unsafe fn read_clock_uvec2_khr>() -> V { + let mut result = V::default(); asm! { "%uint = OpTypeInt 32 0", diff --git a/tests/ui/arch/read_clock_khr.rs b/tests/ui/arch/read_clock_khr.rs new file mode 100644 index 0000000000..eb2a0b85e6 --- /dev/null +++ b/tests/ui/arch/read_clock_khr.rs @@ -0,0 +1,16 @@ +// build-pass +// compile-flags: -Ctarget-feature=+Int64,+ShaderClockKHR,+ext:SPV_KHR_shader_clock + +use glam::UVec2; +use spirv_std::arch::{read_clock_khr, read_clock_uvec2_khr}; + +#[spirv(fragment)] +pub fn main() { + let clock_time = unsafe { + read_clock_khr() + }; + + let clock_time_uvec2: UVec2 = unsafe { + read_clock_uvec2_khr() + }; +} From 7b664ebc41d17a68cf8ceba940f25b69cae4a43a Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 30 Sep 2021 15:58:47 +0200 Subject: [PATCH 05/10] Add cfg flags and scopes as raw u32s --- crates/spirv-std/src/arch.rs | 13 ++++++------- tests/ui/arch/read_clock_khr.rs | 6 +++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/spirv-std/src/arch.rs b/crates/spirv-std/src/arch.rs index 0a3306d27c..2d99e6baaa 100644 --- a/crates/spirv-std/src/arch.rs +++ b/crates/spirv-std/src/arch.rs @@ -153,14 +153,12 @@ pub fn kill() -> ! { /// Read from the subgroup shader clock. /// -/// Requires the `SPV_KHR_shader_clock` extension and both the `Int64` and `ShaderClockKHR` capabilities. -/// /// See: /// -#[cfg(target_feature = "Int64")] +#[cfg(all(target_feature = "Int64", target_feature = "ShaderClockKHR", target_feature = "ext:SPV_KHR_shader_clock"))] #[spirv_std_macros::gpu_only] #[doc(alias = "OpReadClockKHR")] -pub unsafe fn read_clock_khr() -> u64 { +pub unsafe fn read_clock_khr() -> u64 { let mut result: u64; asm! { @@ -168,7 +166,7 @@ pub unsafe fn read_clock_khr() -> u64 { "%scope = OpConstant %uint {scope}", "{result} = OpReadClockKHR typeof*{result} %scope", result = out(reg) result, - scope = const crate::memory::Scope::Subgroup as _, + scope = const SCOPE, }; result @@ -178,9 +176,10 @@ pub unsafe fn read_clock_khr() -> u64 { /// capability. It returns a 'vector of two-components of 32-bit unsigned /// integer type with the first component containing the 32 least significant /// bits and the second component containing the 32 most significant bits.' +#[cfg(all(target_feature = "ShaderClockKHR", target_feature = "ext:SPV_KHR_shader_clock"))] #[spirv_std_macros::gpu_only] #[doc(alias = "OpReadClockKHR")] -pub unsafe fn read_clock_uvec2_khr>() -> V { +pub unsafe fn read_clock_uvec2_khr, const SCOPE: u32>() -> V { let mut result = V::default(); asm! { @@ -189,7 +188,7 @@ pub unsafe fn read_clock_uvec2_khr>() -> V { "%result = OpReadClockKHR typeof*{result} %scope", "OpStore {result} %result", result = in(reg) &mut result, - scope = const crate::memory::Scope::Subgroup as _, + scope = const SCOPE, }; result diff --git a/tests/ui/arch/read_clock_khr.rs b/tests/ui/arch/read_clock_khr.rs index eb2a0b85e6..c4e6774188 100644 --- a/tests/ui/arch/read_clock_khr.rs +++ b/tests/ui/arch/read_clock_khr.rs @@ -2,15 +2,15 @@ // compile-flags: -Ctarget-feature=+Int64,+ShaderClockKHR,+ext:SPV_KHR_shader_clock use glam::UVec2; -use spirv_std::arch::{read_clock_khr, read_clock_uvec2_khr}; +use spirv_std::{arch::{read_clock_khr, read_clock_uvec2_khr}, memory::Scope}; #[spirv(fragment)] pub fn main() { let clock_time = unsafe { - read_clock_khr() + read_clock_khr::<{ Scope::Subgroup as u32 }>() }; let clock_time_uvec2: UVec2 = unsafe { - read_clock_uvec2_khr() + read_clock_uvec2_khr::<_, { Scope::Subgroup as u32 }>() }; } From 3c0f826efebd158088bb7b6e043d7ec0df07c185 Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 30 Sep 2021 16:07:00 +0200 Subject: [PATCH 06/10] Run cargo fmt --- crates/spirv-std/src/arch.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/spirv-std/src/arch.rs b/crates/spirv-std/src/arch.rs index 2d99e6baaa..e8ce2e886d 100644 --- a/crates/spirv-std/src/arch.rs +++ b/crates/spirv-std/src/arch.rs @@ -155,7 +155,11 @@ pub fn kill() -> ! { /// /// See: /// -#[cfg(all(target_feature = "Int64", target_feature = "ShaderClockKHR", target_feature = "ext:SPV_KHR_shader_clock"))] +#[cfg(all( + target_feature = "Int64", + target_feature = "ShaderClockKHR", + target_feature = "ext:SPV_KHR_shader_clock" +))] #[spirv_std_macros::gpu_only] #[doc(alias = "OpReadClockKHR")] pub unsafe fn read_clock_khr() -> u64 { @@ -176,7 +180,10 @@ pub unsafe fn read_clock_khr() -> u64 { /// capability. It returns a 'vector of two-components of 32-bit unsigned /// integer type with the first component containing the 32 least significant /// bits and the second component containing the 32 most significant bits.' -#[cfg(all(target_feature = "ShaderClockKHR", target_feature = "ext:SPV_KHR_shader_clock"))] +#[cfg(all( + target_feature = "ShaderClockKHR", + target_feature = "ext:SPV_KHR_shader_clock" +))] #[spirv_std_macros::gpu_only] #[doc(alias = "OpReadClockKHR")] pub unsafe fn read_clock_uvec2_khr, const SCOPE: u32>() -> V { From 5448b0a0c00a63908c274621dfa8bf38cc9502cf Mon Sep 17 00:00:00 2001 From: Ashley Ruglys Date: Thu, 30 Sep 2021 16:20:15 +0200 Subject: [PATCH 07/10] Fix comment accuracy --- crates/spirv-std/src/arch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/spirv-std/src/arch.rs b/crates/spirv-std/src/arch.rs index e8ce2e886d..6c563f33cf 100644 --- a/crates/spirv-std/src/arch.rs +++ b/crates/spirv-std/src/arch.rs @@ -151,7 +151,7 @@ pub fn kill() -> ! { unsafe { asm!("OpKill", options(noreturn)) } } -/// Read from the subgroup shader clock. +/// Read from the shader clock with either the `Subgroup` or `Device` scope. /// /// See: /// From c63f1327ce711be42b205aa2f6876edb5cef7101 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 30 Sep 2021 20:48:55 +0200 Subject: [PATCH 08/10] Update crates/rustc_codegen_spirv/src/spirv_type_constraints.rs Co-authored-by: Ashley Hauck <953151+khyperia@users.noreply.github.com> --- crates/rustc_codegen_spirv/src/spirv_type_constraints.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs b/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs index ee48675b5f..b4e180f913 100644 --- a/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs +++ b/crates/rustc_codegen_spirv/src/spirv_type_constraints.rs @@ -745,7 +745,10 @@ pub fn instruction_signatures(op: Op) -> Option<&'static [InstSig<'static>]> { // SPV_AMD_shader_fragment_mask Op::FragmentMaskFetchAMD | Op::FragmentFetchAMD => reserved!(SPV_AMD_shader_fragment_mask), // SPV_KHR_shader_clock - Op::ReadClockKHR => {} + Op::ReadClockKHR => { + // NOTE(eddyb) we actually use these despite not being in the standard yet. + // reserved!(SPV_KHR_shader_clock) + } // SPV_NV_mesh_shader Op::WritePackedPrimitiveIndices4x8NV => reserved!(SPV_NV_mesh_shader), // SPV_NV_ray_tracing From f5add5413a9f871b0b22ab549791a7efda80f3a2 Mon Sep 17 00:00:00 2001 From: Ashley Date: Fri, 8 Oct 2021 14:15:42 +0200 Subject: [PATCH 09/10] Run rustfmt --- tests/ui/arch/read_clock_khr.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/ui/arch/read_clock_khr.rs b/tests/ui/arch/read_clock_khr.rs index c4e6774188..25dfc71f2e 100644 --- a/tests/ui/arch/read_clock_khr.rs +++ b/tests/ui/arch/read_clock_khr.rs @@ -2,15 +2,15 @@ // compile-flags: -Ctarget-feature=+Int64,+ShaderClockKHR,+ext:SPV_KHR_shader_clock use glam::UVec2; -use spirv_std::{arch::{read_clock_khr, read_clock_uvec2_khr}, memory::Scope}; +use spirv_std::{ + arch::{read_clock_khr, read_clock_uvec2_khr}, + memory::Scope, +}; #[spirv(fragment)] pub fn main() { - let clock_time = unsafe { - read_clock_khr::<{ Scope::Subgroup as u32 }>() - }; + let clock_time = unsafe { read_clock_khr::<{ Scope::Subgroup as u32 }>() }; - let clock_time_uvec2: UVec2 = unsafe { - read_clock_uvec2_khr::<_, { Scope::Subgroup as u32 }>() - }; + let clock_time_uvec2: UVec2 = + unsafe { read_clock_uvec2_khr::<_, { Scope::Subgroup as u32 }>() }; } From c4ba0231988a9594da2dba8c12e7f1fcf5f2c249 Mon Sep 17 00:00:00 2001 From: Ashley Date: Fri, 8 Oct 2021 14:44:47 +0200 Subject: [PATCH 10/10] Add the shader clock feature and capability to the compile test rust flags and bless the changed test errors --- tests/src/main.rs | 12 +++++++++++- tests/ui/dis/asm_op_decorate.stderr | 2 ++ tests/ui/dis/custom_entry_point.stderr | 2 ++ tests/ui/dis/generic-fn-op-name.stderr | 2 ++ tests/ui/dis/issue-723-indirect-input.stderr | 2 ++ tests/ui/dis/issue-723-output.stderr | 2 ++ 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/src/main.rs b/tests/src/main.rs index 1f32608c4c..ef375d7306 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -294,13 +294,23 @@ struct TestDeps { /// The RUSTFLAGS passed to all SPIR-V builds. // FIXME(eddyb) expose most of these from `spirv-builder`. fn rust_flags(codegen_backend_path: &Path) -> String { + let target_features = [ + "Int8", + "Int16", + "Int64", + "Float64", + // Only needed for `ui/arch/read_clock_khr.rs`. + "ShaderClockKHR", + "ext:SPV_KHR_shader_clock", + ]; + [ &*format!("-Zcodegen-backend={}", codegen_backend_path.display()), "-Coverflow-checks=off", "-Cdebug-assertions=off", "-Cdebuginfo=2", "-Cembed-bitcode=no", - "-Ctarget-feature=+Int8,+Int16,+Int64,+Float64", + &format!("-Ctarget-feature=+{}", target_features.join(",+")), "-Zsymbol-mangling-version=v0", ] .join(" ") diff --git a/tests/ui/dis/asm_op_decorate.stderr b/tests/ui/dis/asm_op_decorate.stderr index 11f4f039a0..2f7f60b8f2 100644 --- a/tests/ui/dis/asm_op_decorate.stderr +++ b/tests/ui/dis/asm_op_decorate.stderr @@ -3,8 +3,10 @@ OpCapability Int16 OpCapability Int64 OpCapability Int8 OpCapability RuntimeDescriptorArray +OpCapability ShaderClockKHR OpCapability Shader OpExtension "SPV_EXT_descriptor_indexing" +OpExtension "SPV_KHR_shader_clock" OpMemoryModel Logical Simple OpEntryPoint Fragment %1 "main" OpExecutionMode %1 OriginUpperLeft diff --git a/tests/ui/dis/custom_entry_point.stderr b/tests/ui/dis/custom_entry_point.stderr index 43683e3089..2881a9b4e5 100644 --- a/tests/ui/dis/custom_entry_point.stderr +++ b/tests/ui/dis/custom_entry_point.stderr @@ -2,7 +2,9 @@ OpCapability Float64 OpCapability Int16 OpCapability Int64 OpCapability Int8 +OpCapability ShaderClockKHR OpCapability Shader +OpExtension "SPV_KHR_shader_clock" OpMemoryModel Logical Simple OpEntryPoint Fragment %1 "hello_world" OpExecutionMode %1 OriginUpperLeft diff --git a/tests/ui/dis/generic-fn-op-name.stderr b/tests/ui/dis/generic-fn-op-name.stderr index 7728d0ee7a..61bee98490 100644 --- a/tests/ui/dis/generic-fn-op-name.stderr +++ b/tests/ui/dis/generic-fn-op-name.stderr @@ -2,7 +2,9 @@ OpCapability Float64 OpCapability Int16 OpCapability Int64 OpCapability Int8 +OpCapability ShaderClockKHR OpCapability Shader +OpExtension "SPV_KHR_shader_clock" OpMemoryModel Logical Simple OpEntryPoint Fragment %1 "main" OpExecutionMode %1 OriginUpperLeft diff --git a/tests/ui/dis/issue-723-indirect-input.stderr b/tests/ui/dis/issue-723-indirect-input.stderr index f43153fa79..d9be9f92bf 100644 --- a/tests/ui/dis/issue-723-indirect-input.stderr +++ b/tests/ui/dis/issue-723-indirect-input.stderr @@ -2,7 +2,9 @@ OpCapability Float64 OpCapability Int16 OpCapability Int64 OpCapability Int8 +OpCapability ShaderClockKHR OpCapability Shader +OpExtension "SPV_KHR_shader_clock" OpMemoryModel Logical Simple OpEntryPoint Fragment %1 "main" %2 OpExecutionMode %1 OriginUpperLeft diff --git a/tests/ui/dis/issue-723-output.stderr b/tests/ui/dis/issue-723-output.stderr index d98cfff1ec..f1783caaf8 100644 --- a/tests/ui/dis/issue-723-output.stderr +++ b/tests/ui/dis/issue-723-output.stderr @@ -2,7 +2,9 @@ OpCapability Float64 OpCapability Int16 OpCapability Int64 OpCapability Int8 +OpCapability ShaderClockKHR OpCapability Shader +OpExtension "SPV_KHR_shader_clock" OpMemoryModel Logical Simple OpEntryPoint Fragment %1 "main" %2 OpExecutionMode %1 OriginUpperLeft