[Flang][OpenMP] Provide an error when the minus operator is used in a reduction
authorKiran Chandramohan <kiran.chandramohan@arm.com>
Thu, 18 Aug 2022 14:34:45 +0000 (14:34 +0000)
committerKiran Chandramohan <kiran.chandramohan@arm.com>
Thu, 18 Aug 2022 14:35:12 +0000 (14:35 +0000)
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

flang/lib/Semantics/check-omp-structure.cpp
flang/test/Lower/OpenMP/Todo/reduction-subtract.f90 [deleted file]
flang/test/Semantics/OpenMP/omp-firstprivate01.f90
flang/test/Semantics/OpenMP/omp-reduction-subtract.f90 [new file with mode: 0644]
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

index 64255d2..5b35150 100644 (file)
@@ -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 (file)
index bd01e4f..0000000
+++ /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
index 09f7b62..18e4cdd 100644 (file)
@@ -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 (file)
index 0000000..d403474
--- /dev/null
@@ -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
index b96dd06..4fd9fbe 100644 (file)
@@ -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
index eb96752..4f08932 100644 (file)
@@ -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
index a9aaa0a..a3b5876 100644 (file)
@@ -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 @@ a = 10
 !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 @@ a = 10
 
 !$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
 
index 7dc0245..095b49b 100644 (file)
@@ -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)