Skip to content

[flang][cuda] Add restriction on implicit data transfer #87720

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 5 commits into from
Apr 5, 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
16 changes: 11 additions & 5 deletions flang/include/flang/Evaluate/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -1226,18 +1226,24 @@ bool CheckForCoindexedObject(parser::ContextualMessages &,
const std::optional<ActualArgument> &, const std::string &procName,
const std::string &argName);

/// Check if any of the symbols part of the expression has a cuda data
/// attribute.
inline bool HasCUDAAttrs(const Expr<SomeType> &expr) {
// Get the number of distinct symbols with CUDA attribute in the expression.
template <typename A> inline int GetNbOfCUDASymbols(const A &expr) {
semantics::UnorderedSymbolSet symbols;
for (const Symbol &sym : CollectSymbols(expr)) {
if (const auto *details =
sym.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()) {
if (details->cudaDataAttr()) {
return true;
symbols.insert(sym);
}
}
}
return false;
return symbols.size();
}

// Check if any of the symbols part of the expression has a CUDA data
// attribute.
template <typename A> inline bool HasCUDAAttrs(const A &expr) {
return GetNbOfCUDASymbols(expr) > 0;
}

} // namespace Fortran::evaluate
Expand Down
16 changes: 16 additions & 0 deletions flang/lib/Semantics/check-cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
#include "check-cuda.h"
#include "flang/Common/template.h"
#include "flang/Evaluate/fold.h"
#include "flang/Evaluate/tools.h"
#include "flang/Evaluate/traverse.h"
#include "flang/Parser/parse-tree-visitor.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Parser/tools.h"
#include "flang/Semantics/expression.h"
#include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h"

// Once labeled DO constructs have been canonicalized and their parse subtrees
// transformed into parser::DoConstructs, scan the parser::Blocks of the program
Expand Down Expand Up @@ -413,4 +415,18 @@ void CUDAChecker::Enter(const parser::CUFKernelDoConstruct &x) {
}
}

void CUDAChecker::Enter(const parser::AssignmentStmt &x) {
const evaluate::Assignment *assign{semantics::GetAssignment(x)};
int nbLhs{evaluate::GetNbOfCUDASymbols(assign->lhs)};
int nbRhs{evaluate::GetNbOfCUDASymbols(assign->rhs)};
auto lhsLoc{std::get<parser::Variable>(x.t).GetSource()};

// device to host transfer with more than one device object on the rhs is not
// legal.
if (nbLhs == 0 && nbRhs > 1) {
context_.Say(lhsLoc,
"More than one reference to a CUDA object on the right hand side of the assigment"_err_en_US);
}
}

} // namespace Fortran::semantics
2 changes: 2 additions & 0 deletions flang/lib/Semantics/check-cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Program;
class Messages;
struct Name;
class CharBlock;
struct AssignmentStmt;
struct ExecutionPartConstruct;
struct ExecutableConstruct;
struct ActionStmt;
Expand All @@ -38,6 +39,7 @@ class CUDAChecker : public virtual BaseChecker {
void Enter(const parser::FunctionSubprogram &);
void Enter(const parser::SeparateModuleSubprogram &);
void Enter(const parser::CUFKernelDoConstruct &);
void Enter(const parser::AssignmentStmt &);

private:
SemanticsContext &context_;
Expand Down
12 changes: 12 additions & 0 deletions flang/test/Semantics/cuf11.cuf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
! RUN: %python %S/test_errors.py %s %flang_fc1

subroutine sub1()
real, device :: adev(10), bdev(10)
real :: ahost(10)

!ERROR: More than one reference to a CUDA object on the right hand side of the assigment
ahost = adev + bdev

ahost = adev + adev

end subroutine