Commit graph

321 commits

Author SHA1 Message Date
Peter Klausler bafbae238a [flang] Initial UTF-8 support in runtime I/O
Implements UTF-8 encoding and decoding for external units
with OPEN(ENCODING='UTF-8').  This encoding applies to default
CHARACTER values that are not 7-bit ASCII as well as to
the wide CHARACTER kinds 2 and 4.  Basic testing is in place
via direct calls to the runtime I/O APIs, but serious checkout
awaits lowering support of the wide CHARACTER kinds.

Differential Revision: https://reviews.llvm.org/D122038
2022-03-22 11:48:14 -07:00
Peter Klausler 461b6fe470 [flang] Expose error recovery cases in external I/O
Some I/O error situations are current handled with fatal
runtime asserts, but should be exposed for user program
error recovery.

Differential Revision: https://reviews.llvm.org/D122049
2022-03-21 12:46:16 -07:00
Peter Klausler 8db4dc8686 [flang] Error recovery improvement in runtime (IOMSG=)
Some refactoring and related fixes for more accurate
user program error recovery in the I/O runtime, especially
for error recovery with IOMSG= character values.

1) Move any work in an EndIoStatement() implementation
that may raise an error into a new CompleteOperation()
member function.  This allows error handling APIs like
GetIoMsg() to complete a pending I/O statement and harvest
any errors that may result.

2) Move the pending error code from ErroneousIoStatementState
to a new pendingError_ data member in IoErrorHandler.
This allows IoErrorHandler::InError() to return a correct
result when there is a pending error that will be recovered
from so that I/O list data transfers don't crash in the meantime.

3) Don't create and leak a unit for a failed OPEN(NEWUNIT=n)
with error recovery, and don't modify 'n'.  (Depends on
changes to API call ordering in lowering, in a separate patch;
code was added to ensure that OPEN statement control list
specifiers, e.g. SetFile(), must be passed before GetNewUnit().)

4) Fix the code that calls a form of strerror to fill an
IOMSG= variable so that it actually works for Fortran's
character type: blank fill with no null or newline termination.

Differential Revision: https://reviews.llvm.org/D122036
2022-03-18 17:24:32 -07:00
Peter Klausler 251d062e4e [flang] Convert RUNTIME_CHECK to better error for user errors in transformational.cpp
In flang/runtime/transformational.cpp, there are many RUNTIME_CHECK assertions
for errors that should have been caught in semantics, but there are alno others
that signify program errors that in principle cannot be detected until
execution.  Convert this second group into readable fatal error messages.
Also clean up some missing braces and incorrect printf formats found
along the way.

Differential Revision: https://reviews.llvm.org/D122037
2022-03-18 16:52:43 -07:00
Jean Perier a69cb78242 [flang] Hanlde COMPLEX 2/3/10 in runtime TypeCode(cat, kind)
Type codes for COMPLEX kinds 2, 3, and 10 were added in https://reviews.llvm.org/D117336
but handling for these kinds in TypeCode(cat, kind) has not been added
yet.

Differential Revision: https://reviews.llvm.org/D121587
2022-03-15 09:26:14 +01:00
Peter Klausler 3b61587c9e [flang] LBOUND() edge case: empty dimension
LBOUND must return 1 for an empty dimension, no matter what
explicit expression might appear in a declaration or arrive in
a descriptor.

Differential Revision: https://reviews.llvm.org/D121488
2022-03-14 11:16:09 -07:00
Diana Picus 873f081e5a [flang] Add runtime support for GET_COMMAND
Implement the GET_COMMAND intrinsic.
Add 2 new parameters (sourceFile and line) so we can create a terminator
for RUNTIME_CHECKs.

Differential Revision: https://reviews.llvm.org/D118777
2022-03-14 09:35:45 +00:00
Peter Steinfeld e3550f1903 [flang] Improve runtime crash messages
Where possible, I added additional information to the messages to help
programmers figure out what went wrong.  I also removed all uses of the word
"bad" from the messages since (to me) that implies a moral judgement rather
than a programming error.  I replaced it with either "invalid" or "unsupported"
where appropriate.

Differential Revision: https://reviews.llvm.org/D121493
2022-03-12 15:15:56 -08:00
Peter Klausler 041080fc9b [flang] Fix extent computation in finalization
The code that computed the extent of a dimension of a
non-allocatable/non-automatic component array during
finalization had a reversed subtraction; fix, and
use variables to make the code a little more readable.

Differential Revision: https://reviews.llvm.org/D121163
2022-03-09 09:01:02 -08:00
Peter Klausler 764363368c [flang] Runtime validation of SPREAD(DIM=dim) argument
Crash when DIM= is not a valid dimension in the result.

Differential Revision: https://reviews.llvm.org/D121145
2022-03-07 13:49:39 -08:00
Peter Klausler ef7f6f7cd7 [flang] Use faster path for default formatted character input
Rather than reading default character variables in formatted
input one byte at a time via NextInField(), skip and read
them via blocks of available buffer data.  This eliminates
a bottleneck that affected reads of large character values.
(It also exposed a problem with sequential reads with RECL=
set on the OPEN statement, so that's fixed too.)

Differential Revision: https://reviews.llvm.org/D121144
2022-03-07 13:01:08 -08:00
Jean Perier 392cba8603 [flang] Handle optional TARGET associate in ASSOCIATED runtime
The TARGET argument of ASSOCIATED may be dynamically optional, in which
case ASSOCIATED(POINTER, TARGET) is equal to ASSOCIATED(TARGET).

Make the runtime argument a pointer so that it can detect and handle
arguments that are dynamically optional.

Also fix the runtime to check if TARGET base address is not null and if
its element size is not null to match the requirement of ASSOCIATED
regarding TARGET:
- if TARGET is an object: true iff [..] TARGET is not a zerosized storage sequence
- if TARGET is a POINTER: true iff [..] POINTER and TARGET are associated

Not that ASSOCIATED will also returns false if TARGET is an unallocated allocatable.
This is not described in the standard, but is a unanimous behaviour of
existing compilers.

Differential Revision: https://reviews.llvm.org/D120835
2022-03-03 10:11:35 +01:00
Peter Klausler 507f7317a0 [flang] Catch READ/WRITE on direct-access file without REC=
A data transfer statement must have REC= in its control list
if (and only if) the unit was opened with ACCESS='DIRECT'.
The runtime wasn't catching this error, but was just silently
advancing to the next record as if the access were sequential.

Differential Revision: https://reviews.llvm.org/D120838
2022-03-02 12:38:11 -08:00
Peter Klausler 3a96446d51 [flang] Honor RECL= in list-directed/namelist output
Advancement to new output lines was taking fixed-sized direct-access
and internal character array element lengths into account, but not
RECL= settings from OPEN statements.

Differential Revision: https://reviews.llvm.org/D120837
2022-03-02 12:07:18 -08:00
Peter Klausler 44ff4df6de [flang] Extension: don't require commas between most edit descriptors in formats
The standard explicitly allows a comma to be omitted between a 'P'
edit descriptor and a following numeric edit descriptor (e.g., 1PE10.1),
and before and after a '/' edit descriptor, but otherwise requires them
between edit descriptors.  Most implementations, however, only require
commas where they prevent ambiguity, and accept things like 1XI10.
This extension is already assumed by the static FORMAT checker in
semantics.  Patch the runtime to behave accordingly.

Differential Revision: https://reviews.llvm.org/D120747
2022-03-01 15:15:59 -08:00
Peter Klausler df38f35acb [flang] Allow data transfer stmt control list errors to be caught
The runtime crashes on several fundamental I/O data transfer statement
control list errors, like list I/O on a direct-access unit, or
input from a write-only unit, &c.  These errors should not be fatal
when ERR= or IOSTAT= are present.

This patch creates a new ErroneousIoStatementState class and
uses it for the state of an I/O statement that is doomed to fail
from these errors.  If there is no ERR= label or IOSTAT= variable,
the error will be raised at the end of the statement.  Data transfer
operations along the way will be no-op failures.

Differential Revision: https://reviews.llvm.org/D120745
2022-03-01 14:39:30 -08:00
Peter Klausler 73b193aec2 [flang] Allow more concurrently open NEWUNIT= values, with recycling
Add a header-only implementation of Briggs & Torczon's fast small
integer set data structure to flang/include/flang/Common, and use
it in the runtime to manage a pool of Fortran unit numbers with
recycling.  This replaces the bit set previously used for that
purpose.  The set is initialized on demand with the negations of
all the NEWUNIT= unit numbers that can be returned to any kind
of integer variable.

For programs that require more concurrently open NEWUNIT= unit
numbers than the pool can hold, they are now allocated with a
non-recycling counter.  This allows as many open units as the
operating system provides.

Many of the top-line comments in flang/unittests/Runtime had the
wrong path name.  I noticed this while adding a unit test for the
fast integer set data structure, and cleaned them up.

Differential Revision: https://reviews.llvm.org/D120685
2022-02-28 16:13:22 -08:00
V Donaldson 338b478e70 [flang] Do not print format tabs
As an extension, tabs are accepted in a format, but should be skipped,
not printed.
2022-02-23 20:03:10 -08:00
Peter Klausler 07b9a44515 [flang] Allow for deferred-length character in EstablishDescriptor
When the runtime is initializing an instance of a derived type,
don't crash if an allocatable character component has deferred length.

Differential Revision: https://reviews.llvm.org/D119731
2022-02-14 10:05:07 -08:00
Peter Steinfeld bdf5736521 [flang] Change internal errors in RESHAPE runtime routine to user errors
There are several checks in the runtime routine for the RESHAPE
intrinsic.  Some checks verify things that should have been checked at
compile time while others represent user errors.

This update changes the checks for user errors into calls to "Crash"
which include information about the failing check.  This identifies them
as user errors rather than compiler errors.

I also verified that the checks that remain as internal errors are also
checked by the front end.  I added a test to the front end's RESHAPE
test to complete the checks.

Differential Revision: https://reviews.llvm.org/D119596
2022-02-11 15:51:21 -08:00
Peter Steinfeld 6cd417bfd8 [flang] Upstream runtime changes for inquiry intrinsics
This change adds runtime routines and tests for LBOUND when passed a DIM argument, SIZE, and UBOUND when not passed a DIM argument.

Associated changes for lowering have already been merged into fir-dev.

Differential Revision: https://reviews.llvm.org/D119360
2022-02-09 12:42:36 -08:00
Peter Klausler 991696c2eb [flang] Debugging of ACCESS='STREAM' I/O (take 2)
Corrects the runtime implementation of I/O on files with
the access mode ACCESS='STREAM'.  This is a collection
of edge-case tweaks to ensure that the distinctions between
stream and direct/sequential files, unformatted or formatted,
are respected where appropriate.

Moves NextInField() from io-stmt.h to io-stmt.cpp --
it was getting too big to keep in a header.

This patch exposed a problem with the I/O runtime
on Windows and it was reverted.  This version also
fixes that problem; files are now opened on Windows
in binary mode to prevent inadvertent insertions of
carriage returns before line feeds, and those line
endings (CR+LF) are now explicitly generated.

Differential Revision: https://reviews.llvm.org/D119015
2022-02-04 18:02:34 -08:00
Andrzej Warzynski 4e53e28374 Revert "[flang] Debugging of ACCESS='STREAM' I/O"
This reverts commit be9946b877.

This change has caused Flang's Windows buildbot to start failing:
* https://lab.llvm.org/buildbot/#/builders/172/builds/7664
2022-02-03 15:19:42 +00:00
Peter Klausler 2b0b9b2e83 [flang] Modify right modes for READ/WRITE vs OPEN
When a mode flag is modified (e.g., BLANK='ZERO') in an I/O data transfer
statement, ensure that the right set of mode flags is modified.
There's one set of mode flags that are captured by an OPEN
statement and maintained in the connection, and another that
is maintained in an I/O statement state record for local mutability.
Some I/O API routines were unconditionally modifying the persistent
set of flags.

Differential Revision: https://reviews.llvm.org/D118835
2022-02-02 13:47:46 -08:00
Peter Klausler be9946b877 [flang] Debugging of ACCESS='STREAM' I/O
Corrects the runtime implementation of I/O on files with
the access mode ACCESS='STREAM'.  This is a collection
of edge-case tweaks to ensure that the distinctions between
stream and direct/sequential files, unformatted or formatted,
are respected where appropriate.
Moves NextInField() from io-stmt.h to io-stmt.cpp --
it was getting too big to keep in a header.

Differential Revision: https://reviews.llvm.org/D118834
2022-02-02 13:09:38 -08:00
Peter Klausler 9772dbba74 [flang] Set right "inNamelist" flag
NAMELIST I/O was inconsistent in its choice of which set of I/O modes
to set the "inNamelist" flag.  The wrong choice was in the set of modes
that are part of the persistent state of an I/O connection; the right
place is the set of modes that are reinitialized at the beginning of
each I/O statement so that they can be modified by READ/WRITE control
list specifiers and FORMAT control edit descriptors.  Fix.

Differential Revision: https://reviews.llvm.org/D118745
2022-02-02 09:33:19 -08:00
Peter Klausler 0f5c60f151 [flang] Fix edge-case I/O regressions
A blank field in an input record that exists must be interpreted
as a zero value for numeric input editing, but advancing to a
next record that doesn't exist should leave an input variable
unmodified (and signal END=).  On internal output, blank fill
the "current record" array element even if nothing has been
written to it if it is the only record.

Differential Revision: https://reviews.llvm.org/D118720
2022-02-01 16:27:47 -08:00
Peter Steinfeld 93ee588232 [flang] Rename the runtime routine that reports a fatal user error
As per Steve Scalpone's suggestion, I've renamed the runtime routine to
better evoke its purpose.

I implemented a routine called "Crash" and added a test.

Differential Revision: https://reviews.llvm.org/D118703
2022-02-01 09:01:50 -08:00
Peter Klausler c7f4c333af [flang] Make NEWUNIT= use a range suitable for INTEGER(KIND=1) and recycle unit numbers
Use a bit-set to manage runtime-generated I/O unit numbers, recycle
them after they're closed, and use a range of values that fits in
a minimal-sized integer.

Differential Revision: https://reviews.llvm.org/D118651
2022-01-31 15:20:21 -08:00
Peter Klausler 702c0cfa07 [flang] runtime perf: larger I/O buffer growth increments
When reallocating an I/O buffer to accommodate a large record,
ensure that the amount of growth is at least as large as the
minimum initial record size (64KiB).  The previous policy was
causing input buffer reallocation for each byte after the minimum
buffer size when scanning input data for record termination
newlines.

Differential Revision: https://reviews.llvm.org/D118649
2022-01-31 14:53:15 -08:00
Peter Klausler f651bbea06 [flang] Correct interpretation of RECL=
When RECL= is set on OPEN(), ensure that it:
  1) enforces a max output record payload size
     (not including header+footer or newline), and
  2) causes padding of short output records only
     for ACCESS='DIRECT'

The previous code was causing some false overrun errors
and applying padding to sequential/stream output files.

Differential Revision: https://reviews.llvm.org/D118630
2022-01-31 12:48:17 -08:00
Peter Steinfeld eb933225f4 [flang] Implement a runtime routine to report fatal errors with source position
The title says it all.

I implemented a routine called "Crash" and added a test.

Differential Revision: https://reviews.llvm.org/D118509
2022-01-28 15:46:00 -08:00
Peter Klausler 896a543e72 [flang] Support DECIMAL='COMMA' mode in namelist I/O
DECIMAL='COMMA' mode affects item separators, real editing, and
complex editing.

Differential Revision: https://reviews.llvm.org/D117906
2022-01-22 09:01:36 -08:00
Peter Klausler e9d0f8baf2 [flang] Don't drop format string for external child I/O
In user-defined derived type I/O to an external unit, don't
omit the format string from the constructor of ChildFormattedIoStatement.
And include any user IOMSG text in the crash message of the
parent, if it doesn't catch errors.

Differential Revision: https://reviews.llvm.org/D117903
2022-01-22 09:01:02 -08:00
Peter Klausler b1856009fb [flang] Allow INQUIRE() on a child unit in user-defined I/O procedure
A procedure that implements a user-defined derived type I/O operation
is allowed to perform an INQUIRE statement on its unit.

Differential Revision: https://reviews.llvm.org/D117905https://reviews.llvm.org/D117905
2022-01-21 18:23:56 -08:00
Peter Klausler 04eb93b1d5 [flang] Fix repeated "DT" editing
User-defined derived type editing in formatted I/O wasn't
working with repeat counts; e.g., "2DT(10)".  The solution required
some code to be moved from GetNextDataEdit() to CueUpNextDataEdit() so
that a stack entry for a nonparenthesized repeated data edit
descriptor would work correctly -- all other data edit descriptors
are capable of dealing with repetition in their callees, so the bug
hadn't been exposed before.

Debugging this problem led to some improvements in error messages
for bad format strings, and those changes have been retained; also,
a dead member function was discovered and expunged.

Differential Revision: https://reviews.llvm.org/D117904
2022-01-21 17:22:51 -08:00
Peter Klausler d1123e3692 [flang] Extension: skip over NAMELIST groups
Implements a near-universal extension in which NAMELIST
input will skip over unrelated namelist groups in the
input stream until the group with the requested name appears.

Differential Revision: https://reviews.llvm.org/D117843
2022-01-20 17:01:29 -08:00
Peter Klausler 922c29ccf1 [flang] Allow explicit '+' in NAMELIST input subscripts
Array subscripts and substring limits in NAMELIST input are
allowed to bear an explicit plus sign.

Differential Revision: https://reviews.llvm.org/D117818
2022-01-20 17:01:02 -08:00
Peter Klausler 9ddd07922f [flang] Handle FLUSH(unknown unit)
The unit number passed to a FLUSH statement is not required to
be a valid open unit; nothing happens (esp. not the creation of
an empty fort.n file) in this case.

Differential Revision: https://reviews.llvm.org/D117819
2022-01-20 15:35:34 -08:00
Peter Klausler 5501c16edf [flang] Fix OPEN/WRITE(SIGN='SUPPRESS')
The keyword value was misspelled in the runtime.

Differential Revision: https://reviews.llvm.org/D117816
2022-01-20 15:34:35 -08:00
Peter Klausler 047884e71e [flang] runtime: catch OPEN(ACCESS='DIRECT',POSITION=)
A POSITION= specifier may not be used on a direct access file.

Differential Revision: https://reviews.llvm.org/D117596
2022-01-18 14:54:53 -08:00
Peter Klausler e847b30369 [flang] runtime error on inappropriate OPEN(UNIT=extant,RECL=n)
Don't let a program set a fixed RECL= on a connected unit unless
it already had one with the same value.

Differential Revision: https://reviews.llvm.org/D117595
2022-01-18 14:54:27 -08:00
Peter Klausler 0ab170803f [flang] Support substring references in NAMELIST input
Implements substring references into potentially partial CHARACTER
scalars and array elements in NAMELIST input.

Differential Revision: https://reviews.llvm.org/D117576
2022-01-18 11:24:17 -08:00
Peter Klausler b77fd01a8f [flang] Don't blank-fill remaining lines in internal output
Internal writes to character arrays should not blank-fill
records (elements) past the last one that was written to.

Differential Revision: https://reviews.llvm.org/D117342
2022-01-14 15:14:48 -08:00
Peter Klausler ac4202fe9d [flang] Signal runtime error on WRITE after ENDFILE
After an ENDFILE statement, a WRITE is an error without
a prior BACKSPACE.  Also fix the return value for the case
of formatted integer input with no input digits to be false
(exposed by new test).

Differential Revision: https://reviews.llvm.org/D117346
2022-01-14 14:46:00 -08:00
V Donaldson e43b2e4f48 [flang] "CFI" types for Fortran REAL and COMPLEX kinds 2, 3, 10, 16
Add additional "CFI" types for Fortran REAL and COMPLEX kinds 2, 3, 10, 16 to allow their use in Fortran descriptors.
2022-01-14 11:06:18 -08:00
Valentin Clement ad3bb7c7da
[flang] Simplify RaggedArrayHeader and make it plain C struct
- Join indirection and rank into a single value `flags`
- Make the struct a plain C struct.

Reviewed By: schweitz

Differential Revision: https://reviews.llvm.org/D115464
2021-12-09 22:28:06 +01:00
Peter Klausler 627a8ac790 [flang] Add CALL FLUSH(n) legacy extension
Prior to the introduction of the FLUSH statement in Fortran 2003,
implementations provided a FLUSH subroutine.

We can't yet put Fortran code into the runtime, so this subroutine
is in C++ with a Fortran-mangled entry point name.

Differential Revision: https://reviews.llvm.org/D115289
2021-12-08 08:56:54 -08:00
Valentin Clement 6116ff297f
[flang][runtime] Add ragged array runtime functions
This patch adds the runtime function to allocate and
deallocate ragged arrays.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D114534

Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
2021-12-07 21:22:34 +01:00
Peter Klausler 5034e1730f [flang] Remove runtime check from OpenFile::Close()
In error cases it is possible to CLOSE a unit that has not
been successfully connected, so don't crash when the file descriptor
is negative.

Differential Revision: https://reviews.llvm.org/D115165
2021-12-07 09:43:24 -08:00