-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[NFC][RootSignatures] Conform to new std::optional calling conventions #136747
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
Conversation
@llvm/pr-subscribers-hlsl Author: Finn Plummer (inbelic) Changes
This is a clean-up task from #133800 Full diff: https://github.com/llvm/llvm-project/pull/136747.diff 2 Files Affected:
diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h
index 3eb3f8ea8422d..d9f121030c1fc 100644
--- a/clang/include/clang/Parse/ParseHLSLRootSignature.h
+++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h
@@ -71,8 +71,9 @@ class RootSignatureParser {
// expected, or, there is a lexing error
/// Root Element parse methods:
- bool parseDescriptorTable();
- bool parseDescriptorTableClause();
+ std::optional<llvm::hlsl::rootsig::DescriptorTable> parseDescriptorTable();
+ std::optional<llvm::hlsl::rootsig::DescriptorTableClause>
+ parseDescriptorTableClause();
/// Parameter arguments (eg. `bReg`, `space`, ...) can be specified in any
/// order and only exactly once. `ParsedClauseParams` denotes the current
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index 4f8bfccfa2243..1bf33b8e8329c 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -26,22 +26,14 @@ RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
bool RootSignatureParser::parse() {
// Iterate as many RootElements as possible
- while (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
- // Dispatch onto parser method.
- // We guard against the unreachable here as we just ensured that CurToken
- // will be one of the kinds in the while condition
- switch (CurToken.TokKind) {
- case TokenKind::kw_DescriptorTable:
- if (parseDescriptorTable())
+ do {
+ if (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
+ auto Table = parseDescriptorTable();
+ if (!Table.has_value())
return true;
- break;
- default:
- llvm_unreachable("Switch for consumed token was not provided");
+ Elements.push_back(*Table);
}
-
- if (!tryConsumeExpectedToken(TokenKind::pu_comma))
- break;
- }
+ } while (tryConsumeExpectedToken(TokenKind::pu_comma));
if (consumeExpectedToken(TokenKind::end_of_stream,
diag::err_hlsl_unexpected_end_of_params,
@@ -51,38 +43,38 @@ bool RootSignatureParser::parse() {
return false;
}
-bool RootSignatureParser::parseDescriptorTable() {
+std::optional<DescriptorTable> RootSignatureParser::parseDescriptorTable() {
assert(CurToken.TokKind == TokenKind::kw_DescriptorTable &&
"Expects to only be invoked starting at given keyword");
- DescriptorTable Table;
-
if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
CurToken.TokKind))
- return true;
-
- // Iterate as many Clauses as possible
- while (tryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV,
- TokenKind::kw_UAV, TokenKind::kw_Sampler})) {
- if (parseDescriptorTableClause())
- return true;
+ return std::nullopt;
- Table.NumClauses++;
+ DescriptorTable Table;
- if (!tryConsumeExpectedToken(TokenKind::pu_comma))
- break;
- }
+ // Iterate as many Clauses as possible
+ do {
+ if (tryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV,
+ TokenKind::kw_UAV, TokenKind::kw_Sampler})) {
+ auto Clause = parseDescriptorTableClause();
+ if (!Clause.has_value())
+ return std::nullopt;
+ Elements.push_back(*Clause);
+ Table.NumClauses++;
+ }
+ } while (tryConsumeExpectedToken(TokenKind::pu_comma));
if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/TokenKind::kw_DescriptorTable))
- return true;
+ return std::nullopt;
- Elements.push_back(Table);
- return false;
+ return Table;
}
-bool RootSignatureParser::parseDescriptorTableClause() {
+std::optional<DescriptorTableClause>
+RootSignatureParser::parseDescriptorTableClause() {
assert((CurToken.TokKind == TokenKind::kw_CBV ||
CurToken.TokKind == TokenKind::kw_SRV ||
CurToken.TokKind == TokenKind::kw_UAV ||
@@ -93,7 +85,7 @@ bool RootSignatureParser::parseDescriptorTableClause() {
if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
CurToken.TokKind))
- return true;
+ return std::nullopt;
DescriptorTableClause Clause;
TokenKind ExpectedReg;
@@ -120,13 +112,13 @@ bool RootSignatureParser::parseDescriptorTableClause() {
auto Params = parseDescriptorTableClauseParams(ExpectedReg);
if (!Params.has_value())
- return true;
+ 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 true;
+ return std::nullopt;
}
Clause.Reg = Params->Reg.value();
@@ -138,10 +130,9 @@ bool RootSignatureParser::parseDescriptorTableClause() {
if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/ParamKind))
- return true;
+ return std::nullopt;
- Elements.push_back(Clause);
- return false;
+ return Clause;
}
std::optional<RootSignatureParser::ParsedClauseParams>
|
@llvm/pr-subscribers-clang Author: Finn Plummer (inbelic) Changes
This is a clean-up task from #133800 Full diff: https://github.com/llvm/llvm-project/pull/136747.diff 2 Files Affected:
diff --git a/clang/include/clang/Parse/ParseHLSLRootSignature.h b/clang/include/clang/Parse/ParseHLSLRootSignature.h
index 3eb3f8ea8422d..d9f121030c1fc 100644
--- a/clang/include/clang/Parse/ParseHLSLRootSignature.h
+++ b/clang/include/clang/Parse/ParseHLSLRootSignature.h
@@ -71,8 +71,9 @@ class RootSignatureParser {
// expected, or, there is a lexing error
/// Root Element parse methods:
- bool parseDescriptorTable();
- bool parseDescriptorTableClause();
+ std::optional<llvm::hlsl::rootsig::DescriptorTable> parseDescriptorTable();
+ std::optional<llvm::hlsl::rootsig::DescriptorTableClause>
+ parseDescriptorTableClause();
/// Parameter arguments (eg. `bReg`, `space`, ...) can be specified in any
/// order and only exactly once. `ParsedClauseParams` denotes the current
diff --git a/clang/lib/Parse/ParseHLSLRootSignature.cpp b/clang/lib/Parse/ParseHLSLRootSignature.cpp
index 4f8bfccfa2243..1bf33b8e8329c 100644
--- a/clang/lib/Parse/ParseHLSLRootSignature.cpp
+++ b/clang/lib/Parse/ParseHLSLRootSignature.cpp
@@ -26,22 +26,14 @@ RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
bool RootSignatureParser::parse() {
// Iterate as many RootElements as possible
- while (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
- // Dispatch onto parser method.
- // We guard against the unreachable here as we just ensured that CurToken
- // will be one of the kinds in the while condition
- switch (CurToken.TokKind) {
- case TokenKind::kw_DescriptorTable:
- if (parseDescriptorTable())
+ do {
+ if (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
+ auto Table = parseDescriptorTable();
+ if (!Table.has_value())
return true;
- break;
- default:
- llvm_unreachable("Switch for consumed token was not provided");
+ Elements.push_back(*Table);
}
-
- if (!tryConsumeExpectedToken(TokenKind::pu_comma))
- break;
- }
+ } while (tryConsumeExpectedToken(TokenKind::pu_comma));
if (consumeExpectedToken(TokenKind::end_of_stream,
diag::err_hlsl_unexpected_end_of_params,
@@ -51,38 +43,38 @@ bool RootSignatureParser::parse() {
return false;
}
-bool RootSignatureParser::parseDescriptorTable() {
+std::optional<DescriptorTable> RootSignatureParser::parseDescriptorTable() {
assert(CurToken.TokKind == TokenKind::kw_DescriptorTable &&
"Expects to only be invoked starting at given keyword");
- DescriptorTable Table;
-
if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
CurToken.TokKind))
- return true;
-
- // Iterate as many Clauses as possible
- while (tryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV,
- TokenKind::kw_UAV, TokenKind::kw_Sampler})) {
- if (parseDescriptorTableClause())
- return true;
+ return std::nullopt;
- Table.NumClauses++;
+ DescriptorTable Table;
- if (!tryConsumeExpectedToken(TokenKind::pu_comma))
- break;
- }
+ // Iterate as many Clauses as possible
+ do {
+ if (tryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV,
+ TokenKind::kw_UAV, TokenKind::kw_Sampler})) {
+ auto Clause = parseDescriptorTableClause();
+ if (!Clause.has_value())
+ return std::nullopt;
+ Elements.push_back(*Clause);
+ Table.NumClauses++;
+ }
+ } while (tryConsumeExpectedToken(TokenKind::pu_comma));
if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/TokenKind::kw_DescriptorTable))
- return true;
+ return std::nullopt;
- Elements.push_back(Table);
- return false;
+ return Table;
}
-bool RootSignatureParser::parseDescriptorTableClause() {
+std::optional<DescriptorTableClause>
+RootSignatureParser::parseDescriptorTableClause() {
assert((CurToken.TokKind == TokenKind::kw_CBV ||
CurToken.TokKind == TokenKind::kw_SRV ||
CurToken.TokKind == TokenKind::kw_UAV ||
@@ -93,7 +85,7 @@ bool RootSignatureParser::parseDescriptorTableClause() {
if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
CurToken.TokKind))
- return true;
+ return std::nullopt;
DescriptorTableClause Clause;
TokenKind ExpectedReg;
@@ -120,13 +112,13 @@ bool RootSignatureParser::parseDescriptorTableClause() {
auto Params = parseDescriptorTableClauseParams(ExpectedReg);
if (!Params.has_value())
- return true;
+ 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 true;
+ return std::nullopt;
}
Clause.Reg = Params->Reg.value();
@@ -138,10 +130,9 @@ bool RootSignatureParser::parseDescriptorTableClause() {
if (consumeExpectedToken(TokenKind::pu_r_paren,
diag::err_hlsl_unexpected_end_of_params,
/*param of=*/ParamKind))
- return true;
+ return std::nullopt;
- Elements.push_back(Clause);
- return false;
+ return Clause;
}
std::optional<RootSignatureParser::ParsedClauseParams>
|
Is this ready to retarget to main? |
if (consumeExpectedToken(TokenKind::end_of_stream, | ||
diag::err_hlsl_unexpected_end_of_params, | ||
/*param of=*/TokenKind::kw_RootSignature)) |
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.
if (consumeExpectedToken(TokenKind::end_of_stream, | |
diag::err_hlsl_unexpected_end_of_params, | |
/*param of=*/TokenKind::kw_RootSignature)) | |
return consumeExpectedToken(TokenKind::end_of_stream, | |
diag::err_hlsl_unexpected_end_of_params, | |
/*param of=*/TokenKind::kw_RootSignature); |
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.
Whoops, good catch. This doesn't relate to this pr but I have it noted to update when we touch the code in the future. Tracked here: #126576
- It was determined to define the parsing methods much more inline with a recursive descent parser to follow the EBNF notation better - As part of this change, we decided to go with a calling convention to the parse.* methods of returning an optional rather than a bool and a reference to the parsed struct
cb85ab7
to
50730f2
Compare
do { | ||
if (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) { | ||
auto Table = parseDescriptorTable(); | ||
if (!Table.has_value()) |
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.
nit: std::optional
should already implement operator bool()
if (!Table.has_value()) | |
if (!Table) |
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.
Looks like there are a bunch of cases like this, I'll just leave the one comment but please update everywhere
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 will merge as-is for now since this explicit version is already established in the file.
I can create a clean-up pr later to adjust this throughout the whole file.
This is a clean-up task from #133800