[flang-commits] [flang] 495b8e1 - [Flang][OpenMP] Provide an error when the minus operator is used in a reduction

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Thu Aug 18 07:43:24 PDT 2022


Author: Kiran Chandramohan
Date: 2022-08-18T14:35:12Z
New Revision: 495b8e104d71e984128101b8286531ba6592c35b

URL: https://github.com/llvm/llvm-project/commit/495b8e104d71e984128101b8286531ba6592c35b
DIFF: https://github.com/llvm/llvm-project/commit/495b8e104d71e984128101b8286531ba6592c35b.diff

LOG: [Flang][OpenMP] Provide an error when the minus operator is used in a reduction

OpenMP 5.2 standard has deprecated the usage of the minus operation in
reductions. The minus operation also is an unpleasant feature with
varied interpretations.

The patch also changes the usage of the minus operator in some existing testcases.

Discussed in https://discourse.llvm.org/t/openmp-runtime-problem-with-subtraction-reduction/64404

Reviewed By: peixin

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

Added: 
    flang/test/Semantics/OpenMP/omp-reduction-subtract.f90

Modified: 
    flang/lib/Semantics/check-omp-structure.cpp
    flang/test/Semantics/OpenMP/omp-firstprivate01.f90
    flang/test/Semantics/OpenMP/omp-reduction02.f90
    flang/test/Semantics/OpenMP/omp-reduction04.f90
    flang/test/Semantics/OpenMP/omp-reduction07.f90
    flang/test/Semantics/OpenMP/omp-reduction09.f90

Removed: 
    flang/test/Lower/OpenMP/Todo/reduction-subtract.f90


################################################################################
diff  --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 64255d2afa004..5b35150e4cd8d 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -1924,13 +1924,17 @@ bool OmpStructureChecker::CheckIntrinsicOperator(
 
   switch (op) {
   case parser::DefinedOperator::IntrinsicOperator::Add:
-  case parser::DefinedOperator::IntrinsicOperator::Subtract:
   case parser::DefinedOperator::IntrinsicOperator::Multiply:
   case parser::DefinedOperator::IntrinsicOperator::AND:
   case parser::DefinedOperator::IntrinsicOperator::OR:
   case parser::DefinedOperator::IntrinsicOperator::EQV:
   case parser::DefinedOperator::IntrinsicOperator::NEQV:
     return true;
+  case parser::DefinedOperator::IntrinsicOperator::Subtract:
+    context_.Say(GetContext().clauseSource,
+        "The minus reduction operator is deprecated since OpenMP 5.2 and is not supported in the REDUCTION clause."_err_en_US,
+        ContextDirectiveAsFortran());
+    break;
   default:
     context_.Say(GetContext().clauseSource,
         "Invalid reduction operator in REDUCTION clause."_err_en_US,

diff  --git a/flang/test/Lower/OpenMP/Todo/reduction-subtract.f90 b/flang/test/Lower/OpenMP/Todo/reduction-subtract.f90
deleted file mode 100644
index bd01e4f63b983..0000000000000
--- a/flang/test/Lower/OpenMP/Todo/reduction-subtract.f90
+++ /dev/null
@@ -1,15 +0,0 @@
-! RUN: %not_todo_cmd bbc -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s
-! RUN: %not_todo_cmd %flang_fc1 -emit-fir -fopenmp -o - %s 2>&1 | FileCheck %s
-
-! CHECK: not yet implemented: Reduction of some intrinsic operators is not supported
-subroutine reduction_subtract
-  integer :: x
-  !$omp parallel
-  !$omp do reduction(-:x)
-  do i=1, 100
-    x = x - i
-  end do
-  !$omp end do
-  !$omp end parallel
-  print *, x
-end subroutine

diff  --git a/flang/test/Semantics/OpenMP/omp-firstprivate01.f90 b/flang/test/Semantics/OpenMP/omp-firstprivate01.f90
index 09f7b62b96a50..18e4cdd8a7d02 100644
--- a/flang/test/Semantics/OpenMP/omp-firstprivate01.f90
+++ b/flang/test/Semantics/OpenMP/omp-firstprivate01.f90
@@ -42,11 +42,11 @@ program omp_firstprivate
   !$omp end do
   !$omp end parallel
 
-  !$omp parallel reduction(-:a)
+  !$omp parallel reduction(*:a)
   !ERROR: FIRSTPRIVATE variable 'a' is PRIVATE in outer context
   !$omp do firstprivate(a,b)
   do i = 1, 10
-    c(i) =  c(i) - a(i) * b(i) * i
+    c(i) =  c(i) * a(i) * b(i) * i
   end do
   !$omp end do
   !$omp end parallel
@@ -59,10 +59,10 @@ program omp_firstprivate
   !$omp end sections
   !$omp end parallel
 
-  !$omp parallel reduction(-:a)
+  !$omp parallel reduction(*:a)
   !ERROR: FIRSTPRIVATE variable 'a' is PRIVATE in outer context
   !$omp task firstprivate(a,b)
-  c =  c - a * b
+  c =  c * a * b
   !$omp end task
   !$omp end parallel
 

diff  --git a/flang/test/Semantics/OpenMP/omp-reduction-subtract.f90 b/flang/test/Semantics/OpenMP/omp-reduction-subtract.f90
new file mode 100644
index 0000000000000..d4034743a14dc
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/omp-reduction-subtract.f90
@@ -0,0 +1,13 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+! OpenMP Version 5.2
+! Minus operation is deprecated in reduction
+
+subroutine reduction_subtract
+  integer :: x
+  !ERROR: The minus reduction operator is deprecated since OpenMP 5.2 and is not supported in the REDUCTION clause.
+  !$omp do reduction(-:x)
+  do i=1, 100
+    x = x - i
+  end do
+  !$omp end do
+end subroutine

diff  --git a/flang/test/Semantics/OpenMP/omp-reduction02.f90 b/flang/test/Semantics/OpenMP/omp-reduction02.f90
index b96dd063d1084..4fd9fbe2d8a53 100644
--- a/flang/test/Semantics/OpenMP/omp-reduction02.f90
+++ b/flang/test/Semantics/OpenMP/omp-reduction02.f90
@@ -8,30 +8,36 @@ program omp_reduction
   integer :: j = 10
 
   !ERROR: 'k' appears in more than one data-sharing clause on the same OpenMP directive
-  !$omp parallel do reduction(+:k), reduction(-:k)
+  !$omp parallel do reduction(+:k), reduction(*:k)
   do i = 1, 10
     k = k + 1
+    k = k * 3
   end do
   !$omp end parallel do
 
   !ERROR: 'k' appears in more than one data-sharing clause on the same OpenMP directive
-  !$omp parallel do reduction(+:k), reduction(-:j), reduction(+:k)
+  !$omp parallel do reduction(+:k), reduction(*:j), reduction(+:k)
   do i = 1, 10
     k = k + 1
+    j = j * 3
   end do
   !$omp end parallel do
 
   !ERROR: 'k' appears in more than one data-sharing clause on the same OpenMP directive
-  !$omp parallel do reduction(+:j), reduction(-:k), reduction(+:k)
+  !$omp parallel do reduction(+:j), reduction(*:k), reduction(+:k)
   do i = 1, 10
+    j = j + 1
     k = k + 1
+    k = k * 3
   end do
   !$omp end parallel do
 
   !ERROR: 'k' appears in more than one data-sharing clause on the same OpenMP directive
-  !$omp parallel do reduction(+:j), reduction(-:k), private(k)
+  !$omp parallel do reduction(+:j), reduction(*:k), private(k)
   do i = 1, 10
+    j = j + 1
     k = k + 1
+    k = k * 3
   end do
   !$omp end parallel do
 end program omp_reduction

diff  --git a/flang/test/Semantics/OpenMP/omp-reduction04.f90 b/flang/test/Semantics/OpenMP/omp-reduction04.f90
index eb9675252815b..4f089322c538e 100644
--- a/flang/test/Semantics/OpenMP/omp-reduction04.f90
+++ b/flang/test/Semantics/OpenMP/omp-reduction04.f90
@@ -14,7 +14,7 @@ program omp_Reduction
   !$omp end parallel do
 
   !ERROR: Variable 'c' on the REDUCTION clause is not definable
-  !$omp parallel do reduction(-:/c/)
+  !$omp parallel do reduction(*:/c/)
   do i = 1, 10
     l = k + 1
   end do

diff  --git a/flang/test/Semantics/OpenMP/omp-reduction07.f90 b/flang/test/Semantics/OpenMP/omp-reduction07.f90
index a9aaa0a925ca8..a3b58765d9a6d 100644
--- a/flang/test/Semantics/OpenMP/omp-reduction07.f90
+++ b/flang/test/Semantics/OpenMP/omp-reduction07.f90
@@ -3,7 +3,7 @@
 ! 2.15.3.6 Reduction Clause
 program omp_reduction
 
-  integer :: i,j,l
+  integer :: a,i,j,l
   integer :: k = 10
   !$omp parallel private(k)
   !ERROR: REDUCTION variable 'k' is PRIVATE in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
@@ -15,7 +15,7 @@ program omp_reduction
   !$omp end parallel
 
 
-  !$omp parallel private(j),reduction(-:k)
+  !$omp parallel private(j),reduction(+:k)
   !ERROR: REDUCTION variable 'k' is REDUCTION in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
   !$omp do reduction(+:k)
   do i = 1, 10
@@ -37,9 +37,10 @@ program omp_reduction
   !$omp parallel private(l,j),firstprivate(k)
   !ERROR: REDUCTION variable 'k' is FIRSTPRIVATE in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
   !ERROR: REDUCTION variable 'j' is PRIVATE in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
-  !$omp sections reduction(ior:k) reduction(-:j)
+  !$omp sections reduction(ior:k) reduction(*:j)
   do i = 1, 10
-    k = k + 1
+    k = ior(k, 1)
+    j = j * 3
   end do
   !$omp end sections
   !$omp end parallel
@@ -69,36 +70,36 @@ program omp_reduction
 
 !$omp parallel reduction(+:a)
 !ERROR: REDUCTION variable 'a' is REDUCTION in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
-!$omp sections reduction(-:a)
-a = 10
+!$omp sections reduction(*:a)
+a = a + 10
 !$omp end sections
 !$omp end parallel
 
-!$omp parallel reduction(-:a)
+!$omp parallel reduction(*:a)
 !$omp end parallel
 
 
 !$omp parallel reduction(+:a)
 !ERROR: REDUCTION clause is not allowed on the WORKSHARE directive
 !ERROR: REDUCTION variable 'a' is REDUCTION in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
-!$omp workshare reduction(-:a)
-a = 10
+!$omp workshare reduction(*:a)
+a = a + 10
 !$omp end workshare
 !$omp end parallel
 
-!$omp parallel reduction(-:a)
+!$omp parallel reduction(*:a)
 !$omp end parallel
 
 
 !$omp parallel reduction(+:a)
 !ERROR: REDUCTION clause is not allowed on the SINGLE directive
 !ERROR: REDUCTION variable 'a' is REDUCTION in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
-!$omp single reduction(-:a)
-a = 10
+!$omp single reduction(*:a)
+a = a + 10
 !$omp end single
 !$omp end parallel
 
-!$omp parallel reduction(-:a)
+!$omp parallel reduction(+:a)
 !$omp end parallel
 
 
@@ -106,7 +107,7 @@ program omp_reduction
 !ERROR: REDUCTION clause is not allowed on the SINGLE directive
 !ERROR: REDUCTION variable 'a' is REDUCTION in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
 !$omp single reduction(iand:a)
-a = 10
+a = a + 10
 !$omp end single
 !$omp end parallel
 
@@ -115,8 +116,8 @@ program omp_reduction
 
 !$omp parallel reduction(ieor:a)
 !ERROR: REDUCTION variable 'a' is REDUCTION in outer context must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
-!$omp sections reduction(-:a)
-a = 10
+!$omp sections reduction(+:a)
+a = ieor(a, 10)
 !$omp end sections
 !$omp end parallel
 

diff  --git a/flang/test/Semantics/OpenMP/omp-reduction09.f90 b/flang/test/Semantics/OpenMP/omp-reduction09.f90
index 7dc02459ee4a1..095b49ba0c400 100644
--- a/flang/test/Semantics/OpenMP/omp-reduction09.f90
+++ b/flang/test/Semantics/OpenMP/omp-reduction09.f90
@@ -66,7 +66,7 @@ program omp_reduction
   !$omp end do
   !$omp end parallel
 
-  !$omp do reduction(-:k) reduction(*:j) reduction(-:l)
+  !$omp do reduction(+:k) reduction(*:j) reduction(+:l)
   !DEF: /omp_reduction/OtherConstruct7/i (OmpPrivate, OmpPreDetermined) HostAssoc INTEGER(4)
   do i=1,10
     !DEF: /omp_reduction/OtherConstruct7/k (OmpReduction) HostAssoc INTEGER(4)


        


More information about the flang-commits mailing list