[flang-commits] [flang] 52ca6ad - [Flang][PFT] Skip continue insertion for OpenMP Loops

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Mon Jun 5 09:32:46 PDT 2023


Author: Kiran Chandramohan
Date: 2023-06-05T16:31:09Z
New Revision: 52ca6ad755b0cc2aa603cfb3124bf58c04a47005

URL: https://github.com/llvm/llvm-project/commit/52ca6ad755b0cc2aa603cfb3124bf58c04a47005
DIFF: https://github.com/llvm/llvm-project/commit/52ca6ad755b0cc2aa603cfb3124bf58c04a47005.diff

LOG: [Flang][PFT] Skip continue insertion for OpenMP Loops

Unstructured regions presents some issues for OpenMP code generation.
While there are no branches out of the OpenMP region, there can be
branches inside. This required the availability of an artificial
target at the end of an OpenMP region. This was implemented by
insertion an artifical `continue` and marking it as a target for
a branch.
(https://github.com/flang-compiler/f18-llvm-project/pull/1178)

The artificial target is not required for OpenMP loops. Since the
DO loop end can itself be a target of a branch. Moreover, insertion
of the continue between the end of the loop and the end of the
OpenMP loop construct presents problems since the OpenMP MLIR
loop construct models both the loop and the construct. This can
cause the terminator of the OpenMP loop construct to be missed.
This patch solves the issue by skipping the insertion of the
continue.

Note: This issue is only hit if the `end openmp loop` directive
is missed.

This patch fixes the issues in:
-> https://github.com/llvm/llvm-project/issues/58378
-> https://github.com/flang-compiler/f18-llvm-project/issues/1426

Fixes #58378

Reviewed By: vdonaldson

Differential Revision: https://reviews.llvm.org/D151700

Added: 
    flang/test/Lower/OpenMP/pre-fir-tree-loop.f90

Modified: 
    flang/lib/Lower/PFTBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/PFTBuilder.cpp b/flang/lib/Lower/PFTBuilder.cpp
index 560a9972148ea..1530e9ed1d51b 100644
--- a/flang/lib/Lower/PFTBuilder.cpp
+++ b/flang/lib/Lower/PFTBuilder.cpp
@@ -324,11 +324,21 @@ class PFTBuilder {
   }
 
   void exitConstructOrDirective() {
+    auto isOpenMPLoopConstruct = [](Fortran::lower::pft::Evaluation *eval) {
+      if (const auto *ompConstruct = eval->getIf<parser::OpenMPConstruct>())
+        if (std::holds_alternative<parser::OpenMPLoopConstruct>(
+                ompConstruct->u))
+          return true;
+      return false;
+    };
+
     rewriteIfGotos();
     auto *eval = constructAndDirectiveStack.back();
-    if (eval->isExecutableDirective()) {
+    if (eval->isExecutableDirective() && !isOpenMPLoopConstruct(eval)) {
       // A construct at the end of an (unstructured) OpenACC or OpenMP
       // construct region must have an exit target inside the region.
+      // This is not applicable to the OpenMP loop construct since the
+      // end of the loop is an available target inside the region.
       Fortran::lower::pft::EvaluationList &evaluationList =
           *eval->evaluationList;
       if (!evaluationList.empty() && evaluationList.back().isConstruct()) {

diff  --git a/flang/test/Lower/OpenMP/pre-fir-tree-loop.f90 b/flang/test/Lower/OpenMP/pre-fir-tree-loop.f90
new file mode 100644
index 0000000000000..eca8fb3049863
--- /dev/null
+++ b/flang/test/Lower/OpenMP/pre-fir-tree-loop.f90
@@ -0,0 +1,70 @@
+! RUN: bbc -fopenmp -pft-test -o %t %s | FileCheck %s
+! RUN: %flang_fc1 -fopenmp -fdebug-dump-pft -o %t %s | FileCheck %s
+
+! Loop constructs always have an `end do` which can be the target of
+! a branch. So OpenMP loop constructs do not need an artificial
+! continue inserted for a target.
+
+!CHECK-LABEL: sb0
+!CHECK-NOT: continue
+subroutine sb0(cond)
+  implicit none
+  logical :: cond
+  integer :: i
+  !$omp parallel do
+  do i = 1, 20
+    if( cond) then
+      cycle
+    end if
+  end do
+  return
+end subroutine
+
+!CHECK-LABEL: sb1
+!CHECK-NOT: continue
+subroutine sb1(cond)
+  implicit none
+  logical :: cond
+  integer :: i
+  !$omp parallel do
+  do i = 1, 20
+    if( cond) then
+      cycle
+    end if
+  end do
+  !$omp end parallel do
+  return
+end subroutine
+
+!CHECK-LABEL: sb2
+!CHECK-NOT: continue
+subroutine sb2
+  integer :: i, n
+  integer :: tmp
+
+  !$omp parallel do
+  do ifld=1,n
+     do isum=1,n
+       if (tmp > n) then
+         exit
+       endif
+     enddo
+     tmp = n
+  enddo
+end subroutine
+
+!CHECK-LABEL: sb3
+!CHECK-NOT: continue
+subroutine sb3
+  integer :: i, n
+  integer :: tmp
+
+  !$omp parallel do
+  do ifld=1,n
+     do isum=1,n
+       if (tmp > n) then
+         exit
+       endif
+     enddo
+  enddo
+end subroutine


        


More information about the flang-commits mailing list