[clang] [llvm] [HLSL] Add handle initialization for simple resource declarations (PR #111207)

Justin Bogner via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 15 07:59:06 PDT 2024


================
@@ -489,3 +494,100 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
       GV->eraseFromParent();
   }
 }
+
+// Returns handle type of a resource, if the type is a resource
+// or an array of resources
+static const HLSLAttributedResourceType *findHandleTypeOnResource(QualType QT) {
+  // If the type is a resource class, the first field must
+  // be the resource handle of type HLSLAttributedResourceType
+  const clang::Type *Ty = QT->getUnqualifiedDesugaredType();
+  if (RecordDecl *RD = Ty->getAsCXXRecordDecl()) {
+    if (!RD->fields().empty()) {
+      const auto &FirstFD = RD->fields().begin();
+      return dyn_cast<HLSLAttributedResourceType>(
+          FirstFD->getType().getTypePtr());
+    }
+  }
+  return nullptr;
+}
+
+void CGHLSLRuntime::handleGlobalVarDefinition(const VarDecl *VD,
+                                              llvm::GlobalVariable *Var) {
+  // If the global variable has resource binding, add it to the list of globals
+  // that need resource binding initialization.
+  const HLSLResourceBindingAttr *RBA = VD->getAttr<HLSLResourceBindingAttr>();
+  if (!RBA)
+    return;
+
+  if (!findHandleTypeOnResource(VD->getType()))
+    // FIXME: Only simple declarations of resources are supported for now.
+    // Arrays of resources or resources in user defined classes are
+    // not implemented yet.
+    return;
+
+  ResourcesToBind.emplace_back(std::make_pair(VD, Var));
----------------
bogner wrote:

This probably isn't doing what you intended - the constructor that emplace_back is calling is pair's copy constructor since we're feeding it with make_pair.

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


More information about the cfe-commits mailing list