[HLSL][RootSignature] Add parsing of Register in params for RootDescriptors (#140148)

- defines the `parseRootDescriptorParams` infrastructure for parsing the
params of a `RootDescriptor`
- defines the register type to illustrate use
- add tests to demonstrate functionality

Part 2 of https://github.com/llvm/llvm-project/issues/126577
This commit is contained in:
Finn Plummer
2025-05-22 14:07:24 -07:00
committed by GitHub
parent 9d3ce55261
commit 0c42aeff9e
4 changed files with 80 additions and 3 deletions

View File

@@ -89,6 +89,12 @@ private:
};
std::optional<ParsedConstantParams> parseRootConstantParams();
struct ParsedRootDescriptorParams {
std::optional<llvm::hlsl::rootsig::Register> Reg;
};
std::optional<ParsedRootDescriptorParams>
parseRootDescriptorParams(RootSignatureToken::Kind RegType);
struct ParsedClauseParams {
std::optional<llvm::hlsl::rootsig::Register> Reg;
std::optional<uint32_t> NumDescriptors;

View File

@@ -176,20 +176,37 @@ std::optional<RootDescriptor> RootSignatureParser::parseRootDescriptor() {
return std::nullopt;
RootDescriptor Descriptor;
TokenKind ExpectedReg;
switch (DescriptorKind) {
default:
llvm_unreachable("Switch for consumed token was not provided");
case TokenKind::kw_CBV:
Descriptor.Type = DescriptorType::CBuffer;
ExpectedReg = TokenKind::bReg;
break;
case TokenKind::kw_SRV:
Descriptor.Type = DescriptorType::SRV;
ExpectedReg = TokenKind::tReg;
break;
case TokenKind::kw_UAV:
Descriptor.Type = DescriptorType::UAV;
ExpectedReg = TokenKind::uReg;
break;
}
auto Params = parseRootDescriptorParams(ExpectedReg);
if (!Params.has_value())
return std::nullopt;
// Check mandatory parameters were provided
if (!Params->Reg.has_value()) {
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_missing_param)
<< ExpectedReg;
return std::nullopt;
}
Descriptor.Reg = Params->Reg.value();
if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/TokenKind::kw_RootConstants))
@@ -398,6 +415,31 @@ RootSignatureParser::parseRootConstantParams() {
return Params;
}
std::optional<RootSignatureParser::ParsedRootDescriptorParams>
RootSignatureParser::parseRootDescriptorParams(TokenKind RegType) {
assert(CurToken.TokKind == TokenKind::pu_l_paren &&
"Expects to only be invoked starting at given token");
ParsedRootDescriptorParams Params;
do {
// ( `b` | `t` | `u`) POS_INT
if (tryConsumeExpectedToken(RegType)) {
if (Params.Reg.has_value()) {
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
<< CurToken.TokKind;
return std::nullopt;
}
auto Reg = parseRegister();
if (!Reg.has_value())
return std::nullopt;
Params.Reg = Reg;
}
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
return Params;
}
std::optional<RootSignatureParser::ParsedClauseParams>
RootSignatureParser::parseDescriptorTableClauseParams(TokenKind RegType) {
assert(CurToken.TokKind == TokenKind::pu_l_paren &&

View File

@@ -346,9 +346,9 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootFlagsTest) {
TEST_F(ParseHLSLRootSignatureTest, ValidParseRootDescriptorsTest) {
const llvm::StringLiteral Source = R"cc(
CBV(),
SRV(),
UAV()
CBV(b0),
SRV(t42),
UAV(u34893247)
)cc";
TrivialModuleLoader ModLoader;
@@ -369,14 +369,20 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootDescriptorsTest) {
RootElement Elem = Elements[0];
ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem));
ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::CBuffer);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.ViewType, RegisterType::BReg);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.Number, 0u);
Elem = Elements[1];
ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem));
ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::SRV);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.ViewType, RegisterType::TReg);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.Number, 42u);
Elem = Elements[2];
ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem));
ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::UAV);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.ViewType, RegisterType::UReg);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.Number, 34893247u);
ASSERT_TRUE(Consumer->isSatisfied());
}
@@ -494,6 +500,28 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidMissingDTParameterTest) {
ASSERT_TRUE(Consumer->isSatisfied());
}
TEST_F(ParseHLSLRootSignatureTest, InvalidMissingRDParameterTest) {
// This test will check that the parsing fails due a mandatory
// parameter (register) not being specified
const llvm::StringLiteral Source = R"cc(
SRV()
)cc";
TrivialModuleLoader ModLoader;
auto PP = createPP(Source, ModLoader);
auto TokLoc = SourceLocation();
hlsl::RootSignatureLexer Lexer(Source, TokLoc);
SmallVector<RootElement> Elements;
hlsl::RootSignatureParser Parser(Elements, Lexer, *PP);
// Test correct diagnostic produced
Consumer->setExpected(diag::err_hlsl_rootsig_missing_param);
ASSERT_TRUE(Parser.parse());
ASSERT_TRUE(Consumer->isSatisfied());
}
TEST_F(ParseHLSLRootSignatureTest, InvalidMissingRCParameterTest) {
// This test will check that the parsing fails due a mandatory
// parameter (num32BitConstants) not being specified

View File

@@ -89,6 +89,7 @@ using DescriptorType = llvm::dxil::ResourceClass;
// Models RootDescriptor : CBV | SRV | UAV, by collecting like parameters
struct RootDescriptor {
DescriptorType Type;
Register Reg;
};
// Models the end of a descriptor table and stores its visibility