[llvm-branch-commits] [clang] [llvm] [HLSL][RootSiganture] Add parsing of new number params in StaticSampler (PR #140291)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri May 16 11:20:15 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Finn Plummer (inbelic)
<details>
<summary>Changes</summary>
- defines in-memory reprsentation of `maxAnisotropy`, `minLOD` and `maxLOD`
- integrates parsing of these number parameters with their respective, `parseUInt` and `parseFloat` respectively
- adds basic unit tests to demonstrate setting functionality
Part 3 of https://github.com/llvm/llvm-project/issues/126574
---
Full diff: https://github.com/llvm/llvm-project/pull/140291.diff
6 Files Affected:
- (modified) clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def (+3)
- (modified) clang/include/clang/Parse/ParseHLSLRootSignature.h (+3)
- (modified) clang/lib/Parse/ParseHLSLRootSignature.cpp (+60)
- (modified) clang/unittests/Lex/LexHLSLRootSignatureTest.cpp (+1-1)
- (modified) clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp (+20-2)
- (modified) llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h (+3)
``````````diff
diff --git a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
index 5d16eaa5b72f6..7ca131349fed4 100644
--- a/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
+++ b/clang/include/clang/Lex/HLSLRootSignatureTokenKinds.def
@@ -102,6 +102,9 @@ KEYWORD(offset)
// StaticSampler Keywords:
KEYWORD(mipLODBias)
+KEYWORD(maxAnisotropy)
+KEYWORD(minLOD)
+KEYWORD(maxLOD)
// Unbounded Enum:
UNBOUNDED_ENUM(unbounded, "unbounded")
diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h
index c12b022a030ef..e62d5de948a44 100644
--- a/clang/include/clang/Parse/ParseHLSLRootSignature.h
+++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h
@@ -112,6 +112,9 @@ class RootSignatureParser {
struct ParsedStaticSamplerParams {
std::optional<llvm::hlsl::rootsig::Register> Reg;
std::optional<float> MipLODBias;
+ std::optional<uint32_t> MaxAnisotropy;
+ std::optional<float> MinLOD;
+ std::optional<float> MaxLOD;
};
std::optional<ParsedStaticSamplerParams> parseStaticSamplerParams();
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index db2e922160062..04fe8d00f25cd 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -384,6 +384,15 @@ std::optional<StaticSampler> RootSignatureParser::parseStaticSampler() {
if (Params->MipLODBias.has_value())
Sampler.MipLODBias = Params->MipLODBias.value();
+ if (Params->MaxAnisotropy.has_value())
+ Sampler.MaxAnisotropy = Params->MaxAnisotropy.value();
+
+ if (Params->MinLOD.has_value())
+ Sampler.MinLOD = Params->MinLOD.value();
+
+ if (Params->MaxLOD.has_value())
+ Sampler.MaxLOD = Params->MaxLOD.value();
+
if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/TokenKind::kw_StaticSampler))
@@ -686,6 +695,57 @@ RootSignatureParser::parseStaticSamplerParams() {
return std::nullopt;
Params.MipLODBias = (float)*MipLODBias;
}
+
+ // `maxAnisotropy` `=` POS_INT
+ if (tryConsumeExpectedToken(TokenKind::kw_maxAnisotropy)) {
+ if (Params.MaxAnisotropy.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 MaxAnisotropy = parseUIntParam();
+ if (!MaxAnisotropy.has_value())
+ return std::nullopt;
+ Params.MaxAnisotropy = MaxAnisotropy;
+ }
+
+ // `minLOD` `=` NUMBER
+ if (tryConsumeExpectedToken(TokenKind::kw_minLOD)) {
+ if (Params.MinLOD.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 MinLOD = parseFloatParam();
+ if (!MinLOD.has_value())
+ return std::nullopt;
+ Params.MinLOD = MinLOD;
+ }
+
+ // `maxLOD` `=` NUMBER
+ if (tryConsumeExpectedToken(TokenKind::kw_maxLOD)) {
+ if (Params.MaxLOD.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 MaxLOD = parseFloatParam();
+ if (!MaxLOD.has_value())
+ return std::nullopt;
+ Params.MaxLOD = MaxLOD;
+ }
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
return Params;
diff --git a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
index b610b8f10f8da..575a97e75a05d 100644
--- a/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Lex/LexHLSLRootSignatureTest.cpp
@@ -136,7 +136,7 @@ TEST_F(LexHLSLRootSignatureTest, ValidLexAllTokensTest) {
space visibility flags
numDescriptors offset
- mipLODBias
+ mipLODBias maxAnisotropy minLOD maxLOD
unbounded
DESCRIPTOR_RANGE_OFFSET_APPEND
diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
index 539232e0bf2c2..94e0464ddf03b 100644
--- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
+++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp
@@ -225,7 +225,11 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseDTClausesTest) {
TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
const llvm::StringLiteral Source = R"cc(
- StaticSampler(s0, mipLODBias = 0)
+ StaticSampler(s0),
+ StaticSampler(s0, maxAnisotropy = 3,
+ minLOD = 4.2f, mipLODBias = 0.23e+3,
+ maxLOD = 9000,
+ )
)cc";
TrivialModuleLoader ModLoader;
@@ -241,13 +245,27 @@ TEST_F(ParseHLSLRootSignatureTest, ValidParseStaticSamplerTest) {
ASSERT_FALSE(Parser.parse());
- ASSERT_EQ(Elements.size(), 1u);
+ ASSERT_EQ(Elements.size(), 2u);
+ // Check default values are as expected
RootElement Elem = Elements[0];
ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
ASSERT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 0.f);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 16u);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).MinLOD, 0.f);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 3.402823466e+38f);
+
+ // Check values can be set as expected
+ Elem = Elements[1];
+ ASSERT_TRUE(std::holds_alternative<StaticSampler>(Elem));
+ ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.ViewType, RegisterType::SReg);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).Reg.Number, 0u);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).MipLODBias, 230.f);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).MaxAnisotropy, 3u);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).MinLOD, 4.2f);
+ ASSERT_EQ(std::get<StaticSampler>(Elem).MaxLOD, 9000.f);
ASSERT_TRUE(Consumer->isSatisfied());
}
diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
index 6b4da48a302bc..8ee58b87f5e49 100644
--- a/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
+++ b/llvm/include/llvm/Frontend/HLSL/HLSLRootSignature.h
@@ -161,6 +161,9 @@ struct DescriptorTableClause {
struct StaticSampler {
Register Reg;
float MipLODBias = 0.f;
+ uint32_t MaxAnisotropy = 16;
+ float MinLOD = 0.f;
+ float MaxLOD = 3.402823466e+38f; // FLT_MAX
};
/// Models RootElement : RootFlags | RootConstants | RootParam
``````````
</details>
https://github.com/llvm/llvm-project/pull/140291
More information about the llvm-branch-commits
mailing list