Skip to content

[flang][runtime] Decouple scalar output APIs from descriptors #92444

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

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flang/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ set(sources
inquiry.cpp
internal-unit.cpp
io-api.cpp
io-api-minimal.cpp
io-error.cpp
io-stmt.cpp
iostat.cpp
Expand Down
3 changes: 1 addition & 2 deletions flang/runtime/descriptor-io.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,7 @@ static RT_API_ATTRS bool DescriptorIO(IoStatementState &io,
return false;
}
if (!io.get_if<IoDirectionState<DIR>>()) {
io.GetIoErrorHandler().Crash(
"DescriptorIO() called for wrong I/O direction");
handler.Crash("DescriptorIO() called for wrong I/O direction");
return false;
}
if constexpr (DIR == Direction::Input) {
Expand Down
93 changes: 93 additions & 0 deletions flang/runtime/io-api-common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//===-- runtime/io-api-common.h ---------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef FLANG_RUNTIME_IO_API_COMMON_H_
#define FLANG_RUNTIME_IO_API_COMMON_H_

#include "io-stmt.h"
#include "terminator.h"
#include "unit.h"
#include "flang/Common/api-attrs.h"
#include "flang/Common/optional.h"
#include "flang/Runtime/io-api.h"

namespace Fortran::runtime::io {

static inline RT_API_ATTRS Cookie NoopUnit(const Terminator &terminator,
int unitNumber, enum Iostat iostat = IostatOk) {
Cookie cookie{&New<NoopStatementState>{terminator}(
terminator.sourceFileName(), terminator.sourceLine(), unitNumber)
.release()
->ioStatementState()};
if (iostat != IostatOk) {
cookie->GetIoErrorHandler().SetPendingError(iostat);
}
return cookie;
}

static inline RT_API_ATTRS ExternalFileUnit *GetOrCreateUnit(int unitNumber,
Direction direction, Fortran::common::optional<bool> isUnformatted,
const Terminator &terminator, Cookie &errorCookie) {
if (ExternalFileUnit *
unit{ExternalFileUnit::LookUpOrCreateAnonymous(
unitNumber, direction, isUnformatted, terminator)}) {
errorCookie = nullptr;
return unit;
} else {
errorCookie = NoopUnit(terminator, unitNumber, IostatBadUnitNumber);
return nullptr;
}
}

template <Direction DIR, template <Direction> class STATE, typename... A>
RT_API_ATTRS Cookie BeginExternalListIO(
int unitNumber, const char *sourceFile, int sourceLine, A &&...xs) {
Terminator terminator{sourceFile, sourceLine};
Cookie errorCookie{nullptr};
ExternalFileUnit *unit{GetOrCreateUnit(
unitNumber, DIR, false /*!unformatted*/, terminator, errorCookie)};
if (!unit) {
return errorCookie;
}
if (!unit->isUnformatted.has_value()) {
unit->isUnformatted = false;
}
Iostat iostat{IostatOk};
if (*unit->isUnformatted) {
iostat = IostatFormattedIoOnUnformattedUnit;
}
if (ChildIo * child{unit->GetChildIo()}) {
if (iostat == IostatOk) {
iostat = child->CheckFormattingAndDirection(false, DIR);
}
if (iostat == IostatOk) {
return &child->BeginIoStatement<ChildListIoStatementState<DIR>>(
*child, sourceFile, sourceLine);
} else {
return &child->BeginIoStatement<ErroneousIoStatementState>(
iostat, nullptr /* no unit */, sourceFile, sourceLine);
}
} else {
if (iostat == IostatOk && unit->access == Access::Direct) {
iostat = IostatListIoOnDirectAccessUnit;
}
if (iostat == IostatOk) {
iostat = unit->SetDirection(DIR);
}
if (iostat == IostatOk) {
return &unit->BeginIoStatement<STATE<DIR>>(
terminator, std::forward<A>(xs)..., *unit, sourceFile, sourceLine);
} else {
return &unit->BeginIoStatement<ErroneousIoStatementState>(
terminator, iostat, unit, sourceFile, sourceLine);
}
}
}

} // namespace Fortran::runtime::io
#endif // FLANG_RUNTIME_IO_API_COMMON_H_
163 changes: 163 additions & 0 deletions flang/runtime/io-api-minimal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//===-- runtime/io-api-minimal.cpp ----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// Implements the subset of the I/O statement API needed for basic
// list-directed output (PRINT *) of intrinsic types.

#include "edit-output.h"
#include "format.h"
#include "io-api-common.h"
#include "io-stmt.h"
#include "terminator.h"
#include "tools.h"
#include "unit.h"
#include "flang/Runtime/io-api.h"

namespace Fortran::runtime::io {
RT_EXT_API_GROUP_BEGIN

Cookie IODEF(BeginExternalListOutput)(
ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
return BeginExternalListIO<Direction::Output, ExternalListIoStatementState>(
unitNumber, sourceFile, sourceLine);
}

enum Iostat IODEF(EndIoStatement)(Cookie cookie) {
IoStatementState &io{*cookie};
return static_cast<enum Iostat>(io.EndIoStatement());
}

template <int KIND, typename INT = CppTypeFor<TypeCategory::Integer, KIND>>
inline RT_API_ATTRS bool FormattedScalarIntegerOutput(
IoStatementState &io, INT x, const char *whence) {
if (io.CheckFormattedStmtType<Direction::Output>(whence)) {
auto edit{io.GetNextDataEdit()};
return edit && EditIntegerOutput<KIND>(io, *edit, x);
} else {
return false;
}
}

bool IODEF(OutputInteger8)(Cookie cookie, std::int8_t n) {
return FormattedScalarIntegerOutput<1>(*cookie, n, "OutputInteger8");
}

bool IODEF(OutputInteger16)(Cookie cookie, std::int16_t n) {
return FormattedScalarIntegerOutput<2>(*cookie, n, "OutputInteger16");
}

bool IODEF(OutputInteger32)(Cookie cookie, std::int32_t n) {
return FormattedScalarIntegerOutput<4>(*cookie, n, "OutputInteger32");
}

bool IODEF(OutputInteger64)(Cookie cookie, std::int64_t n) {
return FormattedScalarIntegerOutput<8>(*cookie, n, "OutputInteger64");
}

#ifdef __SIZEOF_INT128__
bool IODEF(OutputInteger128)(Cookie cookie, common::int128_t n) {
return FormattedScalarIntegerOutput<16>(*cookie, n, "OutputInteger128");
}
#endif

template <int KIND,
typename REAL = typename RealOutputEditing<KIND>::BinaryFloatingPoint>
inline RT_API_ATTRS bool FormattedScalarRealOutput(
IoStatementState &io, REAL x, const char *whence) {
if (io.CheckFormattedStmtType<Direction::Output>(whence)) {
auto edit{io.GetNextDataEdit()};
return edit && RealOutputEditing<KIND>{io, x}.Edit(*edit);
} else {
return false;
}
}

bool IODEF(OutputReal32)(Cookie cookie, float x) {
return FormattedScalarRealOutput<4>(*cookie, x, "OutputReal32");
}

bool IODEF(OutputReal64)(Cookie cookie, double x) {
return FormattedScalarRealOutput<8>(*cookie, x, "OutputReal64");
}

template <int KIND,
typename REAL = typename RealOutputEditing<KIND>::BinaryFloatingPoint>
inline RT_API_ATTRS bool FormattedScalarComplexOutput(
IoStatementState &io, REAL re, REAL im, const char *whence) {
if (io.CheckFormattedStmtType<Direction::Output>(whence)) {
if (io.get_if<ListDirectedStatementState<Direction::Output>>() != nullptr) {
DataEdit rEdit, iEdit;
rEdit.descriptor = DataEdit::ListDirectedRealPart;
iEdit.descriptor = DataEdit::ListDirectedImaginaryPart;
rEdit.modes = iEdit.modes = io.mutableModes();
return RealOutputEditing<KIND>{io, re}.Edit(rEdit) &&
RealOutputEditing<KIND>{io, im}.Edit(iEdit);
} else {
auto reEdit{io.GetNextDataEdit()};
if (reEdit && RealOutputEditing<KIND>{io, re}.Edit(*reEdit)) {
auto imEdit{io.GetNextDataEdit()};
return imEdit && RealOutputEditing<KIND>{io, im}.Edit(*imEdit);
}
}
}
return false;
}

bool IODEF(OutputComplex32)(Cookie cookie, float re, float im) {
return FormattedScalarComplexOutput<4>(*cookie, re, im, "OutputComplex32");
}

bool IODEF(OutputComplex64)(Cookie cookie, double re, double im) {
return FormattedScalarComplexOutput<8>(*cookie, re, im, "OutputComplex64");
}

bool IODEF(OutputAscii)(Cookie cookie, const char *x, std::size_t length) {
IoStatementState &io{*cookie};
if (!x) {
io.GetIoErrorHandler().Crash("Null address for character output item");
} else if (auto *listOutput{
io.get_if<ListDirectedStatementState<Direction::Output>>()}) {
return ListDirectedCharacterOutput(io, *listOutput, x, length);
} else if (io.CheckFormattedStmtType<Direction::Output>("OutputAscii")) {
auto edit{io.GetNextDataEdit()};
return edit && EditCharacterOutput(io, *edit, x, length);
} else {
return false;
}
}

bool IODEF(OutputLogical)(Cookie cookie, bool truth) {
IoStatementState &io{*cookie};
if (auto *listOutput{
io.get_if<ListDirectedStatementState<Direction::Output>>()}) {
return ListDirectedLogicalOutput(io, *listOutput, truth);
} else if (io.CheckFormattedStmtType<Direction::Output>("OutputAscii")) {
auto edit{io.GetNextDataEdit()};
return edit && EditLogicalOutput(io, *edit, truth);
} else {
return false;
}
}

} // namespace Fortran::runtime::io

#if defined(_LIBCPP_VERBOSE_ABORT)
// Provide own definition for `std::__libcpp_verbose_abort` to avoid dependency
// on the version provided by libc++.

void std::__libcpp_verbose_abort(char const *format, ...) {
va_list list;
va_start(list, format);
std::vfprintf(stderr, format, list);
va_end(list);

std::abort();
}
#endif

RT_EXT_API_GROUP_END
Loading
Loading