Skip to content

Commit 6b92a3b

Browse files
authored
[flang][OpenMP] Detect BLOCK construct through lookahead (#150617)
Avoid parsing the entire ExecutionPartConstruct in either the strictly- or the loosely-structured block parser only to discard it when it's not BLOCK (or is BLOCK) respectively. Doing so was not incorrct, but in pathological cases (like Fujitsu 0981_0034) the recursive parsing can take a very long time. Instead, detect the presence of BLOCK first (via a simple lookahead), and fail immediately if necessary.
1 parent b75530f commit 6b92a3b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,11 +1222,14 @@ struct StrictlyStructuredBlockParser {
12221222
using resultType = Block;
12231223

12241224
std::optional<resultType> Parse(ParseState &state) const {
1225-
if (auto epc{Parser<ExecutionPartConstruct>{}.Parse(state)}) {
1226-
if (IsFortranBlockConstruct(*epc)) {
1227-
Block block;
1228-
block.emplace_back(std::move(*epc));
1229-
return std::move(block);
1225+
// Detect BLOCK construct without parsing the entire thing.
1226+
if (lookAhead(skipStuffBeforeStatement >> "BLOCK"_tok).Parse(state)) {
1227+
if (auto epc{Parser<ExecutionPartConstruct>{}.Parse(state)}) {
1228+
if (IsFortranBlockConstruct(*epc)) {
1229+
Block block;
1230+
block.emplace_back(std::move(*epc));
1231+
return std::move(block);
1232+
}
12301233
}
12311234
}
12321235
return std::nullopt;
@@ -1237,6 +1240,10 @@ struct LooselyStructuredBlockParser {
12371240
using resultType = Block;
12381241

12391242
std::optional<resultType> Parse(ParseState &state) const {
1243+
// Detect BLOCK construct without parsing the entire thing.
1244+
if (lookAhead(skipStuffBeforeStatement >> "BLOCK"_tok).Parse(state)) {
1245+
return std::nullopt;
1246+
}
12401247
Block body;
12411248
if (auto epc{attempt(Parser<ExecutionPartConstruct>{}).Parse(state)}) {
12421249
if (!IsFortranBlockConstruct(*epc)) {

0 commit comments

Comments
 (0)