Skip to content

Commit cf4be78

Browse files
authored
[flang][runtime] Debug PRINT *, "HI" on GPU (#172087)
Decrease memory allocation for buffers, allocate the pseudo-unit only once on demand, and avoid using a "%*.s" format.
1 parent 6a41ace commit cf4be78

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

flang-rt/include/flang-rt/runtime/buffer.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,13 @@ template <typename STORE, std::size_t minBuffer = 65536> class FileFrame {
151151
char *old{buffer_};
152152
auto oldSize{size_};
153153
size_ = std::max<std::int64_t>(bytes, size_ + minBuffer);
154-
buffer_ =
155-
reinterpret_cast<char *>(AllocateMemoryOrCrash(terminator, size_));
154+
std::int64_t toAllocate{size_};
155+
#ifdef RT_USE_PSEUDO_FILE_UNIT
156+
// PseudoOpenFile::Write() needs extra space for a NUL byte.
157+
++toAllocate;
158+
#endif
159+
buffer_ = reinterpret_cast<char *>(
160+
AllocateMemoryOrCrash(terminator, toAllocate));
156161
auto chunk{std::min<std::int64_t>(length_, oldSize - start_)};
157162
// "memcpy" in glibc has a "nonnull" attribute on the source pointer.
158163
// Avoid passing a null pointer, since it would result in an undefined

flang-rt/lib/runtime/pseudo-unit.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,13 @@ ExternalFileUnit *ExternalFileUnit::LookUpOrCreate(
3636

3737
ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
3838
Direction direction, common::optional<bool>, IoErrorHandler &handler) {
39-
if (direction != Direction::Output) {
40-
handler.Crash("ExternalFileUnit only supports output IO");
39+
if (direction != Direction::Output || unit != 6) {
40+
handler.Crash("ExternalFileUnit only supports output to unit 6");
41+
}
42+
if (!defaultOutput) { // defer construction until/unless needed
43+
defaultOutput = New<ExternalFileUnit>{handler}(unit).release();
4144
}
42-
return New<ExternalFileUnit>{handler}(unit).release();
45+
return defaultOutput;
4346
}
4447

4548
ExternalFileUnit *ExternalFileUnit::LookUp(const char *, std::size_t) {
@@ -124,15 +127,11 @@ std::size_t PseudoOpenFile::Read(
124127

125128
std::size_t PseudoOpenFile::Write(FileOffset at, const char *buffer,
126129
std::size_t bytes, IoErrorHandler &handler) {
127-
if (at) {
128-
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
129-
}
130-
// TODO: use persistent string buffer that can be reallocated
131-
// as needed, and only freed at destruction of *this.
132-
auto string{SizedNew<char>{handler}(bytes + 1)};
133-
runtime::memcpy(string.get(), buffer, bytes);
134-
string.get()[bytes] = '\0';
135-
std::printf("%s", string.get());
130+
char *mutableBuffer{const_cast<char *>(buffer)};
131+
char save{buffer[bytes]};
132+
mutableBuffer[bytes] = '\0';
133+
std::printf("%s", buffer);
134+
mutableBuffer[bytes] = save;
136135
return bytes;
137136
}
138137

flang-rt/lib/runtime/unit.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,18 @@ using FileFrameClass = FileFrame<ExternalFileUnit>;
9999
#else // defined(RT_USE_PSEUDO_FILE_UNIT)
100100
using OpenFileClass = PseudoOpenFile;
101101
// Use not so big buffer for the pseudo file unit frame.
102-
using FileFrameClass = FileFrame<ExternalFileUnit, 1024>;
102+
using FileFrameClass = FileFrame<ExternalFileUnit, 256>;
103103
#endif // defined(RT_USE_PSEUDO_FILE_UNIT)
104104

105105
class ExternalFileUnit : public ConnectionState,
106106
public OpenFileClass,
107107
public FileFrameClass {
108108
public:
109+
#ifdef RT_USE_PSEUDO_FILE_UNIT
110+
static constexpr int maxAsyncIds{64};
111+
#else
109112
static constexpr int maxAsyncIds{64 * 16};
113+
#endif
110114

111115
explicit RT_API_ATTRS ExternalFileUnit(int unitNumber)
112116
: unitNumber_{unitNumber} {

0 commit comments

Comments
 (0)