[clang] [HLSL] Implement codegen for copying cbuffer structs with resources (PR #204232)
Helena Kotas via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 25 13:54:48 PDT 2026
================
@@ -627,11 +688,53 @@ class HLSLBufferCopyEmitter {
//
// See https://github.com/llvm/wg-hlsl/issues/351
emitElementCopy(SrcPtr.getBasePointer(), LayoutTy, DstPtr.getBasePointer(),
- DstPtr.getElementType());
+ DstPtr.getElementType(), EmitResFn);
return true;
}
};
+// Represents a list resources associated with a global struct whose name
+// starts with the specified prefix.
+// The order of HLSLAssociatedResourceDeclAttr attributes is identical to the
+// order of the depth-first traversal of the corresponding fields in the struct.
+// The resources are always returned in that order, which is the same order
+// we need when a struct is copied element-by-element.
+class AssociatedResourcesList {
+ specific_attr_iterator<HLSLAssociatedResourceDeclAttr> Begin, End, Next;
+
+public:
+ AssociatedResourcesList(const VarDecl *StructVD,
+ StringRef ResourceNamePrefix) {
+ auto I = StructVD->specific_attr_begin<HLSLAssociatedResourceDeclAttr>();
+ auto E = StructVD->specific_attr_end<HLSLAssociatedResourceDeclAttr>();
+
+ // Skip over associated resources that don't match the prefix.
+ while (I != E &&
+ !I->getResDecl()->getName().starts_with(ResourceNamePrefix))
+ ++I;
+ assert(I != E && "expected associated resource not found");
+ Begin = End = I;
+
+ // Scan over associated resources that do match the prefix to find the end
+ // of the range.
+ while (I != E && ((HLSLAssociatedResourceDeclAttr *)*I)
+ ->getResDecl()
+ ->getName()
+ .starts_with(ResourceNamePrefix))
+ End = ++I;
+ Next = Begin;
----------------
hekota wrote:
The number of times `getNextResource` is called is the same as the number of resources found in the struct, which is identical to the number of resource globals associated with the variable declaration (with the provided prefix). The assert on line 2054 makes sure the number is at least the same as the number of resources needed. The asserts after that makes sure the resource type matched. I could add another assert after the `emitCopy` call to make sure there are no resources left of the list (that `getNextResource` returns null).
https://github.com/llvm/llvm-project/pull/204232
More information about the cfe-commits
mailing list