Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flang][OpenMP] Execution error of a WRITE statement with an unopened unit in a parallel region #68856

Closed
yus3710-fj opened this issue Oct 12, 2023 · 3 comments · Fixed by #74468

Comments

@yus3710-fj
Copy link
Contributor

This is an issue from Fujitsu testsuite.

The execution of a program compiled by flang-new terminates with a fatal error.
In the program, there is a WRITE statement in a parallel region and the unit specified in that is not opened explicitly.
The execution terminates normally when OpenMP is disabled.

The following are the test program, Flang-new and gfortran execution result.

! test.f90
!$omp parallel
write(1,*) 'OK'
!$omp end parallel
end
$ flang-new -v test.f90 -fopenmp
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git dae91f5dbc5bee579eac7f4cbb71e86f2934817f)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Selected GCC installation: /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fopenmp -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-f33630.o -x f95-cpp-input test.f90
 "/usr/bin/ld" -pie -EL --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /lib/../lib64/Scrt1.o /lib/../lib64/crti.o /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/crtbeginS.o -L/path/to/install/lib/clang/18/lib/aarch64-unknown-linux-gnu -L/path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0 -L/path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test-f33630.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lomp -L/path/to/install/lib -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/crtendS.o /lib/../lib64/crtn.o
$ ./a.out
$ OMP_NUM_THREADS=4 ./a.out

fatal Fortran runtime error(/path/to/test.f90:2): Attempted output to read-only file
Aborted (core dumped)
$ flang-new test.f90
$ ./a.out
$ cat fort.1 
 OK
$ gfortran -v test.f90 -fopenmp
Driving: gfortran -v test.f90 -fopenmp -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
 :
$ OMP_NUM_THREADS=4 ./a.out
$ cat fort.1
 OK
 OK
 OK
 OK
@github-actions github-actions bot added the flang Flang issues not falling into any other category label Oct 12, 2023
@yus3710-fj yus3710-fj added openmp flang:runtime and removed flang Flang issues not falling into any other category labels Oct 12, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 12, 2023

@llvm/issue-subscribers-openmp

Author: Yusuke MINATO (yus3710-fj)

This is an issue from Fujitsu testsuite.

The execution of a program compiled by flang-new terminates with a fatal error.
In the program, there is a WRITE statement in a parallel region and the unit specified in that is not opened explicitly.
The execution terminates normally when OpenMP is disabled.

The following are the test program, Flang-new and gfortran execution result.

! test.f90
!$omp parallel
write(1,*) 'OK'
!$omp end parallel
end
$ flang-new -v test.f90 -fopenmp
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git dae91f5dbc5bee579eac7f4cbb71e86f2934817f)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Selected GCC installation: /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Candidate multilib: .;@<!-- -->m64
Selected multilib: .;@<!-- -->m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fopenmp -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-f33630.o -x f95-cpp-input test.f90
 "/usr/bin/ld" -pie -EL --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /lib/../lib64/Scrt1.o /lib/../lib64/crti.o /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/crtbeginS.o -L/path/to/install/lib/clang/18/lib/aarch64-unknown-linux-gnu -L/path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0 -L/path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test-f33630.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lomp -L/path/to/install/lib -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/crtendS.o /lib/../lib64/crtn.o
$ ./a.out
$ OMP_NUM_THREADS=4 ./a.out

fatal Fortran runtime error(/path/to/test.f90:2): Attempted output to read-only file
Aborted (core dumped)
$ flang-new test.f90
$ ./a.out
$ cat fort.1 
 OK
$ gfortran -v test.f90 -fopenmp
Driving: gfortran -v test.f90 -fopenmp -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
 :
$ OMP_NUM_THREADS=4 ./a.out
$ cat fort.1
 OK
 OK
 OK
 OK

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 12, 2023

@llvm/issue-subscribers-flang-runtime

Author: Yusuke MINATO (yus3710-fj)

This is an issue from Fujitsu testsuite.

The execution of a program compiled by flang-new terminates with a fatal error.
In the program, there is a WRITE statement in a parallel region and the unit specified in that is not opened explicitly.
The execution terminates normally when OpenMP is disabled.

The following are the test program, Flang-new and gfortran execution result.

! test.f90
!$omp parallel
write(1,*) 'OK'
!$omp end parallel
end
$ flang-new -v test.f90 -fopenmp
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git dae91f5dbc5bee579eac7f4cbb71e86f2934817f)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Selected GCC installation: /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Candidate multilib: .;@<!-- -->m64
Selected multilib: .;@<!-- -->m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -fopenmp -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +neon -target-feature +v8a -o /tmp/test-f33630.o -x f95-cpp-input test.f90
 "/usr/bin/ld" -pie -EL --hash-style=gnu --eh-frame-hdr -m aarch64linux -dynamic-linker /lib/ld-linux-aarch64.so.1 -o a.out /lib/../lib64/Scrt1.o /lib/../lib64/crti.o /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/crtbeginS.o -L/path/to/install/lib/clang/18/lib/aarch64-unknown-linux-gnu -L/path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0 -L/path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/test-f33630.o -L/path/to/install/lib -lFortran_main -lFortranRuntime -lFortranDecimal -lm -lomp -L/path/to/install/lib -lgcc --as-needed -lgcc_s --no-as-needed -lpthread -lc -lgcc --as-needed -lgcc_s --no-as-needed /path/to/gcc/11.2.0/lib/gcc/aarch64-unknown-linux-gnu/11.2.0/crtendS.o /lib/../lib64/crtn.o
$ ./a.out
$ OMP_NUM_THREADS=4 ./a.out

fatal Fortran runtime error(/path/to/test.f90:2): Attempted output to read-only file
Aborted (core dumped)
$ flang-new test.f90
$ ./a.out
$ cat fort.1 
 OK
$ gfortran -v test.f90 -fopenmp
Driving: gfortran -v test.f90 -fopenmp -l gfortran -l m -shared-libgcc
Using built-in specs.
COLLECT_GCC=gfortran
Target: aarch64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-gnu-indirect-function --build=aarch64-redhat-linux
Thread model: posix
gcc version 8.4.1 20200928 (Red Hat 8.4.1-1) (GCC) 
 :
$ OMP_NUM_THREADS=4 ./a.out
$ cat fort.1
 OK
 OK
 OK
 OK

@sihuan
Copy link
Contributor

sihuan commented Nov 1, 2023

It looks like the problem is here.

ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
Direction dir, std::optional<bool> isUnformatted,
const Terminator &terminator) {
bool exists{false};
ExternalFileUnit *result{
GetUnitMap().LookUpOrCreate(unit, terminator, exists)};
if (result && !exists) {
IoErrorHandler handler{terminator};
result->OpenAnonymousUnit(
dir == Direction::Input ? OpenStatus::Unknown : OpenStatus::Replace,
Action::ReadWrite, Position::Rewind, Convert::Unknown, handler);
result->isUnformatted = isUnformatted;
}
return result;
}

The first thread adds Unit 1 to UnitMap, and then opens/initializes the unit via OpenAnonymousUnit. And the other threads can find Unit 1 in UnitMap, but Unit 1 may not have been initialized, I mean mayWrite_ has not been set.
mayWrite_ = *action != Action::Read;

Then got the IostatWriteToReadOnly error.
if (mayWrite()) {
direction_ = Direction::Output;
return IostatOk;
} else {
return IostatWriteToReadOnly;
}

sihuan added a commit that referenced this issue Dec 6, 2023
…74468)

In parallel regions `LookUpOrCreateAnonymous` may return an anonymous
unit that has not yet been opened, which may cause a runtime error.
This commit ensures that the returned anonymous unit has been opened.
For details see:
#68856 (comment)

Fixes #68856
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants