[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:19 PST 2023


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

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



>From 1c8022ca1aaf2af193e5bb2c89aedbd64ce34bfc Mon Sep 17 00:00:00 2001
From: SiHuaN <liyongtai at iscas.ac.cn>
Date: Tue, 5 Dec 2023 21:38:22 +0800
Subject: [PATCH] [flang][runtime] Ensure that the anonymous unit returned by
 LookUpOrCreateAnonymous has been opened.

---
 flang/runtime/unit.cpp | 4 ++++
 1 file changed, 4 insertions(+)

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)};



More information about the flang-commits mailing list