Skip to content

Commit 9b74dce

Browse files
authored
[HLSL][RootSignature] Add parsing of remaining Descriptor Table params (llvm#137038)
- defines the special values for `DESCRIPTOR_RANGE_OFFSET_APPEND` and `unbounded` for the `offset` and `numDescriptors` parameters respectively - adds these parmaters to the `DescriptorClause` struct and the params struct - plugs in parsing of `numDescriptors` and `offset` into `parseDescriptorTableClauseParams` - defines the `unbounded` enum keyword for the lexer to expose to the parser - adds corresponding unit tests Part 5 of llvm#126569
1 parent 290ba28 commit 9b74dce

File tree

6 files changed

+81
-3
lines changed

6 files changed

+81
-3
lines changed

clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#endif
2828

2929
// Defines the various types of enum
30+
#ifndef UNBOUNDED_ENUM
31+
#define UNBOUNDED_ENUM(NAME, LIT) ENUM(NAME, LIT)
32+
#endif
3033
#ifndef DESCRIPTOR_RANGE_OFFSET_ENUM
3134
#define DESCRIPTOR_RANGE_OFFSET_ENUM(NAME, LIT) ENUM(NAME, LIT)
3235
#endif
@@ -87,6 +90,9 @@ KEYWORD(flags)
8790
KEYWORD(numDescriptors)
8891
KEYWORD(offset)
8992

93+
// Unbounded Enum:
94+
UNBOUNDED_ENUM(unbounded, "unbounded")
95+
9096
// Descriptor Range Offset Enum:
9197
DESCRIPTOR_RANGE_OFFSET_ENUM(DescriptorRangeOffsetAppend, "DESCRIPTOR_RANGE_OFFSET_APPEND")
9298

@@ -118,6 +124,7 @@ SHADER_VISIBILITY_ENUM(Mesh, "SHADER_VISIBILITY_MESH")
118124
#undef DESCRIPTOR_RANGE_FLAG_ENUM_ON
119125
#undef ROOT_DESCRIPTOR_FLAG_ENUM
120126
#undef DESCRIPTOR_RANGE_OFFSET_ENUM
127+
#undef UNBOUNDED_ENUM
121128
#undef ENUM
122129
#undef KEYWORD
123130
#undef PUNCTUATOR

clang/include/clang/Parse/ParseHLSLRootSignature.h

+2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ class RootSignatureParser {
8080
/// state of parsed params
8181
struct ParsedClauseParams {
8282
std::optional<llvm::hlsl::rootsig::Register> Reg;
83+
std::optional<uint32_t> NumDescriptors;
8384
std::optional<uint32_t> Space;
85+
std::optional<uint32_t> Offset;
8486
std::optional<llvm::hlsl::rootsig::DescriptorRangeFlags> Flags;
8587
};
8688
std::optional<ParsedClauseParams>

clang/lib/Parse/ParseHLSLRootSignature.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,15 @@ RootSignatureParser::parseDescriptorTableClause() {
145145
Clause.Reg = Params->Reg.value();
146146

147147
// Fill in optional values
148+
if (Params->NumDescriptors.has_value())
149+
Clause.NumDescriptors = Params->NumDescriptors.value();
150+
148151
if (Params->Space.has_value())
149152
Clause.Space = Params->Space.value();
150153

154+
if (Params->Offset.has_value())
155+
Clause.Offset = Params->Offset.value();
156+
151157
if (Params->Flags.has_value())
152158
Clause.Flags = Params->Flags.value();
153159

@@ -182,6 +188,29 @@ RootSignatureParser::parseDescriptorTableClauseParams(TokenKind RegType) {
182188
Params.Reg = Reg;
183189
}
184190

191+
// `numDescriptors` `=` POS_INT | unbounded
192+
if (tryConsumeExpectedToken(TokenKind::kw_numDescriptors)) {
193+
if (Params.NumDescriptors.has_value()) {
194+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
195+
<< CurToken.TokKind;
196+
return std::nullopt;
197+
}
198+
199+
if (consumeExpectedToken(TokenKind::pu_equal))
200+
return std::nullopt;
201+
202+
std::optional<uint32_t> NumDescriptors;
203+
if (tryConsumeExpectedToken(TokenKind::en_unbounded))
204+
NumDescriptors = NumDescriptorsUnbounded;
205+
else {
206+
NumDescriptors = parseUIntParam();
207+
if (!NumDescriptors.has_value())
208+
return std::nullopt;
209+
}
210+
211+
Params.NumDescriptors = NumDescriptors;
212+
}
213+
185214
// `space` `=` POS_INT
186215
if (tryConsumeExpectedToken(TokenKind::kw_space)) {
187216
if (Params.Space.has_value()) {
@@ -199,6 +228,29 @@ RootSignatureParser::parseDescriptorTableClauseParams(TokenKind RegType) {
199228
Params.Space = Space;
200229
}
201230

231+
// `offset` `=` POS_INT | DESCRIPTOR_RANGE_OFFSET_APPEND
232+
if (tryConsumeExpectedToken(TokenKind::kw_offset)) {
233+
if (Params.Offset.has_value()) {
234+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
235+
<< CurToken.TokKind;
236+
return std::nullopt;
237+
}
238+
239+
if (consumeExpectedToken(TokenKind::pu_equal))
240+
return std::nullopt;
241+
242+
std::optional<uint32_t> Offset;
243+
if (tryConsumeExpectedToken(TokenKind::en_DescriptorRangeOffsetAppend))
244+
Offset = DescriptorTableOffsetAppend;
245+
else {
246+
Offset = parseUIntParam();
247+
if (!Offset.has_value())
248+
return std::nullopt;
249+
}
250+
251+
Params.Offset = Offset;
252+
}
253+
202254
// `flags` `=` DESCRIPTOR_RANGE_FLAGS
203255
if (tryConsumeExpectedToken(TokenKind::kw_flags)) {
204256
if (Params.Flags.has_value()) {

clang/unittests/Lex/LexHLSLRootSignatureTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
9393
space visibility flags
9494
numDescriptors offset
9595
96+
unbounded
9697
DESCRIPTOR_RANGE_OFFSET_APPEND
9798
9899
DATA_VOLATILE

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
130130
const llvm::StringLiteral Source = R"cc(
131131
DescriptorTable(
132132
CBV(b0),
133-
SRV(space = 3, t42, flags = 0),
133+
SRV(space = 3, offset = 32, t42, flags = 0, numDescriptors = 4),
134134
visibility = SHADER_VISIBILITY_PIXEL,
135-
Sampler(s987, space = +2),
136-
UAV(u4294967294,
135+
Sampler(s987, space = +2, offset = DESCRIPTOR_RANGE_OFFSET_APPEND),
136+
UAV(u4294967294, numDescriptors = unbounded,
137137
flags = Descriptors_Volatile | Data_Volatile
138138
| Data_Static_While_Set_At_Execute | Data_Static
139139
| Descriptors_Static_Keeping_Buffer_Bounds_Checks
@@ -162,7 +162,10 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
162162
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.ViewType,
163163
RegisterType::BReg);
164164
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.Number, 0u);
165+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).NumDescriptors, 1u);
165166
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Space, 0u);
167+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Offset,
168+
DescriptorTableOffsetAppend);
166169
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Flags,
167170
DescriptorRangeFlags::DataStaticWhileSetAtExecute);
168171

@@ -172,7 +175,9 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
172175
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.ViewType,
173176
RegisterType::TReg);
174177
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.Number, 42u);
178+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).NumDescriptors, 4u);
175179
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Space, 3u);
180+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Offset, 32u);
176181
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Flags,
177182
DescriptorRangeFlags::None);
178183

@@ -182,7 +187,10 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
182187
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.ViewType,
183188
RegisterType::SReg);
184189
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.Number, 987u);
190+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).NumDescriptors, 1u);
185191
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Space, 2u);
192+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Offset,
193+
DescriptorTableOffsetAppend);
186194
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Flags,
187195
DescriptorRangeFlags::None);
188196

@@ -192,7 +200,11 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
192200
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.ViewType,
193201
RegisterType::UReg);
194202
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Reg.Number, 4294967294u);
203+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).NumDescriptors,
204+
NumDescriptorsUnbounded);
195205
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Space, 0u);
206+
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Offset,
207+
DescriptorTableOffsetAppend);
196208
ASSERT_EQ(std::get<DescriptorTableClause>(Elem).Flags,
197209
DescriptorRangeFlags::ValidFlags);
198210

llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h

+4
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ struct DescriptorTable {
6060
uint32_t NumClauses = 0; // The number of clauses in the table
6161
};
6262

63+
static const uint32_t NumDescriptorsUnbounded = 0xffffffff;
64+
static const uint32_t DescriptorTableOffsetAppend = 0xffffffff;
6365
// Models DTClause : CBV | SRV | UAV | Sampler, by collecting like parameters
6466
using ClauseType = llvm::dxil::ResourceClass;
6567
struct DescriptorTableClause {
6668
ClauseType Type;
6769
Register Reg;
70+
uint32_t NumDescriptors = 1;
6871
uint32_t Space = 0;
72+
uint32_t Offset = DescriptorTableOffsetAppend;
6973
DescriptorRangeFlags Flags;
7074

7175
void setDefaultFlags() {

0 commit comments

Comments
 (0)