[flang-commits] [flang] [FLANG][OPENMP] Fix handling of continuation lines in mixed OpenMP an… (PR #120714)

via flang-commits flang-commits at lists.llvm.org
Sun Jan 5 22:35:01 PST 2025


https://github.com/Karthikdhondi updated https://github.com/llvm/llvm-project/pull/120714

>From 0a6e55d54383e3f5dd73a44562053382a5cc3b36 Mon Sep 17 00:00:00 2001
From: karthikdhondi <karthik.dhondi at gmail.com>
Date: Fri, 20 Dec 2024 16:11:39 +0530
Subject: [PATCH 1/5] [FLANG][OPENMP] Fix handling of continuation lines in
 mixed OpenMP and Fortran free-form

---
 flang/lib/Parser/prescan.cpp          | 50 +++++++++++++++-------
 flang/test/Parser/cd_continuation.f90 | 61 +++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 15 deletions(-)
 create mode 100644 flang/test/Parser/cd_continuation.f90

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
+

>From c870322206d3766b40946eff6be8b2d57f335da5 Mon Sep 17 00:00:00 2001
From: karthikdhondi <karthik.dhondi at gmail.com>
Date: Tue, 24 Dec 2024 18:47:59 +0530
Subject: [PATCH 2/5] Reorganized code to eliminate repeated ampersand logic
 and removed whitespace skipping.

---
 ....f90 => compiler-directive_continuation.f90} | 17 -----------------
 1 file changed, 17 deletions(-)
 rename flang/test/Parser/{cd_continuation.f90 => compiler-directive_continuation.f90} (78%)

diff --git a/flang/test/Parser/cd_continuation.f90 b/flang/test/Parser/compiler-directive_continuation.f90
similarity index 78%
rename from flang/test/Parser/cd_continuation.f90
rename to flang/test/Parser/compiler-directive_continuation.f90
index c16deae19447f5..e2ad14c7e57f8f 100644
--- a/flang/test/Parser/cd_continuation.f90
+++ b/flang/test/Parser/compiler-directive_continuation.f90
@@ -17,23 +17,6 @@ subroutine mixed_form1()
 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

>From 5c6d4a5f5d0fc18f1f8cc27e67e367adadc3e7c5 Mon Sep 17 00:00:00 2001
From: karthikdhondi <karthik.dhondi at gmail.com>
Date: Fri, 20 Dec 2024 16:11:39 +0530
Subject: [PATCH 3/5] [FLANG][OPENMP] Fix handling of continuation lines in
 mixed OpenMP and Fortran free-form

---
 flang/test/Parser/cd_continuation.f90 | 61 +++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 flang/test/Parser/cd_continuation.f90

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
+

>From 087f7e153699a67d498a32b335e185bdde2e1691 Mon Sep 17 00:00:00 2001
From: karthikdhondi <karthik.dhondi at gmail.com>
Date: Thu, 26 Dec 2024 17:46:38 +0530
Subject: [PATCH 4/5] Reorganized code to eliminate repeated ampersand logic
 and removed whitespace skipping.

---
 flang/lib/Parser/prescan.cpp                  | 42 ++++---------
 flang/test/Parser/cd_continuation.f90         | 61 -------------------
 .../compiler-directive_continuation.f90       |  4 +-
 3 files changed, 15 insertions(+), 92 deletions(-)
 delete mode 100644 flang/test/Parser/cd_continuation.f90

diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index d5bde4b55c7b2a..fa51f2ae8218a4 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -1290,48 +1290,32 @@ const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
   }
   p = SkipWhiteSpace(p);
   if (*p == '!') {
+    ++p;
     if (InCompilerDirective()) {
-      if (*p++ != '!') {
-        return nullptr;
-      }
       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;
+      ++p;
+    } else {
+      return nullptr;
+    }
+    p = SkipWhiteSpace(p);
+    if (*p == '&') {
+      if (!ampersand) {
+        insertASpace_ = true;
       }
+      return p + 1;
+    } else if (ampersand) {
+      return p;
     } else {
       return nullptr;
     }
   } else {
     if (*p == '&') {
-      p = SkipWhiteSpace(p + 1);
-      return p;
+      return p + 1;
     } 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
deleted file mode 100644
index c16deae19447f5..00000000000000
--- a/flang/test/Parser/cd_continuation.f90
+++ /dev/null
@@ -1,61 +0,0 @@
-! 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
-
diff --git a/flang/test/Parser/compiler-directive_continuation.f90 b/flang/test/Parser/compiler-directive_continuation.f90
index e2ad14c7e57f8f..6efee064569074 100644
--- a/flang/test/Parser/compiler-directive_continuation.f90
+++ b/flang/test/Parser/compiler-directive_continuation.f90
@@ -19,8 +19,8 @@ subroutine mixed_form1()
 
 ! 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
+! CHECK-OMP: i = 1 +10 +100 + 1000 + 10000
+! CHECK: i = 1 +10 +100 + 1000 + 10000
 subroutine mixed_form3()
    i = 1 &
    +10 &

>From 3e691f39e76f26091d8872629d3a96f935d1ab09 Mon Sep 17 00:00:00 2001
From: karthikdhondi <karthik.dhondi at gmail.com>
Date: Mon, 6 Jan 2025 12:02:06 +0530
Subject: [PATCH 5/5] [FLANG][OPENMP] Resolved nitpicks and added missing
 ampersand check.

---
 flang/lib/Parser/prescan.cpp                         |  2 +-
 .../compiler-directive-continuation.f90}             | 12 ++++++------
 2 files changed, 7 insertions(+), 7 deletions(-)
 rename flang/test/Parser/{compiler-directive_continuation.f90 => OpenMP/compiler-directive-continuation.f90} (82%)

diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index fa51f2ae8218a4..b7462ebfb09006 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -1297,7 +1297,7 @@ const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
           return nullptr;
         }
       }
-    } else if (features_.IsEnabled(LanguageFeature::OpenMP)) {
+    } else if (features_.IsEnabled(LanguageFeature::OpenMP) && *p == '$') {
       ++p;
     } else {
       return nullptr;
diff --git a/flang/test/Parser/compiler-directive_continuation.f90 b/flang/test/Parser/OpenMP/compiler-directive-continuation.f90
similarity index 82%
rename from flang/test/Parser/compiler-directive_continuation.f90
rename to flang/test/Parser/OpenMP/compiler-directive-continuation.f90
index 6efee064569074..87e4a72c54294a 100644
--- a/flang/test/Parser/compiler-directive_continuation.f90
+++ b/flang/test/Parser/OpenMP/compiler-directive-continuation.f90
@@ -1,5 +1,5 @@
-! RUN: flang-new -fopenmp -E %s 2>&1 | FileCheck %s --check-prefix=CHECK-OMP
-! RUN: flang-new -E %s 2>&1 | FileCheck %s 
+! RUN: %flang_fc1 -fopenmp -E %s 2>&1 | FileCheck %s --check-prefix=CHECK-OMP
+! RUN: %flang_fc1 -E %s 2>&1 | FileCheck %s 
 
 
 ! Test in mixed way, i.e., combination of Fortran free source form 
@@ -18,10 +18,10 @@ subroutine mixed_form1()
 
 
 ! Testing continuation lines in only Fortran Free form Source
-! CHECK-LABEL: subroutine mixed_form3()
+! CHECK-LABEL: subroutine mixed_form2()
 ! CHECK-OMP: i = 1 +10 +100 + 1000 + 10000
 ! CHECK: i = 1 +10 +100 + 1000 + 10000
-subroutine mixed_form3()
+subroutine mixed_form2()
    i = 1 &
    +10 &
    &+100
@@ -31,10 +31,10 @@ subroutine mixed_form3()
 
 
 ! Testing continuation line in only free source form conditional compilation sentinel.
-! CHECK-LABEL: subroutine mixed_form4()
+! CHECK-LABEL: subroutine mixed_form3()
 ! CHECK-OMP: i=0
 ! CHECK-OMP: i = 1 +10 +100+1000
-subroutine mixed_form4()
+subroutine mixed_form3()
    !$ i=0
    !$ i = 1 &
    !$ & +10 &



More information about the flang-commits mailing list