Skip to content

Commit e2d2b1c

Browse files
committed
[HLSL][SPIRV] Implement the SPIR-V target type for cbuffers.
This change implement the type used to represent cbuffer for SPIR-V. Fixes #138274.
1 parent 6ee30e8 commit e2d2b1c

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

clang/lib/Basic/Targets/SPIR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static const unsigned SPIRDefIsPrivMap[] = {
4747
0, // ptr32_uptr
4848
0, // ptr64
4949
0, // hlsl_groupshared
50-
2, // hlsl_constant
50+
12, // hlsl_constant
5151
10, // hlsl_private
5252
11, // hlsl_device
5353
// Wasm address space values for this target are dummy values,

clang/lib/CodeGen/Targets/SPIR.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ABIInfoImpl.h"
10+
#include "HLSLBufferLayoutBuilder.h"
1011
#include "TargetInfo.h"
1112

1213
using namespace clang;
@@ -410,9 +411,19 @@ llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
410411
{RuntimeArrayType},
411412
{StorageClass, IsWritable});
412413
}
413-
case llvm::dxil::ResourceClass::CBuffer:
414-
llvm_unreachable("CBuffer handles are not implemented for SPIR-V yet");
414+
case llvm::dxil::ResourceClass::CBuffer: {
415+
QualType ContainedTy = ResType->getContainedType();
416+
if (ContainedTy.isNull() || !ContainedTy->isStructureType())
417+
return nullptr;
418+
419+
llvm::Type *BufferLayoutTy =
420+
HLSLBufferLayoutBuilder(CGM, "spirv.Layout")
421+
.createLayoutType(ContainedTy->getAsStructureType(), Packoffsets);
422+
uint32_t StorageClass = /* Uniform storage class */ 2;
423+
return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer", {BufferLayoutTy},
424+
{StorageClass, false});
415425
break;
426+
}
416427
case llvm::dxil::ResourceClass::Sampler:
417428
return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
418429
}

clang/test/CodeGenHLSL/default_cbuffer.hlsl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple dxil-pc-shadermodel6.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple dxil-pc-shadermodel6.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,DXIL
2+
// RUN: %clang_cc1 -Wno-hlsl-implicit-binding -finclude-default-header -triple spirv-pc-vulkan1.3-compute -fnative-half-type -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --check-prefixes=CHECK,SPIRV
23

3-
// CHECK: %"__cblayout_$Globals" = type <{ float, float, target("dx.Layout", %__cblayout_S, 4, 0) }>
4+
// DXIL: %"__cblayout_$Globals" = type <{ float, float, target("dx.Layout", %__cblayout_S, 4, 0) }>
5+
// SPIRV: %"__cblayout_$Globals" = type <{ float, float, target("spirv.Layout", %__cblayout_S, 4, 0) }>
46
// CHECK: %__cblayout_S = type <{ float }>
57

6-
// CHECK-DAG: @"$Globals.cb" = global target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
7-
// CHECK-DAG: @a = external addrspace(2) global float
8-
// CHECK-DAG: @g = external addrspace(2) global float
9-
// CHECK-DAG: @h = external addrspace(2) global target("dx.Layout", %__cblayout_S, 4, 0), align 4
8+
// DXIL-DAG: @"$Globals.cb" = global target("dx.CBuffer", target("dx.Layout", %"__cblayout_$Globals", 20, 0, 4, 16))
9+
// DXIL-DAG: @a = external addrspace(2) global float
10+
// DXIL-DAG: @g = external addrspace(2) global float
11+
// DXIL-DAG: @h = external addrspace(2) global target("dx.Layout", %__cblayout_S, 4, 0), align 4
12+
13+
// SPIRV-DAG: @"$Globals.cb" = global target("spirv.VulkanBuffer", target("spirv.Layout", %"__cblayout_$Globals", 20, 0, 4, 16), 2, 0)
14+
// SPIRV-DAG: @a = external addrspace(12) global float
15+
// SPIRV-DAG: @g = external addrspace(12) global float
16+
// SPIRV-DAG: @h = external addrspace(12) global target("spirv.Layout", %__cblayout_S, 4, 0), align 4
1017

1118
struct EmptyStruct {
1219
};
@@ -35,4 +42,5 @@ void main() {
3542
}
3643

3744
// CHECK: !hlsl.cbs = !{![[CB:.*]]}
38-
// CHECK: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(2) @a, ptr addrspace(2) @g, ptr addrspace(2) @h}
45+
// DXIL: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(2) @a, ptr addrspace(2) @g, ptr addrspace(2) @h}
46+
// SPIRV: ![[CB]] = !{ptr @"$Globals.cb", ptr addrspace(12) @a, ptr addrspace(12) @g, ptr addrspace(12) @h}

0 commit comments

Comments
 (0)