[llvm-branch-commits] [clang] [HLSL] Introduce address space `hlsl_constant(2)` for constant buffer declarations (PR #123411)

Tex Riddell via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jan 17 16:14:05 PST 2025


================
@@ -455,14 +456,22 @@ void createHostLayoutStructForBuffer(Sema &S, HLSLBufferDecl *BufDecl) {
   LS->setImplicit(true);
   LS->startDefinition();
 
-  for (const Decl *D : BufDecl->decls()) {
-    const VarDecl *VD = dyn_cast<VarDecl>(D);
+  for (Decl *D : BufDecl->decls()) {
+    VarDecl *VD = dyn_cast<VarDecl>(D);
     if (!VD || VD->getStorageClass() == SC_Static)
       continue;
     const Type *Ty = VD->getType()->getUnqualifiedDesugaredType();
     if (FieldDecl *FD = createFieldForHostLayoutStruct(
-            S, Ty, VD->getIdentifier(), LS, BufDecl))
+            S, Ty, VD->getIdentifier(), LS, BufDecl)) {
+      // add the field decl to the layout struct
       LS->addDecl(FD);
+      // update address space of the original decl to hlsl_constant
+      // and disable initialization
+      QualType NewTy =
+          AST.getAddrSpaceQualType(VD->getType(), LangAS::hlsl_constant);
+      VD->setType(NewTy);
+      VD->setInit(nullptr);
----------------
tex3d wrote:

Does this `VD->setInit(nullptr);` silently get rid of an initializer if there was one?  This feels a bit sketchy, unless I'm missing something.  I know we don't currently support capturing initializers for constant buffer values, but it is part of HLSL syntax and could in theory be captured.  Silently erasing it from the AST node at this point seems weird.

https://github.com/llvm/llvm-project/pull/123411


More information about the llvm-branch-commits mailing list