@@ -41,8 +41,9 @@ using namespace llvm;
41
41
42
42
using llvm::hlsl::CBufferRowSizeInBytes;
43
43
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);
46
47
47
48
namespace {
48
49
@@ -255,14 +256,14 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
255
256
// Add globals for constant buffer elements and create metadata nodes
256
257
emitBufferGlobalsAndMetadata (BufDecl, BufGV);
257
258
258
- // Resource initialization
259
+ // Initialize cbuffer from binding (implicit or explicit)
259
260
const HLSLResourceBindingAttr *RBA =
260
261
BufDecl->getAttr <HLSLResourceBindingAttr>();
261
262
// FIXME: handle implicit binding if no binding attribute is found
262
263
// (llvm/llvm-project#110722)
263
264
if (RBA)
264
- createResourceInitFn (CGM, BufGV, RBA->getSlotNumber (),
265
- RBA->getSpaceNumber ());
265
+ initializeBufferFromBinding (CGM, BufGV, RBA->getSlotNumber (),
266
+ RBA->getSpaceNumber ());
266
267
}
267
268
268
269
llvm::TargetExtType *
@@ -505,15 +506,15 @@ void CGHLSLRuntime::generateGlobalCtorDtorCalls() {
505
506
}
506
507
}
507
508
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) {
512
512
513
+ LLVMContext &Ctx = CGM.getLLVMContext ();
513
514
llvm::Function *InitResFunc = llvm::Function::Create (
514
515
llvm::FunctionType::get (CGM.VoidTy , false ),
515
516
llvm::GlobalValue::InternalLinkage,
516
- (" _init_resource_ " + GV->getName ()).str (), CGM.getModule ());
517
+ (" _init_buffer_ " + GV->getName ()).str (), CGM.getModule ());
517
518
InitResFunc->addFnAttr (llvm::Attribute::AlwaysInline);
518
519
519
520
llvm::BasicBlock *EntryBB =
@@ -522,28 +523,12 @@ static void createResourceInitFn(CodeGenModule &CGM, llvm::GlobalVariable *GV,
522
523
const DataLayout &DL = CGM.getModule ().getDataLayout ();
523
524
Builder.SetInsertPoint (EntryBB);
524
525
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
527
527
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" );
534
529
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
- };
544
530
llvm::Value *CreateHandle = Builder.CreateIntrinsic (
545
- /* ReturnType=*/ HandleTy,
546
- CGM.getHLSLRuntime ().getCreateHandleFromBindingIntrinsic (), Args, nullptr ,
531
+ /* ReturnType=*/ HandleTy, IntrID, Args, nullptr ,
547
532
Twine (GV->getName ()).concat (" _h" ));
548
533
549
534
llvm::Value *HandleRef = Builder.CreateStructGEP (GV->getValueType (), GV, 0 );
@@ -554,24 +539,20 @@ static void createResourceInitFn(CodeGenModule &CGM, llvm::GlobalVariable *GV,
554
539
CGM.AddCXXGlobalInit (InitResFunc);
555
540
}
556
541
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);
575
556
}
576
557
577
558
llvm::Instruction *CGHLSLRuntime::getConvergenceToken (BasicBlock &BB) {
0 commit comments