[clang] [llvm] [HLSL][RootSignature] Add parsing for empty RootConstants (PR #137999)
Finn Plummer via llvm-commits
llvm-commits at lists.llvm.org
Wed May 7 11:06:08 PDT 2025
https://github.com/inbelic updated https://github.com/llvm/llvm-project/pull/137999
>From d6e88d0ced28861ed0181b2d4aa2269582f768d9 Mon Sep 17 00:00:00 2001
From: Finn Plummer <canadienfinn at gmail.com>
Date: Wed, 30 Apr 2025 17:17:32 +0000
Subject: [PATCH 1/4] pre-req: add missing lexing keywords
---
clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def | 4 ++++
clang/unittests/Lex/LexHLSLRootSignatureTest.cpp | 4 +++-
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
index d94be66b420c7..ecb8cfc7afa16 100644
--- a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
+++ b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
@@ -74,6 +74,10 @@ PUNCTUATOR(minus, '-')
// RootElement Keywords:
KEYWORD(RootSignature) // used only for diagnostic messaging
KEYWORD(DescriptorTable)
+KEYWORD(RootConstants)
+
+// RootConstants Keywords:
+KEYWORD(num32BitConstants)
// DescriptorTable Keywords:
KEYWORD(CBV)
diff --git a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
index 2024ff3a7dba9..89e9a3183ad03 100644
--- a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
@@ -87,7 +87,9 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
RootSignature
- DescriptorTable
+ DescriptorTable RootConstants
+
+ num32BitConstants
CBV SRV UAV Sampler
space visibility flags
>From 81e6e05cdc78ae5b8b212c27084a6111056e4253 Mon Sep 17 00:00:00 2001
From: Finn Plummer <canadienfinn at gmail.com>
Date: Wed, 30 Apr 2025 17:18:06 +0000
Subject: [PATCH 2/4] [HLSL][RootSignature] Add parsing for empty RootConstants
- defines the empty RootConstants in-memory struct
- adds test harness for testing it
---
.../clang/Parse/ParseHLSLRootSignature.h | 1 +
clang/lib/Parse/ParseHLSLRootSignature.cpp | 25 ++++++++++++++++++
.../Parse/ParseHLSLRootSignatureTest.cpp | 26 +++++++++++++++++++
.../llvm/Frontend/HLSL/HLSLRootSignature.h | 8 ++++--
4 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h
index 91640e8bf0354..efa735ea03d94 100644
--- a/clang/include/clang/Parse/ParseHLSLRootSignature.h
+++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h
@@ -71,6 +71,7 @@ class RootSignatureParser {
// expected, or, there is a lexing error
/// Root Element parse methods:
+ std::optional<llvm::hlsl::rootsig::RootConstants> parseRootConstants();
std::optional<llvm::hlsl::rootsig::DescriptorTable> parseDescriptorTable();
std::optional<llvm::hlsl::rootsig::DescriptorTableClause>
parseDescriptorTableClause();
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index 042aedbf1af52..7f0a07409c6aa 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -27,6 +27,13 @@ RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
bool RootSignatureParser::parse() {
// Iterate as many RootElements as possible
do {
+ if (tryConsumeExpectedToken(TokenKind::kw_RootConstants)) {
+ auto Constants = parseRootConstants();
+ if (!Constants.has_value())
+ return true;
+ Elements.push_back(*Constants);
+ }
+
if (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
auto Table = parseDescriptorTable();
if (!Table.has_value())
@@ -43,6 +50,24 @@ bool RootSignatureParser::parse() {
return false;
}
+std::optional<RootConstants> RootSignatureParser::parseRootConstants() {
+ assert(CurToken.TokKind == TokenKind::kw_RootConstants &&
+ "Expects to only be invoked starting at given keyword");
+
+ if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
+ CurToken.TokKind))
+ return std::nullopt;
+
+ RootConstants Constants;
+
+ if (consumeExpectedToken(TokenKind::pu_r_paren,
+ diag::err_hlsl_unexpected_end_of_params,
+ /*param of=*/TokenKind::kw_RootConstants))
+ return std::nullopt;
+
+ return Constants;
+}
+
std::optional<DescriptorTable> RootSignatureParser::parseDescriptorTable() {
assert(CurToken.TokKind == TokenKind::kw_DescriptorTable &&
"Expects to only be invoked starting at given keyword");
diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
index 585ac051d66a2..0a7d8ac86cc5f 100644
--- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
@@ -252,6 +252,32 @@ TEST_F(ParseHLSLRootSignatureTest, ValidSamplerFlagsTest) {
ASSERT_TRUE(Consumer->isSatisfied());
}
+TEST_F(ParseHLSLRootSignatureTest, ValidParseRootConsantsTest) {
+ const llvm::StringLiteral Source = R"cc(
+ RootConstants()
+ )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 no diagnostics produced
+ Consumer->setNoDiag();
+
+ ASSERT_FALSE(Parser.parse());
+
+ ASSERT_EQ(Elements.size(), 1u);
+
+ RootElement Elem = Elements[0];
+ ASSERT_TRUE(std::holds_alternative<RootConstants>(Elem));
+
+ ASSERT_TRUE(Consumer->isSatisfied());
+}
+
TEST_F(ParseHLSLRootSignatureTest, ValidTrailingCommaTest) {
// This test will checks we can handling trailing commas ','
const llvm::StringLiteral Source = R"cc(
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
index 818caccfe1998..05735fa75b318 100644
--- a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
@@ -54,6 +54,9 @@ struct Register {
uint32_t Number;
};
+// Models the parameter values of root constants
+struct RootConstants {};
+
// Models the end of a descriptor table and stores its visibility
struct DescriptorTable {
ShaderVisibility Visibility = ShaderVisibility::All;
@@ -88,8 +91,9 @@ struct DescriptorTableClause {
}
};
-// Models RootElement : DescriptorTable | DescriptorTableClause
-using RootElement = std::variant<DescriptorTable, DescriptorTableClause>;
+// Models RootElement : RootConstants | DescriptorTable | DescriptorTableClause
+using RootElement =
+ std::variant<RootConstants, DescriptorTable, DescriptorTableClause>;
} // namespace rootsig
} // namespace hlsl
>From 6912198648fb70b23728df89fc44916b4f481c2d Mon Sep 17 00:00:00 2001
From: Finn Plummer <canadienfinn at gmail.com>
Date: Wed, 30 Apr 2025 17:21:30 +0000
Subject: [PATCH 3/4] nfc: clean-up review comment from previous pr
---
clang/lib/Parse/ParseHLSLRootSignature.cpp | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index 7f0a07409c6aa..f9e672850012b 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -42,12 +42,9 @@ bool RootSignatureParser::parse() {
}
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
- if (consumeExpectedToken(TokenKind::end_of_stream,
- diag::err_hlsl_unexpected_end_of_params,
- /*param of=*/TokenKind::kw_RootSignature))
- return true;
-
- return false;
+ return consumeExpectedToken(TokenKind::end_of_stream,
+ diag::err_hlsl_unexpected_end_of_params,
+ /*param of=*/TokenKind::kw_RootSignature));
}
std::optional<RootConstants> RootSignatureParser::parseRootConstants() {
>From e96b347c2d528808fb87db5a7f6b645d5dc64f7b Mon Sep 17 00:00:00 2001
From: Finn Plummer <canadienfinn at gmail.com>
Date: Wed, 30 Apr 2025 17:44:43 +0000
Subject: [PATCH 4/4] fix typo
---
clang/lib/Parse/ParseHLSLRootSignature.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index f9e672850012b..48d3e38b0519d 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -44,7 +44,7 @@ bool RootSignatureParser::parse() {
return consumeExpectedToken(TokenKind::end_of_stream,
diag::err_hlsl_unexpected_end_of_params,
- /*param of=*/TokenKind::kw_RootSignature));
+ /*param of=*/TokenKind::kw_RootSignature);
}
std::optional<RootConstants> RootSignatureParser::parseRootConstants() {
More information about the llvm-commits
mailing list