Skip to content

Commit 5494349

Browse files
authored
[HLSL][RootSignature] Add mandatory parameters for RootConstants (#138002)
- defines the `parseRootConstantParams` function and adds handling for the mandatory arguments of `num32BitConstants` and `bReg` - adds corresponding unit tests Part two of implementing #126576
1 parent 6ff3b8e commit 5494349

File tree

4 files changed

+135
-10
lines changed

4 files changed

+135
-10
lines changed

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,14 @@ class RootSignatureParser {
7777
parseDescriptorTableClause();
7878

7979
/// Parameter arguments (eg. `bReg`, `space`, ...) can be specified in any
80-
/// order and only exactly once. `ParsedClauseParams` denotes the current
81-
/// state of parsed params
80+
/// order and only exactly once. The following methods define a
81+
/// `Parsed.*Params` struct to denote the current state of parsed params
82+
struct ParsedConstantParams {
83+
std::optional<llvm::hlsl::rootsig::Register> Reg;
84+
std::optional<uint32_t> Num32BitConstants;
85+
};
86+
std::optional<ParsedConstantParams> parseRootConstantParams();
87+
8288
struct ParsedClauseParams {
8389
std::optional<llvm::hlsl::rootsig::Register> Reg;
8490
std::optional<uint32_t> NumDescriptors;

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ std::optional<RootConstants> RootSignatureParser::parseRootConstants() {
5757

5858
RootConstants Constants;
5959

60+
auto Params = parseRootConstantParams();
61+
if (!Params.has_value())
62+
return std::nullopt;
63+
64+
// Check mandatory parameters where provided
65+
if (!Params->Num32BitConstants.has_value()) {
66+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_missing_param)
67+
<< TokenKind::kw_num32BitConstants;
68+
return std::nullopt;
69+
}
70+
71+
Constants.Num32BitConstants = Params->Num32BitConstants.value();
72+
73+
if (!Params->Reg.has_value()) {
74+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_missing_param)
75+
<< TokenKind::bReg;
76+
return std::nullopt;
77+
}
78+
79+
Constants.Reg = Params->Reg.value();
80+
6081
if (consumeExpectedToken(TokenKind::pu_r_paren,
6182
diag::err_hlsl_unexpected_end_of_params,
6283
/*param of=*/TokenKind::kw_RootConstants))
@@ -187,14 +208,55 @@ RootSignatureParser::parseDescriptorTableClause() {
187208
return Clause;
188209
}
189210

211+
// Parameter arguments (eg. `bReg`, `space`, ...) can be specified in any
212+
// order and only exactly once. The following methods will parse through as
213+
// many arguments as possible reporting an error if a duplicate is seen.
214+
std::optional<RootSignatureParser::ParsedConstantParams>
215+
RootSignatureParser::parseRootConstantParams() {
216+
assert(CurToken.TokKind == TokenKind::pu_l_paren &&
217+
"Expects to only be invoked starting at given token");
218+
219+
ParsedConstantParams Params;
220+
do {
221+
// `num32BitConstants` `=` POS_INT
222+
if (tryConsumeExpectedToken(TokenKind::kw_num32BitConstants)) {
223+
if (Params.Num32BitConstants.has_value()) {
224+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
225+
<< CurToken.TokKind;
226+
return std::nullopt;
227+
}
228+
229+
if (consumeExpectedToken(TokenKind::pu_equal))
230+
return std::nullopt;
231+
232+
auto Num32BitConstants = parseUIntParam();
233+
if (!Num32BitConstants.has_value())
234+
return std::nullopt;
235+
Params.Num32BitConstants = Num32BitConstants;
236+
}
237+
238+
// `b` POS_INT
239+
if (tryConsumeExpectedToken(TokenKind::bReg)) {
240+
if (Params.Reg.has_value()) {
241+
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
242+
<< CurToken.TokKind;
243+
return std::nullopt;
244+
}
245+
auto Reg = parseRegister();
246+
if (!Reg.has_value())
247+
return std::nullopt;
248+
Params.Reg = Reg;
249+
}
250+
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
251+
252+
return Params;
253+
}
254+
190255
std::optional<RootSignatureParser::ParsedClauseParams>
191256
RootSignatureParser::parseDescriptorTableClauseParams(TokenKind RegType) {
192257
assert(CurToken.TokKind == TokenKind::pu_l_paren &&
193258
"Expects to only be invoked starting at given token");
194259

195-
// Parameter arguments (eg. `bReg`, `space`, ...) can be specified in any
196-
// order and only exactly once. Parse through as many arguments as possible
197-
// reporting an error if a duplicate is seen.
198260
ParsedClauseParams Params;
199261
do {
200262
// ( `b` | `t` | `u` | `s`) POS_INT

clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ TEST_F(ParseHLSLRootSignatureTest, ValidSamplerFlagsTest) {
254254

255255
TEST_F(ParseHLSLRootSignatureTest, ValidParseRootConsantsTest) {
256256
const llvm::StringLiteral Source = R"cc(
257-
RootConstants()
257+
RootConstants(num32BitConstants = 1, b0),
258+
RootConstants(b42, num32BitConstants = 4294967295)
258259
)cc";
259260

260261
TrivialModuleLoader ModLoader;
@@ -270,10 +271,19 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootConsantsTest) {
270271

271272
ASSERT_FALSE(Parser.parse());
272273

273-
ASSERT_EQ(Elements.size(), 1u);
274+
ASSERT_EQ(Elements.size(), 2u);
274275

275276
RootElement Elem = Elements[0];
276277
ASSERT_TRUE(std::holds_alternative<RootConstants>(Elem));
278+
ASSERT_EQ(std::get<RootConstants>(Elem).Num32BitConstants, 1u);
279+
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.ViewType, RegisterType::BReg);
280+
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.Number, 0u);
281+
282+
Elem = Elements[1];
283+
ASSERT_TRUE(std::holds_alternative<RootConstants>(Elem));
284+
ASSERT_EQ(std::get<RootConstants>(Elem).Num32BitConstants, 4294967295u);
285+
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.ViewType, RegisterType::BReg);
286+
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.Number, 42u);
277287

278288
ASSERT_TRUE(Consumer->isSatisfied());
279289
}
@@ -367,7 +377,7 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidParseUnexpectedEndOfStreamTest) {
367377
ASSERT_TRUE(Consumer->isSatisfied());
368378
}
369379

370-
TEST_F(ParseHLSLRootSignatureTest, InvalidMissingParameterTest) {
380+
TEST_F(ParseHLSLRootSignatureTest, InvalidMissingDTParameterTest) {
371381
// This test will check that the parsing fails due a mandatory
372382
// parameter (register) not being specified
373383
const llvm::StringLiteral Source = R"cc(
@@ -391,7 +401,29 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidMissingParameterTest) {
391401
ASSERT_TRUE(Consumer->isSatisfied());
392402
}
393403

394-
TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedMandatoryParameterTest) {
404+
TEST_F(ParseHLSLRootSignatureTest, InvalidMissingRCParameterTest) {
405+
// This test will check that the parsing fails due a mandatory
406+
// parameter (num32BitConstants) not being specified
407+
const llvm::StringLiteral Source = R"cc(
408+
RootConstants(b0)
409+
)cc";
410+
411+
TrivialModuleLoader ModLoader;
412+
auto PP = createPP(Source, ModLoader);
413+
auto TokLoc = SourceLocation();
414+
415+
hlsl::RootSignatureLexer Lexer(Source, TokLoc);
416+
SmallVector<RootElement> Elements;
417+
hlsl::RootSignatureParser Parser(Elements, Lexer, *PP);
418+
419+
// Test correct diagnostic produced
420+
Consumer->setExpected(diag::err_hlsl_rootsig_missing_param);
421+
ASSERT_TRUE(Parser.parse());
422+
423+
ASSERT_TRUE(Consumer->isSatisfied());
424+
}
425+
426+
TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedMandatoryDTParameterTest) {
395427
// This test will check that the parsing fails due the same mandatory
396428
// parameter being specified multiple times
397429
const llvm::StringLiteral Source = R"cc(
@@ -415,6 +447,28 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedMandatoryParameterTest) {
415447
ASSERT_TRUE(Consumer->isSatisfied());
416448
}
417449

450+
TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedMandatoryRCParameterTest) {
451+
// This test will check that the parsing fails due the same mandatory
452+
// parameter being specified multiple times
453+
const llvm::StringLiteral Source = R"cc(
454+
RootConstants(num32BitConstants = 32, num32BitConstants = 24)
455+
)cc";
456+
457+
TrivialModuleLoader ModLoader;
458+
auto PP = createPP(Source, ModLoader);
459+
auto TokLoc = SourceLocation();
460+
461+
hlsl::RootSignatureLexer Lexer(Source, TokLoc);
462+
SmallVector<RootElement> Elements;
463+
hlsl::RootSignatureParser Parser(Elements, Lexer, *PP);
464+
465+
// Test correct diagnostic produced
466+
Consumer->setExpected(diag::err_hlsl_rootsig_repeat_param);
467+
ASSERT_TRUE(Parser.parse());
468+
469+
ASSERT_TRUE(Consumer->isSatisfied());
470+
}
471+
418472
TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedOptionalParameterTest) {
419473
// This test will check that the parsing fails due the same optional
420474
// parameter being specified multiple times

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ struct Register {
5555
};
5656

5757
// Models the parameter values of root constants
58-
struct RootConstants {};
58+
struct RootConstants {
59+
uint32_t Num32BitConstants;
60+
Register Reg;
61+
};
5962

6063
// Models the end of a descriptor table and stores its visibility
6164
struct DescriptorTable {

0 commit comments

Comments
 (0)