-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[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
base: users/inbelic/pr-138002
Are you sure you want to change the base?
[HLSL][RootSignature] Add optional parameters for RootConstants #138007
Conversation
- extends `parseRootConstantParams` and the struct to include the optional parameters of a RootConstant - adds corresponding unit tests
@llvm/pr-subscribers-hlsl @llvm/pr-subscribers-clang Author: Finn Plummer (inbelic) Changes
Part three of and resolves #126576 Full diff: https://github.com/llvm/llvm-project/pull/138007.diff 4 Files Affected:
diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h
index 0f05b05ed4df6..2ac2083983741 100644
--- a/clang/include/clang/Parse/ParseHLSLRootSignature.h
+++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h
@@ -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();
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index 2ce8e6e5cca98..a5006b77a6e44 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -78,6 +78,13 @@ std::optional<RootConstants> RootSignatureParser::parseRootConstants() {
Constants.Reg = Params->Reg.value();
+ // Fill in optional parameters
+ if (Params->Visibility.has_value())
+ 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))
@@ -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;
diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
index 336868b579866..150eb3e6e54ef 100644
--- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
@@ -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;
@@ -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());
}
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
index a3f98a9f1944f..8b8324df18bb3 100644
--- a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
@@ -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
|
@@ -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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space is used whenever we have registers, shouldn't this be an optional field in the registers struct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having it separate maps closer and clearer to the metadata and parameter specification
@@ -255,7 +255,9 @@ TEST_F(ParseHLSLRootSignatureTest, ValidSamplerFlagsTest) { | |||
TEST_F(ParseHLSLRootSignatureTest, ValidParseRootConsantsTest) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest having tests cases for mandatory and optional fields. Also test cases to verify the error cases as well, if possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can add more. Just wasn't sure what the right balance is, as many of the error reports are already tested. I guess more tests never hurt tho. Will update.
extends
parseRootConstantParams
and the struct to include the optional parameters of a RootConstantadds corresponding unit tests
Part three of and resolves #126576