Skip to content

[flang][OpenMP] Fix regression in !$ continuation #134756

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

Merged
merged 1 commit into from
Apr 9, 2025
Merged

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Apr 7, 2025

A recent patch that obviated the need to use -fopenmp when using the compiler to preprocess in -E mode broke a case of Fortran line continuation when using OpenMP conditional compilation lines (!$) when not in -E mode. Fix.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Apr 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 7, 2025

@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

A recent patch that obviated the need to use -fopenmp when using the compiler to preprocess in -E mode broke a case of Fortran line continuation when using OpenMP conditional compilation lines (!$) when not in -E mode. Fix.


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

1 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+23-12)
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 31fdadeddef53..a55a610a1f4f2 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -1351,26 +1351,37 @@ const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
   }
   p = SkipWhiteSpaceIncludingEmptyMacros(p);
   if (InCompilerDirective()) {
-    if (preprocessingOnly_ && directiveSentinel_[0] == '$' &&
-        directiveSentinel_[1] == '\0') {
-      // in -E mode, don't treat !$ as a continuation
+    if (directiveSentinel_[0] == '$' && directiveSentinel_[1] == '\0') {
+      if (preprocessingOnly_) {
+        // in -E mode, don't treat !$ as a continuation
+        return nullptr;
+      } else if (p[0] == '!' && p[1] == '$') {
+        // accept but do not require a matching sentinel
+        if (IsLetter(p[2])) {
+          return nullptr; // not !$
+        }
+        p += 2;
+      }
     } else if (*p++ == '!') {
       for (const char *s{directiveSentinel_}; *s != '\0'; ++p, ++s) {
         if (*s != ToLowerCaseLetter(*p)) {
           return nullptr; // not the same directive class
         }
       }
-      p = SkipWhiteSpace(p);
-      if (*p == '&') {
-        if (!ampersand) {
-          insertASpace_ = true;
-        }
-        return p + 1;
-      } else if (ampersand) {
-        return p;
+    } else {
+      return nullptr;
+    }
+    p = SkipWhiteSpace(p);
+    if (*p == '&') {
+      if (!ampersand) {
+        insertASpace_ = true;
       }
+      return p + 1;
+    } else if (ampersand) {
+      return p;
+    } else {
+      return nullptr;
     }
-    return nullptr;
   }
   if (p[0] == '!' && p[1] == '$' && !preprocessingOnly_ &&
       features_.IsEnabled(LanguageFeature::OpenMP)) {

@eugeneepshteyn
Copy link
Contributor

Maybe some tests should be added for the failure fixed by this PR? (I don't remember seeing regression in check-flang, so perhaps there are no tests there that cover this condition...)

A recent patch that obviated the need to use -fopenmp when using
the compiler to preprocess in -E mode broke a case of Fortran line
continuation when using OpenMP conditional compilation lines (!$)
when *not* in -E mode.  Fix.
@klausler
Copy link
Contributor Author

klausler commented Apr 8, 2025

Added tests, and verified changes against test suite failures.

@klausler klausler merged commit 0ae9bb9 into llvm:main Apr 9, 2025
12 checks passed
@klausler klausler deleted the bug494 branch April 9, 2025 19:30
AllinLeeYL pushed a commit to AllinLeeYL/llvm-project that referenced this pull request Apr 10, 2025
A recent patch that obviated the need to use -fopenmp when using the
compiler to preprocess in -E mode broke a case of Fortran line
continuation when using OpenMP conditional compilation lines (!$) when
*not* in -E mode. Fix.
@ro-i
Copy link
Contributor

ro-i commented Apr 14, 2025

This commit (0ae9bb9) breaks building a SPEC openmp benchmark (https://www.spec.org/auto/omp2012/Docs/357.bt331.html). A reduced test case:

program BT
!$     integer  omp_get_max_threads
!$     external omp_get_max_threads

c---------------------------------------------------------------------
       write(*, '(A1)') ' '
       end

This leads to the following compile error:

$ flang -fopenmp -c bt.condcomp.f
error: Could not parse bt.condcomp.f
./bt.condcomp.f:1:1: warning: Character in fixed-form label field must be a digit
  program BT
  ^
./bt.condcomp.f:5:6: error: expected end of statement
  c---------------------------------------------------------------------
       ^
./bt.condcomp.f:3:8: in the context: specification construct
  !$     external omp_get_max_threads
         ^
./bt.condcomp.f:2:8: in the context: specification part
  !$     integer  omp_get_max_threads
         ^

@klausler
Copy link
Contributor Author

klausler commented Apr 14, 2025 via email

var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
A recent patch that obviated the need to use -fopenmp when using the
compiler to preprocess in -E mode broke a case of Fortran line
continuation when using OpenMP conditional compilation lines (!$) when
*not* in -E mode. Fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:openmp flang:parser flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants