From d58243d9041947c99f18b82385e62c082507decb Mon Sep 17 00:00:00 2001 From: Jay Kwak <82421531+jkwak-work@users.noreply.github.com> Date: Sat, 17 May 2025 02:26:44 +0000 Subject: Support Vulkan memory model (#7057) The user can explicitly use Vulkan memory model, or it will be automatically used when cooperative-matrix is used. When vulkan memory model is used, two keywords, "Coherent" and "Volatile", are not allowed. There are many differences regarding atomic and texture but this PR has changes limited to support `globallycoherent` keyword. When variables with `globallycoherent` is used with `OpLoad`, it will use additional options, `MakePointerAvailable|NonPrivatePointer`, that will provide the same effect. For `OpStore`, it will use `MakePointerVisible|NonPrivatePointer`. --- docs/command-line-slangc-reference.md | 3 + docs/user-guide/a2-01-spirv-target-specific.md | 15 + .../user-guide/a3-02-reference-capability-atoms.md | 15 +- docs/user-guide/toc.html | 1 + source/slang/slang-capabilities.capdef | 14 + source/slang/slang-emit-spirv.cpp | 462 +++++++++++++++++---- source/slang/slang-ir-spirv-legalize.cpp | 13 + source/slang/slang-ir-spirv-legalize.h | 2 + tests/cooperative-matrix/add.slang | 2 +- tests/cooperative-matrix/array.slang | 2 +- tests/cooperative-matrix/comparison.slang | 2 +- tests/cooperative-matrix/conversion.slang | 2 +- tests/cooperative-matrix/copyFrom.slang | 2 +- tests/cooperative-matrix/div.slang | 2 +- tests/cooperative-matrix/fill.slang | 2 +- tests/cooperative-matrix/inout.slang | 2 +- tests/cooperative-matrix/length.slang | 2 +- .../load-store-arbitrary-array-vec.slang | 2 +- .../load-store-arbitrary-array.slang | 2 +- .../load-store-groupshared.slang | 2 +- .../load-store-rwbyteaddressbuffer.slang | 4 +- .../load-store-rwstructuredbuffer.slang | 4 +- .../load-store-tensorlayout.slang | 12 +- .../cooperative-matrix/load-store-tensorview.slang | 12 +- tests/cooperative-matrix/map-element-single.slang | 6 +- .../mat-mul-add-spirv-matrix-operands.slang | 2 +- tests/cooperative-matrix/mat-mul-add.slang | 2 +- tests/cooperative-matrix/mod.slang | 2 +- tests/cooperative-matrix/mul.slang | 2 +- tests/cooperative-matrix/out.slang | 2 +- tests/cooperative-matrix/parameter.slang | 2 +- tests/cooperative-matrix/reduce.slang | 8 +- tests/cooperative-matrix/return.slang | 2 +- tests/cooperative-matrix/scalar-mul.slang | 2 +- tests/cooperative-matrix/struct.slang | 2 +- tests/cooperative-matrix/sub.slang | 2 +- tests/cooperative-matrix/subscript-in-func.slang | 2 +- tests/cooperative-matrix/subscript.slang | 2 +- tests/cooperative-matrix/transpose.slang | 2 +- tests/cooperative-matrix/unary_neg.slang | 2 +- tests/diagnostics/extension-full-name.slang.actual | 32 -- tests/hlsl/simple/globallycoherent.hlsl | 8 + tests/spirv/coherent-2.slang | 3 + tests/spirv/coherent-texture.slang | 24 ++ tests/spirv/coherent.slang | 2 + 45 files changed, 528 insertions(+), 164 deletions(-) delete mode 100644 tests/diagnostics/extension-full-name.slang.actual create mode 100644 tests/spirv/coherent-texture.slang diff --git a/docs/command-line-slangc-reference.md b/docs/command-line-slangc-reference.md index 296441a2e..89e7f0b4c 100644 --- a/docs/command-line-slangc-reference.md +++ b/docs/command-line-slangc-reference.md @@ -1162,6 +1162,8 @@ A capability describes an optional feature that a target may or may not support. * `spvTensorAddressingNV` * `spvMaximalReconvergenceKHR` * `spvQuadControlKHR` +* `spvVulkanMemoryModelKHR` +* `spvVulkanMemoryModelDeviceScopeKHR` * `metallib_latest` * `dxil_lib` * `any_target` @@ -1294,6 +1296,7 @@ A capability describes an optional feature that a target may or may not support. * `cooperative_matrix_block_load` * `tensor_addressing` * `cooperative_matrix_2` +* `vk_mem_model` * `pixel` * `tesscontrol` * `tesseval` diff --git a/docs/user-guide/a2-01-spirv-target-specific.md b/docs/user-guide/a2-01-spirv-target-specific.md index 4bdac1953..1ab143cb0 100644 --- a/docs/user-guide/a2-01-spirv-target-specific.md +++ b/docs/user-guide/a2-01-spirv-target-specific.md @@ -14,6 +14,21 @@ Experimental support for the older versions of SPIR-V Slang's SPIR-V backend is stable when emitting SPIR-V 1.3 and later, however, support for SPIR-V 1.0, 1.1 and 1.2 is still experimental. When targeting the older SPIR-V profiles, Slang may produce SPIR-V that uses the instructions and keywords that were introduced in the later versions of SPIR-V. +Memory model +------------ +By default, the Slang compiler produces the SPIRV code using GLSL450 memory model. The user can opt-in to Vulkan Memory Model by specifying `-capability vk_mem_model`. For using APIs, the following lines can be added to explicitly specify the capability. +``` + slang::CompilerOptionEntry entry; + entry.name = slang::CompilerOptionName::Capability; + entry.value.kind = slang::CompilerOptionValueKind::String; + entry.value.stringValue0 = "vk_mem_model"; + + slang::SessionDesc sessionDesc = {}; + sessionDesc.compilerOptionEntries = &entry; + sessionDesc.compilerOptionEntryCount = 1; +``` + +If the shader uses `CoopVec` or `CoopMat` intrinsics, then the Slang compiler will automatically use `vk_mem_model` capability. Combined texture sampler ------------------------ diff --git a/docs/user-guide/a3-02-reference-capability-atoms.md b/docs/user-guide/a3-02-reference-capability-atoms.md index 549d98ece..4596e8fed 100644 --- a/docs/user-guide/a3-02-reference-capability-atoms.md +++ b/docs/user-guide/a3-02-reference-capability-atoms.md @@ -606,6 +606,12 @@ Extensions `spvQuadControlKHR` > Represents the SPIR-V capability for quad group control. +`spvVulkanMemoryModelKHR` +> Represents the SPIR-V capability for vulkan memory model. + +`spvVulkanMemoryModelDeviceScopeKHR` +> Represents the SPIR-V capability for vulkan memory model. + `GL_EXT_buffer_reference` > Represents the GL_EXT_buffer_reference extension. @@ -972,6 +978,9 @@ Compound Capabilities `cooperative_vector_training` > Capabilities needed to train cooperative vectors +`cooperative_matrix` +> Capabilities needed to use cooperative matrices + `cooperative_matrix_reduction` > Capabilities needed to use reduction operations with cooperative matrix @@ -993,6 +1002,9 @@ Compound Capabilities `cooperative_matrix_2` > Capabilities needed to use tensor addressing +`vk_mem_model` +> Capabilities needed to use vulkan memory model + `any_stage` > Collection of all shader stages @@ -1301,9 +1313,6 @@ Other ---------------------- *Capabilities which may be deprecated* -`cooperative_matrix` -> Capabilities needed to use cooperative matrices - `SPIRV_1_0` > Use `spirv_1_0` instead diff --git a/docs/user-guide/toc.html b/docs/user-guide/toc.html index a60780e65..ace7da1d0 100644 --- a/docs/user-guide/toc.html +++ b/docs/user-guide/toc.html @@ -192,6 +192,7 @@
  • SPIR-V-Specific Functionalities