From b98d915efbf7fafaa7419b2f2136c48ba98bf620 Mon Sep 17 00:00:00 2001 From: Brendan Duncan Date: Thu, 9 Jul 2026 08:48:46 -0600 Subject: [PATCH 1/3] fix 8601 check for RayQuery user type --- tools/clang/lib/SPIRV/AstTypeProbe.cpp | 10 ++++-- tools/clang/lib/SPIRV/LowerTypeVisitor.cpp | 6 +++- .../type.rayquery.user-defined-shadow.hlsl | 35 +++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tools/clang/test/CodeGenSPIRV/type.rayquery.user-defined-shadow.hlsl diff --git a/tools/clang/lib/SPIRV/AstTypeProbe.cpp b/tools/clang/lib/SPIRV/AstTypeProbe.cpp index c933a82c16..b8e0ed98b3 100644 --- a/tools/clang/lib/SPIRV/AstTypeProbe.cpp +++ b/tools/clang/lib/SPIRV/AstTypeProbe.cpp @@ -1141,12 +1141,16 @@ bool isOpaqueType(QualType type) { if (name == "RaytracingAccelerationStructure") return true; - if (name == "RayQuery") - return true; - if (name == "SubpassInput") return true; } + + // Use IsHLSLRayQueryType() rather than a plain name comparison so a + // user-defined struct or class named "RayQuery" that shadows the reserved + // name is not mistaken for the opaque ray query type. + if (hlsl::IsHLSLRayQueryType(type)) + return true; + return false; } diff --git a/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp b/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp index 4c06cd4113..60fdf96523 100644 --- a/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp +++ b/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp @@ -965,7 +965,11 @@ LowerTypeVisitor::lowerResourceType(QualType type, SpirvLayoutRule rule, return spvContext.getAccelerationStructureTypeNV(); } - if (name == "RayQuery") + // Use IsHLSLRayQueryType() rather than a plain name comparison: a + // user-defined struct or class named "RayQuery" is allowed to shadow the + // reserved name and must be lowered as an ordinary struct instead of the + // opaque ray query type. + if (hlsl::IsHLSLRayQueryType(type)) return spvContext.getRayQueryTypeKHR(); if (name == "StructuredBuffer" || name == "RWStructuredBuffer" || diff --git a/tools/clang/test/CodeGenSPIRV/type.rayquery.user-defined-shadow.hlsl b/tools/clang/test/CodeGenSPIRV/type.rayquery.user-defined-shadow.hlsl new file mode 100644 index 0000000000..eddf73a31b --- /dev/null +++ b/tools/clang/test/CodeGenSPIRV/type.rayquery.user-defined-shadow.hlsl @@ -0,0 +1,35 @@ +// RUN: %dxc -T cs_6_6 -E MainRayGenShader -fcgl -spirv %s | FileCheck %s + +// A user-defined struct named "RayQuery" that shadows the reserved intrinsic +// name must be lowered as an ordinary struct, not as the opaque ray query +// type. Previously this crashed the SPIR-V backend. +// See https://github.com/microsoft/DirectXShaderCompiler/issues/8601 + +namespace UnifiedRT { +struct RayQuery { + float4 foo; +}; +} // namespace UnifiedRT + +// The shadowing struct is lowered to an ordinary struct with a float4 field, +// and the local variable uses that struct type in the Function storage class. +// CHECK: %RayQuery = OpTypeStruct %v4float +// CHECK: %_ptr_Function_RayQuery = OpTypePointer Function %RayQuery +// CHECK: %rayQuery = OpVariable %_ptr_Function_RayQuery Function + +// It must not be lowered to the opaque ray query type. +// CHECK-NOT: OpTypeRayQueryKHR + +StructuredBuffer _UnifiedRT_DispatchDims; + +[numthreads(128, 1, 1)] +void MainRayGenShader(in uint3 gidx : SV_DispatchThreadID, + in uint lidx : SV_GroupIndex) { + if (gidx.x >= _UnifiedRT_DispatchDims[0] || + gidx.y >= _UnifiedRT_DispatchDims[1] || + gidx.z >= _UnifiedRT_DispatchDims[2]) + return; + + UnifiedRT::RayQuery rayQuery; + rayQuery.foo = float4(1.0, 1.0, 1.0, 1.0); +} From 85ff36f9e4aaedad94604dc700eefb88e5705f1c Mon Sep 17 00:00:00 2001 From: Brendan Duncan Date: Thu, 9 Jul 2026 09:54:01 -0600 Subject: [PATCH 2/3] Update tools/clang/lib/SPIRV/AstTypeProbe.cpp Co-authored-by: Chris B --- tools/clang/lib/SPIRV/AstTypeProbe.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/tools/clang/lib/SPIRV/AstTypeProbe.cpp b/tools/clang/lib/SPIRV/AstTypeProbe.cpp index b8e0ed98b3..a871bd3577 100644 --- a/tools/clang/lib/SPIRV/AstTypeProbe.cpp +++ b/tools/clang/lib/SPIRV/AstTypeProbe.cpp @@ -1145,9 +1145,6 @@ bool isOpaqueType(QualType type) { return true; } - // Use IsHLSLRayQueryType() rather than a plain name comparison so a - // user-defined struct or class named "RayQuery" that shadows the reserved - // name is not mistaken for the opaque ray query type. if (hlsl::IsHLSLRayQueryType(type)) return true; From 99f140c1c08fb9ec0645c491d40c20e7df13d9aa Mon Sep 17 00:00:00 2001 From: Brendan Duncan Date: Thu, 9 Jul 2026 09:54:12 -0600 Subject: [PATCH 3/3] Update tools/clang/lib/SPIRV/LowerTypeVisitor.cpp Co-authored-by: Chris B --- tools/clang/lib/SPIRV/LowerTypeVisitor.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp b/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp index 60fdf96523..b330d40d85 100644 --- a/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp +++ b/tools/clang/lib/SPIRV/LowerTypeVisitor.cpp @@ -965,10 +965,6 @@ LowerTypeVisitor::lowerResourceType(QualType type, SpirvLayoutRule rule, return spvContext.getAccelerationStructureTypeNV(); } - // Use IsHLSLRayQueryType() rather than a plain name comparison: a - // user-defined struct or class named "RayQuery" is allowed to shadow the - // reserved name and must be lowered as an ordinary struct instead of the - // opaque ray query type. if (hlsl::IsHLSLRayQueryType(type)) return spvContext.getRayQueryTypeKHR();