Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d08306e
[SPIR-V] Add SPV_EXT_descriptor_heap + SPV_KHR_untyped_pointers codegen
jzakharovnv Jun 4, 2026
c89e022
Added OpConstantSizeOfEXT
jzakharovnv Jun 26, 2026
002e1ad
clang-format
jzakharovnv Jul 6, 2026
2e1c7fb
Shared max(image,buffer) stride for descriptor heap resource arrays
jzakharovnv Jul 10, 2026
1fe7543
Fixed correct StorageClass for descriptor heap buffer alias pointers …
jzakharovnv Jul 15, 2026
5f82d2c
Ammend keyword usage to follow LLVM Coding Standards
jzakharovnv Jul 22, 2026
9bd2e2d
fix stride test
jzakharovnv Jul 30, 2026
a5f3f45
Cover arrayStrideId uniquing in SpirvContextTest
jzakharovnv Jul 31, 2026
274b51e
Diagnose mixed bound/heap aliasing and correct documentation
jzakharovnv Jul 31, 2026
0066403
Diagnose heap buffer alias use in function calls + returns and suppor…
jzakharovnv Aug 13, 2026
58213f5
Remove useless scope block in tryToAssignDescriptorHeapImageAlias
jzakharovnv Aug 16, 2026
c82a49d
Remove dead append/consume guard in incDecRWACSBufferCounter
jzakharovnv Aug 16, 2026
3f5c342
Remove unreachable !decl guard in heap index codegen path
jzakharovnv Aug 16, 2026
9d74f7d
Propagate requestTargetEnv failure for desc heap
jzakharovnv Aug 16, 2026
478c57c
[SPIR-V] Add release note for SPV_EXT_descriptor_heap native lowering…
jzakharovnv Aug 16, 2026
e5f7a61
nit fixes
jzakharovnv Aug 16, 2026
1f5a41a
Fix tests and test formatting
jzakharovnv Aug 21, 2026
5121b4b
improved comments and convention compliance
jzakharovnv Sep 10, 2026
e182d99
corrected release notes
jzakharovnv Sep 11, 2026
70c4bb9
appease copilot
jzakharovnv Sep 17, 2026
bcae914
corrected release notes
jzakharovnv Sep 11, 2026
6169170
[SPIR-V] Add descriptor heap RaytracingAccelerationStructure support
jzakharovnv Jun 4, 2026
75c5684
removed erronous CLI options from test
jzakharovnv Jul 20, 2026
862b41a
Included AccelerationStructure in resource heap stride calculation
jzakharovnv Jul 22, 2026
56c6ea4
Added AS reassignment diagnostic, fixed tests and docs
jzakharovnv Aug 3, 2026
4aa8557
fix test
jzakharovnv Aug 21, 2026
172125e
improve comments and convention compliance
jzakharovnv Sep 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ The included licenses apply to the following files:

### Upcoming Release

#### SPIR-V

- Extended `SPV_EXT_descriptor_heap` lowering (`-fspv-use-descriptor-heap`) to
buffer-like resources (`StructuredBuffer`, `ByteAddressBuffer`,
`ConstantBuffer`, `TextureBuffer`, and RW variants) via
`SPV_KHR_untyped_pointers`. Requires `-fspv-target-env=vulkan1.3`.
- Extended `-fspv-use-descriptor-heap` to support `RaytracingAccelerationStructure`
loaded from `ResourceDescriptorHeap`.

Place release notes for the upcoming release below this line and remove this
line upon naming the release. Refer to previous for appropriate section names.

Expand Down
174 changes: 173 additions & 1 deletion docs/SPIR-V.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ Supported extensions
* SPV_KHR_float_controls
* SPV_NV_shader_subgroup_partitioned
* SPV_KHR_quad_control
* SPV_KHR_untyped_pointers
* SPV_EXT_descriptor_heap

Vulkan specific attributes
--------------------------
Expand Down Expand Up @@ -1993,10 +1995,14 @@ responsibility to provide proper numbers and avoid binding overlaps.
ResourceDescriptorHeaps & SamplerDescriptorHeaps
------------------------------------------------

The SPIR-V backend supported SM6.6 resource heaps, using 2 extensions:
By default, the SPIR-V backend supports SM6.6 resource heaps by emulating the
heaps with descriptor-indexing runtime arrays, using 2 extensions:

- `SPV_EXT_descriptor_indexing`
- `VK_EXT_mutable_descriptor_type`

This is also the behavior selected by ``-fspv-use-emulated-heap``.

Each type loaded from a heap is considered to be an unbounded RuntimeArray
bound to the descriptor set 0.

Expand Down Expand Up @@ -2074,6 +2080,172 @@ Bindings & sets associated with each heap can be explicitly set using:
- `-fvk-bind-counter-heap <binding> <set>`: Specify Vulkan binding number
and set number for the counter heap.

Native descriptor heap extension lowering
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When ``-fspv-use-descriptor-heap`` is specified, DXC lowers
``ResourceDescriptorHeap`` and ``SamplerDescriptorHeap`` through
``SPV_EXT_descriptor_heap`` instead of the default emulated heap path. This
also requires ``SPV_KHR_untyped_pointers`` and ``-fspv-target-env=vulkan1.3``
(targeting a lower environment is an error), and a SPIRV-Headers / SPIRV-Tools
build that defines these extensions. The emitted module declares the heap
objects as untyped variables in ``UniformConstant`` storage class:

.. code:: spirv

%uptr_uc = OpTypeUntypedPointerKHR UniformConstant
%resource_heap = OpUntypedVariableKHR %uptr_uc UniformConstant
%sampler_heap = OpUntypedVariableKHR %uptr_uc UniformConstant
OpDecorate %resource_heap BuiltIn ResourceHeapEXT
OpDecorate %sampler_heap BuiltIn SamplerHeapEXT

The concrete descriptor type is selected at each heap access. For image,
sampler, and texel buffer resources, DXC forms a runtime array of that
descriptor type, decorates the array with a byte stride, and uses
``OpUntypedAccessChainKHR`` followed by ``OpLoad``. The stride is an
``ArrayStrideIdEXT`` decoration referencing a specialization constant rather
than a literal ``ArrayStride`` (see `Descriptor heap array stride`_ below):

.. code:: spirv

%image_type = OpTypeImage %float 2D 2 0 0 1 Unknown
%image_array = OpTypeRuntimeArray %image_type
OpDecorateId %image_array ArrayStrideIdEXT %resource_stride
%descriptor = OpUntypedAccessChainKHR %uptr_uc %image_array %resource_heap %index
%image = OpLoad %image_type %descriptor

For buffer-like resources, DXC uses ``OpTypeBufferEXT`` as the descriptor type
and ``OpBufferPointerEXT`` to recover the pointer to the buffer data. The
descriptor storage class matches the recovered buffer pointer storage class; for
example, ``ConstantBuffer<T>`` uses ``Uniform`` and ``TextureBuffer<T>`` uses
``StorageBuffer``:

.. code:: spirv

%buffer_type = OpTypeBufferEXT Uniform
%buffer_array = OpTypeRuntimeArray %buffer_type
OpDecorateId %buffer_array ArrayStrideIdEXT %resource_stride
%descriptor = OpUntypedAccessChainKHR %uptr_uc %buffer_array %resource_heap %index
%buffer_ptr = OpBufferPointerEXT %_ptr_Uniform_type_BufferData %descriptor

For ``RWTexture`` resources loaded from ``ResourceDescriptorHeap``, interlocked
operations that need a texel pointer use ``OpUntypedImageTexelPointerEXT``.
The image descriptor pointer produced by ``OpUntypedAccessChainKHR`` is passed
directly to the texel-pointer instruction instead of first storing the image
handle into a function-scope image variable:

.. code:: spirv

%image_type = OpTypeImage %uint 2D 2 0 0 2 R32ui
%image_array = OpTypeRuntimeArray %image_type
%descriptor = OpUntypedAccessChainKHR %uptr_uc %image_array %resource_heap %index
%uptr_image = OpTypeUntypedPointerKHR Image
%texel_ptr = OpUntypedImageTexelPointerEXT %uptr_image %image_type %descriptor %coord %sample
%old = OpAtomicIAdd %uint %texel_ptr %scope %semantics %value

``RaytracingAccelerationStructure`` resources loaded from
``ResourceDescriptorHeap`` follow the same access-chain-then-load shape over a
runtime array of ``OpTypeAccelerationStructureKHR``:

.. code:: spirv

%accel_type = OpTypeAccelerationStructureKHR
%accel_array = OpTypeRuntimeArray %accel_type
OpDecorateId %accel_array ArrayStrideIdEXT %resource_stride
%descriptor = OpUntypedAccessChainKHR %uptr_uc %accel_array %resource_heap %index
%accel = OpLoad %accel_type %descriptor

This path supports texture, RWTexture, sampler, Buffer/RWBuffer,
StructuredBuffer/RWStructuredBuffer without associated counter operations,
ByteAddressBuffer/RWByteAddressBuffer, ConstantBuffer, TextureBuffer, and
``RaytracingAccelerationStructure`` heap loads, including direct field and
array-element accesses for ``ConstantBuffer<T>`` and ``TextureBuffer<T>``.
Acceleration structure loads are additionally subject to the stride requirement
described in `Descriptor heap array stride`_ below.
``NonUniformResourceIndex`` is accepted, but no ``NonUniform`` decoration is
emitted on the ``OpUntypedAccessChainKHR`` result or on the loaded descriptor;
``SPV_EXT_descriptor_heap`` deprecates the decoration for heap accesses and
drivers handle divergent heap indices natively. The index operand itself may
still carry ``NonUniform`` from the surrounding expression.

Append/consume structured buffers and UAV counter heap lowering are not
supported by the native descriptor heap path yet. Those forms should continue
to use the default emulated heap lowering, or DXC will emit a diagnostic for
unsupported append/consume structured-buffer heap loads. Heap-loaded
``RWStructuredBuffer`` resources are supported for ordinary data access, but
associated counter operations such as ``IncrementCounter`` and
``DecrementCounter`` emit a diagnostic because the native descriptor heap path
does not recover an associated counter descriptor.

A local resource variable initialized from a heap access is resolved entirely at
compile time: the variable is recorded as an alias for the heap index, and every
later use is re-lowered as a fresh access chain rather than as a load of a stored
descriptor handle. This is sound only when the variable holds a heap descriptor
on every path that reaches the use. DXC therefore rejects a variable that holds
Comment thread
jzakharovnv marked this conversation as resolved.
both a bound resource and a heap descriptor, whether through a conditional
assignment, a reassignment back to a bound resource, or an assignment inside a
loop::

error: mixing bound and descriptor heap resources in the same variable is not
supported with SPV_EXT_descriptor_heap

Supporting these forms requires modelling the alias as a value with real
control-flow merges instead of as compile-time state.

``StructuredBuffer``, ``ByteAddressBuffer``, and related buffer-type aliases
cannot yet be passed to or returned from user functions; access the resource
directly at each call or return site.

Two further restrictions on the heap access expression itself produce
diagnostics. The object being subscripted must be a direct reference to the
builtin ``ResourceDescriptorHeap`` or ``SamplerDescriptorHeap`` variable; the
subscript result must be immediately converted to a concrete resource type so
that DXC can select a descriptor type for the access, so a subscript whose
result is discarded or used in a context that supplies no target resource type
is rejected; and a local ``RaytracingAccelerationStructure`` must be initialized
from a loadable heap access, since the alias has no backing descriptor
otherwise.

Descriptor heap array stride
++++++++++++++++++++++++++++

All resource heap runtime arrays share a single ``ArrayStrideIdEXT`` decoration
rather than a literal ``ArrayStride``, because descriptor sizes are not known
until pipeline creation. The shared value is built from ``OpConstantSizeOfEXT``
and ``OpSpecConstantOp`` and evaluates to
``max(sizeof(image_descriptor), sizeof(buffer_descriptor))``. The sampler heap
carries its own ``ArrayStrideIdEXT`` equal to ``sizeof(sampler_descriptor)``,
regardless of resource heap contents.

The ``OpConstantSizeOfEXT`` operands are placeholder types chosen only for their
descriptor class. All image types report the same descriptor size, so the
placeholder is a plain sampled 2D float image and bears no relation to the image
types the shader actually uses; a module will normally contain both the
placeholder type and the distinct image types its heap accesses lower to. The
stride value is built once and cached on first use.

When acceleration structure descriptors may appear on the resource heap, the
formula expands to a three-way max
``max(max(sizeof(image_descriptor), sizeof(buffer_descriptor)), sizeof(acceleration_structure))``.

Because the stride is cached on first use, this decision is committed before
code generation and is **not** based on whether the shader actually performs an
acceleration structure heap load: a ray-tracing shader that only heap-loads a
texture still gets the three-way max. The widening happens when either

- any entry point is a ray-tracing stage, or
- the user explicitly passed ``-fspv-extension=SPV_KHR_ray_tracing``,
``-fspv-extension=SPV_NV_ray_tracing``, or
``-fspv-extension=SPV_KHR_ray_query``.

The second condition requires an explicit ``-fspv-extension`` flag. In the
default extension mode DXC allows the ray-tracing and ray-query extensions
implicitly, but that does not widen the stride. Because the stride cannot be
widened once it has been built, a shader that is not a ray-tracing stage and
loads a ``RaytracingAccelerationStructure`` from ``ResourceDescriptorHeap``
without an explicit ray extension flag is rejected rather than given a stride
that may be too narrow.

HLSL Expressions
================

Expand Down
4 changes: 4 additions & 0 deletions tools/clang/include/clang/SPIRV/AstTypeProbe.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ bool isBuffer(QualType type);
/// \brief Returns true if the given type is the HLSL RWBuffer type.
bool isRWBuffer(QualType type);

/// \brief Returns true if the given type is the HLSL
/// RaytracingAccelerationStructure type.
bool isRaytracingAccelerationStructure(QualType type);

/// \brief Returns true if the given type is an HLSL Texture type.
bool isTexture(QualType);

Expand Down
54 changes: 53 additions & 1 deletion tools/clang/include/clang/SPIRV/SpirvBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class SpirvBuilder {
/// \brief Creates an OpUntypedImageTexelPointerEXT SPIR-V instruction with
/// the given parameters.
SpirvUntypedImageTexelPointerEXT *createUntypedImageTexelPointerEXT(
QualType resultType, SpirvInstruction *image,
QualType resultType, const SpirvType *imageType, SpirvInstruction *image,
SpirvInstruction *coordinate, SpirvInstruction *sample, SourceLocation);

/// \brief Creates an OpConverPtrToU SPIR-V instruction with the given
Expand Down Expand Up @@ -831,6 +831,43 @@ class SpirvBuilder {
bool specConst = false);
SpirvConstant *getConstantNull(QualType);
SpirvConstant *getConstantString(llvm::StringRef str, bool specConst = false);

/// \brief Returns the OpConstantSizeOfEXT (SPV_EXT_descriptor_heap) for
/// descriptor operandType: client-API size in bytes as a 32-bit unsigned
/// integer. Cached per operand type.
SpirvConstant *getConstantSizeOfEXT(const SpirvType *operandType);

SpirvSpecConstantTernaryOp *
createSpecConstantTernaryOp(spv::Op op, QualType resultType,
SpirvInstruction *op1, SpirvInstruction *op2,
SpirvInstruction *op3, SourceLocation loc);

/// \brief Record that acceleration structures may occupy the resource heap.
/// Must be called before getResourceHeapArrayStride() (before the code-gen
/// loop in HandleTranslationUnit); the result is cached on the first call.
/// Calling it after the first getResourceHeapArrayStride() invocation has
/// no effect.
void noteResourceHeapHasAccelStruct() { resourceHeapHasAccelStruct = true; }

/// \brief Returns whether the resource-heap stride includes the acceleration
/// structure descriptor size. When false, code-gen must reject AS heap
/// access; the stride is frozen after getResourceHeapArrayStride()'s first
/// call.
bool resourceHeapStrideIncludesAccelStruct() const {
return resourceHeapHasAccelStruct;
}

/// \brief Shared ArrayStrideIdEXT for resource-heap runtime arrays.
/// Default: max(sizeof(image), sizeof(buffer))
/// With RT: max(sizeof(image), sizeof(buffer), sizeof(accel_struct))
/// Via OpSpecConstantOp; cached per module.
/// Precondition: call noteResourceHeapHasAccelStruct() before first call if
/// AS present.
SpirvInstruction *getResourceHeapArrayStride();

/// \brief Shared ArrayStrideIdEXT operand for sampler-heap runtime arrays:
/// the sampler descriptor size. Cached per module.
SpirvInstruction *getSamplerHeapArrayStride();
SpirvUndef *getUndef(QualType);

SpirvString *createString(llvm::StringRef str);
Expand Down Expand Up @@ -947,6 +984,21 @@ class SpirvBuilder {
/// Used as caches for all created builtin variables to avoid duplication.
llvm::SmallVector<BuiltInVarInfo, 16> builtinVars;

/// Cache of OpConstantSizeOfEXT instructions keyed on the descriptor operand
/// type; each distinct descriptor type produces at most one constant.
llvm::DenseMap<const SpirvType *, SpirvConstant *> constantSizeOfEXTMap;

/// Cached shared descriptor-heap array strides, each emitted once per
/// module (see get{Resource,Sampler}HeapArrayStride).
SpirvInstruction *resourceHeapArrayStride = nullptr;
SpirvInstruction *samplerHeapArrayStride = nullptr;

/// Set by noteResourceHeapHasAccelStruct() when HandleTranslationUnit
/// detects shader using ray-tracing features.
/// When true, getResourceHeapArrayStride() includes
/// sizeof(acceleration_structure). Set by noteResourceHeapHasAccelStruct().
bool resourceHeapHasAccelStruct = false;

SpirvDebugInfoNone *debugNone;

/// DebugExpression that does not reference any DebugOperation
Expand Down
10 changes: 8 additions & 2 deletions tools/clang/include/clang/SPIRV/SpirvContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ struct RuntimeArrayTypeMapInfo {
static inline RuntimeArrayType *getTombstoneKey() { return nullptr; }
static unsigned getHashValue(const RuntimeArrayType *Val) {
return llvm::hash_combine(Val->getElementType(),
Val->getStride().hasValue());
Val->getStride().hasValue(),
Val->getArrayStrideId());
}
static bool isEqual(const RuntimeArrayType *LHS,
const RuntimeArrayType *RHS) {
Expand Down Expand Up @@ -284,7 +285,8 @@ class SpirvContext {
llvm::Optional<uint32_t> arrayStride);
const RuntimeArrayType *
getRuntimeArrayType(const SpirvType *elemType,
llvm::Optional<uint32_t> arrayStride);
llvm::Optional<uint32_t> arrayStride,
SpirvInstruction *arrayStrideId = nullptr);
const NodePayloadArrayType *
getNodePayloadArrayType(const SpirvType *elemType,
const ParmVarDecl *nodeDecl);
Expand All @@ -298,6 +300,7 @@ class SpirvContext {
spv::StorageClass);

const UntypedPointerKHRType *getUntypedPointerKHRType(spv::StorageClass sc);
const BufferEXTType *getBufferEXTType(spv::StorageClass sc);

FunctionType *getFunctionType(const SpirvType *ret,
llvm::ArrayRef<const SpirvType *> param);
Expand Down Expand Up @@ -536,6 +539,9 @@ class SpirvContext {
llvm::DenseMap<spv::StorageClass, const UntypedPointerKHRType *,
StorageClassDenseMapInfo>
untypedPointerKHRTypes;
llvm::DenseMap<spv::StorageClass, const BufferEXTType *,
StorageClassDenseMapInfo>
bufferEXTTypes;
llvm::MapVector<QualType, const ForwardPointerType *> forwardPointerTypes;
llvm::MapVector<QualType, const SpirvPointerType *> forwardReferences;
llvm::DenseSet<FunctionType *, FunctionTypeMapInfo> functionTypes;
Expand Down
Loading