Skip to content

[HLSL][RootSignature] Add optional parameters for RootConstants #138007

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 2 commits into from
May 9, 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 @@ -82,6 +82,8 @@ class RootSignatureParser {
struct ParsedConstantParams {
std::optional<llvm::hlsl::rootsig::Register> Reg;
std::optional<uint32_t> Num32BitConstants;
std::optional<uint32_t> Space;
std::optional<llvm::hlsl::rootsig::ShaderVisibility> Visibility;
};
std::optional<ParsedConstantParams> parseRootConstantParams();

Expand Down
41 changes: 41 additions & 0 deletions clang/lib/Parse/ParseHLSLRootSignature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ std::optional<RootConstants> RootSignatureParser::parseRootConstants() {

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

// Fill in optional parameters
if (Params->Visibility.has_value())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
if (Params->Visibility.has_value())
if (Params->Visibility)

Constants.Visibility = Params->Visibility.value();

if (Params->Space.has_value())
Constants.Space = Params->Space.value();

if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/TokenKind::kw_RootConstants))
Expand Down Expand Up @@ -247,6 +254,40 @@ RootSignatureParser::parseRootConstantParams() {
return std::nullopt;
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
36 changes: 34 additions & 2 deletions clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ TEST_F(ParseHLSLRootSignatureTest, ValidSamplerFlagsTest) {
TEST_F(ParseHLSLRootSignatureTest, ValidParseRootConsantsTest) {
const llvm::StringLiteral Source = R"cc(
RootConstants(num32BitConstants = 1, b0),
RootConstants(b42, num32BitConstants = 4294967295)
RootConstants(b42, space = 3, num32BitConstants = 4294967295,
visibility = SHADER_VISIBILITY_HULL
)
)cc";

TrivialModuleLoader ModLoader;
Expand All @@ -278,12 +280,16 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootConsantsTest) {
ASSERT_EQ(std::get<RootConstants>(Elem).Num32BitConstants, 1u);
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.ViewType, RegisterType::BReg);
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.Number, 0u);
ASSERT_EQ(std::get<RootConstants>(Elem).Space, 0u);
ASSERT_EQ(std::get<RootConstants>(Elem).Visibility, ShaderVisibility::All);

Elem = Elements[1];
ASSERT_TRUE(std::holds_alternative<RootConstants>(Elem));
ASSERT_EQ(std::get<RootConstants>(Elem).Num32BitConstants, 4294967295u);
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.ViewType, RegisterType::BReg);
ASSERT_EQ(std::get<RootConstants>(Elem).Reg.Number, 42u);
ASSERT_EQ(std::get<RootConstants>(Elem).Space, 3u);
ASSERT_EQ(std::get<RootConstants>(Elem).Visibility, ShaderVisibility::Hull);

ASSERT_TRUE(Consumer->isSatisfied());
}
Expand Down Expand Up @@ -469,7 +475,7 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedMandatoryRCParameterTest) {
ASSERT_TRUE(Consumer->isSatisfied());
}

TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedOptionalParameterTest) {
TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedOptionalDTParameterTest) {
// This test will check that the parsing fails due the same optional
// parameter being specified multiple times
const llvm::StringLiteral Source = R"cc(
Expand All @@ -493,6 +499,32 @@ TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedOptionalParameterTest) {
ASSERT_TRUE(Consumer->isSatisfied());
}

TEST_F(ParseHLSLRootSignatureTest, InvalidRepeatedOptionalRCParameterTest) {
// This test will check that the parsing fails due the same optional
// parameter being specified multiple times
const llvm::StringLiteral Source = R"cc(
RootConstants(
visibility = Shader_Visibility_All,
b0, num32BitConstants = 1,
visibility = Shader_Visibility_Pixel
)
)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_repeat_param);
ASSERT_TRUE(Parser.parse());

ASSERT_TRUE(Consumer->isSatisfied());
}

TEST_F(ParseHLSLRootSignatureTest, InvalidLexOverflowedNumberTest) {
// This test will check that the lexing fails due to an integer overflow
const llvm::StringLiteral Source = R"cc(
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 @@ -58,6 +58,8 @@ struct Register {
struct RootConstants {
uint32_t Num32BitConstants;
Register Reg;
uint32_t Space = 0;
ShaderVisibility Visibility = ShaderVisibility::All;
};

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