[Flang][OpenMP] Fix crash in MAX intrinsic reduction
authorKavitha Natarajan <kavitha.natarajan@amd.com>
Wed, 29 Mar 2023 15:35:42 +0000 (21:05 +0530)
committerKavitha Natarajan <kavitha.natarajan@amd.com>
Wed, 29 Mar 2023 18:11:21 +0000 (23:41 +0530)
This patch fixes a crash that appear in an OpenMP MAX intrinsic
reduction when the reduction is not expressed using MAX intrinsic
function. Updated flang/test/Lower/OpenMP/wsloop-reduction-max.f90
test case with additional way of expressing MAX reduction.

Reviewed By: kiranchandramohan

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

flang/lib/Lower/OpenMP.cpp
flang/test/Lower/OpenMP/wsloop-reduction-max.f90

index e3b4fb8..5feff7f 100644 (file)
@@ -2324,6 +2324,7 @@ void Fortran::lower::genOpenMPReduction(
                     // Match the pattern here.
                     mlir::Operation *reductionOp =
                         findReductionChain(loadVal, &reductionVal);
+                    if (reductionOp == nullptr) continue;
                     assert(mlir::isa<mlir::arith::SelectOp>(reductionOp) &&
                            "Selection Op not found in reduction intrinsic");
                     mlir::Operation *compareOp =
index 0db5b7f..44ea054 100644 (file)
@@ -63,4 +63,13 @@ subroutine reduction_max_real(y)
   !$omp end do
   !$omp end parallel
   print *, x
+
+  !$omp parallel
+  !$omp do reduction(max:x)
+  do i=1, 100
+    if (y(i) .gt. x) x = y(i)
+  end do
+  !$omp end do
+  !$omp end parallel
+  print *, x
 end subroutine