This is an archive of the discontinued LLVM Phabricator instance.

[flang][OpenMP][OpenACC] Fix exit of a region
ClosedPublic

Authored by peixin on May 26 2022, 7:22 AM.

Details

Diff Detail

Event Timeline

peixin created this revision.May 26 2022, 7:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 26 2022, 7:22 AM
peixin requested review of this revision.May 26 2022, 7:22 AM

@kiranchandramohan left one comment here (https://reviews.llvm.org/D88655#2470610) more than one year ago, but there is no further update. Take up this.

I checked OpenACC 3.0, and there is no explicit description for the stop statement in a region. Then I checked gfortran, and this is OK for gfortran.

$ cat test.f90 
program m
  integer :: i, N=10, a(10)
  !$acc serial
  do i = 1, N
    a(i) = 3.14
    if(i == N-1) THEN
      stop 999 ! no error
    end if
  end do
  !$acc end serial
end
$ gfortran test.f90 -fopenacc
! no error

LGTM, thank you.

LG. Is the generated code valid?

LG. Is the generated code valid?

For semantic analysis, this patch is OK. Branching out of a OpenMP region has wrong semantics such as the slave thread GOTO the non-parallel region. For STOP statement, it aborts the program, which is expected.

For lowering, it hasn't handle this case. I will fix it later.

program m
  use omp_lib
  !$omp parallel num_threads(4)
    stop omp_get_thread_num()
  !$omp end parallel
end
$ bbc -emit-fir -fopenmp test.f90 && cat test.mlir
func.func @_QQmain() {
  %c4_i32 = arith.constant 4 : i32
  omp.parallel   num_threads(%c4_i32 : i32) {
    %0 = fir.call @omp_get_thread_num() : () -> i32
    %false = arith.constant false
    %false_0 = arith.constant false
    %1 = fir.call @_FortranAStopStatement(%0, %false, %false_0) : (i32, i1, i1) -> none
    fir.unreachable
  }
  return
}

Change it by-hand:

func.func @_QQmain() {
  %c4_i32 = arith.constant 4 : i32
  omp.parallel   num_threads(%c4_i32 : i32) {
    %0 = fir.call @omp_get_thread_num() : () -> i32
    %false = arith.constant false
    %false_0 = arith.constant false
    %1 = fir.call @_FortranAStopStatement(%0, %false, %false_0) : (i32, i1, i1) -> none
    omp.terminator
  }
  return
}
tco test.mlir -o test.ll
$ clang++ -L/path-to-lib -lFortran_main -lFortranRuntime -lFortranDecimal -lomp test.ll 
$ ./a.out 
Fortran STOP: code 1

Fortran STOP: code 3

IEEE arithmetic exceptions signaled: INEXACT
IEEE arithmetic exceptions signaled: INEXACT
Fortran STOP: code 2

IEEE arithmetic exceptions signaled: INEXACT
Fortran STOP

IEEE arithmetic exceptions signaled:
$ gfortran test.f90 -fopenmp && ./a.out
STOP 0
STOP 3
STOP 2

I will fix the lowering later.

This revision is now accepted and ready to land.May 27 2022, 12:00 AM

I will fix the lowering later.

Can you file a ticket so that we do not forget?

I will fix the lowering later.

Can you file a ticket so that we do not forget?

Sure, will file the ticket before land this.

This revision was automatically updated to reflect the committed changes.