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][runtime] Support READ after WRITE w/o positioning #74650

Merged
merged 1 commit into from
Dec 11, 2023

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Dec 6, 2023

Most Fortran implementations support a READ statement after a WRITE without repositioning on a sequential unit; it implies on ENDFILE and then hits an EOF condition.

Fixes llvm-test-suite/Fortran/gfortran/regression/backspace_2.f.

@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Dec 6, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 6, 2023

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

Most Fortran implementations support a READ statement after a WRITE without repositioning on a sequential unit; it implies on ENDFILE and then hits an EOF condition.

Fixes llvm-test-suite/Fortran/gfortran/regression/backspace_2.f.


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

2 Files Affected:

  • (modified) flang/runtime/unit.cpp (+9)
  • (modified) flang/runtime/unit.h (+1)
diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 995656b9480c4..b069e93caf354 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -351,6 +351,7 @@ bool ExternalFileUnit::Emit(const char *data, std::size_t bytes,
   }
   positionInRecord += bytes;
   furthestPositionInRecord = furthestAfter;
+  anyWriteSinceLastPositioning_ = true;
   return true;
 }
 
@@ -447,6 +448,11 @@ bool ExternalFileUnit::BeginReadingRecord(IoErrorHandler &handler) {
         HitEndOnRead(handler);
       }
     } else {
+      if (anyWriteSinceLastPositioning_) {
+        // Most Fortran implementations allow a READ after a WRITE;
+        // it just hits an EOF.
+        DoEndfile(handler);
+      }
       recordLength.reset();
       if (IsAtEOF()) {
         handler.SignalEnd();
@@ -611,6 +617,7 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
       }
     }
     BeginRecord();
+    anyWriteSinceLastPositioning_ = false;
   }
 }
 
@@ -663,6 +670,7 @@ void ExternalFileUnit::Rewind(IoErrorHandler &handler) {
     SetPosition(0, handler);
     currentRecordNumber = 1;
     leftTabLimit.reset();
+    anyWriteSinceLastPositioning_ = false;
   }
 }
 
@@ -924,6 +932,7 @@ void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
   TruncateFrame(frameOffsetInFile_, handler);
   BeginRecord();
   impliedEndfile_ = false;
+  anyWriteSinceLastPositioning_ = false;
 }
 
 void ExternalFileUnit::CommitWrites() {
diff --git a/flang/runtime/unit.h b/flang/runtime/unit.h
index 1ec3013ba82b3..140fda3c4d2a8 100644
--- a/flang/runtime/unit.h
+++ b/flang/runtime/unit.h
@@ -140,6 +140,7 @@ class ExternalFileUnit : public ConnectionState,
   Direction direction_{Direction::Output};
   bool impliedEndfile_{false}; // sequential/stream output has taken place
   bool beganReadingRecord_{false};
+  bool anyWriteSinceLastPositioning_{false};
   bool directAccessRecWasSet_{false}; // REC= appeared
   // Subtle: The beginning of the frame can't be allowed to advance
   // during a single list-directed READ due to the possibility of a

@klausler klausler force-pushed the backspace2 branch 2 times, most recently from fff8a34 to 151a9b8 Compare December 8, 2023 22:54
Most Fortran implementations support a READ statement after
a WRITE without repositioning on a sequential unit; it
implies on ENDFILE and then hits an EOF condition.

Fixes llvm-test-suite/Fortran/gfortran/regression/backspace_2.f.
@klausler klausler merged commit c94f780 into llvm:main Dec 11, 2023
4 checks passed
@klausler klausler deleted the backspace2 branch December 11, 2023 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants