[llvm] 2b64b15 - [HLSL][RootSignature] Add parsing of optional parameters for RootDescriptor (#140151)
via llvm-commits
llvm-commits at lists.llvm.org
Thu May 22 19:20:38 PDT 2025
Author: Finn Plummer
Date: 2025-05-22T19:20:34-07:00
New Revision: 2b64b1566c2b62e46b2e2268c78aab204eccb9a1
URL: https://github.com/llvm/llvm-project/commit/2b64b1566c2b62e46b2e2268c78aab204eccb9a1
DIFF: https://github.com/llvm/llvm-project/commit/2b64b1566c2b62e46b2e2268c78aab204eccb9a1.diff
LOG: [HLSL][RootSignature] Add parsing of optional parameters for RootDescriptor (#140151)
- define in-memory representation of optional non-flag parameters to
`RootDescriptor`
- fill in data to parse these params in `parseRootDescriptorParams`
- add unit tests
Part 3 of https://github.com/llvm/llvm-project/issues/126577
Added:
Modified:
clang/include/clang/Parse/ParseHLSLRootSignature.h
clang/lib/Parse/ParseHLSLRootSignature.cpp
clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
Removed:
################################################################################
diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h
index 0d56d2a16a268..739a14d43584e 100644
--- a/clang/include/clang/Parse/ParseHLSLRootSignature.h
+++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h
@@ -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);
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index da01df04ac78e..92be49b8a96b2 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -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))
@@ -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;
diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
index 665ef5b518be7..07b4f625a2e05 100644
--- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
@@ -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;
@@ -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());
}
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
index 4b1bbd0e3bd12..7c496844ab7a6 100644
--- a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
@@ -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
More information about the llvm-commits
mailing list