Skip to content

Commit a625bc6

Browse files
authored
[HLSL][SPIR-V] Add hlsl_private address space for SPIR-V (#133464)
This is an alternative to #122103 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 when targeting this backend. This is covered in this proposal: llvm/wg-hlsl@4c9e11a This PR will cause addrspacecast to show up in several cases, like class member functions or assignment. Those will have to be handled in the backend later on, particularly to fixup pointer storage classes in some functions. Before this change, global variable were emitted with the 'Function' storage class, which was wrong.
1 parent 344a491 commit a625bc6

26 files changed

+186
-28
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/include/clang/Sema/Sema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4328,6 +4328,7 @@ class Sema final : public SemaBase {
43284328
NamedDecl *findLocallyScopedExternCDecl(DeclarationName Name);
43294329

43304330
void deduceOpenCLAddressSpace(ValueDecl *decl);
4331+
void deduceHLSLAddressSpace(VarDecl *decl);
43314332

43324333
/// Adjust the \c DeclContext for a function or variable that might be a
43334334
/// function-local external declaration.

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ class SemaHLSL : public SemaBase {
154154
bool TransformInitList(const InitializedEntity &Entity,
155155
const InitializationKind &Kind, InitListExpr *Init);
156156

157+
void deduceAddressSpace(VarDecl *Decl);
158+
157159
private:
158160
// HLSL resource type attributes need to be processed all at once.
159161
// This is a list to collect them.

clang/lib/AST/Type.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ bool Qualifiers::isTargetAddressSpaceSupersetOf(LangAS A, LangAS B,
9494
(A == LangAS::Default &&
9595
(B == LangAS::cuda_constant || B == LangAS::cuda_device ||
9696
B == LangAS::cuda_shared)) ||
97+
// `this` overloading depending on address space is not ready,
98+
// so this is a hack to allow generating addrspacecasts.
99+
// IR legalization will be required when this address space is used.
100+
(A == LangAS::Default && B == LangAS::hlsl_private) ||
97101
// Conversions from target specific address spaces may be legal
98102
// depending on the target information.
99103
Ctx.getTargetInfo().isAddressSpaceSupersetOf(A, B);

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,8 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
25812581
return "groupshared";
25822582
case LangAS::hlsl_constant:
25832583
return "hlsl_constant";
2584+
case LangAS::hlsl_private:
2585+
return "hlsl_private";
25842586
case LangAS::wasm_funcref:
25852587
return "__funcref";
25862588
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ 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+
// FIXME(pr/122103): hlsl_private -> PRIVATE is wrong, but at least this
64+
// will break loudly.
65+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_private
6366
};
6467

6568
const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
@@ -85,6 +88,7 @@ const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
8588
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
8689
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
8790
llvm::AMDGPUAS::CONSTANT_ADDRESS, // hlsl_constant
91+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // hlsl_private
8892
};
8993
} // namespace targets
9094
} // namespace clang

clang/lib/Basic/Targets/DirectX.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static const unsigned DirectXAddrSpaceMap[] = {
4343
0, // ptr64
4444
3, // hlsl_groupshared
4545
2, // hlsl_constant
46+
0, // 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

0 commit comments

Comments
 (0)