Skip to content

Commit fe2ff54

Browse files
authored
[flang][runtime] Decouple scalar output APIs from descriptors (#92444)
For testing purposes the implementations of the output APIs like OutputInteger32 have been simply constructing descriptors and executing the operation through the general DescriptorIO template. This patch decouples those APIs from that mechanism so that programs using simple "PRINT *" statements for output can link to a smaller portion of the I/O runtime support library. (This is the only form of I/O accepted in GPU device code by previous CUDA Fortran and Fortran OpenACC compilers.)
1 parent 8da3a8f commit fe2ff54

File tree

5 files changed

+264
-216
lines changed

5 files changed

+264
-216
lines changed

flang/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ set(sources
136136
inquiry.cpp
137137
internal-unit.cpp
138138
io-api.cpp
139+
io-api-minimal.cpp
139140
io-error.cpp
140141
io-stmt.cpp
141142
iostat.cpp

flang/runtime/descriptor-io.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ static RT_API_ATTRS bool DescriptorIO(IoStatementState &io,
499499
return false;
500500
}
501501
if (!io.get_if<IoDirectionState<DIR>>()) {
502-
io.GetIoErrorHandler().Crash(
503-
"DescriptorIO() called for wrong I/O direction");
502+
handler.Crash("DescriptorIO() called for wrong I/O direction");
504503
return false;
505504
}
506505
if constexpr (DIR == Direction::Input) {

flang/runtime/io-api-common.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//===-- runtime/io-api-common.h ---------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FLANG_RUNTIME_IO_API_COMMON_H_
10+
#define FLANG_RUNTIME_IO_API_COMMON_H_
11+
12+
#include "io-stmt.h"
13+
#include "terminator.h"
14+
#include "unit.h"
15+
#include "flang/Common/api-attrs.h"
16+
#include "flang/Common/optional.h"
17+
#include "flang/Runtime/io-api.h"
18+
19+
namespace Fortran::runtime::io {
20+
21+
static inline RT_API_ATTRS Cookie NoopUnit(const Terminator &terminator,
22+
int unitNumber, enum Iostat iostat = IostatOk) {
23+
Cookie cookie{&New<NoopStatementState>{terminator}(
24+
terminator.sourceFileName(), terminator.sourceLine(), unitNumber)
25+
.release()
26+
->ioStatementState()};
27+
if (iostat != IostatOk) {
28+
cookie->GetIoErrorHandler().SetPendingError(iostat);
29+
}
30+
return cookie;
31+
}
32+
33+
static inline RT_API_ATTRS ExternalFileUnit *GetOrCreateUnit(int unitNumber,
34+
Direction direction, Fortran::common::optional<bool> isUnformatted,
35+
const Terminator &terminator, Cookie &errorCookie) {
36+
if (ExternalFileUnit *
37+
unit{ExternalFileUnit::LookUpOrCreateAnonymous(
38+
unitNumber, direction, isUnformatted, terminator)}) {
39+
errorCookie = nullptr;
40+
return unit;
41+
} else {
42+
errorCookie = NoopUnit(terminator, unitNumber, IostatBadUnitNumber);
43+
return nullptr;
44+
}
45+
}
46+
47+
template <Direction DIR, template <Direction> class STATE, typename... A>
48+
RT_API_ATTRS Cookie BeginExternalListIO(
49+
int unitNumber, const char *sourceFile, int sourceLine, A &&...xs) {
50+
Terminator terminator{sourceFile, sourceLine};
51+
Cookie errorCookie{nullptr};
52+
ExternalFileUnit *unit{GetOrCreateUnit(
53+
unitNumber, DIR, false /*!unformatted*/, terminator, errorCookie)};
54+
if (!unit) {
55+
return errorCookie;
56+
}
57+
if (!unit->isUnformatted.has_value()) {
58+
unit->isUnformatted = false;
59+
}
60+
Iostat iostat{IostatOk};
61+
if (*unit->isUnformatted) {
62+
iostat = IostatFormattedIoOnUnformattedUnit;
63+
}
64+
if (ChildIo * child{unit->GetChildIo()}) {
65+
if (iostat == IostatOk) {
66+
iostat = child->CheckFormattingAndDirection(false, DIR);
67+
}
68+
if (iostat == IostatOk) {
69+
return &child->BeginIoStatement<ChildListIoStatementState<DIR>>(
70+
*child, sourceFile, sourceLine);
71+
} else {
72+
return &child->BeginIoStatement<ErroneousIoStatementState>(
73+
iostat, nullptr /* no unit */, sourceFile, sourceLine);
74+
}
75+
} else {
76+
if (iostat == IostatOk && unit->access == Access::Direct) {
77+
iostat = IostatListIoOnDirectAccessUnit;
78+
}
79+
if (iostat == IostatOk) {
80+
iostat = unit->SetDirection(DIR);
81+
}
82+
if (iostat == IostatOk) {
83+
return &unit->BeginIoStatement<STATE<DIR>>(
84+
terminator, std::forward<A>(xs)..., *unit, sourceFile, sourceLine);
85+
} else {
86+
return &unit->BeginIoStatement<ErroneousIoStatementState>(
87+
terminator, iostat, unit, sourceFile, sourceLine);
88+
}
89+
}
90+
}
91+
92+
} // namespace Fortran::runtime::io
93+
#endif // FLANG_RUNTIME_IO_API_COMMON_H_

flang/runtime/io-api-minimal.cpp

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//===-- runtime/io-api-minimal.cpp ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// Implements the subset of the I/O statement API needed for basic
10+
// list-directed output (PRINT *) of intrinsic types.
11+
12+
#include "edit-output.h"
13+
#include "format.h"
14+
#include "io-api-common.h"
15+
#include "io-stmt.h"
16+
#include "terminator.h"
17+
#include "tools.h"
18+
#include "unit.h"
19+
#include "flang/Runtime/io-api.h"
20+
21+
namespace Fortran::runtime::io {
22+
RT_EXT_API_GROUP_BEGIN
23+
24+
Cookie IODEF(BeginExternalListOutput)(
25+
ExternalUnit unitNumber, const char *sourceFile, int sourceLine) {
26+
return BeginExternalListIO<Direction::Output, ExternalListIoStatementState>(
27+
unitNumber, sourceFile, sourceLine);
28+
}
29+
30+
enum Iostat IODEF(EndIoStatement)(Cookie cookie) {
31+
IoStatementState &io{*cookie};
32+
return static_cast<enum Iostat>(io.EndIoStatement());
33+
}
34+
35+
template <int KIND, typename INT = CppTypeFor<TypeCategory::Integer, KIND>>
36+
inline RT_API_ATTRS bool FormattedScalarIntegerOutput(
37+
IoStatementState &io, INT x, const char *whence) {
38+
if (io.CheckFormattedStmtType<Direction::Output>(whence)) {
39+
auto edit{io.GetNextDataEdit()};
40+
return edit && EditIntegerOutput<KIND>(io, *edit, x);
41+
} else {
42+
return false;
43+
}
44+
}
45+
46+
bool IODEF(OutputInteger8)(Cookie cookie, std::int8_t n) {
47+
return FormattedScalarIntegerOutput<1>(*cookie, n, "OutputInteger8");
48+
}
49+
50+
bool IODEF(OutputInteger16)(Cookie cookie, std::int16_t n) {
51+
return FormattedScalarIntegerOutput<2>(*cookie, n, "OutputInteger16");
52+
}
53+
54+
bool IODEF(OutputInteger32)(Cookie cookie, std::int32_t n) {
55+
return FormattedScalarIntegerOutput<4>(*cookie, n, "OutputInteger32");
56+
}
57+
58+
bool IODEF(OutputInteger64)(Cookie cookie, std::int64_t n) {
59+
return FormattedScalarIntegerOutput<8>(*cookie, n, "OutputInteger64");
60+
}
61+
62+
#ifdef __SIZEOF_INT128__
63+
bool IODEF(OutputInteger128)(Cookie cookie, common::int128_t n) {
64+
return FormattedScalarIntegerOutput<16>(*cookie, n, "OutputInteger128");
65+
}
66+
#endif
67+
68+
template <int KIND,
69+
typename REAL = typename RealOutputEditing<KIND>::BinaryFloatingPoint>
70+
inline RT_API_ATTRS bool FormattedScalarRealOutput(
71+
IoStatementState &io, REAL x, const char *whence) {
72+
if (io.CheckFormattedStmtType<Direction::Output>(whence)) {
73+
auto edit{io.GetNextDataEdit()};
74+
return edit && RealOutputEditing<KIND>{io, x}.Edit(*edit);
75+
} else {
76+
return false;
77+
}
78+
}
79+
80+
bool IODEF(OutputReal32)(Cookie cookie, float x) {
81+
return FormattedScalarRealOutput<4>(*cookie, x, "OutputReal32");
82+
}
83+
84+
bool IODEF(OutputReal64)(Cookie cookie, double x) {
85+
return FormattedScalarRealOutput<8>(*cookie, x, "OutputReal64");
86+
}
87+
88+
template <int KIND,
89+
typename REAL = typename RealOutputEditing<KIND>::BinaryFloatingPoint>
90+
inline RT_API_ATTRS bool FormattedScalarComplexOutput(
91+
IoStatementState &io, REAL re, REAL im, const char *whence) {
92+
if (io.CheckFormattedStmtType<Direction::Output>(whence)) {
93+
if (io.get_if<ListDirectedStatementState<Direction::Output>>() != nullptr) {
94+
DataEdit rEdit, iEdit;
95+
rEdit.descriptor = DataEdit::ListDirectedRealPart;
96+
iEdit.descriptor = DataEdit::ListDirectedImaginaryPart;
97+
rEdit.modes = iEdit.modes = io.mutableModes();
98+
return RealOutputEditing<KIND>{io, re}.Edit(rEdit) &&
99+
RealOutputEditing<KIND>{io, im}.Edit(iEdit);
100+
} else {
101+
auto reEdit{io.GetNextDataEdit()};
102+
if (reEdit && RealOutputEditing<KIND>{io, re}.Edit(*reEdit)) {
103+
auto imEdit{io.GetNextDataEdit()};
104+
return imEdit && RealOutputEditing<KIND>{io, im}.Edit(*imEdit);
105+
}
106+
}
107+
}
108+
return false;
109+
}
110+
111+
bool IODEF(OutputComplex32)(Cookie cookie, float re, float im) {
112+
return FormattedScalarComplexOutput<4>(*cookie, re, im, "OutputComplex32");
113+
}
114+
115+
bool IODEF(OutputComplex64)(Cookie cookie, double re, double im) {
116+
return FormattedScalarComplexOutput<8>(*cookie, re, im, "OutputComplex64");
117+
}
118+
119+
bool IODEF(OutputAscii)(Cookie cookie, const char *x, std::size_t length) {
120+
IoStatementState &io{*cookie};
121+
if (!x) {
122+
io.GetIoErrorHandler().Crash("Null address for character output item");
123+
} else if (auto *listOutput{
124+
io.get_if<ListDirectedStatementState<Direction::Output>>()}) {
125+
return ListDirectedCharacterOutput(io, *listOutput, x, length);
126+
} else if (io.CheckFormattedStmtType<Direction::Output>("OutputAscii")) {
127+
auto edit{io.GetNextDataEdit()};
128+
return edit && EditCharacterOutput(io, *edit, x, length);
129+
} else {
130+
return false;
131+
}
132+
}
133+
134+
bool IODEF(OutputLogical)(Cookie cookie, bool truth) {
135+
IoStatementState &io{*cookie};
136+
if (auto *listOutput{
137+
io.get_if<ListDirectedStatementState<Direction::Output>>()}) {
138+
return ListDirectedLogicalOutput(io, *listOutput, truth);
139+
} else if (io.CheckFormattedStmtType<Direction::Output>("OutputAscii")) {
140+
auto edit{io.GetNextDataEdit()};
141+
return edit && EditLogicalOutput(io, *edit, truth);
142+
} else {
143+
return false;
144+
}
145+
}
146+
147+
} // namespace Fortran::runtime::io
148+
149+
#if defined(_LIBCPP_VERBOSE_ABORT)
150+
// Provide own definition for `std::__libcpp_verbose_abort` to avoid dependency
151+
// on the version provided by libc++.
152+
153+
void std::__libcpp_verbose_abort(char const *format, ...) {
154+
va_list list;
155+
va_start(list, format);
156+
std::vfprintf(stderr, format, list);
157+
va_end(list);
158+
159+
std::abort();
160+
}
161+
#endif
162+
163+
RT_EXT_API_GROUP_END

0 commit comments

Comments
 (0)