Skip to content

[flang][cuda] Set implicit CUDA device attribute in block construct #140637

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 1 commit into from
May 20, 2025
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
43 changes: 35 additions & 8 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9372,11 +9372,40 @@ void ResolveNamesVisitor::CreateGeneric(const parser::GenericSpec &x) {
info.Resolve(&MakeSymbol(symbolName, Attrs{}, std::move(genericDetails)));
}

static void SetImplicitCUDADevice(bool inDeviceSubprogram, Symbol &symbol) {
if (inDeviceSubprogram && symbol.has<ObjectEntityDetails>()) {
auto *object{symbol.detailsIf<ObjectEntityDetails>()};
if (!object->cudaDataAttr() && !IsValue(symbol) &&
(IsDummy(symbol) || object->IsArray())) {
// Implicitly set device attribute if none is set in device context.
object->set_cudaDataAttr(common::CUDADataAttr::Device);
}
}
}

void ResolveNamesVisitor::FinishSpecificationPart(
const std::list<parser::DeclarationConstruct> &decls) {
misparsedStmtFuncFound_ = false;
funcResultStack().CompleteFunctionResultType();
CheckImports();
bool inDeviceSubprogram{false};
Symbol *scopeSym{currScope().symbol()};
if (currScope().kind() == Scope::Kind::BlockConstruct) {
scopeSym = currScope().parent().symbol();
}
if (scopeSym) {
if (auto *details{scopeSym->detailsIf<SubprogramDetails>()}) {
// Check the current procedure is a device procedure to apply implicit
// attribute at the end.
if (auto attrs{details->cudaSubprogramAttrs()}) {
if (*attrs == common::CUDASubprogramAttrs::Device ||
*attrs == common::CUDASubprogramAttrs::Global ||
*attrs == common::CUDASubprogramAttrs::Grid_Global) {
inDeviceSubprogram = true;
}
}
}
}
for (auto &pair : currScope()) {
auto &symbol{*pair.second};
if (inInterfaceBlock()) {
Expand Down Expand Up @@ -9411,6 +9440,11 @@ void ResolveNamesVisitor::FinishSpecificationPart(
SetBindNameOn(symbol);
}
}
if (currScope().kind() == Scope::Kind::BlockConstruct) {
// Only look for specification in BlockConstruct. Other cases are done in
// ResolveSpecificationParts.
SetImplicitCUDADevice(inDeviceSubprogram, symbol);
}
}
currScope().InstantiateDerivedTypes();
for (const auto &decl : decls) {
Expand Down Expand Up @@ -9970,14 +10004,7 @@ void ResolveNamesVisitor::ResolveSpecificationParts(ProgramTree &node) {
}
ApplyImplicitRules(symbol);
// Apply CUDA implicit attributes if needed.
if (inDeviceSubprogram && symbol.has<ObjectEntityDetails>()) {
auto *object{symbol.detailsIf<ObjectEntityDetails>()};
if (!object->cudaDataAttr() && !IsValue(symbol) &&
(IsDummy(symbol) || object->IsArray())) {
// Implicitly set device attribute if none is set in device context.
object->set_cudaDataAttr(common::CUDADataAttr::Device);
}
}
SetImplicitCUDADevice(inDeviceSubprogram, symbol);
// Main program local objects usually don't have an implied SAVE attribute,
// as one might think, but in the exceptional case of a derived type
// local object that contains a coarray, we have to mark it as an
Expand Down
11 changes: 11 additions & 0 deletions flang/test/Semantics/cuf09.cuf
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,14 @@ attributes(host,device) subroutine do2(a,b,c,i)
c(i) = a(i) - b(i) ! ok. Should not error with Host array
! cannot be present in device context
end

attributes(global) subroutine blockTest
block
integer(8) :: xloc
integer(8) :: s(7)
integer(4) :: i
do i = 1, 7
s = xloc ! ok.
end do
end block
end subroutine