From 765b0c1c785ecbac765cdd31e1a82e90658c2d18 Mon Sep 17 00:00:00 2001 From: Anders Leino Date: Mon, 4 Nov 2024 14:48:13 +0200 Subject: [PATCH] Make various parameters and return types require specialization when targeting WGSL Structured buffer types translate to array types in the WGSL emitter. WGSL doesn't allow passing runtime-sized arrays to functions. Similarly for pointers to texture handles. Also, structured buffers (runtime-sized arrays) cannot be returned in WGSL. This closes issue #5228, issue #5278 and issue #5288 by enabling specialized functions to be generated in these cases, in order to work around these constraints. --- source/slang/slang-compiler.h | 3 +++ source/slang/slang-ir-specialize-resources.cpp | 13 +++++++++++-- source/slang/slang-ir-specialize-resources.h | 1 + source/slang/slang-type-layout.cpp | 14 ++++++++++++++ tests/autodiff/bug-1.slang | 1 - .../mutating/resource-specialization-inout.slang | 1 - tests/compute/func-resource-param.slang | 1 - tests/expected-failure-github.txt | 3 --- .../func-resource-result-simple.slang | 1 - 9 files changed, 29 insertions(+), 9 deletions(-) diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h index 624e6ec3bd..9fea93b7c3 100644 --- a/source/slang/slang-compiler.h +++ b/source/slang/slang-compiler.h @@ -1958,6 +1958,9 @@ bool isCUDATarget(TargetRequest* targetReq); // Are we generating code for a CPU target bool isCPUTarget(TargetRequest* targetReq); +/// Are we generating code for the WebGPU API? +bool isWGPUTarget(TargetRequest* targetReq); + /// A request to generate output in some target format. class TargetRequest : public RefObject { diff --git a/source/slang/slang-ir-specialize-resources.cpp b/source/slang/slang-ir-specialize-resources.cpp index 6aca4bbc72..56f468ac77 100644 --- a/source/slang/slang-ir-specialize-resources.cpp +++ b/source/slang/slang-ir-specialize-resources.cpp @@ -67,7 +67,10 @@ struct ResourceParameterSpecializationCondition : FunctionCallSpecializeConditio else return isIllegalGLSLParameterType(type); } - + else if (isWGPUTarget(targetRequest)) + { + return isIllegalWGSLParameterType(type); + } // For now, we will not treat any other parameters as // needing specialization, even if they use resource @@ -1220,7 +1223,7 @@ bool specializeResourceOutputs( HashSet& unspecializableFuncs) { auto targetRequest = codeGenContext->getTargetReq(); - if (isD3DTarget(targetRequest) || isKhronosTarget(targetRequest)) + if (isD3DTarget(targetRequest) || isKhronosTarget(targetRequest) || isWGPUTarget(targetRequest)) { } else @@ -1354,4 +1357,10 @@ bool isIllegalSPIRVParameterType(IRType* type, bool isArray) } return false; } + +bool isIllegalWGSLParameterType(IRType* type) +{ + return isIllegalGLSLParameterType(type); +} + } // namespace Slang diff --git a/source/slang/slang-ir-specialize-resources.h b/source/slang/slang-ir-specialize-resources.h index 911fa3bb99..feb61c0266 100644 --- a/source/slang/slang-ir-specialize-resources.h +++ b/source/slang/slang-ir-specialize-resources.h @@ -26,5 +26,6 @@ bool specializeResourceUsage(CodeGenContext* codeGenContext, IRModule* irModule) bool isIllegalGLSLParameterType(IRType* type); bool isIllegalSPIRVParameterType(IRType* type, bool isArray); +bool isIllegalWGSLParameterType(IRType* type); } // namespace Slang diff --git a/source/slang/slang-type-layout.cpp b/source/slang/slang-type-layout.cpp index a63d6401e5..e36e54531d 100644 --- a/source/slang/slang-type-layout.cpp +++ b/source/slang/slang-type-layout.cpp @@ -2397,6 +2397,20 @@ bool isCUDATarget(TargetRequest* targetReq) } } +bool isWGPUTarget(TargetRequest* targetReq) +{ + switch (targetReq->getTarget()) + { + default: + return false; + + case CodeGenTarget::WGSL: + case CodeGenTarget::WGSLSPIRV: + case CodeGenTarget::WGSLSPIRVAssembly: + return true; + } +} + SourceLanguage getIntermediateSourceLanguageForTarget(TargetProgram* targetProgram) { // If we are emitting directly, there is no intermediate source language diff --git a/tests/autodiff/bug-1.slang b/tests/autodiff/bug-1.slang index b92c840980..deb7e8461e 100644 --- a/tests/autodiff/bug-1.slang +++ b/tests/autodiff/bug-1.slang @@ -1,5 +1,4 @@ //TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK): -slang -compute -shaderobj -output-using-type -//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu #define DO_FLOOR #define MANUAL_DERIVATIVE diff --git a/tests/bugs/mutating/resource-specialization-inout.slang b/tests/bugs/mutating/resource-specialization-inout.slang index 78e8f44fcb..d2f9a3b9ec 100644 --- a/tests/bugs/mutating/resource-specialization-inout.slang +++ b/tests/bugs/mutating/resource-specialization-inout.slang @@ -1,7 +1,6 @@ // Bug related to resource specialization on unused resource typed fields. //TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu //TEST_INPUT: Texture2D(size=4, content = one):name t2D Texture2D t2D; diff --git a/tests/compute/func-resource-param.slang b/tests/compute/func-resource-param.slang index 3689417e0b..4b67c909e5 100644 --- a/tests/compute/func-resource-param.slang +++ b/tests/compute/func-resource-param.slang @@ -8,7 +8,6 @@ //TEST(compute, vulkan):COMPARE_COMPUTE_EX:-dx12 -compute -shaderobj //TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj //TEST(compute, vulkan):COMPARE_COMPUTE_EX:-cpu -compute -shaderobj -//DISABLE_TEST(compute):COMPARE_COMPUTE:-wgpu //NO_TEST:SIMPLE:-target glsl -entry computeMain -stage compute -validate-ir -dump-ir diff --git a/tests/expected-failure-github.txt b/tests/expected-failure-github.txt index 150e36ad1b..dfb99eb351 100644 --- a/tests/expected-failure-github.txt +++ b/tests/expected-failure-github.txt @@ -32,7 +32,6 @@ tests/bugs/gh-3980.slang.7 syn (wgpu) tests/bugs/gh-471.slang.1 syn (wgpu) tests/bugs/gh-518.slang.2 syn (wgpu) tests/bugs/gh-566.slang.1 syn (wgpu) -tests/bugs/mutating/resource-specialization-inout.slang.1 syn (wgpu) tests/bugs/nested-switch.slang.3 syn (wgpu) tests/bugs/obfuscate-specialization-naming.slang.2 syn (wgpu) tests/bugs/op-assignment-unify-mat.slang.4 syn (wgpu) @@ -51,7 +50,6 @@ tests/compute/compile-time-loop.slang.2 syn (wgpu) tests/compute/constexpr.slang.2 syn (wgpu) tests/compute/discard-stmt.slang.2 syn (wgpu) tests/compute/func-param-legalize.slang.1 syn (wgpu) -tests/compute/func-resource-param.slang.4 syn (wgpu) tests/compute/global-init.slang.2 syn (wgpu) tests/compute/interface-shader-param-in-struct.slang.4 syn (wgpu) tests/compute/interface-shader-param.slang.5 syn (wgpu) @@ -98,5 +96,4 @@ tests/language-feature/types/opaque/return-opaque-type-in-struct.slang.2 syn (wg tests/language-feature/types/opaque/return-opaque-type.slang.1 syn (wgpu) tests/metal/groupshared-threadlocal-same-parameter.slang.4 syn (wgpu) tests/optimization/func-resource-result/func-resource-result-complex.slang.2 syn (wgpu) -tests/optimization/func-resource-result/func-resource-result-simple.slang.4 syn (wgpu) tests/pipeline/compute/compute-system-values.slang.3 syn (wgpu) diff --git a/tests/optimization/func-resource-result/func-resource-result-simple.slang b/tests/optimization/func-resource-result/func-resource-result-simple.slang index 7366bc1793..80342e5690 100644 --- a/tests/optimization/func-resource-result/func-resource-result-simple.slang +++ b/tests/optimization/func-resource-result/func-resource-result-simple.slang @@ -4,7 +4,6 @@ //TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj //TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -shaderobj //TEST(compute):COMPARE_COMPUTE:-slang -shaderobj -mtl -//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu // Test that a function that returns a resource type can be // compiled for targets that don't natively support resource