[clang] [HLSL] Fix global resource initialization (PR #123394)
Helena Kotas via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 22 10:18:30 PST 2025
================
@@ -536,89 +536,84 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
}
}
-void CGHLSLRuntime::handleGlobalVarDefinition(const VarDecl *VD,
- llvm::GlobalVariable *GV) {
- // 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 (!HLSLAttributedResourceType::findHandleTypeOnResource(
- VD->getType().getTypePtr()))
- // 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(VD, GV);
-}
-
-bool CGHLSLRuntime::needsResourceBindingInitFn() {
- return !ResourcesToBind.empty();
+// Returns true if the type is an HLSL resource class
+static bool isResourceRecordType(const clang::Type *Ty) {
+ return HLSLAttributedResourceType::findHandleTypeOnResource(Ty) != nullptr;
}
-llvm::Function *CGHLSLRuntime::createResourceBindingInitFn() {
- // No resources to bind
- assert(needsResourceBindingInitFn() && "no resources to bind");
-
+static void createResourceInitFn(CodeGenModule &CGM, const VarDecl *VD,
+ llvm::GlobalVariable *GV, unsigned Slot,
+ unsigned Space) {
LLVMContext &Ctx = CGM.getLLVMContext();
llvm::Type *Int1Ty = llvm::Type::getInt1Ty(Ctx);
- llvm::Function *InitResBindingsFunc =
- llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy, false),
- llvm::GlobalValue::InternalLinkage,
- "_init_resource_bindings", CGM.getModule());
+ llvm::Function *InitResFunc = llvm::Function::Create(
+ llvm::FunctionType::get(CGM.VoidTy, false),
+ llvm::GlobalValue::InternalLinkage,
+ ("_init_resource_" + VD->getName()).str(), CGM.getModule());
----------------
hekota wrote:
Sounds good. I have added the AlwaysInline attribute. BTW where did the code you used in the example come from? Existing test?
https://github.com/llvm/llvm-project/pull/123394
More information about the cfe-commits
mailing list