[flang-commits] [flang] [flang][runtime] Ensure that the anonymous unit returned by LookUpOrCreateAnonymous has been opened. (PR #74468)

via flang-commits flang-commits at lists.llvm.org
Tue Dec 5 07:56:35 PST 2023


================
@@ -20,6 +20,7 @@ namespace Fortran::runtime::io {
 // The per-unit data structures are created on demand so that Fortran I/O
 // should work without a Fortran main program.
 static Lock unitMapLock;
+static Lock createOpenLock;
----------------
sihuan wrote:

If I move this declaration into `*ExternalFileUnit::LookUpOrCreateAnonymous`, like the following:
```cpp
ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
    Direction dir, std::optional<bool> isUnformatted,
    const Terminator &terminator) {
  // Make sure that the returned anonymous unit has been opened
  // not just created in the unitMap.
  static Lock createOpenLock;
  CriticalSection critical{createOpenLock};
  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;
}
```
I will have to add the `-lstdc++` flag when compiling with flang, or I will get a linker error:
```console
$ cat test.f90
! test.f90
!$omp parallel
write(1,*) 'OK'
!$omp end parallel
end
$ ../../llvm-project/build/bin/flang-new test.f90 -fopenmp
/usr/bin/ld: /path/to/llvm-project/build/lib/libFortranRuntime.a(unit.cpp.o): in function `Fortran::runtime::io::ExternalFileUnit::LookUpOrCreateAnonymous(int, Fortran::runtime::io::Direction, std::optional<bool>, Fortran::runtime::Terminator const&)':
/path/to/llvm-project/flang/runtime/unit.cpp:57:(.text+0x32e): undefined reference to `__cxa_guard_acquire'
/usr/bin/ld: /path/to/llvm-project/flang/runtime/unit.cpp:57:(.text+0x369): undefined reference to `__cxa_guard_release'
flang-new: error: linker command failed with exit code 1 (use -v to see invocation)
$ ../../llvm-project/build/bin/flang-new test.f90 -fopenmp -lstdc++
$ OMP_NUM_THREADS=4 ./a.out  
$ cat fort.1
 OK
 OK
 OK    
```


https://github.com/llvm/llvm-project/pull/74468


More information about the flang-commits mailing list