[llvm-branch-commits] [clang] [HLSL] Add binding attributes to resources from structs (PR #184731)

Justin Bogner via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Mar 19 14:03:44 PDT 2026


================
@@ -4616,13 +4643,99 @@ void SemaHLSL::deduceAddressSpace(VarDecl *Decl) {
 
 namespace {
 
+// Helper class for assigning bindings to resources declared within a struct.
+// It keeps track of all binding attributes declared on a struct instance, and
+// the offsets for each register type that have been assigned so far.
+// Handles both explicit and implicit bindings.
+class StructBindingContext {
+  // Bindings and offsets per register type. We only need to support four
+  // register types - SRV (u), UAV (t), CBuffer (c), and Sampler (s).
+  HLSLResourceBindingAttr *RegBindingsAttrs[4];
+  unsigned RegBindingOffset[4];
+
+  // Vulkan binding attribute does not vary by register type.
+  HLSLVkBindingAttr *VkBindingAttr;
+  unsigned VkBindingOffset;
+
+public:
+  // Constructor: gather all binding attributes on a struct instance and
+  // initialize offsets.
+  StructBindingContext(VarDecl *VD) {
+    for (unsigned i = 0; i < 4; ++i) {
+      RegBindingsAttrs[i] = nullptr;
+      RegBindingOffset[i] = 0;
+    }
+    VkBindingAttr = nullptr;
+    VkBindingOffset = 0;
+
+    ASTContext &AST = VD->getASTContext();
+    bool IsSpirv = AST.getTargetInfo().getTriple().isSPIRV();
+
+    for (Attr *A : VD->attrs()) {
+      if (auto *RBA = dyn_cast<HLSLResourceBindingAttr>(A)) {
+        RegisterType RegType = RBA->getRegisterType();
+        unsigned RegTypeIdx = static_cast<unsigned>(RegType);
+        // Ignore unsupported register annotations, such as 'c' or 'i'.
+        if (RegTypeIdx < 4)
+          RegBindingsAttrs[RegTypeIdx] = RBA;
----------------
bogner wrote:

The assumption here is that the valid/interesting register types are contiguous enum values starting from zero, and that C and I are at the end of that list. Unfortunately I find that that assumption is fairly implicit (and the "such as..." wording in the comment doesn't help).

Can we throw some static asserts here that all of SRV, UAV, CBuffer, and Sampler are the values 0-4 or something, just to make this assumption harder to break?

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


More information about the llvm-branch-commits mailing list