Skip to content

Commit c11ace7

Browse files
committed
[SPIR-V] Add hlsl_private address space for SPIR-V
In SPIR-V, private global variables have the `Private` storage class. This PR adds a new address space which allows frontend to emit variable with this storage class. Before this change, global variable were emitted with the 'Function' storage class, which was wrong. Signed-off-by: Nathan Gauër <[email protected]>
1 parent 156cdcf commit c11ace7

File tree

17 files changed

+107
-45
lines changed

17 files changed

+107
-45
lines changed

clang/include/clang/Basic/AddressSpaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ enum class LangAS : unsigned {
5959
// HLSL specific address spaces.
6060
hlsl_groupshared,
6161
hlsl_constant,
62+
hlsl_private,
6263

6364
// Wasm specific address spaces.
6465
wasm_funcref,

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,8 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
25762576
return "groupshared";
25772577
case LangAS::hlsl_constant:
25782578
return "hlsl_constant";
2579+
case LangAS::hlsl_private:
2580+
return "hlsl_private";
25792581
case LangAS::wasm_funcref:
25802582
return "__funcref";
25812583
default:

clang/lib/Basic/TargetInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ static const LangASMap FakeAddrSpaceMap = {
4747
11, // ptr32_uptr
4848
12, // ptr64
4949
13, // hlsl_groupshared
50+
14, // hlsl_constant
51+
15, // hlsl_private
5052
20, // wasm_funcref
5153
};
5254

clang/lib/Basic/Targets/AArch64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static const unsigned ARM64AddrSpaceMap[] = {
4545
static_cast<unsigned>(AArch64AddrSpace::ptr64),
4646
0, // hlsl_groupshared
4747
0, // hlsl_constant
48+
0, // hlsl_private
4849
// Wasm address space values for this target are dummy values,
4950
// as it is only enabled for Wasm targets.
5051
20, // wasm_funcref

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
6060
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
6161
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
6262
llvm::AMDGPUAS::CONSTANT_ADDRESS, // hlsl_constant
63+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_private
6364
};
6465

6566
const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
@@ -85,6 +86,7 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
8586
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
8687
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
8788
llvm::AMDGPUAS::CONSTANT_ADDRESS, // hlsl_constant
89+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_private
8890
};
8991
} // namespace targets
9092
} // namespace clang

clang/lib/Basic/Targets/DirectX.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,17 @@ static const unsigned DirectXAddrSpaceMap[] = {
3333
0, // cuda_constant
3434
0, // cuda_shared
3535
// SYCL address space values for this map are dummy
36-
0, // sycl_global
37-
0, // sycl_global_device
38-
0, // sycl_global_host
39-
0, // sycl_local
40-
0, // sycl_private
41-
0, // ptr32_sptr
42-
0, // ptr32_uptr
43-
0, // ptr64
44-
3, // hlsl_groupshared
45-
2, // hlsl_constant
36+
0, // sycl_global
37+
0, // sycl_global_device
38+
0, // sycl_global_host
39+
0, // sycl_local
40+
0, // sycl_private
41+
0, // ptr32_sptr
42+
0, // ptr32_uptr
43+
0, // ptr64
44+
3, // hlsl_groupshared
45+
2, // hlsl_constant
46+
10, // hlsl_private
4647
// Wasm address space values for this target are dummy values,
4748
// as it is only enabled for Wasm targets.
4849
20, // wasm_funcref

clang/lib/Basic/Targets/NVPTX.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static const unsigned NVPTXAddrSpaceMap[] = {
4747
0, // ptr64
4848
0, // hlsl_groupshared
4949
0, // hlsl_constant
50+
0, // hlsl_private
5051
// Wasm address space values for this target are dummy values,
5152
// as it is only enabled for Wasm targets.
5253
20, // wasm_funcref

clang/lib/Basic/Targets/SPIR.h

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,17 @@ static const unsigned SPIRDefIsPrivMap[] = {
3838
0, // cuda_constant
3939
0, // cuda_shared
4040
// SYCL address space values for this map are dummy
41-
0, // sycl_global
42-
0, // sycl_global_device
43-
0, // sycl_global_host
44-
0, // sycl_local
45-
0, // sycl_private
46-
0, // ptr32_sptr
47-
0, // ptr32_uptr
48-
0, // ptr64
49-
0, // hlsl_groupshared
50-
2, // hlsl_constant
41+
0, // sycl_global
42+
0, // sycl_global_device
43+
0, // sycl_global_host
44+
0, // sycl_local
45+
0, // sycl_private
46+
0, // ptr32_sptr
47+
0, // ptr32_uptr
48+
0, // ptr64
49+
0, // hlsl_groupshared
50+
2, // hlsl_constant
51+
10, // hlsl_private
5152
// Wasm address space values for this target are dummy values,
5253
// as it is only enabled for Wasm targets.
5354
20, // wasm_funcref
@@ -70,18 +71,19 @@ static const unsigned SPIRDefIsGenMap[] = {
7071
// cuda_constant pointer can be casted to default/"flat" pointer, but in
7172
// SPIR-V casts between constant and generic pointers are not allowed. For
7273
// this reason cuda_constant is mapped to SPIR-V CrossWorkgroup.
73-
1, // cuda_constant
74-
3, // cuda_shared
75-
1, // sycl_global
76-
5, // sycl_global_device
77-
6, // sycl_global_host
78-
3, // sycl_local
79-
0, // sycl_private
80-
0, // ptr32_sptr
81-
0, // ptr32_uptr
82-
0, // ptr64
83-
0, // hlsl_groupshared
84-
0, // hlsl_constant
74+
1, // cuda_constant
75+
3, // cuda_shared
76+
1, // sycl_global
77+
5, // sycl_global_device
78+
6, // sycl_global_host
79+
3, // sycl_local
80+
0, // sycl_private
81+
0, // ptr32_sptr
82+
0, // ptr32_uptr
83+
0, // ptr64
84+
0, // hlsl_groupshared
85+
0, // hlsl_constant
86+
10, // hlsl_private
8587
// Wasm address space values for this target are dummy values,
8688
// as it is only enabled for Wasm targets.
8789
20, // wasm_funcref

clang/lib/Basic/Targets/SystemZ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static const unsigned ZOSAddressMap[] = {
4343
0, // ptr64
4444
0, // hlsl_groupshared
4545
0, // hlsl_constant
46+
0, // hlsl_private
4647
0 // wasm_funcref
4748
};
4849

clang/lib/Basic/Targets/TCE.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static const unsigned TCEOpenCLAddrSpaceMap[] = {
5252
0, // ptr64
5353
0, // hlsl_groupshared
5454
0, // hlsl_constant
55+
0, // hlsl_private
5556
// Wasm address space values for this target are dummy values,
5657
// as it is only enabled for Wasm targets.
5758
20, // wasm_funcref

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static const unsigned WebAssemblyAddrSpaceMap[] = {
4343
0, // ptr64
4444
0, // hlsl_groupshared
4545
0, // hlsl_constant
46+
0, // hlsl_private
4647
20, // wasm_funcref
4748
};
4849

clang/lib/Basic/Targets/X86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static const unsigned X86AddrSpaceMap[] = {
4747
272, // ptr64
4848
0, // hlsl_groupshared
4949
0, // hlsl_constant
50+
0, // hlsl_private
5051
// Wasm address space values for this target are dummy values,
5152
// as it is only enabled for Wasm targets.
5253
20, // wasm_funcref

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5386,6 +5386,23 @@ LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
53865386
if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
53875387
return AS;
53885388
}
5389+
5390+
if (LangOpts.HLSL) {
5391+
if (D == nullptr)
5392+
return LangAS::hlsl_private;
5393+
5394+
// Except resources (Uniform, UniformConstant) & instanglble (handles)
5395+
if (D->getType()->isHLSLResourceType() ||
5396+
D->getType()->isHLSLIntangibleType())
5397+
return D->getType().getAddressSpace();
5398+
5399+
if (D->getStorageClass() != SC_Static)
5400+
return D->getType().getAddressSpace();
5401+
5402+
LangAS AS = D->getType().getAddressSpace();
5403+
return AS == LangAS::Default ? LangAS::hlsl_private : AS;
5404+
}
5405+
53895406
return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
53905407
}
53915408

clang/test/CodeGenHLSL/GlobalDestructors.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void main(unsigned GI : SV_GroupIndex) {
7171

7272
// NOINLINE: define internal void @_GLOBAL__D_a() [[IntAttr:\#[0-9]+]]
7373
// NOINLINE-NEXT: entry:
74-
// NOINLINE-NEXT: call void @_ZN4TailD1Ev(ptr @_ZZ3WagvE1T)
74+
// NOINLINE-NEXT: call void @_ZN4TailD1Ev(ptr addrspacecast (ptr addrspace(10) @_ZZ3WagvE1T to ptr))
7575
// NOINLINE-NEXT: call void @_ZN6PupperD1Ev(ptr @GlobalPup)
7676
// NOINLINE-NEXT: ret void
7777

clang/test/CodeGenHLSL/cbuffer_with_static_global_and_function.hlsl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33

44
// CHECK: %__cblayout_A = type <{ float }>
55

6-
// CHECK: @A.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_A, 4, 0))
7-
// CHECK: @a = external addrspace(2) global float, align 4
8-
// CHECK-DAG: @_ZL1b = internal global float 3.000000e+00, align 4
96
// CHECK-NOT: @B.cb
7+
cbuffer B {
8+
// intentionally empty
9+
}
1010

11+
// CHECK-DAG: @_ZL1b = internal addrspace(10) global float 3.000000e+00, align 4
12+
static float b = 3;
13+
14+
// CHECK-DAG: @A.cb = external constant target("dx.CBuffer", target("dx.Layout", %__cblayout_A, 4, 0))
1115
cbuffer A {
16+
// CHECK-DAG: @a = external addrspace(2) global float, align 4
1217
float a;
13-
static float b = 3;
14-
float foo() { return a + b; }
15-
}
1618

17-
cbuffer B {
18-
// intentionally empty
19+
// CHECK: define {{.*}} float @_Z3foov() #0 {
20+
// CHECK-DAG: load float, ptr addrspace(2) @a, align 4
21+
// CHECK-DAG: load float, ptr addrspacecast (ptr addrspace(10) @_ZL1b to ptr), align 4
22+
float foo() { return a + b; }
1923
}
2024

21-
// CHECK: define {{.*}} float @_Z3foov() #0 {
22-
// CHECK: load float, ptr addrspace(2) @a, align 4
2325

2426
extern float bar() {
2527
return foo();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -std=hlsl202x -emit-llvm -o - -disable-llvm-passes %s | FileCheck %s --check-prefixes=CHECK
2+
// RUN: %clang_cc1 -triple spirv-pc-vulkan1.3-compute -std=hlsl202x -emit-llvm -o - -disable-llvm-passes %s | FileCheck %s --check-prefixes=CHECK
3+
4+
struct S {
5+
static int Value;
6+
};
7+
8+
int S::Value = 1;
9+
// CHECK: @_ZN1S5ValueE = global i32 1, align 4
10+
11+
[shader("compute")]
12+
[numthreads(1,1,1)]
13+
void main() {
14+
S s;
15+
int value1, value2;
16+
// CHECK: %s = alloca %struct.S, align 1
17+
// CHECK: %value1 = alloca i32, align 4
18+
// CHECK: %value2 = alloca i32, align 4
19+
20+
// CHECK: [[tmp:%.*]] = load i32, ptr @_ZN1S5ValueE, align 4
21+
// CHECK: store i32 [[tmp]], ptr %value1, align 4
22+
value1 = S::Value;
23+
24+
// CHECK: [[tmp:%.*]] = load i32, ptr @_ZN1S5ValueE, align 4
25+
// CHECK: store i32 [[tmp]], ptr %value2, align 4
26+
value2 = s.Value;
27+
}

clang/test/SemaTemplate/address_space-dependent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void neg() {
4343

4444
template <long int I>
4545
void tooBig() {
46-
__attribute__((address_space(I))) int *bounds; // expected-error {{address space is larger than the maximum supported (8388585)}}
46+
__attribute__((address_space(I))) int *bounds; // expected-error {{address space is larger than the maximum supported (8388584)}}
4747
}
4848

4949
template <long int I>
@@ -101,7 +101,7 @@ int main() {
101101
car<1, 2, 3>(); // expected-note {{in instantiation of function template specialization 'car<1, 2, 3>' requested here}}
102102
HasASTemplateFields<1> HASTF;
103103
neg<-1>(); // expected-note {{in instantiation of function template specialization 'neg<-1>' requested here}}
104-
correct<0x7FFFE9>();
104+
correct<0x7FFFE8>();
105105
tooBig<8388650>(); // expected-note {{in instantiation of function template specialization 'tooBig<8388650L>' requested here}}
106106

107107
__attribute__((address_space(1))) char *x;

0 commit comments

Comments
 (0)