This is an archive of the discontinued LLVM Phabricator instance.

[Flang][PFT] Skip continue insertion for OpenMP Loops
ClosedPublic

Authored by kiranchandramohan on May 30 2023, 3:17 AM.

Details

Summary

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

Diff Detail

Event Timeline

Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
kiranchandramohan requested review of this revision.May 30 2023, 3:17 AM
flang/lib/Lower/PFTBuilder.cpp
344

Another option could be to extend the check here for an End Do Construct.

vdonaldson added inline comments.
flang/lib/Lower/PFTBuilder.cpp
344

Checking for an EndDoStmt would have the advantage of also being applicable for OpenACC code.

Thanks @vdonaldson for having a look.

flang/lib/Lower/PFTBuilder.cpp
344

Looking at this again, not sure whether checking for EndDoStmt or EndDoConstruct will work since there is insufficient context to distinguish between,

!$omp parallel
do i=1,10
if (cond) exit
end do
!$omp end parallel
!$omp do
do i=1,10
if (cond) cycle
end do

For the !$omp parallel case, the continue is required, whereas for the !$omp do case it is not required.

vdonaldson accepted this revision.May 31 2023, 9:47 AM

@kiranchandramohan Thanks for investigating fix options.

As a side note, there is no End DoConstruct node in the PFT. A PFT dump tag without spaces, such as EndSubroutineStmt is a language syntax element with a corresponding PFT node. For a DO loop the reference is to f18 syntax rule R1119 in clause 11.1.7.1. Any dump line with "End ", with a space, is a hint/reminder that it is just a bracketing convenience for dump readers.

What you have looks ok to me for addressing the problem if you want to proceed with that.

This revision is now accepted and ready to land.May 31 2023, 9:47 AM

@kiranchandramohan Thanks for investigating fix options.

As a side note, there is no End DoConstruct node in the PFT. A PFT dump tag without spaces, such as EndSubroutineStmt is a language syntax element with a corresponding PFT node. For a DO loop the reference is to f18 syntax rule R1119 in clause 11.1.7.1. Any dump line with "End ", with a space, is a hint/reminder that it is just a bracketing convenience for dump readers.

What you have looks ok to me for addressing the problem if you want to proceed with that.

Thanks @vdonaldson for the info.
I will proceed with this fix for now.