llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.97k stars 11.54k forks source link

[flang][OpenMP] Interaction of STOP with runtime I/O calls in parallel region causes runtime exception #73104

Open NimishMishra opened 9 months ago

NimishMishra commented 9 months ago

The following reproducer causes a RUNTIME_CHECK(length_ >= n) failed at /home/user1/.llvm/main/flang/runtime/buffer.h(165)

program sample
    implicit none
    integer i
    integer, parameter :: N = 8
    integer :: M = 99

    !$omp parallel do
        do i = 1, N
             print *, "some string"
             stop 1
        end do
    !$omp end parallel do
end program sample

Enclosing the runtime calls in a critical section causes the runtime failure to no longer be observed, hinting that maybe parallelization is the issue here.

        do i = 1, N
             !$omp critical
             print *, "some string"
             stop 1
             !$omp end critical
        end do
llvmbot commented 9 months ago

@llvm/issue-subscribers-openmp

Author: None (NimishMishra)

The following reproducer causes a `RUNTIME_CHECK(length_ >= n) failed at /home/user1/.llvm/main/flang/runtime/buffer.h(165)` ``` program sample implicit none integer i integer, parameter :: N = 8 integer :: M = 99 !$omp threadprivate (M) !$omp parallel do copyin(M) do i = 1, N print *, "sample string" stop 1 end do !$omp end parallel do end program sample ``` Note that should the copyin be removed, then the runtime failure is not observable. Moreover, enclosing the runtime calls in a critical section also causes the runtime failure to no longer be observed, hinting that maybe parallelization is the issue here. ``` do i = 1, N !$omp critical print *, "sample string" stop 1 !$omp end critical end do ```
NimishMishra commented 9 months ago

The IR for this test case:

  func.func @_QQmain() attributes {fir.bindc_name = "sample"} {
    %false = arith.constant false
    %c11 = arith.constant 11 : index
    %c10_i32 = arith.constant 10 : i32
    %c-1_i32 = arith.constant -1 : i32
    %c8_i32 = arith.constant 8 : i32
    %c1_i32 = arith.constant 1 : i32
    %0 = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFEi"}
    %1 = fir.declare %0 {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> !fir.ref<i32>
    %2 = fir.address_of(@_QFEm) : !fir.ref<i32>
    %3 = fir.declare %2 {uniq_name = "_QFEm"} : (!fir.ref<i32>) -> !fir.ref<i32>
    %4 = fir.address_of(@_QFECn) : !fir.ref<i32>
    %5 = fir.declare %4 {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = "_QFECn"} : (!fir.ref<i32>) -> !fir.ref<i32>
    omp.parallel {
      %6 = fir.alloca i32 {adapt.valuebyref, pinned}
      %7 = fir.declare %6 {uniq_name = "_QFEi"} : (!fir.ref<i32>) -> !fir.ref<i32>
      omp.wsloop for  (%arg0) : i32 = (%c1_i32) to (%c8_i32) inclusive step (%c1_i32) {
        fir.store %arg0 to %7 : !fir.ref<i32>
        cf.br ^bb1
      ^bb1:  // pred: ^bb0
        %8 = fir.address_of(@_QQclX3b042ba7859634e385c8cc1454630e07) : !fir.ref<!fir.char<1,34>>
        %9 = fir.convert %8 : (!fir.ref<!fir.char<1,34>>) -> !fir.ref<i8>
        %10 = fir.call @_FortranAioBeginExternalListOutput(%c-1_i32, %9, %c10_i32) fastmath<contract> : (i32, !fir.ref<i8>, i32) -> !fir.ref<i8>
        %11 = fir.address_of(@_QQclX736F6D6520737472696E67) : !fir.ref<!fir.char<1,11>>
        %12 = fir.declare %11 typeparams %c11 {fortran_attrs = #fir.var_attrs<parameter>, uniq_name = "_QQclX736F6D6520737472696E67"} : (!fir.ref<!fir.char<1,11>>, index) -> !fir.ref<!fir.char<1,11>>
        %13 = fir.convert %12 : (!fir.ref<!fir.char<1,11>>) -> !fir.ref<i8>
        %14 = fir.convert %c11 : (index) -> i64
        %15 = fir.call @_FortranAioOutputAscii(%10, %13, %14) fastmath<contract> : (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
        %16 = fir.call @_FortranAioEndIoStatement(%10) fastmath<contract> : (!fir.ref<i8>) -> i32
        %17 = fir.call @_FortranAStopStatement(%c1_i32, %false, %false) fastmath<contract> : (i32, i1, i1) -> none
        omp.yield
      }
      omp.terminator
    }
    cf.br ^bb1
  ^bb1:  // pred: ^bb0
    return
  }
llvmbot commented 9 months ago

@llvm/issue-subscribers-flang-runtime

Author: None (NimishMishra)

The following reproducer causes a `RUNTIME_CHECK(length_ >= n) failed at /home/user1/.llvm/main/flang/runtime/buffer.h(165)` ``` program sample implicit none integer i integer, parameter :: N = 8 integer :: M = 99 !$omp parallel do do i = 1, N print *, "some string" stop 1 end do !$omp end parallel do end program sample ``` Enclosing the runtime calls in a critical section causes the runtime failure to no longer be observed, hinting that maybe parallelization is the issue here. ``` do i = 1, N !$omp critical print *, "some string" stop 1 !$omp end critical end do ```