[flang-commits] [flang] [flang] [cuda] Move SetImplicityCUDADevice after symbols in block construct are converted to objects (PR #143791)
Zhen Wang via flang-commits
flang-commits at lists.llvm.org
Wed Jun 11 14:46:02 PDT 2025
https://github.com/wangzpgi created https://github.com/llvm/llvm-project/pull/143791
`SetImplicitCUDADevice` looks for `symbol.has<ObjectEntityDetails>()` to set the device attribute before symbols inside block constructs are converted to ObjectEntity. Fix is to move the call to `SetImplicitCUDADevice` after those symbols are converted.
>From 76b296ff762ca79ff244b37268f9de1ee296ada3 Mon Sep 17 00:00:00 2001
From: Zhen Wang <zhenw at nvidia.com>
Date: Wed, 11 Jun 2025 14:23:58 -0700
Subject: [PATCH 1/2] move SetImplicitCUDADevice call after symbols inside
block construct are converted to object
---
flang/lib/Semantics/resolve-names.cpp | 50 ++++++++++++++-------------
1 file changed, 26 insertions(+), 24 deletions(-)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 7db447aee0026..63ebcb01b9b96 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -58,6 +58,8 @@ using MessageFormattedText = parser::MessageFormattedText;
class ResolveNamesVisitor;
class ScopeHandler;
+void SetImplicitCUDADevice(bool inDeviceSubprogram, Symbol &symbol);
+
// ImplicitRules maps initial character of identifier to the DeclTypeSpec
// representing the implicit type; std::nullopt if none.
// It also records the presence of IMPLICIT NONE statements.
@@ -2867,8 +2869,31 @@ void ScopeHandler::PopScope() {
// Entities that are not yet classified as objects or procedures are now
// assumed to be objects.
// TODO: Statement functions
+ 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()) {
ConvertToObjectEntity(*pair.second);
+ if (currScope_->kind() == Scope::Kind::BlockConstruct) {
+ // Only look for specification in BlockConstruct. Other cases are done in
+ // ResolveSpecificationParts.
+ SetImplicitCUDADevice(inDeviceSubprogram, *pair.second);
+ }
}
funcResultStack_.Pop();
// If popping back into a global scope, pop back to the top scope.
@@ -9555,7 +9580,7 @@ void ResolveNamesVisitor::CreateGeneric(const parser::GenericSpec &x) {
info.Resolve(&MakeSymbol(symbolName, Attrs{}, std::move(genericDetails)));
}
-static void SetImplicitCUDADevice(bool inDeviceSubprogram, Symbol &symbol) {
+void SetImplicitCUDADevice(bool inDeviceSubprogram, Symbol &symbol) {
if (inDeviceSubprogram && symbol.has<ObjectEntityDetails>()) {
auto *object{symbol.detailsIf<ObjectEntityDetails>()};
if (!object->cudaDataAttr() && !IsValue(symbol) &&
@@ -9571,24 +9596,6 @@ void ResolveNamesVisitor::FinishSpecificationPart(
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()) {
@@ -9623,11 +9630,6 @@ 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) {
>From e7d055634fc5ff51eb8e92a87e724daed1708a7d Mon Sep 17 00:00:00 2001
From: Zhen Wang <zhenw at nvidia.com>
Date: Wed, 11 Jun 2025 14:25:58 -0700
Subject: [PATCH 2/2] add test
---
flang/test/Semantics/cuf21.cuf | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/flang/test/Semantics/cuf21.cuf b/flang/test/Semantics/cuf21.cuf
index 077657c8a52d5..db32f1dbd0e7b 100644
--- a/flang/test/Semantics/cuf21.cuf
+++ b/flang/test/Semantics/cuf21.cuf
@@ -13,18 +13,21 @@ contains
implicit none
logical, intent(in), value :: back
real(4) :: mval
-
- call maxlocUpdate(mval, back)
-
+ block
+ integer(8) :: xloc
+ call maxlocUpdate(mval, xloc, back)
+ end block
end subroutine maxlocPartialMaskR_32F1D
- attributes(device) subroutine maxlocUpdateR_32F(mval, back)
+ attributes(device) subroutine maxlocUpdateR_32F(mval, xloc, back)
real(4) :: mval
+ integer(8) :: xloc
logical :: back
end subroutine maxlocUpdateR_32F
- attributes(device) subroutine maxlocUpdateR_64F(mval, back)
+ attributes(device) subroutine maxlocUpdateR_64F(mval, xloc, back)
real(8) :: mval
+ integer(8) :: xloc
logical :: back
end subroutine maxlocUpdateR_64F
end module
More information about the flang-commits
mailing list