Skip to content

Commit 518c328

Browse files
authored
[flang][runtime] Fix GPU output for multiple statements (#172363)
I recently broke PRINT statements in GPU device code when multiple PRINTs occur in the same kernel by trying to preserve the allocated pseudo-unit. This turned out to be a bad idea overall, and I'm reverting to the original protocol that minimizes allocated memory.
1 parent dbb4f5c commit 518c328

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

flang-rt/lib/runtime/io-stmt.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,11 @@ int ExternalIoStatementBase::EndIoStatement() {
270270
}
271271
}
272272
#else
273-
// Fetch the unit pointer before *this disappears.
274-
ExternalFileUnit *unitPtr{&unit_};
275273
// The pseudo file units are dynamically allocated
276-
// and are not tracked in the unit map.
277-
// They have to be destructed and deallocated here.
278-
unitPtr->~ExternalFileUnit();
279-
FreeMemory(unitPtr);
274+
// and are not tracked in any unit map.
275+
// They have to be destroyed and deallocated here.
276+
unit_.~ExternalFileUnit();
277+
FreeMemory(&unit_);
280278
#endif
281279
return result;
282280
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
3939
if (direction != Direction::Output || unit != 6) {
4040
handler.Crash("ExternalFileUnit only supports output to unit 6");
4141
}
42-
if (!defaultOutput) { // defer construction until/unless needed
43-
defaultOutput = New<ExternalFileUnit>{handler}(unit).release();
44-
}
45-
return defaultOutput;
42+
// The pseudo-unit allocated here will be released in
43+
// ExternalIoStatementBase::EndIoStatement().
44+
return New<ExternalFileUnit>{handler}(unit).release();
4645
}
4746

4847
ExternalFileUnit *ExternalFileUnit::LookUp(const char *, std::size_t) {

0 commit comments

Comments
 (0)