From d0756fe7c3832ac09cb6db8e269303d658d7e351 Mon Sep 17 00:00:00 2001 From: Jerran Schmidt Date: Tue, 1 Jul 2025 17:11:59 +1000 Subject: [PATCH 1/4] WIP opaque type decoration fix --- source/slang/slang-emit-spirv.cpp | 3 ++- source/slang/slang-ir-util.cpp | 13 +++++++++++++ source/slang/slang-ir-util.h | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index dc535204110..25a9d748308 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -2056,6 +2056,7 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex { auto irArrayType = static_cast(inst); const auto elementType = irArrayType->getElementType(); + bool isOpaqueType = isIROpaqueType(elementType); const auto arrayType = inst->getOp() == kIROp_ArrayType ? emitOpTypeArray(inst, elementType, irArrayType->getElementCount()) @@ -2086,7 +2087,7 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex stride = (int)sizeAndAlignment.getStride(); } - if (stride != 0) + if (!isOpaqueType && stride != 0) { emitOpDecorateArrayStride( getSection(SpvLogicalSectionID::Annotations), diff --git a/source/slang/slang-ir-util.cpp b/source/slang/slang-ir-util.cpp index 497281114f6..b40a6848ac3 100644 --- a/source/slang/slang-ir-util.cpp +++ b/source/slang/slang-ir-util.cpp @@ -2408,4 +2408,17 @@ bool isSignedType(IRType* type) } } +bool isIROpaqueType(IRType* type) +{ + switch (type->getOp()) + { + case kIROp_TextureType: + case kIROp_SamplerStateType: + case kIROp_SamplerComparisonStateType: + return true; + default: + return false; + } +} + } // namespace Slang diff --git a/source/slang/slang-ir-util.h b/source/slang/slang-ir-util.h index edb8fdf0934..e325fbdc266 100644 --- a/source/slang/slang-ir-util.h +++ b/source/slang/slang-ir-util.h @@ -421,6 +421,8 @@ constexpr bool anyOf(Range&& range, Predicate&& pred) IRType* getUnsignedTypeFromSignedType(IRBuilder* builder, IRType* type); bool isSignedType(IRType* type); + +bool isIROpaqueType(IRType* type); } // namespace Slang #endif From deac18386b4228f3c1cd6bd10fd036cebdf251ed Mon Sep 17 00:00:00 2001 From: Jerran Schmidt Date: Wed, 2 Jul 2025 08:51:43 +1000 Subject: [PATCH 2/4] Clearer intent --- source/slang/slang-emit-spirv.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index 25a9d748308..4628a8e3db0 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -2056,12 +2056,12 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex { auto irArrayType = static_cast(inst); const auto elementType = irArrayType->getElementType(); - bool isOpaqueType = isIROpaqueType(elementType); const auto arrayType = inst->getOp() == kIROp_ArrayType ? emitOpTypeArray(inst, elementType, irArrayType->getElementCount()) : emitOpTypeRuntimeArray(inst, elementType); - if (shouldEmitArrayStride(irArrayType->getElementType())) + // Arrays of opaque types should not emit a stride + if (!isIROpaqueType(elementType) && shouldEmitArrayStride(irArrayType->getElementType())) { auto stride = 0; // If the array type has no stride, it indicates that this array type is only @@ -2087,7 +2087,7 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex stride = (int)sizeAndAlignment.getStride(); } - if (!isOpaqueType && stride != 0) + if (stride != 0) { emitOpDecorateArrayStride( getSection(SpvLogicalSectionID::Annotations), From e8111036a4a6b68da66d2b3ea8156aca11fd0ee5 Mon Sep 17 00:00:00 2001 From: Jerran Schmidt Date: Wed, 2 Jul 2025 09:24:50 +1000 Subject: [PATCH 3/4] Formatting --- source/slang/slang-emit-spirv.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index 4628a8e3db0..6c3ef9d14cd 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -2061,7 +2061,8 @@ struct SPIRVEmitContext : public SourceEmitterBase, public SPIRVEmitSharedContex ? emitOpTypeArray(inst, elementType, irArrayType->getElementCount()) : emitOpTypeRuntimeArray(inst, elementType); // Arrays of opaque types should not emit a stride - if (!isIROpaqueType(elementType) && shouldEmitArrayStride(irArrayType->getElementType())) + if (!isIROpaqueType(elementType) && + shouldEmitArrayStride(irArrayType->getElementType())) { auto stride = 0; // If the array type has no stride, it indicates that this array type is only From 4e83dfe21a98723ad2622b74e4cb3f9c8ddbde86 Mon Sep 17 00:00:00 2001 From: Jerran Schmidt Date: Wed, 2 Jul 2025 10:57:54 +1000 Subject: [PATCH 4/4] Added test for fix --- .../constants/unsized-opaque-array.slang | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/language-feature/constants/unsized-opaque-array.slang diff --git a/tests/language-feature/constants/unsized-opaque-array.slang b/tests/language-feature/constants/unsized-opaque-array.slang new file mode 100644 index 00000000000..2aab695f667 --- /dev/null +++ b/tests/language-feature/constants/unsized-opaque-array.slang @@ -0,0 +1,14 @@ +//TEST:SIMPLE(filecheck=SPIRV): -target spirv -entry main -emit-spirv-directly + +// SPIRV-NOT: OpDecorate {{.*}} ArrayStride 8 +layout(binding = 0) +Texture2D textures[]; + +RWTexture2D tex; + +[shader("compute")] +[numthreads(1,1,1)] +void main(uint3 threadId : SV_DispatchThreadID) +{ + tex[threadId.xy] = uint(textures[0][threadId.xy].r); +} \ No newline at end of file