Skip to content

Conversation

@kparzysz
Copy link
Contributor

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.

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.
@kparzysz kparzysz requested a review from luporl July 25, 2025 13:24
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Jul 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2025

@llvm/pr-subscribers-flang-parser

Author: Krzysztof Parzyszek (kparzysz)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/150617.diff

1 Files Affected:

  • (modified) flang/lib/Parser/openmp-parsers.cpp (+12-5)
diff --git a/flang/lib/Parser/openmp-parsers.cpp b/flang/lib/Parser/openmp-parsers.cpp
index 25a692deba3cc..1c626148a03ae 100644
--- a/flang/lib/Parser/openmp-parsers.cpp
+++ b/flang/lib/Parser/openmp-parsers.cpp
@@ -1222,11 +1222,14 @@ struct StrictlyStructuredBlockParser {
   using resultType = Block;
 
   std::optional<resultType> Parse(ParseState &state) const {
-    if (auto epc{Parser<ExecutionPartConstruct>{}.Parse(state)}) {
-      if (IsFortranBlockConstruct(*epc)) {
-        Block block;
-        block.emplace_back(std::move(*epc));
-        return std::move(block);
+    // Detect BLOCK construct without parsing the entire thing.
+    if (lookAhead(skipStuffBeforeStatement >> "BLOCK"_tok).Parse(state)) {
+      if (auto epc{Parser<ExecutionPartConstruct>{}.Parse(state)}) {
+        if (IsFortranBlockConstruct(*epc)) {
+          Block block;
+          block.emplace_back(std::move(*epc));
+          return std::move(block);
+        }
       }
     }
     return std::nullopt;
@@ -1237,6 +1240,10 @@ struct LooselyStructuredBlockParser {
   using resultType = Block;
 
   std::optional<resultType> Parse(ParseState &state) const {
+    // Detect BLOCK construct without parsing the entire thing.
+    if (lookAhead(skipStuffBeforeStatement >> "BLOCK"_tok).Parse(state)) {
+      return std::nullopt;
+    }
     Block body;
     if (auto epc{attempt(Parser<ExecutionPartConstruct>{}).Parse(state)}) {
       if (!IsFortranBlockConstruct(*epc)) {

Copy link
Contributor

@luporl luporl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the quick fix.

With this PR the compilation of 0981_0034.f90 finishes almost instantly, as before.

@kparzysz kparzysz merged commit 6b92a3b into llvm:main Jul 25, 2025
12 checks passed
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:parser flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants