Skip to content

[flang][cuda] Allow list-directed PRINT and WRITE stmt in device code #87415

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 4 commits into from
Apr 8, 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
64 changes: 64 additions & 0 deletions flang/lib/Semantics/check-cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,73 @@ template <bool IsCUFKernelDo> class DeviceContextChecker {
},
ec.u);
}
template <typename SEEK, typename A>
static const SEEK *GetIOControl(const A &stmt) {
for (const auto &spec : stmt.controls) {
if (const auto *result{std::get_if<SEEK>(&spec.u)}) {
return result;
}
}
return nullptr;
}
template <typename A> static bool IsInternalIO(const A &stmt) {
if (stmt.iounit.has_value()) {
return std::holds_alternative<Fortran::parser::Variable>(stmt.iounit->u);
}
if (auto *unit{GetIOControl<Fortran::parser::IoUnit>(stmt)}) {
return std::holds_alternative<Fortran::parser::Variable>(unit->u);
}
return false;
}
void WarnOnIoStmt(const parser::CharBlock &source) {
context_.Say(
source, "I/O statement might not be supported on device"_warn_en_US);
}
template <typename A>
void WarnIfNotInternal(const A &stmt, const parser::CharBlock &source) {
if (!IsInternalIO(stmt)) {
WarnOnIoStmt(source);
}
}
void Check(const parser::ActionStmt &stmt, const parser::CharBlock &source) {
common::visit(
common::visitors{
[&](const common::Indirection<parser::PrintStmt> &) {},
[&](const common::Indirection<parser::WriteStmt> &x) {
if (x.value().format) { // Formatted write to '*' or '6'
if (std::holds_alternative<Fortran::parser::Star>(
x.value().format->u)) {
if (x.value().iounit) {
if (std::holds_alternative<Fortran::parser::Star>(
x.value().iounit->u)) {
return;
}
}
}
}
WarnIfNotInternal(x.value(), source);
},
[&](const common::Indirection<parser::CloseStmt> &x) {
WarnOnIoStmt(source);
},
[&](const common::Indirection<parser::EndfileStmt> &x) {
WarnOnIoStmt(source);
},
[&](const common::Indirection<parser::OpenStmt> &x) {
WarnOnIoStmt(source);
},
[&](const common::Indirection<parser::ReadStmt> &x) {
WarnIfNotInternal(x.value(), source);
},
[&](const common::Indirection<parser::InquireStmt> &x) {
WarnOnIoStmt(source);
},
[&](const common::Indirection<parser::RewindStmt> &x) {
WarnOnIoStmt(source);
},
[&](const common::Indirection<parser::BackspaceStmt> &x) {
WarnOnIoStmt(source);
},
[&](const auto &x) {
if (auto msg{ActionStmtChecker<IsCUFKernelDo>::WhyNotOk(x)}) {
context_.Say(source, std::move(*msg));
Expand Down
8 changes: 8 additions & 0 deletions flang/test/Semantics/cuf09.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ module m
do k=1,10
end do
end
attributes(device) subroutine devsub2
real, device :: x(10)
print*,'from device'
print '(f10.5)', (x(ivar), ivar = 1, 10)
write(*,*), "Hello world from device!"
!WARNING: I/O statement might not be supported on device
write(12,'(10F4.1)'), x
end
end

program main
Expand Down
Loading