Skip to content

Commit 04cabdd

Browse files
committed
[HLSL] Implement vk::ext_builtin_input attribute
This variable attribute is used in HLSL to add Vulkan specific builtins in a shader. The attribute is documented here: https://github.com/microsoft/hlsl-specs/blob/17727e88fd1cb09013cb3a144110826af05f4dd5/proposals/0011-inline-spirv.md Those variable, even if marked as `static` are externally initialized by the pipeline/driver/GPU. This is handled by moving them to a specific address space `hlsl_input`, also added by this commit. The design for input variables in Clang can be found here: https://github.com/llvm/wg-hlsl/blob/355771361ef69259fef39a65caef8bff9cb4046d/proposals/0019-spirv-input-builtin.md Related to #136920
1 parent a10f6c1 commit 04cabdd

26 files changed

+124
-3
lines changed

clang/include/clang/Basic/AddressSpaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ enum class LangAS : unsigned {
6161
hlsl_constant,
6262
hlsl_private,
6363
hlsl_device,
64+
hlsl_input,
6465

6566
// Wasm specific address spaces.
6667
wasm_funcref,

clang/include/clang/Basic/Attr.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ def SharedVar : SubsetSubject<Var,
140140
[{S->hasGlobalStorage() && !S->getTLSKind()}],
141141
"global variables">;
142142

143+
def HLSLInputBuiltin : SubsetSubject<Var, [{S->hasGlobalStorage() &&
144+
S->getStorageClass()==StorageClass::SC_Static &&
145+
S->getType().isConstQualified()}],
146+
"input builtin">;
147+
143148
def GlobalVar : SubsetSubject<Var,
144149
[{S->hasGlobalStorage()}], "global variables">;
145150

@@ -4908,6 +4913,14 @@ def HLSLWaveSize: InheritableAttr {
49084913
let Documentation = [WaveSizeDocs];
49094914
}
49104915

4916+
def HLSLVkExtBuiltinInput : InheritableAttr {
4917+
let Spellings = [CXX11<"vk", "ext_builtin_input">];
4918+
let Args = [IntArgument<"BuiltIn">];
4919+
let Subjects = SubjectList<[HLSLInputBuiltin], ErrorDiag>;
4920+
let LangOpts = [HLSL];
4921+
let Documentation = [HLSLVkExtBuiltinInputDocs];
4922+
}
4923+
49114924
def RandomizeLayout : InheritableAttr {
49124925
let Spellings = [GCC<"randomize_layout">];
49134926
let Subjects = SubjectList<[Record]>;

clang/include/clang/Basic/AttrDocs.td

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8497,6 +8497,28 @@ and copied back to the argument after the callee returns.
84978497
}];
84988498
}
84998499

8500+
def HLSLVkExtBuiltinInputDocs : Documentation {
8501+
let Category = DocCatVariable;
8502+
let Content = [{
8503+
Vulkan shaders have `Input` builtins. Those variables are externally
8504+
initialized by the driver/pipeline, but each copy is private to the current
8505+
lane.
8506+
8507+
Those builtins can be declared using the `[[vk::ext_builtin_input]]` attribute
8508+
like follows:
8509+
8510+
.. code-block:: c++
8511+
[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
8512+
static const uint3 groupid;
8513+
8514+
This variable will be lowered into a module-level variable, with the `Input`
8515+
storage class, and the `BuiltIn 26` decoration.
8516+
8517+
The full documentation for this inline SPIR-V attribute can be found here:
8518+
https://github.com/microsoft/hlsl-specs/blob/main/proposals/0011-inline-spirv.md
8519+
}];
8520+
}
8521+
85008522
def AnnotateTypeDocs : Documentation {
85018523
let Category = DocCatType;
85028524
let Heading = "annotate_type";

clang/include/clang/Basic/AttributeCommonInfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class AttributeCommonInfo {
6767
IgnoredAttribute,
6868
UnknownAttribute,
6969
};
70-
enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };
70+
enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, VK, GSL, RISCV };
7171
enum class AttrArgsInfo {
7272
None,
7373
Optional,

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ class SemaHLSL : public SemaBase {
130130
void handleParamModifierAttr(Decl *D, const ParsedAttr &AL);
131131
bool handleResourceTypeAttr(QualType T, const ParsedAttr &AL);
132132

133+
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
134+
133135
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
134136
QualType ProcessResourceTypeAttributes(QualType Wrapped);
135137
HLSLAttributedResourceLocInfo

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ bool Qualifiers::isTargetAddressSpaceSupersetOf(LangAS A, LangAS B,
100100
// address spaces to default to work around this problem.
101101
(A == LangAS::Default && B == LangAS::hlsl_private) ||
102102
(A == LangAS::Default && B == LangAS::hlsl_device) ||
103+
(A == LangAS::Default && B == LangAS::hlsl_input) ||
103104
// Conversions from target specific address spaces may be legal
104105
// depending on the target information.
105106
Ctx.getTargetInfo().isAddressSpaceSupersetOf(A, B);

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,8 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
26192619
return "hlsl_private";
26202620
case LangAS::hlsl_device:
26212621
return "hlsl_device";
2622+
case LangAS::hlsl_input:
2623+
return "hlsl_input";
26222624
case LangAS::wasm_funcref:
26232625
return "__funcref";
26242626
default:

clang/lib/Basic/Attributes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ getScopeFromNormalizedScopeName(StringRef ScopeName) {
189189
.Case("gnu", AttributeCommonInfo::Scope::GNU)
190190
.Case("gsl", AttributeCommonInfo::Scope::GSL)
191191
.Case("hlsl", AttributeCommonInfo::Scope::HLSL)
192+
.Case("vk", AttributeCommonInfo::Scope::VK)
192193
.Case("msvc", AttributeCommonInfo::Scope::MSVC)
193194
.Case("omp", AttributeCommonInfo::Scope::OMP)
194195
.Case("riscv", AttributeCommonInfo::Scope::RISCV);

clang/lib/Basic/TargetInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static const LangASMap FakeAddrSpaceMap = {
4949
13, // hlsl_groupshared
5050
14, // hlsl_constant
5151
15, // hlsl_private
52+
16, // hlsl_device
53+
17, // hlsl_input
5254
20, // wasm_funcref
5355
};
5456

clang/lib/Basic/Targets/AArch64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ static const unsigned ARM64AddrSpaceMap[] = {
4747
0, // hlsl_constant
4848
0, // hlsl_private
4949
0, // hlsl_device
50+
0, // hlsl_input
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)