Skip to content

Commit fb3dec4

Browse files
committed
[HLSL][SPIR-V] Implement vk::push_constant
Implements initial support for vk::push_constant. As is, this allows handling simple push constants, but has one main issue: layout can be incorrect. The old fix would be to use target specific types, but this is actively being reworked on for cbuffers (#147352). So for now, this part is marked as XFAIL.
1 parent 53e3f8e commit fb3dec4

33 files changed

+222
-23
lines changed

clang/include/clang/Basic/AddressSpaces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ enum class LangAS : unsigned {
6262
hlsl_private,
6363
hlsl_device,
6464
hlsl_input,
65+
hlsl_push_constant,
6566

6667
// Wasm specific address spaces.
6768
wasm_funcref,

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5146,6 +5146,14 @@ def HLSLVkExtBuiltinInput : InheritableAttr {
51465146
let Documentation = [HLSLVkExtBuiltinInputDocs];
51475147
}
51485148

5149+
def HLSLVkPushConstant : InheritableAttr {
5150+
let Spellings = [CXX11<"vk", "push_constant">];
5151+
let Args = [];
5152+
let Subjects = SubjectList<[GlobalVar], ErrorDiag>;
5153+
let LangOpts = [HLSL];
5154+
let Documentation = [HLSLVkPushConstantDocs];
5155+
}
5156+
51495157
def HLSLVkConstantId : InheritableAttr {
51505158
let Spellings = [CXX11<"vk", "constant_id">];
51515159
let Args = [IntArgument<"Id">];

clang/include/clang/Basic/AttrDocs.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8777,6 +8777,11 @@ https://github.com/microsoft/hlsl-specs/blob/main/proposals/0011-inline-spirv.md
87778777
}];
87788778
}
87798779

8780+
def HLSLVkPushConstantDocs : Documentation {
8781+
let Category = DocCatVariable;
8782+
let Content = [{ FIXME }];
8783+
}
8784+
87808785
def AnnotateTypeDocs : Documentation {
87818786
let Category = DocCatType;
87828787
let Heading = "annotate_type";

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13183,6 +13183,9 @@ def err_hlsl_attr_invalid_type : Error<
1318313183
"attribute %0 only applies to a field or parameter of type '%1'">;
1318413184
def err_hlsl_attr_invalid_ast_node : Error<
1318513185
"attribute %0 only applies to %1">;
13186+
def err_hlsl_attr_incompatible
13187+
: Error<"%0 attribute is not compatible with %1 attribute">;
13188+
1318613189
def err_hlsl_entry_shader_attr_mismatch : Error<
1318713190
"%0 attribute on entry function does not match the target profile">;
1318813191
def err_hlsl_numthreads_argument_oor : Error<"argument '%select{X|Y|Z}0' to numthreads attribute cannot exceed %1">;
@@ -13294,6 +13297,9 @@ def err_hlsl_incomplete_resource_array_in_function_param: Error<
1329413297
def err_hlsl_assign_to_global_resource: Error<
1329513298
"assignment to global resource variable %0 is not allowed">;
1329613299

13300+
def err_hlsl_push_constant_unique
13301+
: Error<"cannot have more than one push constant block">;
13302+
1329713303
// Layout randomization diagnostics.
1329813304
def err_non_designated_init_used : Error<
1329913305
"a randomized struct can only be initialized with a designated initializer">;

clang/include/clang/Basic/HLSLRuntime.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef CLANG_BASIC_HLSLRUNTIME_H
1515
#define CLANG_BASIC_HLSLRUNTIME_H
1616

17+
#include "clang/Basic/AddressSpaces.h"
1718
#include "clang/Basic/LangOptions.h"
1819
#include <cstdint>
1920

@@ -30,6 +31,10 @@ getStageFromEnvironment(const llvm::Triple::EnvironmentType &E) {
3031
return static_cast<ShaderStage>(Pipeline);
3132
}
3233

34+
constexpr bool isInitializedByPipeline(LangAS AS) {
35+
return AS == LangAS::hlsl_input || AS == LangAS::hlsl_push_constant;
36+
}
37+
3338
#define ENUM_COMPARE_ASSERT(Value) \
3439
static_assert( \
3540
getStageFromEnvironment(llvm::Triple::Value) == ShaderStage::Value, \

clang/include/clang/Sema/SemaHLSL.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ class SemaHLSL : public SemaBase {
190190
void handleSemanticAttr(Decl *D, const ParsedAttr &AL);
191191

192192
void handleVkExtBuiltinInputAttr(Decl *D, const ParsedAttr &AL);
193+
void handleVkPushConstantAttr(Decl *D, const ParsedAttr &AL);
193194

194195
bool CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall);
195196
QualType ProcessResourceTypeAttributes(QualType Wrapped);
@@ -239,6 +240,8 @@ class SemaHLSL : public SemaBase {
239240

240241
IdentifierInfo *RootSigOverrideIdent = nullptr;
241242

243+
bool HasDeclaredAPushConstant = false;
244+
242245
struct SemanticInfo {
243246
HLSLParsedSemanticAttr *Semantic;
244247
std::optional<uint32_t> Index;

clang/lib/AST/Type.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ bool Qualifiers::isTargetAddressSpaceSupersetOf(LangAS A, LangAS B,
101101
(A == LangAS::Default && B == LangAS::hlsl_private) ||
102102
(A == LangAS::Default && B == LangAS::hlsl_device) ||
103103
(A == LangAS::Default && B == LangAS::hlsl_input) ||
104+
(A == LangAS::Default && B == LangAS::hlsl_push_constant) ||
104105
// Conversions from target specific address spaces may be legal
105106
// depending on the target information.
106107
Ctx.getTargetInfo().isAddressSpaceSupersetOf(A, B);

clang/lib/AST/TypePrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,6 +2749,8 @@ std::string Qualifiers::getAddrSpaceAsString(LangAS AS) {
27492749
return "hlsl_device";
27502750
case LangAS::hlsl_input:
27512751
return "hlsl_input";
2752+
case LangAS::hlsl_push_constant:
2753+
return "hlsl_push_constant";
27522754
case LangAS::wasm_funcref:
27532755
return "__funcref";
27542756
default:

clang/lib/Basic/TargetInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static const LangASMap FakeAddrSpaceMap = {
5252
15, // hlsl_private
5353
16, // hlsl_device
5454
17, // hlsl_input
55+
18, // hlsl_push_constant
5556
20, // wasm_funcref
5657
};
5758

clang/lib/Basic/Targets/AArch64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static const unsigned ARM64AddrSpaceMap[] = {
4848
0, // hlsl_private
4949
0, // hlsl_device
5050
0, // hlsl_input
51+
0, // hlsl_push_constant
5152
// Wasm address space values for this target are dummy values,
5253
// as it is only enabled for Wasm targets.
5354
20, // wasm_funcref

0 commit comments

Comments
 (0)