@@ -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 *
@@ -494,15 +495,15 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
494495 }
495496}
496497
497- static void createResourceInitFn (CodeGenModule &CGM, llvm::GlobalVariable *GV,
498- unsigned Slot, unsigned Space) {
499- LLVMContext &Ctx = CGM.getLLVMContext ();
500- llvm::Type *Int1Ty = llvm::Type::getInt1Ty (Ctx);
498+ static void initializeBuffer (CodeGenModule &CGM, llvm::GlobalVariable *GV,
499+ Intrinsic::ID IntrID,
500+ ArrayRef<llvm::Value *> Args) {
501501
502+ LLVMContext &Ctx = CGM.getLLVMContext ();
502503 llvm::Function *InitResFunc = llvm::Function::Create (
503504 llvm::FunctionType::get (CGM.VoidTy , false ),
504505 llvm::GlobalValue::InternalLinkage,
505- (" _init_resource_ " + GV->getName ()).str (), CGM.getModule ());
506+ (" _init_buffer_ " + GV->getName ()).str (), CGM.getModule ());
506507 InitResFunc->addFnAttr (llvm::Attribute::AlwaysInline);
507508
508509 llvm::BasicBlock *EntryBB =
@@ -511,28 +512,12 @@ static void createResourceInitFn(CodeGenModule &CGM, llvm::GlobalVariable *GV,
511512 const DataLayout &DL = CGM.getModule ().getDataLayout ();
512513 Builder.SetInsertPoint (EntryBB);
513514
514- // Make sure the global variable is resource handle (cbuffer) or
515- // resource class (=class where the first element is a resource handle).
515+ // Make sure the global variable is buffer resource handle
516516 llvm::Type *HandleTy = GV->getValueType ();
517- assert ((HandleTy->isTargetExtTy () ||
518- (HandleTy->isStructTy () &&
519- HandleTy->getStructElementType (0 )->isTargetExtTy ())) &&
520- " unexpected type of the global" );
521- if (!HandleTy->isTargetExtTy ())
522- HandleTy = HandleTy->getStructElementType (0 );
517+ assert (HandleTy->isTargetExtTy () && " unexpected type of the buffer global" );
523518
524- llvm::Value *Args[] = {
525- llvm::ConstantInt::get (CGM.IntTy , Space), /* reg_space */
526- llvm::ConstantInt::get (CGM.IntTy , Slot), /* lower_bound */
527- // FIXME: resource arrays are not yet implemented
528- llvm::ConstantInt::get (CGM.IntTy , 1 ), /* range_size */
529- llvm::ConstantInt::get (CGM.IntTy , 0 ), /* index */
530- // FIXME: NonUniformResourceIndex bit is not yet implemented
531- llvm::ConstantInt::get (Int1Ty, false ) /* non-uniform */
532- };
533519 llvm::Value *CreateHandle = Builder.CreateIntrinsic (
534- /* ReturnType=*/ HandleTy,
535- CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic (), Args, nullptr ,
520+ /* ReturnType=*/ HandleTy, IntrID, Args, nullptr ,
536521 Twine (GV->getName ()).concat (" _h" ));
537522
538523 llvm::Value *HandleRef = Builder.CreateStructGEP (GV->getValueType (), GV, 0 );
@@ -543,26 +528,25 @@ static void createResourceInitFn(CodeGenModule &CGM, llvm::GlobalVariable *GV,
543528 CGM.AddCXXGlobalInit (InitResFunc);
544529}
545530
546- void CGHLSLRuntime::handleGlobalVarDefinition (const VarDecl *VD,
547- llvm::GlobalVariable *GV) {
548-
549- // If the global variable has resource binding, create an init function
550- // for the resource
551- const HLSLResourceBindingAttr *RBA = VD->getAttr <HLSLResourceBindingAttr>();
552- if (!RBA)
553- // FIXME: collect unbound resources for implicit binding resolution later
554- // on?
555- return ;
556-
557- if (!VD->getType ().getTypePtr ()->isHLSLResourceRecord ())
558- // FIXME: Only simple declarations of resources are supported for now.
559- // Arrays of resources or resources in user defined classes are
560- // not implemented yet.
561- return ;
562-
563- createResourceInitFn (CGM, GV, RBA->getSlotNumber (), RBA->getSpaceNumber ());
531+ static void initializeBufferFromBinding (CodeGenModule &CGM,
532+ llvm::GlobalVariable *GV, unsigned Slot,
533+ unsigned Space) {
534+ llvm::Type *Int1Ty = llvm::Type::getInt1Ty (CGM.getLLVMContext ());
535+ llvm::Value *Args[] = {
536+ llvm::ConstantInt::get (CGM.IntTy , Space), /* reg_space */
537+ llvm::ConstantInt::get (CGM.IntTy , Slot), /* lower_bound */
538+ llvm::ConstantInt::get (CGM.IntTy , 1 ), /* range_size */
539+ llvm::ConstantInt::get (CGM.IntTy , 0 ), /* index */
540+ llvm::ConstantInt::get (Int1Ty, false ) /* non-uniform */
541+ };
542+ initializeBuffer (CGM, GV,
543+ CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic (),
544+ Args);
564545}
565546
547+ void CGHLSLRuntime::handleGlobalVarDefinition (const VarDecl *VD,
548+ llvm::GlobalVariable *GV) {}
549+
566550llvm::Instruction *CGHLSLRuntime::getConvergenceToken (BasicBlock &BB) {
567551 if (!CGM.shouldEmitConvergenceTokens ())
568552 return nullptr ;
0 commit comments