[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 05:49:48 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-runtime

Author: SiHuaN (sihuan)

<details>
<summary>Changes</summary>

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: https://github.com/llvm/llvm-project/issues/68856#issuecomment-1788632511

Fixes https://github.com/llvm/llvm-project/issues/68856



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


1 Files Affected:

- (modified) flang/runtime/unit.cpp (+4) 


``````````diff
diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 995656b9480c4..fa9efee78127e 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -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;
 static UnitMap *unitMap{nullptr};
 static ExternalFileUnit *defaultInput{nullptr}; // unit 5
 static ExternalFileUnit *defaultOutput{nullptr}; // unit 6
@@ -52,6 +53,9 @@ ExternalFileUnit *ExternalFileUnit::LookUpOrCreate(
 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.
+  CriticalSection critical{createOpenLock};
   bool exists{false};
   ExternalFileUnit *result{
       GetUnitMap().LookUpOrCreate(unit, terminator, exists)};

``````````

</details>


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


More information about the flang-commits mailing list