[flang-commits] [flang] [flang][runtime] Terminate last partial record after non-advancing write (PR #74524)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Dec 8 12:12:43 PST 2023


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/74524

>From 2d4be9d6265fbc2f87534c26ee76308aa225ce92 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 5 Dec 2023 13:10:11 -0800
Subject: [PATCH] [flang][runtime] Terminate last partial record after
 non-advancing write

After a non-advancing WRITE to a unit, ensure that any ENDFILE
operation (explicit or implicit) flushes and, if not stream output,
terminates the record.  (All other Fortran implementations do so
except XLF.)

Fixes llvm-test-suite/Fortran/gfortran/regression/advance_6.f90.
---
 flang/runtime/unit.cpp | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 5fa8565c2f61fc..11dfcdd6665b50 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -595,9 +595,6 @@ void ExternalFileUnit::BackspaceRecord(IoErrorHandler &handler) {
     if (IsAfterEndfile()) {
       // BACKSPACE after explicit ENDFILE
       currentRecordNumber = *endfileRecordNumber;
-    } else if (leftTabLimit) {
-      // BACKSPACE after non-advancing I/O
-      leftTabLimit.reset();
     } else {
       DoImpliedEndfile(handler);
       if (frameOffsetInFile_ + recordOffsetInFrame_ > 0) {
@@ -896,28 +893,34 @@ void ExternalFileUnit::BackspaceVariableFormattedRecord(
 }
 
 void ExternalFileUnit::DoImpliedEndfile(IoErrorHandler &handler) {
-  if (!impliedEndfile_ && direction_ == Direction::Output && IsRecordFile() &&
-      access != Access::Direct && leftTabLimit) {
-    // Complete partial record after non-advancing write before
-    // positioning or closing the unit.  Usually sets impliedEndfile_.
-    AdvanceRecord(handler);
+  if (access == Access::Direct || direction_ == Direction::Input) {
+    impliedEndfile_ = false;
+    return;
+  }
+  if (!impliedEndfile_ && leftTabLimit) {
+    // Complete a partial record after non-advancing I/O
+    if (access == Access::Sequential) {
+      AdvanceRecord(handler);
+    }
+    impliedEndfile_ = true;
   }
   if (impliedEndfile_) {
     impliedEndfile_ = false;
-    if (access != Access::Direct && IsRecordFile() && mayPosition()) {
+    if (mayPosition()) {
       DoEndfile(handler);
     }
   }
 }
 
 void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
+  if (direction_ != Direction::Output) {
+    return;
+  }
   if (IsRecordFile() && access != Access::Direct) {
     furthestPositionInRecord =
         std::max(positionInRecord, furthestPositionInRecord);
-    if (leftTabLimit) {
-      // Last read/write was non-advancing, so AdvanceRecord() was not called.
-      leftTabLimit.reset();
-      ++currentRecordNumber;
+    if (leftTabLimit && access == Access::Sequential) {
+      AdvanceRecord(handler); // last write was non-advancing
     }
     endfileRecordNumber = currentRecordNumber;
   }
@@ -927,7 +930,6 @@ void ExternalFileUnit::DoEndfile(IoErrorHandler &handler) {
   Truncate(frameOffsetInFile_, handler);
   TruncateFrame(frameOffsetInFile_, handler);
   BeginRecord();
-  impliedEndfile_ = false;
 }
 
 void ExternalFileUnit::CommitWrites() {



More information about the flang-commits mailing list