aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/CodeGen/CGHLSLRuntime.cpp
diff options
context:
space:
mode:
authorHelena Kotas <hekotas@microsoft.com>2025-05-15 19:49:29 -0700
committerGitHub <noreply@github.com>2025-05-15 19:49:29 -0700
commitf95f3030e595b76a3aa0295997e7dcf865c28796 (patch)
tree076dea0ee314eec398c71decaa5c943421e8a91a /clang/lib/CodeGen/CGHLSLRuntime.cpp
parent664c937b4378b4ecc12ac193f4205966a3aa2aac (diff)
downloadllvm-f95f3030e595b76a3aa0295997e7dcf865c28796.zip
llvm-f95f3030e595b76a3aa0295997e7dcf865c28796.tar.gz
llvm-f95f3030e595b76a3aa0295997e7dcf865c28796.tar.bz2
[HLSL] Implicit resource binding for cbuffers (#139022)
Constant buffers defined with the `cbuffer` keyword do not have a constructor. Instead, the call to initialize the resource handle based on its binding is generated in codegen. This change adds initialization of `cbuffer` handles that have implicit binding. Closes #139617
Diffstat (limited to 'clang/lib/CodeGen/CGHLSLRuntime.cpp')
-rw-r--r--clang/lib/CodeGen/CGHLSLRuntime.cpp50
1 files changed, 29 insertions, 21 deletions
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 5bc71a9..a708b3a 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -23,6 +23,7 @@
#include "clang/AST/Type.h"
#include "clang/Basic/TargetOptions.h"
#include "llvm/ADT/SmallVector.h"
+#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/LLVMContext.h"
@@ -42,8 +43,8 @@ using namespace llvm;
using llvm::hlsl::CBufferRowSizeInBytes;
static void initializeBufferFromBinding(CodeGenModule &CGM,
- llvm::GlobalVariable *GV, unsigned Slot,
- unsigned Space);
+ llvm::GlobalVariable *GV,
+ HLSLResourceBindingAttr *RBA);
namespace {
@@ -271,13 +272,10 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) {
emitBufferGlobalsAndMetadata(BufDecl, BufGV);
// Initialize cbuffer from binding (implicit or explicit)
- const HLSLResourceBindingAttr *RBA =
- BufDecl->getAttr<HLSLResourceBindingAttr>();
- // FIXME: handle implicit binding if no binding attribute is found
- // (llvm/llvm-project#110722)
- if (RBA && RBA->hasRegisterSlot())
- initializeBufferFromBinding(CGM, BufGV, RBA->getSlotNumber(),
- RBA->getSpaceNumber());
+ HLSLResourceBindingAttr *RBA = BufDecl->getAttr<HLSLResourceBindingAttr>();
+ assert(RBA &&
+ "cbuffer/tbuffer should always have resource binding attribute");
+ initializeBufferFromBinding(CGM, BufGV, RBA);
}
llvm::TargetExtType *
@@ -560,19 +558,29 @@ static void initializeBuffer(CodeGenModule &CGM, llvm::GlobalVariable *GV,
}
static void initializeBufferFromBinding(CodeGenModule &CGM,
- llvm::GlobalVariable *GV, unsigned Slot,
- unsigned Space) {
+ llvm::GlobalVariable *GV,
+ HLSLResourceBindingAttr *RBA) {
llvm::Type *Int1Ty = llvm::Type::getInt1Ty(CGM.getLLVMContext());
- llvm::Value *Args[] = {
- llvm::ConstantInt::get(CGM.IntTy, Space), /* reg_space */
- llvm::ConstantInt::get(CGM.IntTy, Slot), /* lower_bound */
- llvm::ConstantInt::get(CGM.IntTy, 1), /* range_size */
- llvm::ConstantInt::get(CGM.IntTy, 0), /* index */
- llvm::ConstantInt::get(Int1Ty, false) /* non-uniform */
- };
- initializeBuffer(CGM, GV,
- CGM.getHLSLRuntime().getCreateHandleFromBindingIntrinsic(),
- Args);
+ auto *NonUniform = llvm::ConstantInt::get(Int1Ty, false);
+ auto *Index = llvm::ConstantInt::get(CGM.IntTy, 0);
+ auto *RangeSize = llvm::ConstantInt::get(CGM.IntTy, 1);
+ auto *Space =
+ llvm::ConstantInt::get(CGM.IntTy, RBA ? RBA->getSpaceNumber() : 0);
+
+ if (RBA->hasRegisterSlot()) {
+ auto *RegSlot = llvm::ConstantInt::get(CGM.IntTy, RBA->getSlotNumber());
+ Intrinsic::ID Intr =
+ CGM.getHLSLRuntime().getCreateHandleFromBindingIntrinsic();
+ initializeBuffer(CGM, GV, Intr,
+ {Space, RegSlot, RangeSize, Index, NonUniform});
+ } else {
+ auto *OrderID =
+ llvm::ConstantInt::get(CGM.IntTy, RBA->getImplicitBindingOrderID());
+ Intrinsic::ID Intr =
+ CGM.getHLSLRuntime().getCreateHandleFromImplicitBindingIntrinsic();
+ initializeBuffer(CGM, GV, Intr,
+ {OrderID, Space, RangeSize, Index, NonUniform});
+ }
}
llvm::Instruction *CGHLSLRuntime::getConvergenceToken(BasicBlock &BB) {