-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[HLSL][SPIRV] Add vk::binding attribute #150957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,10 +273,14 @@ void CGHLSLRuntime::addBuffer(const HLSLBufferDecl *BufDecl) { | |
emitBufferGlobalsAndMetadata(BufDecl, BufGV); | ||
|
||
// Initialize cbuffer from binding (implicit or explicit) | ||
HLSLResourceBindingAttr *RBA = BufDecl->getAttr<HLSLResourceBindingAttr>(); | ||
assert(RBA && | ||
"cbuffer/tbuffer should always have resource binding attribute"); | ||
initializeBufferFromBinding(BufDecl, BufGV, RBA); | ||
if (HLSLVkBindingAttr *VkBinding = BufDecl->getAttr<HLSLVkBindingAttr>()) { | ||
initializeBufferFromBinding(BufDecl, BufGV, VkBinding); | ||
} else { | ||
HLSLResourceBindingAttr *RBA = BufDecl->getAttr<HLSLResourceBindingAttr>(); | ||
assert(RBA && | ||
"cbuffer/tbuffer should always have resource binding attribute"); | ||
initializeBufferFromBinding(BufDecl, BufGV, RBA); | ||
} | ||
} | ||
|
||
llvm::TargetExtType * | ||
|
@@ -593,6 +597,31 @@ static void initializeBuffer(CodeGenModule &CGM, llvm::GlobalVariable *GV, | |
CGM.AddCXXGlobalInit(InitResFunc); | ||
} | ||
|
||
static Value *buildNameForResource(llvm::StringRef BaseName, | ||
CodeGenModule &CGM) { | ||
std::string Str(BaseName); | ||
std::string GlobalName(Str + ".str"); | ||
return CGM.GetAddrOfConstantCString(Str, GlobalName.c_str()).getPointer(); | ||
} | ||
|
||
void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl, | ||
llvm::GlobalVariable *GV, | ||
HLSLVkBindingAttr *VkBinding) { | ||
assert(VkBinding && "expect a nonnull binding attribute"); | ||
llvm::Type *Int1Ty = llvm::Type::getInt1Ty(CGM.getLLVMContext()); | ||
auto *NonUniform = llvm::ConstantInt::get(Int1Ty, false); | ||
auto *Index = llvm::ConstantInt::get(CGM.IntTy, 0); | ||
auto *RangeSize = llvm::ConstantInt::get(CGM.IntTy, 1); | ||
auto *Set = llvm::ConstantInt::get(CGM.IntTy, VkBinding->getSet()); | ||
auto *Binding = llvm::ConstantInt::get(CGM.IntTy, VkBinding->getBinding()); | ||
Value *Name = buildNameForResource(BufDecl->getName(), CGM); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please update the DXIL version of
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with merging this PR without the change I suggested. |
||
llvm::Intrinsic::ID IntrinsicID = | ||
CGM.getHLSLRuntime().getCreateHandleFromBindingIntrinsic(); | ||
|
||
SmallVector<Value *> Args{Set, Binding, RangeSize, Index, NonUniform, Name}; | ||
initializeBuffer(CGM, GV, IntrinsicID, Args); | ||
} | ||
|
||
void CGHLSLRuntime::initializeBufferFromBinding(const HLSLBufferDecl *BufDecl, | ||
llvm::GlobalVariable *GV, | ||
HLSLResourceBindingAttr *RBA) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// RUN: %clang_cc1 -triple spirv-unknown-vulkan1.3-library -finclude-default-header -ast-dump -o - %s | FileCheck %s -check-prefixes=SPV,CHECK | ||
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.8-library -finclude-default-header -ast-dump -o - %s | FileCheck %s -check-prefixes=DXIL,CHECK | ||
|
||
// CHECK: VarDecl {{.*}} Buf 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>' | ||
// SPV-NEXT: CXXConstructExpr {{.*}} 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>' 'void (unsigned int, unsigned int, int, unsigned int, const char *)' | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 23 | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 102 | ||
// DXIL-NEXT: CXXConstructExpr {{.*}} 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>' 'void (unsigned int, int, unsigned int, unsigned int, const char *)' | ||
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0 | ||
// DXIL-NEXT: IntegerLiteral {{.*}} 'int' 1 | ||
// SPV: HLSLVkBindingAttr {{.*}} 23 102 | ||
// DXIL-NOT: HLSLVkBindingAttr | ||
[[vk::binding(23, 102)]] StructuredBuffer<float> Buf; | ||
|
||
// CHECK: VarDecl {{.*}} Buf2 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>' | ||
// CHECK-NEXT: CXXConstructExpr {{.*}} 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>' 'void (unsigned int, unsigned int, int, unsigned int, const char *)' | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 14 | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 1 | ||
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 23 | ||
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 102 | ||
// SPV: HLSLVkBindingAttr {{.*}} 14 1 | ||
// DXIL-NOT: HLSLVkBindingAttr | ||
// CHECK: HLSLResourceBindingAttr {{.*}} "t23" "space102" | ||
[[vk::binding(14, 1)]] StructuredBuffer<float> Buf2 : register(t23, space102); | ||
|
||
// CHECK: VarDecl {{.*}} Buf3 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>' | ||
// CHECK-NEXT: CXXConstructExpr {{.*}} 'StructuredBuffer<float>':'hlsl::StructuredBuffer<float>' 'void (unsigned int, unsigned int, int, unsigned int, const char *)' | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 14 | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 0 | ||
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 23 | ||
// DXIL-NEXT: IntegerLiteral {{.*}} 'unsigned int' 102 | ||
// SPV: HLSLVkBindingAttr {{.*}} 14 0 | ||
// DXIL-NOT: HLSLVkBindingAttr | ||
// CHECK: HLSLResourceBindingAttr {{.*}} "t23" "space102" | ||
[[vk::binding(14)]] StructuredBuffer<float> Buf3 : register(t23, space102); | ||
|
||
// CHECK: HLSLBufferDecl {{.*}} cbuffer CB | ||
// CHECK-NEXT: HLSLResourceClassAttr {{.*}} Implicit CBuffer | ||
// SPV-NEXT: HLSLVkBindingAttr {{.*}} 1 2 | ||
// DXIL-NOT: HLSLVkBindingAttr | ||
[[vk::binding(1, 2)]] cbuffer CB { | ||
float a; | ||
} | ||
|
||
// CHECK: VarDecl {{.*}} Buf4 'Buffer<int>':'hlsl::Buffer<int>' | ||
// SPV-NEXT: CXXConstructExpr {{.*}} 'Buffer<int>':'hlsl::Buffer<int>' 'void (unsigned int, unsigned int, int, unsigned int, const char *)' | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 24 | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 103 | ||
// DXL-NEXT: CXXConstructExpr {{.*}} 'Buffer<int>':'hlsl::Buffer<int>' 'void (unsigned int, int, unsigned int, unsigned int, const char *)' | ||
// SPV: HLSLVkBindingAttr {{.*}} 24 103 | ||
// DXIL-NOT: HLSLVkBindingAttr | ||
[[vk::binding(24, 103)]] Buffer<int> Buf4; | ||
|
||
// CHECK: VarDecl {{.*}} Buf5 'RWBuffer<int2>':'hlsl::RWBuffer<vector<int, 2>>' | ||
// SPV-NEXT: CXXConstructExpr {{.*}} 'RWBuffer<int2>':'hlsl::RWBuffer<vector<int, 2>>' 'void (unsigned int, unsigned int, int, unsigned int, const char *)' | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 25 | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 104 | ||
// DXL-NEXT: CXXConstructExpr {{.*}} 'Buffer<int>':'hlsl::Buffer<int>' 'void (unsigned int, int, unsigned int, unsigned int, const char *)' | ||
// SPV: HLSLVkBindingAttr {{.*}} 25 104 | ||
// DXIL-NOT: HLSLVkBindingAttr | ||
[[vk::binding(25, 104)]] RWBuffer<int2> Buf5; | ||
|
||
// CHECK: VarDecl {{.*}} Buf6 'RWStructuredBuffer<int>':'hlsl::RWStructuredBuffer<int>' | ||
// SPV-NEXT: CXXConstructExpr {{.*}} 'RWStructuredBuffer<int>':'hlsl::RWStructuredBuffer<int>' 'void (unsigned int, unsigned int, int, unsigned int, const char *)' | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 26 | ||
// SPV-NEXT: IntegerLiteral {{.*}} 'unsigned int' 105 | ||
// DXL-NEXT: CXXConstructExpr {{.*}} 'Buffer<int>':'hlsl::Buffer<int>' 'void (unsigned int, int, unsigned int, unsigned int, const char *)' | ||
// SPV: HLSLVkBindingAttr {{.*}} 26 105 | ||
// DXIL-NOT: HLSLVkBindingAttr | ||
[[vk::binding(26, 105)]] RWStructuredBuffer<int> Buf6; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// RUN: %clang_cc1 -triple spirv-unknown-vulkan1.3-library -finclude-default-header -O3 -emit-llvm -o - %s | FileCheck %s | ||
// CHECK: [[Buf:@.*]] = private unnamed_addr constant [4 x i8] c"Buf\00" | ||
// CHECK: [[Buf2:@.*]] = private unnamed_addr constant [5 x i8] c"Buf2\00" | ||
// CHECK: [[Buf3:@.*]] = private unnamed_addr constant [5 x i8] c"Buf3\00" | ||
// CHECK: [[CB:@.*]] = private unnamed_addr constant [3 x i8] c"CB\00" | ||
// CHECK: [[CB2:@.*]] = private unnamed_addr constant [4 x i8] c"CB2\00" | ||
// CHECK: [[Buf4:@.*]] = private unnamed_addr constant [5 x i8] c"Buf4\00" | ||
// CHECK: [[Buf5:@.*]] = private unnamed_addr constant [5 x i8] c"Buf5\00" | ||
// CHECK: [[Buf6:@.*]] = private unnamed_addr constant [5 x i8] c"Buf6\00" | ||
|
||
[[vk::binding(23, 102)]] StructuredBuffer<float> Buf; | ||
[[vk::binding(14, 1)]] StructuredBuffer<float> Buf2 : register(t23, space102); | ||
[[vk::binding(14)]] StructuredBuffer<float> Buf3 : register(t23, space102); | ||
|
||
[[vk::binding(1, 2)]] cbuffer CB { | ||
float a; | ||
}; | ||
|
||
[[vk::binding(10,20)]] cbuffer CB2 { | ||
float b; | ||
}; | ||
|
||
|
||
[[vk::binding(24, 103)]] Buffer<int> Buf4; | ||
[[vk::binding(25, 104)]] RWBuffer<int2> Buf5; | ||
[[vk::binding(26, 105)]] RWStructuredBuffer<float> Buf6; | ||
|
||
[numthreads(1,1,1)] | ||
void main() { | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 102, i32 23, {{.*}} [[Buf]]) | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 1, i32 14, {{.*}} [[Buf2]]) | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 0, i32 14, {{.*}} [[Buf3]]) | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 2, i32 1, {{.*}} [[CB]]) | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 20, i32 10, {{.*}} [[CB2]]) | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 103, i32 24, {{.*}} [[Buf4]]) | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 104, i32 25, {{.*}} [[Buf5]]) | ||
// CHECK: call {{.*}} @llvm.spv.resource.handlefrombinding{{.*}}(i32 105, i32 26, {{.*}} [[Buf6]]) | ||
float f1 = Buf.Load(0); | ||
float f2 = Buf2.Load(0); | ||
float f3 = Buf3.Load(0); | ||
int i = Buf4.Load(0); | ||
Buf5[0] = i; | ||
Buf6[0] = f1+f2+f3+a+b; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.