Skip to content

[HLSL][RootSignature] Add parsing of optional parameters for RootDescriptor #140151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Parse/ParseHLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class RootSignatureParser {

struct ParsedRootDescriptorParams {
std::optional<llvm::hlsl::rootsig::Register> Reg;
std::optional<uint32_t> Space;
std::optional<llvm::hlsl::rootsig::ShaderVisibility> Visibility;
};
std::optional<ParsedRootDescriptorParams>
parseRootDescriptorParams(RootSignatureToken::Kind RegType);
Expand Down
40 changes: 40 additions & 0 deletions clang/lib/Parse/ParseHLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ std::optional<RootDescriptor> RootSignatureParser::parseRootDescriptor() {

Descriptor.Reg = Params->Reg.value();

// Fill in optional values
if (Params->Space.has_value())
Descriptor.Space = Params->Space.value();

if (Params->Visibility.has_value())
Descriptor.Visibility = Params->Visibility.value();

if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/TokenKind::kw_RootConstants))
Expand Down Expand Up @@ -435,6 +442,39 @@ RootSignatureParser::parseRootDescriptorParams(TokenKind RegType) {
Params.Reg = Reg;
}

// `space` `=` POS_INT
if (tryConsumeExpectedToken(TokenKind::kw_space)) {
if (Params.Space.has_value()) {
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
<< CurToken.TokKind;
return std::nullopt;
}

if (consumeExpectedToken(TokenKind::pu_equal))
return std::nullopt;

auto Space = parseUIntParam();
if (!Space.has_value())
return std::nullopt;
Params.Space = Space;
}

// `visibility` `=` SHADER_VISIBILITY
if (tryConsumeExpectedToken(TokenKind::kw_visibility)) {
if (Params.Visibility.has_value()) {
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
<< CurToken.TokKind;
return std::nullopt;
}

if (consumeExpectedToken(TokenKind::pu_equal))
return std::nullopt;

auto Visibility = parseShaderVisibility();
if (!Visibility.has_value())
return std::nullopt;
Params.Visibility = Visibility;
}
} while (tryConsumeExpectedToken(TokenKind::pu_comma));

return Params;
Expand Down
11 changes: 9 additions & 2 deletions clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootFlagsTest) {
TEST_F(ParseHLSLRootSignatureTest, ValidParseRootDescriptorsTest) {
const llvm::StringLiteral Source = R"cc(
CBV(b0),
SRV(t42),
UAV(u34893247)
SRV(space = 4, t42, visibility = SHADER_VISIBILITY_GEOMETRY),
UAV(visibility = SHADER_VISIBILITY_HULL, u34893247)
)cc";

TrivialModuleLoader ModLoader;
Expand All @@ -371,18 +371,25 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootDescriptorsTest) {
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);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Space, 0u);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Visibility, ShaderVisibility::All);

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);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Space, 4u);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Visibility,
ShaderVisibility::Geometry);

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_EQ(std::get<RootDescriptor>(Elem).Space, 0u);
ASSERT_EQ(std::get<RootDescriptor>(Elem).Visibility, ShaderVisibility::Hull);

ASSERT_TRUE(Consumer->isSatisfied());
}
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ using DescriptorType = llvm::dxil::ResourceClass;
struct RootDescriptor {
DescriptorType Type;
Register Reg;
uint32_t Space = 0;
ShaderVisibility Visibility = ShaderVisibility::All;
};

// Models the end of a descriptor table and stores its visibility
Expand Down