This is an archive of the discontinued LLVM Phabricator instance.

[Flang] Fix for a crash in OpenMP MAX intrinsic reduction
ClosedPublic

Authored by kavitha-natarajan on Mar 27 2023, 7:56 AM.

Details

Summary

Crash while using OpenMP MAX intrinsic reduction when the reduction is implemented in the following way:

!$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

Although it is benefitted to use MAX intrinsic in the loop, this is also supported as per the OpenMP specification. This patch fixes the crash in this case.

Diff Detail

Event Timeline

kavitha-natarajan requested review of this revision.Mar 27 2023, 7:56 AM

Thanks @kavitha-natarajan for the patch.

Although it is benefitted to use MAX intrinsic in the loop, this is also supported as per the OpenMP specification. This patch fixes the crash in this case.

Is your point here that an OpenMP reduction operation should be generated in this case? Or that it should not crash but ignore the reduction? A pointer to the standard would be helpful.

jdoerfert retitled this revision from Fix for a crash in OpenMP MAX intrinsic reduction to [Flang] Fix for a crash in OpenMP MAX intrinsic reduction.Mar 27 2023, 10:07 AM

Thanks @kavitha-natarajan for the patch.

Although it is benefitted to use MAX intrinsic in the loop, this is also supported as per the OpenMP specification. This patch fixes the crash in this case.

Is your point here that an OpenMP reduction operation should be generated in this case? Or that it should not crash but ignore the reduction? A pointer to the standard would be helpful.

I missed to note that reduction is not generated for this loop. I suppose the standard says reduction has to be generated for this case as well. Please refer to section A.36 - Example A.36.1f at page#266 in OpenMP 3.1 specification document: https://www.openmp.org/wp-content/uploads/OpenMP3.1.pdf

"
The following example demonstrates the reduction clause (Section2.9.3.6 on page 103); note that some reductions can be expressed in the loop in several ways, as shown for the max and min reductions below:

[. . .]
!$OMP PARALLEL DO PRIVATE(I) SHARED(X, Y, N) REDUCTION(+:A) &
!$OMP& REDUCTION(IEOR:B) REDUCTION(MIN:C) REDUCTION(MAX:D)

DO I=1,N
  A = A + X(I)
  B = IEOR(B, Y(I))
  C = MIN(C, Y(I))
  IF (D < X(I)) D = X(I)
END DO

"

Please correct me if my understanding is wrong. Otherwise, this patch needs more work to generate reduction when the intrinsic is not used directly as in the case of MIN in the above example.

Thanks @kavitha-natarajan for the patch.

Although it is benefitted to use MAX intrinsic in the loop, this is also supported as per the OpenMP specification. This patch fixes the crash in this case.

Is your point here that an OpenMP reduction operation should be generated in this case? Or that it should not crash but ignore the reduction? A pointer to the standard would be helpful.

I missed to note that reduction is not generated for this loop. I suppose the standard says reduction has to be generated for this case as well. Please refer to section A.36 - Example A.36.1f at page#266 in OpenMP 3.1 specification document: https://www.openmp.org/wp-content/uploads/OpenMP3.1.pdf

"
The following example demonstrates the reduction clause (Section2.9.3.6 on page 103); note that some reductions can be expressed in the loop in several ways, as shown for the max and min reductions below:

[. . .]
!$OMP PARALLEL DO PRIVATE(I) SHARED(X, Y, N) REDUCTION(+:A) &
!$OMP& REDUCTION(IEOR:B) REDUCTION(MIN:C) REDUCTION(MAX:D)

DO I=1,N
  A = A + X(I)
  B = IEOR(B, Y(I))
  C = MIN(C, Y(I))
  IF (D < X(I)) D = X(I)
END DO

"

Please correct me if my understanding is wrong. Otherwise, this patch needs more work to generate reduction when the intrinsic is not used directly as in the case of MIN in the above example.

From what is written in the examples document, it seems like this is supported. Classic Flang also supports this. I wasn't able to find the relevant section in the Standards Document though. I think we can merge the fix to avoid the crash. But could you create a github issue to fix this properly by generating a reduction operation?

flang/test/Lower/OpenMP/wsloop-reduction-max.f90
69

Please add a !CHECK-NOT omp.reduction here.

This revision is now accepted and ready to land.Mar 28 2023, 11:18 AM
This revision was landed with ongoing or failed builds.Mar 29 2023, 11:11 AM
This revision was automatically updated to reflect the committed changes.