llvm-project/flang-rt/lib/runtime/pseudo-unit.cpp
agozillon 30d2cb5a7e
[Flang][OpenMP][Runtime] Minor Flang runtime for OpenMP AMDGPU modifications (#152631)
We have some modifications downstream to compile the flang runtime for
amdgpu using clang OpenMP, some more hacky than others to workaround
(hopefully temporary) compiler issues. The additions here are the
non-hacky alterations.

Main changes:
* Create freestanding versions of memcpy, strlen and memmove, and
replace std:: references with these so that we can default to std:: when
it's available, or our own Flang implementation when it's not. * Wrap
more bits and pieces of the library in declare target wrappers (RT_*
macros). * Fix some warnings that'll pose issues with werror on, in this
case having the namespace infront of variables passed to templates.

Another minor issues that'll likely still pop up depending on the
program you're linking with is that abort will be undefined, it is
perhaps possible to solve it with a freestanding implementation as with
memcpy etc. but we end up with multiple definitions in this case. An
alternative is to create an empty extern "c" version (which can be empty
or forwrd on to the builtin).

Co-author: Dan Palermo Dan.Palermo@amd.com
2025-08-29 23:04:48 +02:00

168 lines
5.5 KiB
C++

//===-- lib/runtime/pseudo-unit.cpp -----------------------------*- 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
//
//===----------------------------------------------------------------------===//
//
// Implemenation of ExternalFileUnit and PseudoOpenFile for
// RT_USE_PSEUDO_FILE_UNIT=1.
//
//===----------------------------------------------------------------------===//
#include "unit.h"
#include "flang-rt/runtime/io-error.h"
#include "flang-rt/runtime/tools.h"
// NOTE: the header files above may define OpenMP declare target
// variables, so they have to be included unconditionally
// so that the offload entries are consistent between host and device.
#if defined(RT_USE_PSEUDO_FILE_UNIT)
#include <cstdio>
namespace Fortran::runtime::io {
void FlushOutputOnCrash(const Terminator &) {}
ExternalFileUnit *ExternalFileUnit::LookUp(int) {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
ExternalFileUnit *ExternalFileUnit::LookUpOrCreate(
int, const Terminator &, bool &) {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
ExternalFileUnit *ExternalFileUnit::LookUpOrCreateAnonymous(int unit,
Direction direction, common::optional<bool>, IoErrorHandler &handler) {
if (direction != Direction::Output) {
handler.Crash("ExternalFileUnit only supports output IO");
}
return New<ExternalFileUnit>{handler}(unit).release();
}
ExternalFileUnit *ExternalFileUnit::LookUp(const char *, std::size_t) {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
ExternalFileUnit &ExternalFileUnit::CreateNew(int, const Terminator &) {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
ExternalFileUnit *ExternalFileUnit::LookUpForClose(int) {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
ExternalFileUnit &ExternalFileUnit::NewUnit(const Terminator &, bool) {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
bool ExternalFileUnit::OpenUnit(common::optional<OpenStatus> status,
common::optional<Action>, Position, OwningPtr<char> &&, std::size_t,
Convert, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
bool ExternalFileUnit::OpenAnonymousUnit(common::optional<OpenStatus>,
common::optional<Action>, Position, Convert convert,
IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
void ExternalFileUnit::CloseUnit(CloseStatus, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
void ExternalFileUnit::DestroyClosed() {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
Iostat ExternalFileUnit::SetDirection(Direction direction) {
if (direction != Direction::Output) {
return IostatReadFromWriteOnly;
}
direction_ = direction;
return IostatOk;
}
void ExternalFileUnit::CloseAll(IoErrorHandler &) {}
void ExternalFileUnit::FlushAll(IoErrorHandler &) {}
int ExternalFileUnit::GetAsynchronousId(IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
bool ExternalFileUnit::Wait(int) {
Terminator{__FILE__, __LINE__}.Crash("unsupported");
}
void PseudoOpenFile::set_mayAsynchronous(bool yes) {
if (yes) {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
}
common::optional<PseudoOpenFile::FileOffset> PseudoOpenFile::knownSize() const {
Terminator{__FILE__, __LINE__}.Crash("unsupported");
}
void PseudoOpenFile::Open(
OpenStatus, common::optional<Action>, Position, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
void PseudoOpenFile::Close(CloseStatus, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
std::size_t PseudoOpenFile::Read(
FileOffset, char *, std::size_t, std::size_t, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
std::size_t PseudoOpenFile::Write(FileOffset at, const char *buffer,
std::size_t bytes, IoErrorHandler &handler) {
if (at) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
// TODO: use persistent string buffer that can be reallocated
// as needed, and only freed at destruction of *this.
auto string{SizedNew<char>{handler}(bytes + 1)};
runtime::memcpy(string.get(), buffer, bytes);
string.get()[bytes] = '\0';
std::printf("%s", string.get());
return bytes;
}
void PseudoOpenFile::Truncate(FileOffset, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
int PseudoOpenFile::ReadAsynchronously(
FileOffset, char *, std::size_t, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
int PseudoOpenFile::WriteAsynchronously(
FileOffset, const char *, std::size_t, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
void PseudoOpenFile::Wait(int, IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
void PseudoOpenFile::WaitAll(IoErrorHandler &handler) {
handler.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
Position PseudoOpenFile::InquirePosition(FileOffset) const {
Terminator{__FILE__, __LINE__}.Crash("%s: unsupported", RT_PRETTY_FUNCTION);
}
} // namespace Fortran::runtime::io
#endif // defined(RT_USE_PSEUDO_FILE_UNIT)