Skip to content

Commit 4336e42

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 7060d2a commit 4336e42

File tree

17 files changed

+95
-35
lines changed

17 files changed

+95
-35
lines changed

clang/include/clang/Basic/AddressSpaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ enum class LangAS : unsigned {
5858

5959
// HLSL specific address spaces.
6060
hlsl_groupshared,
61+
hlsl_private,
6162

6263
// Wasm specific address spaces.
6364
wasm_funcref,

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,8 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
25532553
return "__funcref";
25542554
case LangAS::hlsl_groupshared:
25552555
return "groupshared";
2556+
case LangAS::hlsl_private:
2557+
return "hlsl_private";
25562558
default:
25572559
return std::to_string(toTargetAddressSpace(AS));
25582560
}

clang/lib/Basic/TargetInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static const LangASMap FakeAddrSpaceMap = {
4747
11, // ptr32_uptr
4848
12, // ptr64
4949
13, // hlsl_groupshared
50+
14, // hlsl_private
5051
20, // wasm_funcref
5152
};
5253

clang/lib/Basic/Targets/AArch64.h

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

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
5959
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_uptr
6060
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
6161
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
62+
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_private
6263
};
6364

6465
const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
@@ -83,6 +84,7 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
8384
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_uptr
8485
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
8586
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
87+
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_private
8688

8789
};
8890
} // namespace targets

clang/lib/Basic/Targets/DirectX.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ 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
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+
10, // hlsl_private
4546
// Wasm address space values for this target are dummy values,
4647
// as it is only enabled for Wasm targets.
4748
20, // wasm_funcref

clang/lib/Basic/Targets/NVPTX.h

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

clang/lib/Basic/Targets/SPIR.h

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ 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
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+
10, // 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
@@ -69,17 +70,18 @@ static const unsigned SPIRDefIsGenMap[] = {
6970
// cuda_constant pointer can be casted to default/"flat" pointer, but in
7071
// SPIR-V casts between constant and generic pointers are not allowed. For
7172
// this reason cuda_constant is mapped to SPIR-V CrossWorkgroup.
72-
1, // cuda_constant
73-
3, // cuda_shared
74-
1, // sycl_global
75-
5, // sycl_global_device
76-
6, // sycl_global_host
77-
3, // sycl_local
78-
0, // sycl_private
79-
0, // ptr32_sptr
80-
0, // ptr32_uptr
81-
0, // ptr64
82-
0, // hlsl_groupshared
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+
10, // hlsl_private
8385
// Wasm address space values for this target are dummy values,
8486
// as it is only enabled for Wasm targets.
8587
20, // wasm_funcref

clang/lib/Basic/Targets/SystemZ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static const unsigned ZOSAddressMap[] = {
4242
1, // ptr32_uptr
4343
0, // ptr64
4444
0, // hlsl_groupshared
45+
0, // hlsl_private
4546
0 // wasm_funcref
4647
};
4748

clang/lib/Basic/Targets/TCE.h

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

clang/lib/Basic/Targets/WebAssembly.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static const unsigned WebAssemblyAddrSpaceMap[] = {
4242
0, // ptr32_uptr
4343
0, // ptr64
4444
0, // hlsl_groupshared
45+
0, // hlsl_private
4546
20, // wasm_funcref
4647
};
4748

clang/lib/Basic/Targets/X86.h

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

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5362,6 +5362,23 @@ LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
53625362
if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
53635363
return AS;
53645364
}
5365+
5366+
if (LangOpts.HLSL) {
5367+
if (D == nullptr)
5368+
return LangAS::hlsl_private;
5369+
5370+
// Except resources (Uniform, UniformConstant) & instanglble (handles)
5371+
if (D->getType()->isHLSLResourceType() ||
5372+
D->getType()->isHLSLIntangibleType())
5373+
return D->getType().getAddressSpace();
5374+
5375+
if (D->getStorageClass() != SC_Static)
5376+
return D->getType().getAddressSpace();
5377+
5378+
LangAS AS = D->getType().getAddressSpace();
5379+
return AS == LangAS::Default ? LangAS::hlsl_private : AS;
5380+
}
5381+
53655382
return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
53665383
}
53675384

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

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/CodeGenHLSL/static_global_and_function_in_cb.hlsl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
// CHECK-DAG: @[[CB:.+]] = external constant { float }
55

6+
// CHECK-DAG:@_ZL1b = internal addrspace(10) global float 3.000000e+00, align 4
7+
static float b = 3;
8+
69
cbuffer A {
7-
float a;
8-
// CHECK-DAG:@_ZL1b = internal global float 3.000000e+00, align 4
9-
static float b = 3;
10+
float a;
1011
// CHECK:load float, ptr @[[CB]], align 4
11-
// CHECK:load float, ptr @_ZL1b, align 4
12+
// CHECK:load float, ptr addrspacecast (ptr addrspace(10) @_ZL1b to ptr), align 4
1213
float foo() { return a + b; }
1314
}
1415

clang/test/SemaTemplate/address_space-dependent.cpp

Lines changed: 1 addition & 1 deletion
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 (8388586)}}
46+
__attribute__((address_space(I))) int *bounds; // expected-error {{address space is larger than the maximum supported (8388585)}}
4747
}
4848

4949
template <long int I>

0 commit comments

Comments
 (0)