Skip to content

Commit 20d7019

Browse files
Keenutsbogner
andauthored
[HLSL][SPIR-V] Implement vk::ext_builtin_input attribute (#138530)
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 Co-authored-by: Justin Bogner <mail@justinbogner.com>
1 parent d7c7c46 commit 20d7019

28 files changed

+187
-4
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ def SharedVar : SubsetSubject<Var,
140140
[{S->hasGlobalStorage() && !S->getTLSKind()}],
141141
"global variables">;
142142

143+
def HLSLInputBuiltin
144+
: SubsetSubject<Var, [{S->hasGlobalStorage() &&
145+
S->getStorageClass() == StorageClass::SC_Static &&
146+
S->getType().isConstQualified()}],
147+
"static const globals">;
148+
143149
def GlobalVar : SubsetSubject<Var,
144150
[{S->hasGlobalStorage()}], "global variables">;
145151

@@ -4951,6 +4957,14 @@ def HLSLWaveSize: InheritableAttr {
49514957
let Documentation = [WaveSizeDocs];
49524958
}
49534959

4960+
def HLSLVkExtBuiltinInput : InheritableAttr {
4961+
let Spellings = [CXX11<"vk", "ext_builtin_input">];
4962+
let Args = [UnsignedArgument<"BuiltIn">];
4963+
let Subjects = SubjectList<[HLSLInputBuiltin], ErrorDiag>;
4964+
let LangOpts = [HLSL];
4965+
let Documentation = [HLSLVkExtBuiltinInputDocs];
4966+
}
4967+
49544968
def RandomizeLayout : InheritableAttr {
49554969
let Spellings = [GCC<"randomize_layout">];
49564970
let Subjects = SubjectList<[Record]>;

clang/include/clang/Basic/AttrDocs.td

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8508,6 +8508,29 @@ and copied back to the argument after the callee returns.
85088508
}];
85098509
}
85108510

8511+
def HLSLVkExtBuiltinInputDocs : Documentation {
8512+
let Category = DocCatVariable;
8513+
let Content = [{
8514+
Vulkan shaders have `Input` builtins. Those variables are externally
8515+
initialized by the driver/pipeline, but each copy is private to the current
8516+
lane.
8517+
8518+
Those builtins can be declared using the `[[vk::ext_builtin_input]]` attribute
8519+
like follows:
8520+
8521+
.. code-block:: c++
8522+
8523+
[[vk::ext_builtin_input(/* WorkgroupId */ 26)]]
8524+
static const uint3 groupid;
8525+
8526+
This variable will be lowered into a module-level variable, with the `Input`
8527+
storage class, and the `BuiltIn 26` decoration.
8528+
8529+
The full documentation for this inline SPIR-V attribute can be found here:
8530+
https://github.com/microsoft/hlsl-specs/blob/main/proposals/0011-inline-spirv.md
8531+
}];
8532+
}
8533+
85118534
def AnnotateTypeDocs : Documentation {
85128535
let Category = DocCatType;
85138536
let Heading = "annotate_type";

clang/include/clang/Basic/AttributeCommonInfo.h

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

clang/include/clang/Sema/SemaHLSL.h

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

134+
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
135+
134136
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
135137
QualType ProcessResourceTypeAttributes(QualType Wrapped);
136138
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
@@ -2667,6 +2667,8 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
26672667
return "hlsl_private";
26682668
case LangAS::hlsl_device:
26692669
return "hlsl_device";
2670+
case LangAS::hlsl_input:
2671+
return "hlsl_input";
26702672
case LangAS::wasm_funcref:
26712673
return "__funcref";
26722674
default:

clang/lib/Basic/Attributes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ getScopeFromNormalizedScopeName(StringRef ScopeName) {
210210
.Case("gnu", AttributeCommonInfo::Scope::GNU)
211211
.Case("gsl", AttributeCommonInfo::Scope::GSL)
212212
.Case("hlsl", AttributeCommonInfo::Scope::HLSL)
213+
.Case("vk", AttributeCommonInfo::Scope::VK)
213214
.Case("msvc", AttributeCommonInfo::Scope::MSVC)
214215
.Case("omp", AttributeCommonInfo::Scope::OMP)
215216
.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)