Skip to content

[FLANG][OPENMP] Fix handling of continuation lines in mixed OpenMP an… #120714

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

Conversation

Karthikdhondi
Copy link
Contributor

…d Fortran free-form
OpenMP feature was not enabled in the flang-new for the continuation line, when we used the continuation line marker in combination of free-form and OpenMP directive, it was throwing an error. PR is the fix for that issue.

Added a fix for the following issue #89559

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Dec 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2024

@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-flang-parser

Author: kd0608 (Karthikdhondi)

Changes

…d Fortran free-form
OpenMP feature was not enabled in the flang-new for the continuation line, when we used the continuation line marker in combination of free-form and OpenMP directive, it was throwing an error. PR is the fix for that issue.

Added a fix for the following issue #89559


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

2 Files Affected:

  • (modified) flang/lib/Parser/prescan.cpp (+35-15)
  • (added) flang/test/Parser/cd_continuation.f90 (+61)
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index 3cd32d7e6c92e8..d5bde4b55c7b2a 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -1289,29 +1289,49 @@ const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
     return nullptr;
   }
   p = SkipWhiteSpace(p);
-  if (InCompilerDirective()) {
-    if (*p++ != '!') {
-      return nullptr;
-    }
-    for (const char *s{directiveSentinel_}; *s != '\0'; ++p, ++s) {
-      if (*s != ToLowerCaseLetter(*p)) {
+  if (*p == '!') {
+    if (InCompilerDirective()) {
+      if (*p++ != '!') {
         return nullptr;
       }
-    }
-    p = SkipWhiteSpace(p);
-    if (*p == '&') {
-      if (!ampersand) {
-        insertASpace_ = true;
+      for (const char *s{directiveSentinel_}; *s != '\0'; ++p, ++s) {
+        if (*s != ToLowerCaseLetter(*p)) {
+          return nullptr;
+        }
+      }
+      p = SkipWhiteSpace(p);
+      if (*p == '&') {
+        if (!ampersand) {
+          insertASpace_ = true;
+        }
+        return p + 1;
+      } else if (ampersand) {
+        return p;
+      } else {
+        return nullptr;
+      }
+    } else if (features_.IsEnabled(LanguageFeature::OpenMP)) {
+      if (*p + 1 == '$')
+        return nullptr;
+      p += 2;
+      p = SkipWhiteSpace(p);
+      if (*p == '&') {
+        if (!ampersand) {
+          insertASpace_ = true;
+        }
+        return p + 1;
+      } else if (ampersand) {
+        return p;
+      } else {
+        return nullptr;
       }
-      return p + 1;
-    } else if (ampersand) {
-      return p;
     } else {
       return nullptr;
     }
   } else {
     if (*p == '&') {
-      return p + 1;
+      p = SkipWhiteSpace(p + 1);
+      return p;
     } else if (*p == '!' || *p == '\n' || *p == '#') {
       return nullptr;
     } else if (ampersand || IsImplicitContinuation()) {
diff --git a/flang/test/Parser/cd_continuation.f90 b/flang/test/Parser/cd_continuation.f90
new file mode 100644
index 00000000000000..c16deae19447f5
--- /dev/null
+++ b/flang/test/Parser/cd_continuation.f90
@@ -0,0 +1,61 @@
+! RUN: flang-new -fopenmp -E %s 2>&1 | FileCheck %s --check-prefix=CHECK-OMP
+! RUN: flang-new -E %s 2>&1 | FileCheck %s 
+
+
+! Test in mixed way, i.e., combination of Fortran free source form 
+! and free source form with conditional compilation sentinel.
+! CHECK-LABEL: subroutine mixed_form1()
+! CHECK-OMP: i = 1 +100+ 1000+ 10 + 1 +1000000000 + 1000000
+! CHECK: i = 1 + 10 + 10000 + 1000000
+subroutine mixed_form1()
+   i = 1 &
+  !$+100&
+  !$&+ 1000&
+   &+ 10 + 1&
+  !$& +100000&
+   &0000 + 1000000
+end subroutine	
+
+
+
+! CHECK-LABEL: subroutine mixed_form2()
+! CHECK-OMP: i = 0
+! CHECK-OMP: i = 1 +100+ 1000+ 10 + 1 +1000000000 + 1000000
+! CHECK: i = 1 + 10 + 10000 + 1000000
+subroutine mixed_form2()
+!$ i = 0
+   i = 1 &
+  !$+100&
+  !$&+ 1000&
+   &+ 10 + 1&
+  !$& +100000&
+   & 0000 + 1000000
+
+end subroutine
+
+
+! Testing continuation lines in only Fortran Free form Source
+! CHECK-LABEL: subroutine mixed_form3()
+! CHECK-OMP: i = 1 +10 +100+ 1000 + 10000
+! CHECK: i = 1 +10 +100+ 1000 + 10000
+subroutine mixed_form3()
+   i = 1 &
+   +10 &
+   &+100
+   & + 1000 &
+   + 10000
+end subroutine
+
+
+! Testing continuation line in only free source form conditional compilation sentinel.
+! CHECK-LABEL: subroutine mixed_form4()
+! CHECK-OMP: i=0
+! CHECK-OMP: i = 1 +10 +100+1000
+subroutine mixed_form4()
+   !$ i=0
+   !$ i = 1 &
+   !$ & +10 &
+   !$&+100&
+   !$ +1000 
+end subroutine
+

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.

Thank you for fixing this issue.

There are some adjustments that need to be made, but overall this seems to me to be the best way to fix it.

@@ -0,0 +1,61 @@
! RUN: flang-new -fopenmp -E %s 2>&1 | FileCheck %s --check-prefix=CHECK-OMP
Copy link
Contributor

Choose a reason for hiding this comment

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

In the file name, what does cd stands for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank for the review.
cd stands for Compiler directive. I will change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I missed this before, but as this tests OpenMP, it's better to move the test to flang/test/Parser/OpenMP.

And for consistency, replace _ with -: compiler-directive-continuation.f90

return nullptr;
}
} else if (features_.IsEnabled(LanguageFeature::OpenMP)) {
if (*p + 1 == '$')
Copy link
Contributor

Choose a reason for hiding this comment

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

if (p[1] == '$') {
  return nullptr;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review.

@Karthikdhondi Karthikdhondi force-pushed the openmp-freeform-mixed-continuation branch 2 times, most recently from 5af6d2f to 7cc7adf Compare December 26, 2024 12:18
Copy link

github-actions bot commented Dec 26, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@Karthikdhondi Karthikdhondi force-pushed the openmp-freeform-mixed-continuation branch 3 times, most recently from 15b3108 to 7200f64 Compare December 26, 2024 14:28
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.

Thank you for addressing all my comments.

I have just one more comment and a few nitpicks.

@Karthikdhondi Karthikdhondi force-pushed the openmp-freeform-mixed-continuation branch from 7200f64 to 3e691f3 Compare January 6, 2025 06:34
@Karthikdhondi Karthikdhondi force-pushed the openmp-freeform-mixed-continuation branch from 3e691f3 to 0a56d91 Compare January 6, 2025 09:15
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!

@Karthikdhondi Karthikdhondi merged commit 19c9348 into llvm:main Jan 7, 2025
8 checks passed
Copy link

github-actions bot commented Jan 7, 2025

@Karthikdhondi Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@Karthikdhondi Karthikdhondi deleted the openmp-freeform-mixed-continuation branch February 25, 2025 14:55
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