From 22b611e75d9a01e59468def2dce48ea2199a961f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nathan=20Gau=C3=ABr?= Date: Wed, 4 Mar 2026 18:20:39 +0100 Subject: [PATCH] [SPIR-V] Add parent map when handling var init ResourceHeap resource type is not know looking at the type, but at the context around it (we need to look how the variable is assigned). This means we need a parent map. When the heap is used in a function, the parentMap is set to the function scope, so everything is fine. But when a static variable is set, we are in the global scope, and the parent should be the current variable declaration. Fixes #8220 --- tools/clang/lib/SPIRV/SpirvEmitter.cpp | 2 ++ .../sm6_6.descriptorheap.global.lib.hlsl | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.global.lib.hlsl diff --git a/tools/clang/lib/SPIRV/SpirvEmitter.cpp b/tools/clang/lib/SPIRV/SpirvEmitter.cpp index ad4fe19238..c135a0080b 100644 --- a/tools/clang/lib/SPIRV/SpirvEmitter.cpp +++ b/tools/clang/lib/SPIRV/SpirvEmitter.cpp @@ -14740,11 +14740,13 @@ bool SpirvEmitter::emitEntryFunctionWrapperForRayTracing( const auto varInfo = declIdMapper.getDeclEvalInfo(varDecl, varDecl->getLocation()); if (const auto *init = varDecl->getInit()) { + parentMap = std::make_unique(const_cast(init)); storeValue(varInfo, loadIfGLValue(init), varDecl->getType(), init->getLocStart()); // Update counter variable associated with global variables tryToAssignCounterVar(varDecl, init); + parentMap.reset(nullptr); } // If not explicitly initialized, initialize with their zero values if not // resource objects diff --git a/tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.global.lib.hlsl b/tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.global.lib.hlsl new file mode 100644 index 0000000000..c9c074f212 --- /dev/null +++ b/tools/clang/test/CodeGenSPIRV/sm6_6.descriptorheap.global.lib.hlsl @@ -0,0 +1,27 @@ +// RUN: %dxc -T lib_6_8 -fspv-target-env=vulkan1.3 -Od -spirv %s | FileCheck %s + +// CHECK-DAG: OpCapability RuntimeDescriptorArray +// CHECK-DAG: OpExtension "SPV_EXT_descriptor_indexing" + +// CHECK-DAG: [[uint_00:%[_a-zA-Z0-9]+]] = OpConstantComposite %v2uint %uint_0 %uint_0 +// CHECK-DAG: [[float_0000:%[_a-zA-Z0-9]+]] = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0 +// CHECK-DAG: [[image_t:%[_a-zA-Z0-9]+]] = OpTypeImage %float 2D 2 0 0 2 Rgba32f +// CHECK-DAG: [[ptr_u_image_t:%[_a-zA-Z0-9]+]] = OpTypePointer UniformConstant [[image_t]] +// CHECK-DAG: [[ra_image_t:%[_a-zA-Z0-9]+]] = OpTypeRuntimeArray [[image_t]] +// CHECK-DAG: [[ptr_u_ra_image_t:%[_a-zA-Z0-9]+]] = OpTypePointer UniformConstant [[ra_image_t]] + +// CHECK-DAG: OpDecorate %ResourceDescriptorHeap DescriptorSet 0 +// CHECK-DAG: OpDecorate %ResourceDescriptorHeap Binding 0 + +// CHECK: %ResourceDescriptorHeap = OpVariable [[ptr_u_ra_image_t]] UniformConstant + +// CHECK: [[ptr:%[_a-zA-Z0-9]+]] = OpAccessChain [[ptr_u_image_t]] %ResourceDescriptorHeap %uint_0 +// CHECK: [[img:%[_a-zA-Z0-9]+]] = OpLoad %type_2d_image [[ptr]] +// CHECK: OpImageWrite [[img]] [[uint_00]] [[float_0000]] None + +static RWTexture2D OutputTexture = ResourceDescriptorHeap[0]; + +[shader("raygeneration")] +void RayGenMain() { + OutputTexture[uint2(0, 0)] = float4(0, 0, 0, 0); +}