[clang] [llvm] [HLSL][SPIRV] Reject textures and typed buffers with 16-bit or double elements on Vulkan (PR #243566)
Deric C. via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 17:52:45 PST 2026
https://github.com/Icohedron updated https://github.com/llvm/llvm-project/pull/243566
>From 3c264cac4c415c076b842dd85de81f1cb6be1db5 Mon Sep 17 00:00:00 2001
From: Deric Cheung <cheung.deric at gmail.com>
Date: Fri, 6 Nov 2026 00:03:04 +0000
Subject: [PATCH 1/4] [HLSL] Reject textures and typed buffers with 16-bit or
double elements on Vulkan
Vulkan images can only hold 32-bit types and 64-bit integers
(VUID-StandaloneSpirv-OpTypeImage-04656), but Clang accepted 16-bit and
double typed resources on SPIR-V and emitted invalid SPIR-V.
Diagnose every variable and parameter declaration of such a resource in
Sema, including unused, instantiated and struct-embedded ones. half
without native 16-bit types is 32-bit and stays legal.
Assisted by: Claude Opus 5.5
---
clang/docs/HLSL/ResourceTypes.md | 26 ++++
.../clang/Basic/DiagnosticSemaKinds.td | 4 +
clang/include/clang/Sema/SemaHLSL.h | 1 +
clang/lib/Sema/SemaDecl.cpp | 6 +
clang/lib/Sema/SemaHLSL.cpp | 54 ++++++++
.../resources/RWBuffer-imageformat.hlsl | 8 --
.../resources/TypedBuffers-constructor.hlsl | 26 ++--
.../resources/TypedBuffers-elementtype.hlsl | 76 +++++------
.../resources/res-array-global.hlsl | 10 +-
.../Textures-integer-element-type.hlsl | 7 +-
.../TypedResources-element-type-Vulkan.hlsl | 126 ++++++++++++++++++
11 files changed, 276 insertions(+), 68 deletions(-)
create mode 100644 clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl
diff --git a/clang/docs/HLSL/ResourceTypes.md b/clang/docs/HLSL/ResourceTypes.md
index 86898b093fa650..102ab498149f82 100644
--- a/clang/docs/HLSL/ResourceTypes.md
+++ b/clang/docs/HLSL/ResourceTypes.md
@@ -48,8 +48,34 @@ IR. These types are target specific and differ between DXIL and SPIR-V
generation, providing the necessary information for the targets to generate
binding metadata for their respective target runtimes.
+## Vulkan Element Types
+
+Textures and typed buffers lower to SPIR-V images on Vulkan. The
+scalar element type, or vector component type, of an image must
+be a 32-bit floating-point type or a 32-bit or 64-bit integer type
+([VUID-StandaloneSpirv-OpTypeImage-04656][vulkan-image-sampled-type]).
+
+When targeting SPIR-V, Clang reports an error for each variable or parameter
+declaration of a typed resource whose element type breaks this rule, whether
+or not the shader uses it. This covers global, `static` and local variables,
+function parameters, arrays of resources, declarations in instantiated
+templates, and resources embedded in structs, which are diagnosed at their
+field. A parameter of a function template is checked while its template
+arguments are deduced, so the error makes that candidate non-viable. Native
+16-bit types and `double` are rejected. Without native 16-bit types, `half`
+is a 32-bit type and stays valid. A typed resource is any SRV or UAV resource
+whose handle has a contained type and is not a raw buffer, so structured and
+byte-address buffers are not affected.
+
+The rule concerns the image's Sampled Type, not the precision of the
+texel format: an `R16f` image uses a 32-bit floating-point Sampled
+Type, as specified by Vulkan's [image format and type matching
+rules][vulkan-image-format-type-matching].
+
[constantbuffer]: https://learn.microsoft.com/en-us/windows/win32/direct3d12/resource-binding-in-hlsl#constant-buffers
[rasterizerorderedtexture2d]: https://learn.microsoft.com/en-us/windows/win32/direct3d11/rasterizer-order-views
[rwbyteaddressbuffer]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-rwbyteaddressbuffer
[texture3d]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture3d
+[vulkan-image-sampled-type]: https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-StandaloneSpirv-OpTypeImage-04656
+[vulkan-image-format-type-matching]: https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#spirvenv-format-type-matching
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 4f274b922368b8..13b4899d032636 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -14115,6 +14115,10 @@ def err_hlsl_samplecmp_requires_float
"a floating point type">;
def err_hlsl_gathercmp_invalid_component
: Error<"gatherCmp%select{Red|Green|Blue|Alpha}0 operations on the Vulkan target are not supported; only GatherCmp and GatherCmpRed are allowed">;
+def err_hlsl_vulkan_resource_element_type
+ : Error<"resource type %0 with element type %1 is not supported when "
+ "targeting Vulkan; %1 is %2 bits wide, but Vulkan requires a "
+ "32-bit type or a 64-bit integer type">;
def err_hlsl_resource_member_array_access_not_constant
: Error<"index for struct array inside cbuffer that contains resources must be a constant integer expression">;
diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h
index 20b52737fd5d3d..0ef33bd9a0f12c 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -133,6 +133,7 @@ class SemaHLSL : public SemaBase {
void ActOnTopLevelFunction(FunctionDecl *FD);
void ActOnVariableDeclarator(VarDecl *VD);
bool ActOnUninitializedVarDecl(VarDecl *D);
+ void DiagnoseVulkanResourceElementTypes(const VarDecl *VD);
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU);
bool ActOnResourceMemberAccessExpr(MemberExpr *ME);
void CheckEntryPoint(FunctionDecl *FD);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 63c581d3fb4f34..e6a1895f220f12 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -9389,6 +9389,9 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
}
}
+ if (getLangOpts().HLSL)
+ HLSL().DiagnoseVulkanResourceElementTypes(NewVD);
+
if (T.hasAddressSpace() &&
!CheckVarDeclSizeAddressSpace(NewVD, T.getAddressSpace())) {
NewVD->setInvalidDecl();
@@ -16272,6 +16275,9 @@ ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc,
New->setInvalidDecl();
}
+ if (getLangOpts().HLSL)
+ HLSL().DiagnoseVulkanResourceElementTypes(New);
+
return New;
}
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 95bfd6bbc6ed2b..b4449c60aec95b 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -6449,6 +6449,60 @@ void SemaHLSL::handleGlobalStructOrArrayOfWithResources(VarDecl *VD) {
}
}
+// Vulkan requires the Sampled Type of every image, which each typed resource
+// lowers to, to be a 32-bit float or a 32-bit or 64-bit integer
+// (VUID-StandaloneSpirv-OpTypeImage-04656). Diagnoses each typed resource that
+// the variable or parameter VD declares, or embeds in a struct, at the
+// declaration or at the field.
+void SemaHLSL::DiagnoseVulkanResourceElementTypes(const VarDecl *VD) {
+ ASTContext &Ctx = getASTContext();
+ if (!Ctx.getTargetInfo().getTriple().isSPIRV() ||
+ VD->getType()->isDependentType() ||
+ !SemaRef.isCompleteType(VD->getLocation(),
+ Ctx.getBaseElementType(VD->getType())))
+ return;
+
+ // The methods of the built-in resource types declare locals and parameters
+ // of their own resource type without a source location. Skip them: the
+ // user's declaration that instantiated the method is diagnosed instead.
+ if (VD->getLocation().isInvalid())
+ return;
+
+ SmallVector<std::pair<QualType, SourceLocation>, 4> Worklist = {
+ {VD->getType(), VD->getLocation()}};
+ while (!Worklist.empty()) {
+ auto [Ty, Loc] = Worklist.pop_back_val();
+ Ty = Ctx.getBaseElementType(Ty);
+ if (!Ty->isHLSLIntangibleType())
+ continue;
+
+ const HLSLAttributedResourceType *HandleTy =
+ HLSLAttributedResourceType::findHandleTypeOnResource(Ty.getTypePtr());
+ if (!HandleTy) {
+ if (const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl()) {
+ for (const CXXBaseSpecifier &Base : RD->bases())
+ Worklist.emplace_back(Base.getType(), Loc);
+ for (const FieldDecl *FD : RD->fields())
+ Worklist.emplace_back(FD->getType(), FD->getLocation());
+ }
+ continue;
+ }
+
+ ResourceClass RC = HandleTy->getAttrs().ResourceClass;
+ if (HandleTy->isRaw() || !HandleTy->hasContainedType() ||
+ (RC != ResourceClass::SRV && RC != ResourceClass::UAV))
+ continue;
+ QualType EltTy =
+ getElementTypeOf(HandleTy->getContainedType(), /*IncludeMatrix=*/false);
+ if (!EltTy->isArithmeticType())
+ continue;
+ uint64_t Size = Ctx.getTypeSize(EltTy);
+ if (Size != 32 && !(Size == 64 && EltTy->isIntegerType()))
+ Diag(Loc, diag::err_hlsl_vulkan_resource_element_type)
+ << Ty << EltTy << Size;
+ }
+}
+
void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
if (VD->hasGlobalStorage()) {
// make sure the declaration has a complete type
diff --git a/clang/test/CodeGenHLSL/resources/RWBuffer-imageformat.hlsl b/clang/test/CodeGenHLSL/resources/RWBuffer-imageformat.hlsl
index 6b967bb1e3e3a5..0d10c729a1e69e 100644
--- a/clang/test/CodeGenHLSL/resources/RWBuffer-imageformat.hlsl
+++ b/clang/test/CodeGenHLSL/resources/RWBuffer-imageformat.hlsl
@@ -40,12 +40,6 @@ RWBuffer<float3> rwb_float3;
// CHECK: %"class.hlsl::RWBuffer.14" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 1) }
RWBuffer<float4> rwb_float4;
-// Other types that should get Unknown format
-// CHECK: %"class.hlsl::RWBuffer.15" = type { target("spirv.Image", half, 5, 2, 0, 0, 2, 0) }
-RWBuffer<half> rwb_half;
-// CHECK: %"class.hlsl::RWBuffer.16" = type { target("spirv.Image", double, 5, 2, 0, 0, 2, 0) }
-RWBuffer<double> rwb_double;
-
// Non-UAV resource
// CHECK: %"class.hlsl::Buffer" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 1, 0) }
Buffer<int> b_int;
@@ -68,7 +62,5 @@ void main(int GI : SV_GroupIndex) {
rwb_float2[GI] = 0;
rwb_float3[GI] = 0;
rwb_float4[GI] = 0;
- rwb_half[GI] = 0;
- rwb_double[GI] = 0;
int val = b_int[GI];
}
diff --git a/clang/test/CodeGenHLSL/resources/TypedBuffers-constructor.hlsl b/clang/test/CodeGenHLSL/resources/TypedBuffers-constructor.hlsl
index b8888e50b5db61..85485fa7357ec1 100644
--- a/clang/test/CodeGenHLSL/resources/TypedBuffers-constructor.hlsl
+++ b/clang/test/CodeGenHLSL/resources/TypedBuffers-constructor.hlsl
@@ -13,7 +13,7 @@
RWBuffer<float> Buf1 : register(u5, space3);
// Resource with implicit binding
-Buffer<double> Buf2;
+Buffer<unsigned int> Buf2;
export void foo() {
// Local resource declaration
@@ -21,10 +21,10 @@ export void foo() {
}
// CHECK-DXIL: %"class.hlsl::RWBuffer" = type { target("dx.TypedBuffer", float, 1, 0, 0) }
-// CHECK-DXIL: %"class.hlsl::Buffer" = type { target("dx.TypedBuffer", double, 0, 0, 0) }
+// CHECK-DXIL: %"class.hlsl::Buffer" = type { target("dx.TypedBuffer", i32, 0, 0, 0) }
// CHECK-DXIL: %"class.hlsl::RWBuffer.0" = type { target("dx.TypedBuffer", i32, 1, 0, 1) }
// CHECK-SPIRV: %"class.hlsl::RWBuffer" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 3) }
-// CHECK-SPIRV: %"class.hlsl::Buffer" = type { target("spirv.Image", double, 5, 2, 0, 0, 1, 0) }
+// CHECK-SPIRV: %"class.hlsl::Buffer" = type { target("spirv.Image", i32, 5, 2, 0, 0, 1, 0) }
// CHECK-SPIRV: %"class.hlsl::RWBuffer.0" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 24) }
// CHECK-DXIL: @Buf1 = internal global %"class.hlsl::RWBuffer" poison, align 4
@@ -64,24 +64,24 @@ export void foo() {
// CHECK-SPIRV: define internal spir_func void @__cxx_global_var_init.1()
// CHECK-NEXT: entry:
// CHECK-NEXT: %[[#C_ENTRY:]] = call token @llvm.experimental.convergence.entry()
-// CHECK-NEXT: call {{(spir_func )?}}void @hlsl::Buffer<double>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
+// CHECK-NEXT: call {{(spir_func )?}}void @hlsl::Buffer<unsigned int>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
// CHECK-SAME: (ptr {{.*}} @Buf2, i32 noundef 0, i32 noundef 0, i32 noundef 1, i32 noundef 0, ptr noundef @[[Buf2Str]])
-// Buf2 initialization part 2 - body of Buffer<double>::__createFromImplicitBinding call
-// CHECK: define linkonce_odr hidden {{(spir_func )?}}void @hlsl::Buffer<double>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
+// Buf2 initialization part 2 - body of Buffer<unsigned int>::__createFromImplicitBinding call
+// CHECK: define linkonce_odr hidden {{(spir_func )?}}void @hlsl::Buffer<unsigned int>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
// CHECK-DXIL-SAME: (ptr {{.*}} sret(%"class.hlsl::Buffer") align 4 %[[RetValue2:.*]], i32 noundef %orderId,
// CHECK-SPIRV-SAME: (ptr {{.*}} sret(%"class.hlsl::Buffer") align 4 %[[RetValue2:.*]], i32 noundef %orderId,
// CHECK-SAME: i32 noundef %spaceNo, i32 noundef %range, i32 noundef %index, ptr noundef %name)
// CHECK-DXIL: %[[Tmp2:.*]] = alloca %"class.hlsl::Buffer", align 4
// CHECK-SPIRV: %[[Tmp2:.*]] = alloca %"class.hlsl::Buffer", align 4
-// CHECK-DXIL: %[[Handle2:.*]] = call target("dx.TypedBuffer", double, 0, 0, 0)
-// CHECK-DXIL-SAME: @llvm.dx.resource.handlefromimplicitbinding.tdx.TypedBuffer_f64_0_0_0t(
-// CHECK-SPIRV: %[[Handle2:.*]] = call target("spirv.Image", double, 5, 2, 0, 0, 1, 0)
-// CHECK-SPIRV-SAME: @llvm.spv.resource.handlefromimplicitbinding.tspirv.Image_f64_5_2_0_0_1_0t(
+// CHECK-DXIL: %[[Handle2:.*]] = call target("dx.TypedBuffer", i32, 0, 0, 0)
+// CHECK-DXIL-SAME: @llvm.dx.resource.handlefromimplicitbinding.tdx.TypedBuffer_i32_0_0_0t(
+// CHECK-SPIRV: %[[Handle2:.*]] = call target("spirv.Image", i32, 5, 2, 0, 0, 1, 0)
+// CHECK-SPIRV-SAME: @llvm.spv.resource.handlefromimplicitbinding.tspirv.Image_i32_5_2_0_0_1_0t(
// CHECK: %__handle = getelementptr inbounds nuw %"class.hlsl::Buffer", ptr %[[Tmp2]], i32 0, i32 0
-// CHECK-DXIL: store target("dx.TypedBuffer", double, 0, 0, 0) %[[Handle2]], ptr %__handle, align 4
-// CHECK-SPIRV: store target("spirv.Image", double, 5, 2, 0, 0, 1, 0) %[[Handle2]], ptr %__handle, align 4
-// CHECK: call {{(spir_func )?}}void @hlsl::Buffer<double>::Buffer(hlsl::Buffer<double> const&)(ptr {{.*}} %[[RetValue2]], ptr {{.*}} %[[Tmp2]])
+// CHECK-DXIL: store target("dx.TypedBuffer", i32, 0, 0, 0) %[[Handle2]], ptr %__handle, align 4
+// CHECK-SPIRV: store target("spirv.Image", i32, 5, 2, 0, 0, 1, 0) %[[Handle2]], ptr %__handle, align 4
+// CHECK: call {{(spir_func )?}}void @hlsl::Buffer<unsigned int>::Buffer(hlsl::Buffer<unsigned int> const&)(ptr {{.*}} %[[RetValue2]], ptr {{.*}} %[[Tmp2]])
// Buf3 initialization part 1 - local variable declared in function foo() is initialized by RWBuffer<int> C1 default constructor
// CHECK-DXIL: define void @foo()
diff --git a/clang/test/CodeGenHLSL/resources/TypedBuffers-elementtype.hlsl b/clang/test/CodeGenHLSL/resources/TypedBuffers-elementtype.hlsl
index 7d59bc5fed5ead..5519d3aa6b5a22 100644
--- a/clang/test/CodeGenHLSL/resources/TypedBuffers-elementtype.hlsl
+++ b/clang/test/CodeGenHLSL/resources/TypedBuffers-elementtype.hlsl
@@ -25,48 +25,44 @@
// DXIL: %"class.hlsl::[[RESOURCE]].11" = type { target("dx.TypedBuffer", <3 x float>, [[RW]], 0, 0) }
// DXIL: %"class.hlsl::[[RESOURCE]].12" = type { target("dx.TypedBuffer", <4 x i32>, [[RW]], 0, 1) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]]" = type { target("spirv.SignedImage", i16, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].0" = type { target("spirv.Image", i16, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].1" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].2" = type { target("spirv.Image", i32, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].3" = type { target("spirv.SignedImage", i64, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].4" = type { target("spirv.Image", i64, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].5" = type { target("spirv.Image", half, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].6" = type { target("spirv.Image", float, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].7" = type { target("spirv.Image", double, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].8" = type { target("spirv.SignedImage", i16, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].9" = type { target("spirv.Image", i32, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].10" = type { target("spirv.Image", half, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].11" = type { target("spirv.Image", float, 5, 2, 0, 0, 1, 0) }
-// SPV-RO: %"class.hlsl::[[RESOURCE]].12" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]]" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]].0" = type { target("spirv.Image", i32, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]].1" = type { target("spirv.SignedImage", i64, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]].2" = type { target("spirv.Image", i64, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]].3" = type { target("spirv.Image", float, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]].4" = type { target("spirv.Image", i32, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]].5" = type { target("spirv.Image", float, 5, 2, 0, 0, 1, 0) }
+// SPV-RO: %"class.hlsl::[[RESOURCE]].6" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 1, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]]" = type { target("spirv.SignedImage", i16, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].0" = type { target("spirv.Image", i16, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].1" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 24) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].2" = type { target("spirv.Image", i32, 5, 2, 0, 0, 2, 33) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].3" = type { target("spirv.SignedImage", i64, 5, 2, 0, 0, 2, 41) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].4" = type { target("spirv.Image", i64, 5, 2, 0, 0, 2, 40) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].5" = type { target("spirv.Image", half, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].6" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 3) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].7" = type { target("spirv.Image", double, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].8" = type { target("spirv.SignedImage", i16, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].9" = type { target("spirv.Image", i32, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].10" = type { target("spirv.Image", half, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].11" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 0) }
-// SPV-RW: %"class.hlsl::[[RESOURCE]].12" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 21) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]]" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 24) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]].0" = type { target("spirv.Image", i32, 5, 2, 0, 0, 2, 33) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]].1" = type { target("spirv.SignedImage", i64, 5, 2, 0, 0, 2, 41) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]].2" = type { target("spirv.Image", i64, 5, 2, 0, 0, 2, 40) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]].3" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 3) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]].4" = type { target("spirv.Image", i32, 5, 2, 0, 0, 2, 0) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]].5" = type { target("spirv.Image", float, 5, 2, 0, 0, 2, 0) }
+// SPV-RW: %"class.hlsl::[[RESOURCE]].6" = type { target("spirv.SignedImage", i32, 5, 2, 0, 0, 2, 21) }
-RESOURCE<int16_t> BufI16;
-RESOURCE<uint16_t> BufU16;
+// Vulkan images cannot hold 16-bit or double elements, so those buffers are
+// only declared for DXIL.
+#ifdef __SPIRV__
+#define DXIL_ONLY(...)
+#else
+#define DXIL_ONLY(...) __VA_ARGS__
+#endif
+
+DXIL_ONLY(RESOURCE<int16_t> BufI16;)
+DXIL_ONLY(RESOURCE<uint16_t> BufU16;)
RESOURCE<int> BufI32;
RESOURCE<uint> BufU32;
RESOURCE<int64_t> BufI64;
RESOURCE<uint64_t> BufU64;
-RESOURCE<half> BufF16;
+DXIL_ONLY(RESOURCE<half> BufF16;)
RESOURCE<float> BufF32;
-RESOURCE<double> BufF64;
-RESOURCE< vector<int16_t, 4> > BufI16x4;
+DXIL_ONLY(RESOURCE<double> BufF64;)
+DXIL_ONLY(RESOURCE< vector<int16_t, 4> > BufI16x4;)
RESOURCE< vector<uint, 3> > BufU32x3;
-RESOURCE<half2> BufF16x2;
+DXIL_ONLY(RESOURCE<half2> BufF16x2;)
RESOURCE<float3> BufF32x3;
RESOURCE<int4> BufI32x4;
// TODO: RESOURCE<snorm half> BufSNormF16; -> 11
@@ -78,17 +74,17 @@ RESOURCE<int4> BufI32x4;
[numthreads(1,1,1)]
void main(int GI : SV_GroupIndex) {
- int16_t v1 = BufI16[GI];
- uint16_t v2 = BufU16[GI];
+ DXIL_ONLY(int16_t v1 = BufI16[GI];)
+ DXIL_ONLY(uint16_t v2 = BufU16[GI];)
int v3 = BufI32[GI];
uint v4 = BufU32[GI];
int64_t v5 = BufI64[GI];
uint64_t v6 = BufU64[GI];
- half v7 = BufF16[GI];
+ DXIL_ONLY(half v7 = BufF16[GI];)
float v8 = BufF32[GI];
- double v9 = BufF64[GI];
- vector<int16_t,4> v10 = BufI16x4[GI];
+ DXIL_ONLY(double v9 = BufF64[GI];)
+ DXIL_ONLY(vector<int16_t,4> v10 = BufI16x4[GI];)
vector<int, 3> v11 = BufU32x3[GI];
- half2 v12 = BufF16x2[GI];
+ DXIL_ONLY(half2 v12 = BufF16x2[GI];)
float3 v13 = BufF32x3[GI];
}
diff --git a/clang/test/CodeGenHLSL/resources/res-array-global.hlsl b/clang/test/CodeGenHLSL/resources/res-array-global.hlsl
index 41fae2ae6f5b8a..ed3852890cc169 100644
--- a/clang/test/CodeGenHLSL/resources/res-array-global.hlsl
+++ b/clang/test/CodeGenHLSL/resources/res-array-global.hlsl
@@ -21,7 +21,7 @@ RWBuffer<int> B[5]; // DXIL implicit binding in space0
RWBuffer<int> C[3] : register(u2);
// implicit binding for both DXIL and SPIR-V in space/set 0
-RWBuffer<double> D[10];
+RWBuffer<float2> D[10];
// implicit binding for both DXIL and SPIR-V with specified space/set 0
RWBuffer<uint> E[15] : register(space2);
@@ -67,12 +67,12 @@ void main() {
// SPV-SAME: (ptr {{.*}} sret(%"class.hlsl::RWBuffer.0") align 4 %[[Tmp2]],
// SPV-SAME: i32 noundef 2, i32 noundef 0, i32 noundef 3, i32 noundef 1, ptr noundef @[[BufC]])
- // Make sure D[7] is translated to a RWBuffer<double>::__createFromImplicitBinding call
+ // Make sure D[7] is translated to a RWBuffer<float2>::__createFromImplicitBinding call
// for both DXIL and SPIR-V
- // DXIL: call void @hlsl::RWBuffer<double>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
+ // DXIL: call void @hlsl::RWBuffer<float vector[2]>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
// DXIL-SAME: (ptr {{.*}} sret(%"class.hlsl::RWBuffer.1") align 4 %[[Tmp3]],
// DXIL-SAME: i32 noundef 1, i32 noundef 0, i32 noundef 10, i32 noundef 7, ptr noundef @D.str)
- // SPV: call spir_func void @hlsl::RWBuffer<double>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
+ // SPV: call spir_func void @hlsl::RWBuffer<float vector[2]>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
// SPV-SAME: (ptr {{.*}} sret(%"class.hlsl::RWBuffer.1") align 4 %[[Tmp3]],
// SPV-SAME: i32 noundef 0, i32 noundef 0, i32 noundef 10, i32 noundef 7, ptr noundef @[[BufD]])
@@ -84,5 +84,5 @@ void main() {
// SPV: call spir_func void @hlsl::RWBuffer<unsigned int>::__createFromImplicitBinding(unsigned int, unsigned int, int, unsigned int, char const*)
// SPV-SAME: (ptr {{.*}} sret(%"class.hlsl::RWBuffer.2") align 4 %[[Tmp4]],
// SPV-SAME: i32 noundef 1, i32 noundef 2, i32 noundef 15, i32 noundef 5, ptr noundef @[[BufE]])
- Out[0] = A[2][0] + (float)B[3][0] + (float)C[1][0] + (float)D[7][0] + (float)E[5][0];
+ Out[0] = A[2][0] + (float)B[3][0] + (float)C[1][0] + D[7][0].x + (float)E[5][0];
}
diff --git a/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl b/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl
index 4e15ff1354a194..ab923d4242f713 100644
--- a/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl
+++ b/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl
@@ -17,7 +17,7 @@
// RUN: %clang_cc1 -triple spirv-unknown-vulkan-library -x hlsl -fsyntax-only \
// RUN: -finclude-default-header -fnative-half-type -fnative-int16-type \
// RUN: -DTEXTURE=Texture2D -DGRAD_TYPE=float2 -DCOORD_TYPE=float2 \
-// RUN: -verify=expected %s
+// RUN: -verify=vk,expected %s
// Parameterized over the texture types in the RUN lines above; adding a texture
// of another dimension only requires new RUN lines.
@@ -30,16 +30,19 @@
//
// Check prefixes:
// sm66 shader model 6.6 diagnostics
+// vk Vulkan diagnostics
//
// Sampling textures with an integer element type was introduced in shader model
// 6.7, so the `sm66` diagnostics are only expected in the shader model 6.6 runs.
// The Vulkan target has no shader model and does not restrict integer sampling.
// Comparison sampling requires a floating point element type at every shader
-// model, so those diagnostics use the `expected` prefix.
+// model, so those diagnostics use the `expected` prefix. Vulkan rejects
+// resources with 16-bit elements.
TEXTURE<int4> TexVec;
TEXTURE<uint> Tex;
TEXTURE<int16_t> TexShort;
+// vk-error at -1 {{resource type 'Texture2D<int16_t>' (aka 'Texture2D<short>') with element type 'short' is not supported when targeting Vulkan; 'short' is 16 bits wide, but Vulkan requires a 32-bit type or a 64-bit integer type}}
SamplerState Samp;
SamplerComparisonState SampCmp;
diff --git a/clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl b/clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl
new file mode 100644
index 00000000000000..8be1c91588513e
--- /dev/null
+++ b/clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl
@@ -0,0 +1,126 @@
+// RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -x hlsl -fsyntax-only \
+// RUN: -finclude-default-header -fnative-half-type -fnative-int16-type \
+// RUN: -verify=vk,vk16 %s
+// RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -x hlsl -fsyntax-only \
+// RUN: -finclude-default-header -verify=vk %s
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-compute -x hlsl -fsyntax-only \
+// RUN: -finclude-default-header -fnative-half-type -fnative-int16-type \
+// RUN: -verify=dxil %s
+
+// dxil-no-diagnostics
+
+// Textures and typed buffers require a 32-bit float or a 32-bit or 64-bit
+// integer element type on Vulkan. DXIL accepts the other element types. Without
+// native 16-bit types, `half` is a 32-bit type.
+
+// Rejected element types.
+
+Texture2D<half> TexHalf;
+// vk16-error at -1 {{resource type 'Texture2D<half>' with element type 'half' is not supported when targeting Vulkan; 'half' is 16 bits wide, but Vulkan requires a 32-bit type or a 64-bit integer type}}
+Texture2DArray<half4> TexArrayHalf4;
+// vk16-error-re at -1 {{resource type 'Texture2DArray<half4>' {{.*}}with element type 'half' is not supported}}
+TextureCube<double> TexCubeDouble;
+// vk-error at -1 {{resource type 'TextureCube<double>' with element type 'double' is not supported when targeting Vulkan; 'double' is 64 bits wide, but Vulkan requires a 32-bit type or a 64-bit integer type}}
+RWTexture2D<double2> RWTexDouble2;
+// vk-error-re at -1 {{resource type 'RWTexture2D<double2>' {{.*}}with element type 'double' is not supported}}
+Buffer<double> BufDouble;
+// vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+
+#ifdef __HLSL_ENABLE_16_BIT
+Texture2DMS<float16_t2> TexMSFloat16x2;
+// vk16-error-re at -1 {{resource type 'Texture2DMS<float16_t2>' {{.*}}with element type 'half' is not supported}}
+RWBuffer<int16_t> RWBufInt16;
+// vk16-error-re at -1 {{resource type 'RWBuffer<int16_t>' {{.*}}with element type 'short' is not supported}}
+Buffer<uint16_t4> BufUInt16x4;
+// vk16-error-re at -1 {{resource type 'Buffer<uint16_t4>' {{.*}}with element type 'unsigned short' is not supported}}
+RasterizerOrderedBuffer<vector<int16_t, 3> > ROBufInt16x3;
+// vk16-error at -1 {{resource type 'RasterizerOrderedBuffer<vector<int16_t, 3>>' with element type 'short' is not supported}}
+#endif
+
+// Legal element types.
+Texture2D<float> TexFloat;
+Texture2D<int> TexInt;
+RWTexture2D<uint> RWTexUInt;
+Buffer<int64_t> BufInt64;
+RWBuffer<uint64_t2> RWBufUInt64x2;
+// HLSL bool has a 32-bit integer memory representation.
+RasterizerOrderedBuffer<bool4> ROBufBool4;
+
+// Structured and byte-address buffers are not images.
+StructuredBuffer<half4> SBufHalf4;
+StructuredBuffer<double> SBufDouble;
+ByteAddressBuffer BAB;
+
+// Declaration forms.
+
+Buffer<double> UsedBufDouble;
+// vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+static RWBuffer<half> StaticBufHalf;
+// vk16-error at -1 {{resource type 'RWBuffer<half>' with element type 'half' is not supported}}
+typedef Texture2D<double2> DoubleTexture;
+DoubleTexture TypedefTex;
+// vk-error at -1 {{resource type 'DoubleTexture' (aka 'Texture2D<vector<double, 2>>') with element type 'double' is not supported}}
+Texture2D<double> TexArray[4];
+// vk-error at -1 {{resource type 'Texture2D<double>' with element type 'double' is not supported}}
+RWBuffer<double> BufArray2D[2][3];
+// vk-error at -1 {{resource type 'RWBuffer<double>' with element type 'double' is not supported}}
+
+// Embedded resources are diagnosed at the field, once for each global
+// declaration that contains them: GlobalStruct, GlobalStructArray, GlobalOuter
+// and GlobalDerived.
+struct S {
+ Texture2D<double> Tex;
+ // vk-error at -1 4 {{resource type 'Texture2D<double>' with element type 'double' is not supported}}
+ Texture2D<float> Legal;
+};
+S GlobalStruct;
+S GlobalStructArray[2];
+
+struct Outer {
+ RWBuffer<double> Bufs[2];
+ // vk-error at -1 {{resource type 'RWBuffer<double>' with element type 'double' is not supported}}
+ S Inner;
+};
+Outer GlobalOuter;
+
+struct Derived : S {
+ RWBuffer<half> Extra;
+ // vk16-error at -1 {{resource type 'RWBuffer<half>' with element type 'half' is not supported}}
+};
+Derived GlobalDerived;
+
+template<typename T> struct ResourceMember {
+ Texture2D<T> Tex;
+ // vk-error at -1 {{resource type 'Texture2D<double>' with element type 'double' is not supported}}
+};
+ResourceMember<double> InstantiatedMember;
+
+// Static resources in a template are diagnosed when it is instantiated.
+template<typename T> void staticInTemplate() {
+ static Buffer<T> B;
+ // vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+}
+
+void resourceParameter(Buffer<double> P);
+// vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+
+// A template parameter is checked while the template arguments are deduced,
+// so the error makes the candidate non-viable.
+template<typename T>
+void parameterInTemplate(Buffer<T> P) {}
+// vk-note at -1 {{candidate template ignored: substitution failure [with T = double]: resource type 'Buffer<double>' with element type 'double' is not supported}}
+
+[numthreads(1, 1, 1)]
+void main() {
+ double D = UsedBufDouble[0];
+ staticInTemplate<double>();
+ // vk-note at -1 {{in instantiation of function template specialization 'staticInTemplate<double>' requested here}}
+ parameterInTemplate(UsedBufDouble);
+ // vk-error at -1 {{no matching function for call to 'parameterInTemplate'}}
+
+ RWBuffer<double> FromHeap = ResourceDescriptorHeap[0];
+ // vk-error at -1 {{resource type 'RWBuffer<double>' with element type 'double' is not supported}}
+ // A local copy of a diagnosed global is diagnosed again.
+ Buffer<double> Copy = UsedBufDouble;
+ // vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+}
>From 53c4297bd73f3ad139dbda3a2151bb23c0ec891c Mon Sep 17 00:00:00 2001
From: Deric Cheung <cheung.deric at gmail.com>
Date: Mon, 16 Nov 2026 21:44:22 +0000
Subject: [PATCH 2/4] [SPIRV] Reject invalid image Sampled Types when targeting
Vulkan
Vulkan requires an OpTypeImage Sampled Type to be a 32-bit float or a
32-bit or 64-bit integer (VUID-StandaloneSpirv-OpTypeImage-04656). Report
an error instead of emitting invalid SPIR-V.
Assisted by: Claude Opus 5.5
---
llvm/docs/SPIRVUsage.md | 8 +++
llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp | 16 +++++
.../SPIRV/image-sampled-type-vulkan.ll | 65 +++++++++++++++++++
3 files changed, 89 insertions(+)
create mode 100644 llvm/test/CodeGen/SPIRV/image-sampled-type-vulkan.ll
diff --git a/llvm/docs/SPIRVUsage.md b/llvm/docs/SPIRVUsage.md
index 4aeed0814bd75b..277e45e65cc927 100644
--- a/llvm/docs/SPIRVUsage.md
+++ b/llvm/docs/SPIRVUsage.md
@@ -357,6 +357,13 @@ integer instead of unsigned. This is required because llvm-ir will create the
same sampled type for signed and unsigned integers. If the image format is
unknown, the backend cannot distinguish the two case.
+When targeting Vulkan, the Sampled Type of every `OpTypeImage`, sampled or
+storage, must be a scalar 32-bit float, 32-bit integer or 64-bit integer
+([VUID-StandaloneSpirv-OpTypeImage-04656][vulkan-image-sampled-type]). The
+backend reports an error for any other Sampled Type, whichever frontend produced
+the LLVM IR. Other target environments are not affected; OpenCL images, for
+example, use a `void` Sampled Type.
+
See [wg-hlsl proposal 0018](https://github.com/llvm/wg-hlsl/blob/main/proposals/0018-spirv-resource-representation.md)
for details on `spirv.VulkanBuffer`.
@@ -721,3 +728,4 @@ LLVM IR representations.
[inline spir-v]: https://microsoft.github.io/hlsl-specs/proposals/0011-inline-spirv.html#types
[target extension types for inline spir-v and decorated types]: https://github.com/llvm/wg-hlsl/blob/main/proposals/0017-inline-spirv-and-decorated-types.md
+[vulkan-image-sampled-type]: https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-StandaloneSpirv-OpTypeImage-04656
diff --git a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
index b3ce4a4b6c40a3..89ac18f53b3603 100644
--- a/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
@@ -1823,6 +1823,22 @@ SPIRVTypeInst SPIRVGlobalRegistry::getOrCreateOpTypeImage(
ImageFormat, AccessQual);
if (const MachineInstr *MI = findMI(Key, &MIRBuilder.getMF()))
return MI;
+
+ // Vulkan restricts the Sampled Type of every image
+ // (VUID-StandaloneSpirv-OpTypeImage-04656).
+ const auto &ST = MIRBuilder.getMF().getSubtarget<SPIRVSubtarget>();
+ if (ST.getTargetTriple().isVulkanOS()) {
+ unsigned Opcode = SampledType->getOpcode();
+ uint64_t Width = Opcode == SPIRV::OpTypeInt || Opcode == SPIRV::OpTypeFloat
+ ? SampledType->getOperand(1).getImm()
+ : 0;
+ if (Width != 32 && !(Width == 64 && Opcode == SPIRV::OpTypeInt))
+ reportFatalUsageError(
+ "OpTypeImage Sampled Type must be a scalar 32-bit float, 32-bit "
+ "integer or 64-bit integer for Vulkan "
+ "(VUID-StandaloneSpirv-OpTypeImage-04656)");
+ }
+
const MachineInstr *NewMI = createConstOrTypeAtFunctionEntry(
MIRBuilder, [&](MachineIRBuilder &MIRBuilder) {
auto MIB =
diff --git a/llvm/test/CodeGen/SPIRV/image-sampled-type-vulkan.ll b/llvm/test/CodeGen/SPIRV/image-sampled-type-vulkan.ll
new file mode 100644
index 00000000000000..57c05670abeb7c
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/image-sampled-type-vulkan.ll
@@ -0,0 +1,65 @@
+; RUN: split-file %s %t
+; RUN: not llc -O0 -mtriple=spirv-unknown-vulkan1.3-library %t/f16.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=INVALID
+; RUN: not llc -O0 -mtriple=spirv-unknown-vulkan1.3-library %t/f64.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=INVALID
+; RUN: not llc -O0 -mtriple=spirv-unknown-vulkan1.3-library %t/i16.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=INVALID
+; RUN: not llc -O0 -mtriple=spirv-unknown-vulkan1.3-library %t/bool.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=INVALID
+; RUN: not llc -O0 -mtriple=spirv-unknown-vulkan1.3-library %t/vector.ll -o /dev/null 2>&1 | FileCheck %s --check-prefix=INVALID
+; RUN: llc -O0 -mtriple=spirv-unknown-vulkan1.3-library --spirv-ext=+SPV_EXT_shader_image_int64 %t/legal.ll -o - | FileCheck %s --check-prefix=LEGAL
+; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %t/opencl.ll -o - | FileCheck %s --check-prefix=OPENCL
+; RUN: llc -O0 -mtriple=spirv-unknown-unknown-library %t/f16.ll -o - | FileCheck %s --check-prefix=GENERIC
+
+; Vulkan requires the Sampled Type of every image, sampled or storage, to be a
+; scalar 32-bit float, 32-bit integer or 64-bit integer.
+; INVALID: OpTypeImage Sampled Type must be a scalar 32-bit float, 32-bit integer or 64-bit integer for Vulkan (VUID-StandaloneSpirv-OpTypeImage-04656)
+
+; LEGAL-DAG: %[[F32:[0-9]+]] = OpTypeFloat 32
+; LEGAL-DAG: %[[I32:[0-9]+]] = OpTypeInt 32 0
+; LEGAL-DAG: %[[I64:[0-9]+]] = OpTypeInt 64 0
+; LEGAL-DAG: OpTypeImage %[[F32]] 2D 0 0 0 1 Unknown
+; LEGAL-DAG: OpTypeImage %[[I32]] Buffer 0 0 0 2 R32ui
+; LEGAL-DAG: OpTypeImage %[[I64]] 2D 0 0 0 2 R64ui
+
+; OpenCL images use a void Sampled Type.
+; OPENCL: %[[VOID:[0-9]+]] = OpTypeVoid
+; OPENCL: OpTypeImage %[[VOID]] 2D 0 0 0 0 Unknown ReadOnly
+
+; The restriction is specific to Vulkan.
+; GENERIC: %[[HALF:[0-9]+]] = OpTypeFloat 16
+; GENERIC: OpTypeImage %[[HALF]] 2D 0 0 0 1 Unknown
+
+;--- f16.ll
+define spir_func void @sampled_f16(target("spirv.Image", half, 1, 0, 0, 0, 1, 0) %image) {
+ ret void
+}
+
+;--- f64.ll
+define spir_func void @storage_f64(target("spirv.Image", double, 1, 0, 0, 0, 2, 0) %image) {
+ ret void
+}
+
+;--- i16.ll
+define spir_func void @storage_i16(target("spirv.SignedImage", i16, 5, 0, 0, 0, 2, 0) %image) {
+ ret void
+}
+
+;--- bool.ll
+define spir_func void @sampled_bool(target("spirv.Image", i1, 5, 0, 0, 0, 1, 0) %image) {
+ ret void
+}
+
+;--- vector.ll
+define spir_func void @storage_vector(target("spirv.Image", <2 x float>, 1, 0, 0, 0, 2, 0) %image) {
+ ret void
+}
+
+;--- legal.ll
+define spir_func void @legal_images(target("spirv.Image", float, 1, 0, 0, 0, 1, 0) %sampled,
+ target("spirv.Image", i32, 5, 0, 0, 0, 2, 33) %buffer,
+ target("spirv.Image", i64, 1, 0, 0, 0, 2, 40) %wide) {
+ ret void
+}
+
+;--- opencl.ll
+define spir_func void @opencl_image(target("spirv.Image", void, 1, 0, 0, 0, 0, 0, 0) %image) {
+ ret void
+}
>From bdfc17d1217b8c80d5cdd8d7a0ea73a08d663dcc Mon Sep 17 00:00:00 2001
From: "Deric C." <cheung.deric at gmail.com>
Date: Fri, 20 Nov 2026 14:42:17 -0700
Subject: [PATCH 3/4] Removed unnecessary explanation about image's Sampled
Type and texel format.
---
clang/docs/HLSL/ResourceTypes.md | 7 -------
1 file changed, 7 deletions(-)
diff --git a/clang/docs/HLSL/ResourceTypes.md b/clang/docs/HLSL/ResourceTypes.md
index 102ab498149f82..70f9cd8a354182 100644
--- a/clang/docs/HLSL/ResourceTypes.md
+++ b/clang/docs/HLSL/ResourceTypes.md
@@ -67,15 +67,8 @@ is a 32-bit type and stays valid. A typed resource is any SRV or UAV resource
whose handle has a contained type and is not a raw buffer, so structured and
byte-address buffers are not affected.
-The rule concerns the image's Sampled Type, not the precision of the
-texel format: an `R16f` image uses a 32-bit floating-point Sampled
-Type, as specified by Vulkan's [image format and type matching
-rules][vulkan-image-format-type-matching].
-
[constantbuffer]: https://learn.microsoft.com/en-us/windows/win32/direct3d12/resource-binding-in-hlsl#constant-buffers
[rasterizerorderedtexture2d]: https://learn.microsoft.com/en-us/windows/win32/direct3d11/rasterizer-order-views
[rwbyteaddressbuffer]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-rwbyteaddressbuffer
[texture3d]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture3d
[vulkan-image-sampled-type]: https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-StandaloneSpirv-OpTypeImage-04656
-[vulkan-image-format-type-matching]: https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#spirvenv-format-type-matching
-
>From 3446813c39bcaac42d13833d870773d52428bafc Mon Sep 17 00:00:00 2001
From: Deric Cheung <cheung.deric at gmail.com>
Date: Wed, 25 Nov 2026 21:09:47 +0000
Subject: [PATCH 4/4] [HLSL] Validate Vulkan resource elements with concepts
Vulkan limits image sampled types to 32-bit floats and 32-/64-bit integers.
Reject incompatible elements of supported textures and typed buffers through
a new Vulkan-specific typed resource constraint.
Assisted-by: GitHub Copilot (GPT-6.1 Sol)
---
clang/docs/HLSL/ResourceTypes.md | 14 +-
clang/include/clang/Basic/BuiltinTraits.td | 5 +
.../clang/Basic/DiagnosticSemaKinds.td | 4 -
clang/include/clang/Sema/SemaHLSL.h | 2 +-
clang/lib/Sema/HLSLExternalSemaSource.cpp | 14 +-
clang/lib/Sema/SemaDecl.cpp | 6 -
clang/lib/Sema/SemaHLSL.cpp | 65 ++-----
clang/lib/Sema/SemaTypeTraits.cpp | 4 +
.../Textures-integer-element-type.hlsl | 5 +-
.../TypedResources-element-type-Vulkan.hlsl | 184 +++++++++++-------
llvm/docs/SPIRVUsage.md | 5 +-
11 files changed, 155 insertions(+), 153 deletions(-)
diff --git a/clang/docs/HLSL/ResourceTypes.md b/clang/docs/HLSL/ResourceTypes.md
index 70f9cd8a354182..9f92152b130f89 100644
--- a/clang/docs/HLSL/ResourceTypes.md
+++ b/clang/docs/HLSL/ResourceTypes.md
@@ -54,18 +54,8 @@ Textures and typed buffers lower to SPIR-V images on Vulkan. The
scalar element type, or vector component type, of an image must
be a 32-bit floating-point type or a 32-bit or 64-bit integer type
([VUID-StandaloneSpirv-OpTypeImage-04656][vulkan-image-sampled-type]).
-
-When targeting SPIR-V, Clang reports an error for each variable or parameter
-declaration of a typed resource whose element type breaks this rule, whether
-or not the shader uses it. This covers global, `static` and local variables,
-function parameters, arrays of resources, declarations in instantiated
-templates, and resources embedded in structs, which are diagnosed at their
-field. A parameter of a function template is checked while its template
-arguments are deduced, so the error makes that candidate non-viable. Native
-16-bit types and `double` are rejected. Without native 16-bit types, `half`
-is a 32-bit type and stays valid. A typed resource is any SRV or UAV resource
-whose handle has a contained type and is not a raw buffer, so structured and
-byte-address buffers are not affected.
+Clang rejects other element types when targeting SPIR-V. `StructuredBuffer`
+and `ByteAddressBuffer` do not lower to images and are not affected.
[constantbuffer]: https://learn.microsoft.com/en-us/windows/win32/direct3d12/resource-binding-in-hlsl#constant-buffers
[rasterizerorderedtexture2d]: https://learn.microsoft.com/en-us/windows/win32/direct3d11/rasterizer-order-views
diff --git a/clang/include/clang/Basic/BuiltinTraits.td b/clang/include/clang/Basic/BuiltinTraits.td
index f80234dd21e145..f56c04b95fe461 100644
--- a/clang/include/clang/Basic/BuiltinTraits.td
+++ b/clang/include/clang/Basic/BuiltinTraits.td
@@ -626,6 +626,11 @@ def IsTypedResourceElementCompatible : UnaryTrait {
let KeyFlags = [KEYHLSL];
}
+def IsVulkanTypedResourceElementCompatible : UnaryTrait {
+ let Spelling = "__builtin_hlsl_is_vulkan_typed_resource_element_compatible";
+ let KeyFlags = [KEYHLSL];
+}
+
def IsConstantBufferElementCompatible : UnaryTrait {
let Spelling = "__builtin_hlsl_is_constant_buffer_element_compatible";
let KeyFlags = [KEYHLSL];
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 13b4899d032636..4f274b922368b8 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -14115,10 +14115,6 @@ def err_hlsl_samplecmp_requires_float
"a floating point type">;
def err_hlsl_gathercmp_invalid_component
: Error<"gatherCmp%select{Red|Green|Blue|Alpha}0 operations on the Vulkan target are not supported; only GatherCmp and GatherCmpRed are allowed">;
-def err_hlsl_vulkan_resource_element_type
- : Error<"resource type %0 with element type %1 is not supported when "
- "targeting Vulkan; %1 is %2 bits wide, but Vulkan requires a "
- "32-bit type or a 64-bit integer type">;
def err_hlsl_resource_member_array_access_not_constant
: Error<"index for struct array inside cbuffer that contains resources must be a constant integer expression">;
diff --git a/clang/include/clang/Sema/SemaHLSL.h b/clang/include/clang/Sema/SemaHLSL.h
index 0ef33bd9a0f12c..6747b9c987ebdf 100644
--- a/clang/include/clang/Sema/SemaHLSL.h
+++ b/clang/include/clang/Sema/SemaHLSL.h
@@ -133,7 +133,6 @@ class SemaHLSL : public SemaBase {
void ActOnTopLevelFunction(FunctionDecl *FD);
void ActOnVariableDeclarator(VarDecl *VD);
bool ActOnUninitializedVarDecl(VarDecl *D);
- void DiagnoseVulkanResourceElementTypes(const VarDecl *VD);
void ActOnEndOfTranslationUnit(TranslationUnitDecl *TU);
bool ActOnResourceMemberAccessExpr(MemberExpr *ME);
void CheckEntryPoint(FunctionDecl *FD);
@@ -215,6 +214,7 @@ class SemaHLSL : public SemaBase {
// HLSL Type trait implementations
bool IsScalarizedLayoutCompatible(QualType T1, QualType T2) const;
bool IsTypedResourceElementCompatible(QualType T1);
+ bool IsVulkanTypedResourceElementCompatible(QualType T1);
bool IsConstantBufferElementCompatible(QualType T1);
bool CheckCompatibleParameterABI(FunctionDecl *New, FunctionDecl *Old);
diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp
index ed6461537318ae..4348bb06b565fe 100644
--- a/clang/lib/Sema/HLSLExternalSemaSource.cpp
+++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp
@@ -20,6 +20,7 @@
#include "clang/AST/Type.h"
#include "clang/Basic/AddressSpaces.h"
#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Sema.h"
@@ -518,6 +519,7 @@ addVectorTexturePartialSpecialization(Sema &S, NamespaceDecl *HLSLNamespace,
// this concept:
// template<typename T> concept is_typed_resource_element_compatible =
// __is_typed_resource_element_compatible<T>;
+// For SPIR-V, use the Vulkan-specific concept and builtin instead.
static Expr *constructTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc,
TemplateTypeParmDecl *T) {
ASTContext &Context = S.getASTContext();
@@ -532,9 +534,11 @@ static Expr *constructTypedBufferConstraintExpr(Sema &S, SourceLocation NameLoc,
TypeSourceInfo *TTypeSourceInfo =
Context.getTrivialTypeSourceInfo(TType, NameLoc);
+ TypeTrait Trait = Context.getTargetInfo().getTriple().isSPIRV()
+ ? UTT_IsVulkanTypedResourceElementCompatible
+ : UTT_IsTypedResourceElementCompatible;
TypeTraitExpr *TypedResExpr = TypeTraitExpr::Create(
- Context, BoolTy, NameLoc, UTT_IsTypedResourceElementCompatible,
- {TTypeSourceInfo}, NameLoc, true);
+ Context, BoolTy, NameLoc, Trait, {TTypeSourceInfo}, NameLoc, true);
return TypedResExpr;
}
@@ -644,8 +648,10 @@ static ConceptDecl *constructBufferConceptDecl(Sema &S, NamespaceDecl *NSD,
switch (BT) {
case HLSLBufferType::Typed:
- DeclName = DeclarationName(
- &Context.Idents.get("__is_typed_resource_element_compatible"));
+ DeclName = DeclarationName(&Context.Idents.get(
+ Context.getTargetInfo().getTriple().isSPIRV()
+ ? "__is_vulkan_typed_resource_element_compatible"
+ : "__is_typed_resource_element_compatible"));
ConstraintExpr = constructTypedBufferConstraintExpr(S, DeclLoc, T);
break;
case HLSLBufferType::Structured:
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index e6a1895f220f12..63c581d3fb4f34 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -9389,9 +9389,6 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
}
}
- if (getLangOpts().HLSL)
- HLSL().DiagnoseVulkanResourceElementTypes(NewVD);
-
if (T.hasAddressSpace() &&
!CheckVarDeclSizeAddressSpace(NewVD, T.getAddressSpace())) {
NewVD->setInvalidDecl();
@@ -16275,9 +16272,6 @@ ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc,
New->setInvalidDecl();
}
- if (getLangOpts().HLSL)
- HLSL().DiagnoseVulkanResourceElementTypes(New);
-
return New;
}
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index b4449c60aec95b..0cda22625b9378 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -5798,6 +5798,17 @@ bool SemaHLSL::IsTypedResourceElementCompatible(clang::QualType QT) {
return false;
}
+bool SemaHLSL::IsVulkanTypedResourceElementCompatible(QualType QT) {
+ if (!IsTypedResourceElementCompatible(QT))
+ return false;
+
+ // Vulkan permits only 32-bit floats and 32-/64-bit integers as image
+ // Sampled Types (VUID-StandaloneSpirv-OpTypeImage-04656).
+ QualType EltTy = getElementTypeOf(QT, /*IncludeMatrix=*/false);
+ uint64_t Size = getASTContext().getTypeSize(EltTy);
+ return Size == 32 || (Size == 64 && EltTy->isIntegerType());
+}
+
bool SemaHLSL::IsScalarizedLayoutCompatible(QualType T1, QualType T2) const {
if (T1.isNull() || T2.isNull())
return false;
@@ -6449,60 +6460,6 @@ void SemaHLSL::handleGlobalStructOrArrayOfWithResources(VarDecl *VD) {
}
}
-// Vulkan requires the Sampled Type of every image, which each typed resource
-// lowers to, to be a 32-bit float or a 32-bit or 64-bit integer
-// (VUID-StandaloneSpirv-OpTypeImage-04656). Diagnoses each typed resource that
-// the variable or parameter VD declares, or embeds in a struct, at the
-// declaration or at the field.
-void SemaHLSL::DiagnoseVulkanResourceElementTypes(const VarDecl *VD) {
- ASTContext &Ctx = getASTContext();
- if (!Ctx.getTargetInfo().getTriple().isSPIRV() ||
- VD->getType()->isDependentType() ||
- !SemaRef.isCompleteType(VD->getLocation(),
- Ctx.getBaseElementType(VD->getType())))
- return;
-
- // The methods of the built-in resource types declare locals and parameters
- // of their own resource type without a source location. Skip them: the
- // user's declaration that instantiated the method is diagnosed instead.
- if (VD->getLocation().isInvalid())
- return;
-
- SmallVector<std::pair<QualType, SourceLocation>, 4> Worklist = {
- {VD->getType(), VD->getLocation()}};
- while (!Worklist.empty()) {
- auto [Ty, Loc] = Worklist.pop_back_val();
- Ty = Ctx.getBaseElementType(Ty);
- if (!Ty->isHLSLIntangibleType())
- continue;
-
- const HLSLAttributedResourceType *HandleTy =
- HLSLAttributedResourceType::findHandleTypeOnResource(Ty.getTypePtr());
- if (!HandleTy) {
- if (const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl()) {
- for (const CXXBaseSpecifier &Base : RD->bases())
- Worklist.emplace_back(Base.getType(), Loc);
- for (const FieldDecl *FD : RD->fields())
- Worklist.emplace_back(FD->getType(), FD->getLocation());
- }
- continue;
- }
-
- ResourceClass RC = HandleTy->getAttrs().ResourceClass;
- if (HandleTy->isRaw() || !HandleTy->hasContainedType() ||
- (RC != ResourceClass::SRV && RC != ResourceClass::UAV))
- continue;
- QualType EltTy =
- getElementTypeOf(HandleTy->getContainedType(), /*IncludeMatrix=*/false);
- if (!EltTy->isArithmeticType())
- continue;
- uint64_t Size = Ctx.getTypeSize(EltTy);
- if (Size != 32 && !(Size == 64 && EltTy->isIntegerType()))
- Diag(Loc, diag::err_hlsl_vulkan_resource_element_type)
- << Ty << EltTy << Size;
- }
-}
-
void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
if (VD->hasGlobalStorage()) {
// make sure the declaration has a complete type
diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp
index cb30ff9fd02da9..770b1c729ddafc 100644
--- a/clang/lib/Sema/SemaTypeTraits.cpp
+++ b/clang/lib/Sema/SemaTypeTraits.cpp
@@ -369,6 +369,7 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S, TypeTrait UTT,
case UTT_IsCompound:
case UTT_IsMemberPointer:
case UTT_IsTypedResourceElementCompatible:
+ case UTT_IsVulkanTypedResourceElementCompatible:
case UTT_IsConstantBufferElementCompatible:
// Fall-through
@@ -1148,11 +1149,14 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, TypeTrait UTT,
return T->isHLSLIntangibleType();
case UTT_IsTypedResourceElementCompatible:
+ case UTT_IsVulkanTypedResourceElementCompatible:
assert(Self.getLangOpts().HLSL &&
"typed resource element compatible types are an HLSL-only feature");
if (T->isIncompleteType())
return false;
+ if (UTT == UTT_IsVulkanTypedResourceElementCompatible)
+ return Self.HLSL().IsVulkanTypedResourceElementCompatible(T);
return Self.HLSL().IsTypedResourceElementCompatible(T);
case UTT_IsConstantBufferElementCompatible:
diff --git a/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl b/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl
index ab923d4242f713..ec62ac901a5332 100644
--- a/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl
+++ b/clang/test/SemaHLSL/Resources/Textures-integer-element-type.hlsl
@@ -41,8 +41,9 @@
TEXTURE<int4> TexVec;
TEXTURE<uint> Tex;
+#ifndef __SPIRV__
TEXTURE<int16_t> TexShort;
-// vk-error at -1 {{resource type 'Texture2D<int16_t>' (aka 'Texture2D<short>') with element type 'short' is not supported when targeting Vulkan; 'short' is 16 bits wide, but Vulkan requires a 32-bit type or a 64-bit integer type}}
+#endif
SamplerState Samp;
SamplerComparisonState SampCmp;
@@ -71,9 +72,11 @@ void main(COORD_TYPE uv) {
// sm66-note-re@*:* {{in instantiation of member function 'hlsl::Texture{{.+}}<vector<int, 4>>::Sample' requested here}}
TexVec.Sample(Samp, uv);
+#ifndef __SPIRV__
// sm66-error@* {{'SampleLevel' on resources containing 'short' requires shader model 6.7 or newer; the target shader model is 6.6}}
// sm66-note-re@*:* {{in instantiation of member function 'hlsl::Texture{{.+}}<short>::SampleLevel' requested here}}
TexShort.SampleLevel(Samp, uv, 0.0f);
+#endif
// Comparison sampling rejects integer element types at every shader model.
// expected-error@* {{'SampleCmp' and 'SampleCmpLevelZero' require resource to contain a floating point type}}
diff --git a/clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl b/clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl
index 8be1c91588513e..1a764cea483bdf 100644
--- a/clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl
+++ b/clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl
@@ -1,13 +1,11 @@
// RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -x hlsl -fsyntax-only \
// RUN: -finclude-default-header -fnative-half-type -fnative-int16-type \
-// RUN: -verify=vk,vk16 %s
+// RUN: -verify=common,vk,vk16 %s
// RUN: %clang_cc1 -triple spirv-unknown-vulkan-compute -x hlsl -fsyntax-only \
-// RUN: -finclude-default-header -verify=vk %s
+// RUN: -finclude-default-header -verify=common,vk %s
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.2-compute -x hlsl -fsyntax-only \
// RUN: -finclude-default-header -fnative-half-type -fnative-int16-type \
-// RUN: -verify=dxil %s
-
-// dxil-no-diagnostics
+// RUN: -verify=common,dxil %s
// Textures and typed buffers require a 32-bit float or a 32-bit or 64-bit
// integer element type on Vulkan. DXIL accepts the other element types. Without
@@ -16,25 +14,39 @@
// Rejected element types.
Texture2D<half> TexHalf;
-// vk16-error at -1 {{resource type 'Texture2D<half>' with element type 'half' is not supported when targeting Vulkan; 'half' is 16 bits wide, but Vulkan requires a 32-bit type or a 64-bit integer type}}
+// vk16-error at -1 {{constraints not satisfied for class template 'Texture2D' [with element_type = half]}}
+// vk16-note@*:* {{because 'half' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk16-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(half)' evaluated to false}}
Texture2DArray<half4> TexArrayHalf4;
-// vk16-error-re at -1 {{resource type 'Texture2DArray<half4>' {{.*}}with element type 'half' is not supported}}
+// vk16-error at -1 {{constraints not satisfied for class template 'Texture2DArray' [with element_type = half4]}}
+// vk16-note@*:* {{because 'half4' (aka 'vector<half, 4>') does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk16-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(vector<half, 4>)' evaluated to false}}
TextureCube<double> TexCubeDouble;
-// vk-error at -1 {{resource type 'TextureCube<double>' with element type 'double' is not supported when targeting Vulkan; 'double' is 64 bits wide, but Vulkan requires a 32-bit type or a 64-bit integer type}}
+// vk-error at -1 {{constraints not satisfied for class template 'TextureCube' [with element_type = double]}}
+// vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
RWTexture2D<double2> RWTexDouble2;
-// vk-error-re at -1 {{resource type 'RWTexture2D<double2>' {{.*}}with element type 'double' is not supported}}
+// vk-error at -1 {{constraints not satisfied for class template 'RWTexture2D' [with element_type = double2]}}
+// vk-note@*:* {{because 'double2' (aka 'vector<double, 2>') does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(vector<double, 2>)' evaluated to false}}
Buffer<double> BufDouble;
-// vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+// vk-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = double]}}
+// vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
#ifdef __HLSL_ENABLE_16_BIT
Texture2DMS<float16_t2> TexMSFloat16x2;
-// vk16-error-re at -1 {{resource type 'Texture2DMS<float16_t2>' {{.*}}with element type 'half' is not supported}}
+// vk16-error at -1 {{constraints not satisfied for class template 'Texture2DMS' [with element_type = float16_t2, sample_count = 0]}}
+// vk16-note@*:* {{because 'float16_t2' (aka 'vector<float16_t, 2>') does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk16-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(vector<half, 2>)' evaluated to false}}
RWBuffer<int16_t> RWBufInt16;
-// vk16-error-re at -1 {{resource type 'RWBuffer<int16_t>' {{.*}}with element type 'short' is not supported}}
+// vk16-error at -1 {{constraints not satisfied for class template 'RWBuffer' [with element_type = int16_t]}}
+// vk16-note@*:* {{because 'int16_t' (aka 'short') does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk16-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(short)' evaluated to false}}
Buffer<uint16_t4> BufUInt16x4;
-// vk16-error-re at -1 {{resource type 'Buffer<uint16_t4>' {{.*}}with element type 'unsigned short' is not supported}}
-RasterizerOrderedBuffer<vector<int16_t, 3> > ROBufInt16x3;
-// vk16-error at -1 {{resource type 'RasterizerOrderedBuffer<vector<int16_t, 3>>' with element type 'short' is not supported}}
+// vk16-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = uint16_t4]}}
+// vk16-note@*:* {{because 'uint16_t4' (aka 'vector<uint16_t, 4>') does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk16-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(vector<unsigned short, 4>)' evaluated to false}}
#endif
// Legal element types.
@@ -43,84 +55,122 @@ Texture2D<int> TexInt;
RWTexture2D<uint> RWTexUInt;
Buffer<int64_t> BufInt64;
RWBuffer<uint64_t2> RWBufUInt64x2;
-// HLSL bool has a 32-bit integer memory representation.
-RasterizerOrderedBuffer<bool4> ROBufBool4;
-// Structured and byte-address buffers are not images.
+// Vulkan's constraint still enforces the general typed-resource restrictions.
+Buffer<bool> BoolBuffer;
+// common-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = bool]}}
+// vk-note@*:* {{because 'bool' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(bool)' evaluated to false}}
+// dxil-note@*:* {{because 'bool' does not satisfy '__is_typed_resource_element_compatible'}}
+// dxil-note@*:* {{because '__builtin_hlsl_is_typed_resource_element_compatible(bool)' evaluated to false}}
+Buffer<float[4]> ArrayElement;
+// common-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = float[4]]}}
+// vk-note@*:* {{because 'float[4]' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(float[4])' evaluated to false}}
+// dxil-note@*:* {{because 'float[4]' does not satisfy '__is_typed_resource_element_compatible'}}
+// dxil-note@*:* {{because '__builtin_hlsl_is_typed_resource_element_compatible(float[4])' evaluated to false}}
+Buffer<uint64_t3> OversizedElement;
+// common-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = uint64_t3]}}
+// vk-note@*:* {{because 'uint64_t3' (aka 'vector<uint64_t, 3>') does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(vector<unsigned long, 3>)' evaluated to false}}
+// dxil-note@*:* {{because 'uint64_t3' (aka 'vector<uint64_t, 3>') does not satisfy '__is_typed_resource_element_compatible'}}
+// dxil-note@*:* {{because '__builtin_hlsl_is_typed_resource_element_compatible(vector<unsigned long, 3>)' evaluated to false}}
+struct Element { float Value; };
+Texture2D<Element> StructElement;
+// common-error at -1 {{constraints not satisfied for class template 'Texture2D' [with element_type = Element]}}
+// vk-note@*:* {{because 'Element' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(Element)' evaluated to false}}
+// dxil-note@*:* {{because 'Element' does not satisfy '__is_typed_resource_element_compatible'}}
+// dxil-note@*:* {{because '__builtin_hlsl_is_typed_resource_element_compatible(Element)' evaluated to false}}
+
+// The Vulkan builtin must be a complete alternative, including when queried
+// directly rather than through a resource's generated concept.
+static_assert(__builtin_hlsl_is_vulkan_typed_resource_element_compatible(float), "");
+static_assert(__builtin_hlsl_is_vulkan_typed_resource_element_compatible(uint64_t2), "");
+static_assert(!__builtin_hlsl_is_vulkan_typed_resource_element_compatible(bool), "");
+static_assert(!__builtin_hlsl_is_vulkan_typed_resource_element_compatible(Element), "");
+static_assert(!__builtin_hlsl_is_vulkan_typed_resource_element_compatible(float[4]), "");
+static_assert(!__builtin_hlsl_is_vulkan_typed_resource_element_compatible(uint64_t3), "");
+static_assert(!__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double), "");
+
+// Structured and byte-address buffers do not lower to images.
StructuredBuffer<half4> SBufHalf4;
StructuredBuffer<double> SBufDouble;
+RWStructuredBuffer<double2> RWSBufDouble2;
ByteAddressBuffer BAB;
+RWByteAddressBuffer RWBAB;
-// Declaration forms.
+// Reject invalid types even without a variable declaration or resource use.
+typedef Texture2D<double2> DoubleTexture;
+// vk-error at -1 {{constraints not satisfied for class template 'Texture2D' [with element_type = double2]}}
+// vk-note@*:* {{because 'double2' (aka 'vector<double, 2>') does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(vector<double, 2>)' evaluated to false}}
+Buffer<double> resourceResult();
+// vk-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = double]}}
+// vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
+void resourceParameter(Buffer<double> P);
+// vk-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = double]}}
+// vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
-Buffer<double> UsedBufDouble;
-// vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
static RWBuffer<half> StaticBufHalf;
-// vk16-error at -1 {{resource type 'RWBuffer<half>' with element type 'half' is not supported}}
-typedef Texture2D<double2> DoubleTexture;
-DoubleTexture TypedefTex;
-// vk-error at -1 {{resource type 'DoubleTexture' (aka 'Texture2D<vector<double, 2>>') with element type 'double' is not supported}}
+// vk16-error at -1 {{constraints not satisfied for class template 'RWBuffer' [with element_type = half]}}
+// vk16-note@*:* {{because 'half' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk16-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(half)' evaluated to false}}
Texture2D<double> TexArray[4];
-// vk-error at -1 {{resource type 'Texture2D<double>' with element type 'double' is not supported}}
+// vk-error at -1 {{constraints not satisfied for class template 'Texture2D' [with element_type = double]}}
+// vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
RWBuffer<double> BufArray2D[2][3];
-// vk-error at -1 {{resource type 'RWBuffer<double>' with element type 'double' is not supported}}
+// vk-error at -1 {{constraints not satisfied for class template 'RWBuffer' [with element_type = double]}}
+// vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+// vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
-// Embedded resources are diagnosed at the field, once for each global
-// declaration that contains them: GlobalStruct, GlobalStructArray, GlobalOuter
-// and GlobalDerived.
-struct S {
+// A field's resource type is checked even if the struct is never used.
+struct Unused {
Texture2D<double> Tex;
- // vk-error at -1 4 {{resource type 'Texture2D<double>' with element type 'double' is not supported}}
- Texture2D<float> Legal;
-};
-S GlobalStruct;
-S GlobalStructArray[2];
-
-struct Outer {
- RWBuffer<double> Bufs[2];
- // vk-error at -1 {{resource type 'RWBuffer<double>' with element type 'double' is not supported}}
- S Inner;
+ // vk-error at -1 {{constraints not satisfied for class template 'Texture2D' [with element_type = double]}}
+ // vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+ // vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
};
-Outer GlobalOuter;
-
-struct Derived : S {
- RWBuffer<half> Extra;
- // vk16-error at -1 {{resource type 'RWBuffer<half>' with element type 'half' is not supported}}
-};
-Derived GlobalDerived;
template<typename T> struct ResourceMember {
Texture2D<T> Tex;
- // vk-error at -1 {{resource type 'Texture2D<double>' with element type 'double' is not supported}}
+ // vk-error at -1 {{constraints not satisfied for class template 'Texture2D' [with element_type = double]}}
+ // vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+ // vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
};
ResourceMember<double> InstantiatedMember;
+// vk-note at -1 {{in instantiation of template class 'ResourceMember<double>' requested here}}
-// Static resources in a template are diagnosed when it is instantiated.
template<typename T> void staticInTemplate() {
static Buffer<T> B;
- // vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+ // vk-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = double]}}
+ // vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+ // vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
}
-void resourceParameter(Buffer<double> P);
-// vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
-
-// A template parameter is checked while the template arguments are deduced,
-// so the error makes the candidate non-viable.
-template<typename T>
-void parameterInTemplate(Buffer<T> P) {}
-// vk-note at -1 {{candidate template ignored: substitution failure [with T = double]: resource type 'Buffer<double>' with element type 'double' is not supported}}
+// Constraint failure during deduction must discard the candidate, not produce
+// a hard error. In DXIL, Buffer<double> is valid and the template is selected.
+template<typename T> Buffer<T> deduceResource(T Value);
+float deduceResource(...);
+#ifdef __SPIRV__
+static_assert(__is_same(decltype(deduceResource(1.0L)), float), "");
+#else
+static_assert(__is_same(decltype(deduceResource(1.0L)), Buffer<double>), "");
+#endif
[numthreads(1, 1, 1)]
void main() {
- double D = UsedBufDouble[0];
staticInTemplate<double>();
// vk-note at -1 {{in instantiation of function template specialization 'staticInTemplate<double>' requested here}}
- parameterInTemplate(UsedBufDouble);
- // vk-error at -1 {{no matching function for call to 'parameterInTemplate'}}
-
RWBuffer<double> FromHeap = ResourceDescriptorHeap[0];
- // vk-error at -1 {{resource type 'RWBuffer<double>' with element type 'double' is not supported}}
- // A local copy of a diagnosed global is diagnosed again.
- Buffer<double> Copy = UsedBufDouble;
- // vk-error at -1 {{resource type 'Buffer<double>' with element type 'double' is not supported}}
+ // vk-error at -1 {{constraints not satisfied for class template 'RWBuffer' [with element_type = double]}}
+ // vk-note@*:* {{because 'double' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+ // vk-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(double)' evaluated to false}}
+ Buffer<half> LocalHalf;
+ // vk16-error at -1 {{constraints not satisfied for class template 'Buffer' [with element_type = half]}}
+ // vk16-note@*:* {{because 'half' does not satisfy '__is_vulkan_typed_resource_element_compatible'}}
+ // vk16-note@*:* {{because '__builtin_hlsl_is_vulkan_typed_resource_element_compatible(half)' evaluated to false}}
}
diff --git a/llvm/docs/SPIRVUsage.md b/llvm/docs/SPIRVUsage.md
index 277e45e65cc927..1b6943a229258f 100644
--- a/llvm/docs/SPIRVUsage.md
+++ b/llvm/docs/SPIRVUsage.md
@@ -359,10 +359,7 @@ unknown, the backend cannot distinguish the two case.
When targeting Vulkan, the Sampled Type of every `OpTypeImage`, sampled or
storage, must be a scalar 32-bit float, 32-bit integer or 64-bit integer
-([VUID-StandaloneSpirv-OpTypeImage-04656][vulkan-image-sampled-type]). The
-backend reports an error for any other Sampled Type, whichever frontend produced
-the LLVM IR. Other target environments are not affected; OpenCL images, for
-example, use a `void` Sampled Type.
+([VUID-StandaloneSpirv-OpTypeImage-04656][vulkan-image-sampled-type]).
See [wg-hlsl proposal 0018](https://github.com/llvm/wg-hlsl/blob/main/proposals/0018-spirv-resource-representation.md)
for details on `spirv.VulkanBuffer`.
More information about the cfe-commits
mailing list