https://github.com/inbelic updated https://github.com/llvm/llvm-project/pull/140148
>From ae3cffb72588f7afa5107dbd0b7d47137336273a Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Thu, 15 May 2025 19:49:44 +0000 Subject: [PATCH 1/2] [HLSL][RootSignature] Add parsing of Register in params for RootParams - defines the `parseRootParamParams` infastructure for parsing the params of a RootParam - defines the register type to illustrate use - add tests to demonstrate functionality --- .../clang/Parse/ParseHLSLRootSignature.h | 6 +++ clang/lib/Parse/ParseHLSLRootSignature.cpp | 42 +++++++++++++++++++ .../Parse/ParseHLSLRootSignatureTest.cpp | 12 ++++-- .../llvm/Frontend/HLSL/HLSLRootSignature.h | 1 + 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h index 939d45e0c01bc..100942bc37682 100644 --- a/clang/include/clang/Parse/ParseHLSLRootSignature.h +++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h @@ -89,6 +89,12 @@ class RootSignatureParser { }; std::optional<ParsedConstantParams> parseRootConstantParams(); + struct ParsedRootParamParams { + std::optional<llvm::hlsl::rootsig::Register> Reg; + }; + std::optional<ParsedRootParamParams> + parseRootParamParams(RootSignatureToken::Kind RegType); + struct ParsedClauseParams { std::optional<llvm::hlsl::rootsig::Register> Reg; std::optional<uint32_t> NumDescriptors; diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp index d71f2c22d5d4b..759b60bc59d09 100644 --- a/clang/lib/Parse/ParseHLSLRootSignature.cpp +++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp @@ -176,20 +176,37 @@ std::optional<RootDescriptor> RootSignatureParser::parseRootDescriptor() { return std::nullopt; RootDescriptor Descriptor; + TokenKind ExpectedReg; switch (DescriptorKind) { default: llvm_unreachable("Switch for consumed token was not provided"); case TokenKind::kw_CBV: Descriptor.Type = DescriptorType::CBuffer; + ExpectedReg = TokenKind::bReg; break; case TokenKind::kw_SRV: Descriptor.Type = DescriptorType::SRV; + ExpectedReg = TokenKind::tReg; break; case TokenKind::kw_UAV: Descriptor.Type = DescriptorType::UAV; + ExpectedReg = TokenKind::uReg; break; } + auto Params = parseRootParamParams(ExpectedReg); + if (!Params.has_value()) + return std::nullopt; + + // Check mandatory parameters were provided + if (!Params->Reg.has_value()) { + getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_missing_param) + << ExpectedReg; + return std::nullopt; + } + + Param.Reg = Params->Reg.value(); + if (consumeExpectedToken(TokenKind::pu_r_paren, diag::err_hlsl_unexpected_end_of_params, /*param of=*/TokenKind::kw_RootConstants)) @@ -398,6 +415,31 @@ RootSignatureParser::parseRootConstantParams() { return Params; } +std::optional<RootSignatureParser::ParsedRootParamParams> +RootSignatureParser::parseRootParamParams(TokenKind RegType) { + assert(CurToken.TokKind == TokenKind::pu_l_paren && + "Expects to only be invoked starting at given token"); + + ParsedRootParamParams Params; + do { + // ( `b` | `t` | `u`) POS_INT + if (tryConsumeExpectedToken(RegType)) { + if (Params.Reg.has_value()) { + getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param) + << CurToken.TokKind; + return std::nullopt; + } + auto Reg = parseRegister(); + if (!Reg.has_value()) + return std::nullopt; + Params.Reg = Reg; + } + + } while (tryConsumeExpectedToken(TokenKind::pu_comma)); + + return Params; +} + std::optional<RootSignatureParser::ParsedClauseParams> RootSignatureParser::parseDescriptorTableClauseParams(TokenKind RegType) { assert(CurToken.TokKind == TokenKind::pu_l_paren && diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp index 7d03dda1f516b..3a638ec1ff075 100644 --- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp +++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp @@ -346,9 +346,9 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootFlagsTest) { TEST_F(ParseHLSLRootSignatureTest, ValidParseRootDescriptorsTest) { const llvm::StringLiteral Source = R"cc( - CBV(), - SRV(), - UAV() + CBV(b0), + SRV(t42), + UAV(u34893247) )cc"; TrivialModuleLoader ModLoader; @@ -369,14 +369,20 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootDescriptorsTest) { RootElement Elem = Elements[0]; ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem)); ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::CBuffer); + ASSERT_EQ(std::get<RootParam>(Elem).Reg.ViewType, RegisterType::BReg); + ASSERT_EQ(std::get<RootParam>(Elem).Reg.Number, 0u); Elem = Elements[1]; ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem)); ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::SRV); + ASSERT_EQ(std::get<RootParam>(Elem).Reg.ViewType, RegisterType::TReg); + ASSERT_EQ(std::get<RootParam>(Elem).Reg.Number, 42u); Elem = Elements[2]; ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem)); ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::UAV); + ASSERT_EQ(std::get<RootParam>(Elem).Reg.ViewType, RegisterType::UReg); + ASSERT_EQ(std::get<RootParam>(Elem).Reg.Number, 34893247u); ASSERT_TRUE(Consumer->isSatisfied()); } diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h index 7829b842fa2be..4b1bbd0e3bd12 100644 --- a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h +++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h @@ -89,6 +89,7 @@ using DescriptorType = llvm::dxil::ResourceClass; // Models RootDescriptor : CBV | SRV | UAV, by collecting like parameters struct RootDescriptor { DescriptorType Type; + Register Reg; }; // Models the end of a descriptor table and stores its visibility >From bc276aa51ddb0b692952fd8d7d187c4b667121ef Mon Sep 17 00:00:00 2001 From: Finn Plummer <canadienf...@gmail.com> Date: Wed, 21 May 2025 19:02:30 +0000 Subject: [PATCH 2/2] review: rename RootParam to RootDescriptor --- clang/include/clang/Parse/ParseHLSLRootSignature.h | 6 +++--- clang/lib/Parse/ParseHLSLRootSignature.cpp | 8 ++++---- clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp | 12 ++++++------ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h index 100942bc37682..0d56d2a16a268 100644 --- a/clang/include/clang/Parse/ParseHLSLRootSignature.h +++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h @@ -89,11 +89,11 @@ class RootSignatureParser { }; std::optional<ParsedConstantParams> parseRootConstantParams(); - struct ParsedRootParamParams { + struct ParsedRootDescriptorParams { std::optional<llvm::hlsl::rootsig::Register> Reg; }; - std::optional<ParsedRootParamParams> - parseRootParamParams(RootSignatureToken::Kind RegType); + std::optional<ParsedRootDescriptorParams> + parseRootDescriptorParams(RootSignatureToken::Kind RegType); struct ParsedClauseParams { std::optional<llvm::hlsl::rootsig::Register> Reg; diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp index 759b60bc59d09..1accaef1ada91 100644 --- a/clang/lib/Parse/ParseHLSLRootSignature.cpp +++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp @@ -194,7 +194,7 @@ std::optional<RootDescriptor> RootSignatureParser::parseRootDescriptor() { break; } - auto Params = parseRootParamParams(ExpectedReg); + auto Params = parseRootDescriptorParams(ExpectedReg); if (!Params.has_value()) return std::nullopt; @@ -415,12 +415,12 @@ RootSignatureParser::parseRootConstantParams() { return Params; } -std::optional<RootSignatureParser::ParsedRootParamParams> -RootSignatureParser::parseRootParamParams(TokenKind RegType) { +std::optional<RootSignatureParser::ParsedRootDescriptorParams> +RootSignatureParser::parseRootDescriptorParams(TokenKind RegType) { assert(CurToken.TokKind == TokenKind::pu_l_paren && "Expects to only be invoked starting at given token"); - ParsedRootParamParams Params; + ParsedRootDescriptorParams Params; do { // ( `b` | `t` | `u`) POS_INT if (tryConsumeExpectedToken(RegType)) { diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp index 3a638ec1ff075..a6ee424ff45fd 100644 --- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp +++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp @@ -369,20 +369,20 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseRootDescriptorsTest) { RootElement Elem = Elements[0]; ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem)); ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::CBuffer); - ASSERT_EQ(std::get<RootParam>(Elem).Reg.ViewType, RegisterType::BReg); - ASSERT_EQ(std::get<RootParam>(Elem).Reg.Number, 0u); + ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.ViewType, RegisterType::BReg); + ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.Number, 0u); Elem = Elements[1]; ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem)); ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::SRV); - ASSERT_EQ(std::get<RootParam>(Elem).Reg.ViewType, RegisterType::TReg); - ASSERT_EQ(std::get<RootParam>(Elem).Reg.Number, 42u); + ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.ViewType, RegisterType::TReg); + ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.Number, 42u); Elem = Elements[2]; ASSERT_TRUE(std::holds_alternative<RootDescriptor>(Elem)); ASSERT_EQ(std::get<RootDescriptor>(Elem).Type, DescriptorType::UAV); - ASSERT_EQ(std::get<RootParam>(Elem).Reg.ViewType, RegisterType::UReg); - ASSERT_EQ(std::get<RootParam>(Elem).Reg.Number, 34893247u); + ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.ViewType, RegisterType::UReg); + ASSERT_EQ(std::get<RootDescriptor>(Elem).Reg.Number, 34893247u); ASSERT_TRUE(Consumer->isSatisfied()); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits