https://github.com/s-perron created 
https://github.com/llvm/llvm-project/pull/145577

The testing only tried `unsigned int` and not `uint`. We want to
correctly handle these surgared types as specialization constants.


>From 2bed922b3bd084fa43c8dc1843fb5bf67731e5ab Mon Sep 17 00:00:00 2001
From: Steven Perron <stevenper...@google.com>
Date: Tue, 24 Jun 2025 15:44:10 -0400
Subject: [PATCH] [HLSL][SPIRV] Handle `uint` type for spec constant

The testing only tried `unsigned int` and not `uint`. We want to
correctly handle these surgared types as specialization constants.
---
 clang/lib/Sema/SemaHLSL.cpp                     |  8 +++++---
 clang/test/AST/HLSL/vk.spec-constant.usage.hlsl | 11 +++++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index d003967a522a1..e36db2ee10c20 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -120,7 +120,7 @@ static ResourceClass getResourceClass(RegisterType RT) {
   llvm_unreachable("unexpected RegisterType value");
 }
 
-static Builtin::ID getSpecConstBuiltinId(QualType Type) {
+static Builtin::ID getSpecConstBuiltinId(const Type *Type) {
   const auto *BT = dyn_cast<BuiltinType>(Type);
   if (!BT) {
     if (!Type->isEnumeralType())
@@ -654,7 +654,8 @@ SemaHLSL::mergeVkConstantIdAttr(Decl *D, const 
AttributeCommonInfo &AL,
 
   auto *VD = cast<VarDecl>(D);
 
-  if (getSpecConstBuiltinId(VD->getType()) == Builtin::NotBuiltin) {
+  if (getSpecConstBuiltinId(VD->getType()->getUnqualifiedDesugaredType()) ==
+      Builtin::NotBuiltin) {
     Diag(VD->getLocation(), diag::err_specialization_const);
     return nullptr;
   }
@@ -3920,7 +3921,8 @@ bool SemaHLSL::handleInitialization(VarDecl *VDecl, Expr 
*&Init) {
     return false;
   }
 
-  Builtin::ID BID = getSpecConstBuiltinId(VDecl->getType());
+  Builtin::ID BID =
+      getSpecConstBuiltinId(VDecl->getType()->getUnqualifiedDesugaredType());
 
   // Argument 1: The ID from the attribute
   int ConstantID = ConstIdAttr->getId();
diff --git a/clang/test/AST/HLSL/vk.spec-constant.usage.hlsl 
b/clang/test/AST/HLSL/vk.spec-constant.usage.hlsl
index c0955c1ea7b43..733c4e2ee5a36 100644
--- a/clang/test/AST/HLSL/vk.spec-constant.usage.hlsl
+++ b/clang/test/AST/HLSL/vk.spec-constant.usage.hlsl
@@ -64,6 +64,17 @@ const unsigned short ushort_const = 10;
 [[vk::constant_id(6)]]
 const unsigned int uint_const = 12;
 
+// CHECK: VarDecl {{.*}} uint_const_2 'const hlsl_private uint':'const 
hlsl_private unsigned int' static cinit
+// CHECK-NEXT: CallExpr {{.*}} 'unsigned int'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'unsigned int (*)(unsigned int, 
unsigned int) noexcept' <FunctionToPointerDecay>
+// CHECK-NEXT: DeclRefExpr {{.*}} 'unsigned int (unsigned int, unsigned int) 
noexcept' lvalue Function {{.*}} '__builtin_get_spirv_spec_constant_uint' 
'unsigned int (unsigned int, unsigned int) noexcept'
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'unsigned int' <IntegralCast>
+// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 6
+// CHECK-NEXT: ImplicitCastExpr {{.*}} 'unsigned int' <IntegralCast>
+// CHECK-NEXT: IntegerLiteral {{.*}} 'int' 12
+[[vk::constant_id(6)]]
+const uint uint_const_2 = 12;
+
 
 // CHECK: VarDecl {{.*}} ulong_const 'const hlsl_private unsigned long long' 
static cinit
 // CHECK-NEXT: CallExpr {{.*}} 'unsigned long long'

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to