Skip to content

Conversation

hekota
Copy link
Member

@hekota hekota commented Aug 5, 2025

Adds support for fixed-size resource arrays declared at the global scope.

When a global resource array is indexed to access an individual resource, codegen will translate the ArraySubscriptExpr into a constructor call for the specific resource record type and binding.

Related to 145424

Adds support for fixed-size resource arrays declared at the global scope.

When a global resource array is indexed to access an individual resource,
codegen will translate the `ArraySubscriptExpr` into a constructor call for
the specific resource record type and binding.

Closes llvm#145424
@hekota
Copy link
Member Author

hekota commented Aug 5, 2025

Making this a draft for now because it only supports register binding and it will have conflicts with the change that adds [[vk:binding]] attribute (#150957). Once that gets merged, I will update this PR to resolve the conflicts and add [[vk:binding]] support for arrays as well.

@s-perron - is your change ready to get merged?

@s-perron
Copy link
Contributor

s-perron commented Aug 5, 2025

I'll see what I can do. I'm on vacation right now. I need to make the change you suggested

@hekota
Copy link
Member Author

hekota commented Aug 6, 2025

I'll see what I can do. I'm on vacation right now. I need to make the change you suggested

If that's the only thing blocking the merge, feel free to go ahead and merge it without my suggested change. I can do the update on my side. I think I need to move that function higher in the file anyway.

@hekota
Copy link
Member Author

hekota commented Aug 6, 2025

@s-perron - when you come back from your vacation - do you have any thoughts on how to update the CodeGenHLSL/convergence/global_array.hlsl? With this change resource arrays are no longer initialized with a loop that calls the default constructor.

The tests has been added in #140120.

Copy link

github-actions bot commented Aug 7, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- clang/include/clang/AST/Type.h clang/include/clang/Sema/SemaHLSL.h clang/lib/AST/Type.cpp clang/lib/CodeGen/CGExpr.cpp clang/lib/CodeGen/CGHLSLRuntime.cpp clang/lib/CodeGen/CGHLSLRuntime.h clang/lib/CodeGen/CodeGenModule.cpp clang/lib/Sema/SemaHLSL.cpp
View the diff from clang-format here.
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 8a362cc55..915e967c3 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -857,7 +857,8 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
   }
 
   // find binding info for the resource array
-  // (for implicit binding an HLSLResourceBindingAttr should have been added by SemaHLSL)
+  // (for implicit binding an HLSLResourceBindingAttr should have been added by
+  // SemaHLSL)
   QualType ResourceTy = ArraySubsExpr->getType();
   HLSLVkBindingAttr *VkBinding = ArrayDecl->getAttr<HLSLVkBindingAttr>();
   HLSLResourceBindingAttr *RBA = ArrayDecl->getAttr<HLSLResourceBindingAttr>();
@@ -865,8 +866,9 @@ std::optional<LValue> CGHLSLRuntime::emitResourceArraySubscriptExpr(
 
   // lookup the resource class constructor based on the resource type and
   // binding
-  CXXConstructorDecl *CD = findResourceConstructorDecl(
-      ArrayDecl->getASTContext(), ResourceTy, VkBinding || RBA->hasRegisterSlot());
+  CXXConstructorDecl *CD =
+      findResourceConstructorDecl(ArrayDecl->getASTContext(), ResourceTy,
+                                  VkBinding || RBA->hasRegisterSlot());
 
   // create a temporary variable for the resource class instance (we need to
   // return an LValue)
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index 5bfb8b933..baf30f6a9 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -3750,9 +3750,9 @@ void SemaHLSL::createResourceRecordCtorArgs(const Type *ResourceTy,
     uint32_t OrderID = (RBA && RBA->hasImplicitBindingOrderID())
                            ? RBA->getImplicitBindingOrderID()
                            : getNextImplicitBindingOrderID();
-    IntegerLiteral *OrderId = IntegerLiteral::Create(
-        AST, llvm::APInt(UIntTySize, OrderID), AST.UnsignedIntTy,
-        SourceLocation());
+    IntegerLiteral *OrderId =
+        IntegerLiteral::Create(AST, llvm::APInt(UIntTySize, OrderID),
+                               AST.UnsignedIntTy, SourceLocation());
     Args.append({Space, RangeSize, Index, OrderId, Name});
   }
 }

@hekota
Copy link
Member Author

hekota commented Aug 7, 2025

I am going to split the PR into 2 to make it easier to review.

@hekota hekota closed this Aug 7, 2025
@hekota hekota deleted the resource-array-subscript-one branch August 19, 2025 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants