@@ -41,8 +41,9 @@ using namespace llvm;
4141
4242using llvm::hlsl::CBufferRowSizeInBytes;
4343
44- static void createResourceInitFn (CodeGenModule &CGM, llvm::GlobalVariable *GV,
45- unsigned Slot, unsigned Space);
44+ static void initializeBufferFromBinding (CodeGenModule &CGM,
45+ llvm::GlobalVariable *GV, unsigned Slot,
46+ unsigned Space);
4647
4748namespace {
4849
@@ -255,14 +256,14 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
255256 // Add globals for constant buffer elements and create metadata nodes
256257 emitBufferGlobalsAndMetadata (BufDecl, BufGV);
257258
258- // Resource initialization
259+ // Initialize cbuffer from binding (implicit or explicit)
259260 const HLSLResourceBindingAttr *RBA =
260261 BufDecl->getAttr <HLSLResourceBindingAttr>();
261262 // FIXME: handle implicit binding if no binding attribute is found
262263 // (llvm/llvm-project#110722)
263264 if (RBA)
264- createResourceInitFn (CGM, BufGV, RBA->getSlotNumber (),
265- RBA->getSpaceNumber ());
265+ initializeBufferFromBinding (CGM, BufGV, RBA->getSlotNumber (),
266+ RBA->getSpaceNumber ());
266267}
267268
268269llvm::TargetExtType *
@@ -505,15 +506,15 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
505506 }
506507}
507508
508- static void createResourceInitFn (CodeGenModule &CGM, llvm::GlobalVariable *GV,
509- unsigned Slot, unsigned Space) {
510- LLVMContext &Ctx = CGM.getLLVMContext ();
511- llvm::Type *Int1Ty = llvm::Type::getInt1Ty (Ctx);
509+ static void initializeBuffer (CodeGenModule &CGM, llvm::GlobalVariable *GV,
510+ Intrinsic::ID IntrID,
511+ ArrayRef<llvm::Value *> Args) {
512512
513+ LLVMContext &Ctx = CGM.getLLVMContext ();
513514 llvm::Function *InitResFunc = llvm::Function::Create (
514515 llvm::FunctionType::get (CGM.VoidTy , false ),
515516 llvm::GlobalValue::InternalLinkage,
516- (" _init_resource_ " + GV->getName ()).str (), CGM.getModule ());
517+ (" _init_buffer_ " + GV->getName ()).str (), CGM.getModule ());
517518 InitResFunc->addFnAttr (llvm::Attribute::AlwaysInline);
518519
519520 llvm::BasicBlock *EntryBB =
@@ -522,28 +523,12 @@ static void createResourceInitFn(CodeGenModule &CGM, llvm::GlobalVariable *GV,
522523 const DataLayout &DL = CGM.getModule ().getDataLayout ();
523524 Builder.SetInsertPoint (EntryBB);
524525
525- // Make sure the global variable is resource handle (cbuffer) or
526- // resource class (=class where the first element is a resource handle).
526+ // Make sure the global variable is buffer resource handle
527527 llvm::Type *HandleTy = GV->getValueType ();
528- assert ((HandleTy->isTargetExtTy () ||
529- (HandleTy->isStructTy () &&
530- HandleTy->getStructElementType (0 )->isTargetExtTy ())) &&
531- " unexpected type of the global" );
532- if (!HandleTy->isTargetExtTy ())
533- HandleTy = HandleTy->getStructElementType (0 );
528+ assert (HandleTy->isTargetExtTy () && " unexpected type of the buffer global" );
534529
535- llvm::Value *Args[] = {
536- llvm::ConstantInt::get (CGM.IntTy , Space), /* reg_space */
537- llvm::ConstantInt::get (CGM.IntTy , Slot), /* lower_bound */
538- // FIXME: resource arrays are not yet implemented
539- llvm::ConstantInt::get (CGM.IntTy , 1 ), /* range_size */
540- llvm::ConstantInt::get (CGM.IntTy , 0 ), /* index */
541- // FIXME: NonUniformResourceIndex bit is not yet implemented
542- llvm::ConstantInt::get (Int1Ty, false ) /* non-uniform */
543- };
544530 llvm::Value *CreateHandle = Builder.CreateIntrinsic (
545- /* ReturnType=*/ HandleTy,
546- CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic (), Args, nullptr ,
531+ /* ReturnType=*/ HandleTy, IntrID, Args, nullptr ,
547532 Twine (GV->getName ()).concat (" _h" ));
548533
549534 llvm::Value *HandleRef = Builder.CreateStructGEP (GV->getValueType (), GV, 0 );
@@ -554,24 +539,20 @@ static void createResourceInitFn(CodeGenModule &CGM, llvm::GlobalVariable *GV,
554539 CGM.AddCXXGlobalInit (InitResFunc);
555540}
556541
557- void CGHLSLRuntime::handleGlobalVarDefinition (const VarDecl *VD,
558- llvm::GlobalVariable *GV) {
559-
560- // If the global variable has resource binding, create an init function
561- // for the resource
562- const HLSLResourceBindingAttr *RBA = VD->getAttr <HLSLResourceBindingAttr>();
563- if (!RBA)
564- // FIXME: collect unbound resources for implicit binding resolution later
565- // on?
566- return ;
567-
568- if (!VD->getType ().getTypePtr ()->isHLSLResourceRecord ())
569- // FIXME: Only simple declarations of resources are supported for now.
570- // Arrays of resources or resources in user defined classes are
571- // not implemented yet.
572- return ;
573-
574- createResourceInitFn (CGM, GV, RBA->getSlotNumber (), RBA->getSpaceNumber ());
542+ static void initializeBufferFromBinding (CodeGenModule &CGM,
543+ llvm::GlobalVariable *GV, unsigned Slot,
544+ unsigned Space) {
545+ llvm::Type *Int1Ty = llvm::Type::getInt1Ty (CGM.getLLVMContext ());
546+ llvm::Value *Args[] = {
547+ llvm::ConstantInt::get (CGM.IntTy , Space), /* reg_space */
548+ llvm::ConstantInt::get (CGM.IntTy , Slot), /* lower_bound */
549+ llvm::ConstantInt::get (CGM.IntTy , 1 ), /* range_size */
550+ llvm::ConstantInt::get (CGM.IntTy , 0 ), /* index */
551+ llvm::ConstantInt::get (Int1Ty, false ) /* non-uniform */
552+ };
553+ initializeBuffer (CGM, GV,
554+ CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic (),
555+ Args);
575556}
576557
577558llvm::Instruction *CGHLSLRuntime::getConvergenceToken (BasicBlock &BB) {
0 commit comments