Skip to content
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
042d2f6
[flang] Draft of the work to look up COMMON declarations in the modules
eugeneepshteyn Oct 6, 2025
f31c976
Merge branch 'main' into 2-acc-common
eugeneepshteyn Oct 6, 2025
931b300
Try to handle use association, but didn't work, because no symbol
eugeneepshteyn Oct 9, 2025
0bfb06c
Merge branch 'main' into 2-acc-common
eugeneepshteyn Oct 9, 2025
86cb8b6
Searching for COMMON definition in top level modules
eugeneepshteyn Oct 9, 2025
f462bf5
Guard against null cb
eugeneepshteyn Oct 9, 2025
14ce6b6
Unit test
eugeneepshteyn Oct 9, 2025
8c344c3
Merge branch 'llvm:main' into 2-acc-common
eugeneepshteyn Oct 9, 2025
e00d8f5
Merge branch '2-acc-common' into 3-acc-common
eugeneepshteyn Oct 10, 2025
827f8f0
Merge branch 'llvm:main' into 3-acc-common
eugeneepshteyn Oct 12, 2025
430660f
Merge branch 'llvm:main' into 3-acc-common
eugeneepshteyn Oct 13, 2025
ff637dd
Updated Scope::FindCommonBlock() to be similar to Scope::FindSymbol()…
eugeneepshteyn Oct 13, 2025
9e19979
clang-format
eugeneepshteyn Oct 13, 2025
66028ef
Split FindCommonBlock() into FindCB() and FindCommonBlockInScopes(). …
eugeneepshteyn Oct 13, 2025
41336f1
Add USE association details to COMMON blocks
eugeneepshteyn Oct 13, 2025
eac75b7
Handle the case of redeclared COMMON block
eugeneepshteyn Oct 13, 2025
553a3c3
clang-format
eugeneepshteyn Oct 13, 2025
63662d8
Merge branch 'llvm:main' into 3-acc-common
eugeneepshteyn Oct 13, 2025
adcd69b
Merge branch 'llvm:main' into 2-acc-common
eugeneepshteyn Oct 13, 2025
74de636
Merge branch '3-acc-common' into 2-acc-common
eugeneepshteyn Oct 13, 2025
6527159
Merge branch 'llvm:main' into 3-acc-common
eugeneepshteyn Oct 16, 2025
c042379
Code review feedback + separated COMMON lists into COMMON in the curr…
eugeneepshteyn Oct 16, 2025
740894f
COMMON block uses renames. Extended test to using module chain
eugeneepshteyn Oct 16, 2025
ca82173
clang-format
eugeneepshteyn Oct 16, 2025
2474da0
Enabled creating of USE symbols from USE symbols of COMMON blocks
eugeneepshteyn Oct 16, 2025
68363d2
Merge branch 'llvm:main' into 3-acc-common
eugeneepshteyn Oct 16, 2025
5c84d48
clang-format
eugeneepshteyn Oct 16, 2025
cc4145c
Merge branch 'llvm:main' into 2-acc-common
eugeneepshteyn Oct 16, 2025
06b2a01
Merge branch '3-acc-common' into 2-acc-common
eugeneepshteyn Oct 16, 2025
8f802f7
Code review feedback
eugeneepshteyn Oct 16, 2025
b711009
Renamed to FindCommonBlockInVisibleScope()
eugeneepshteyn Oct 17, 2025
ee0cdde
clang-format
eugeneepshteyn Oct 17, 2025
5349a65
Merge branch 'llvm:main' into 2-acc-common
eugeneepshteyn Oct 17, 2025
b268f4e
Merge branch 'llvm:main' into 3-acc-common
eugeneepshteyn Oct 20, 2025
6c8e7a0
Code review feedback
eugeneepshteyn Oct 16, 2025
bab4e67
Renamed to FindCommonBlockInVisibleScope()
eugeneepshteyn Oct 17, 2025
ab01526
clang-format
eugeneepshteyn Oct 17, 2025
f2028c7
Code review feedback
eugeneepshteyn Oct 20, 2025
258050f
Merge branch 'llvm:main' into 3-acc-common
eugeneepshteyn Oct 20, 2025
adf6b5f
Merge branch 'llvm:main' into 2-acc-common
eugeneepshteyn Oct 20, 2025
86dc6a7
Merge branch '3-acc-common' into 2-acc-common
eugeneepshteyn Oct 20, 2025
acde775
clang-format
eugeneepshteyn Oct 20, 2025
abd2f40
[AddOrUpdateCommonBlockUse] Implemented selection of largest COMMON b…
eugeneepshteyn Oct 20, 2025
fc6beb7
[AddOrUpdateCommonBlockUse] Implemented selection of largest COMMON b…
eugeneepshteyn Oct 20, 2025
19c1534
clang-format
eugeneepshteyn Oct 20, 2025
8689f0a
Merge branch 'llvm:main' into 2-acc-common
eugeneepshteyn Oct 21, 2025
e7370be
Updated the test with a_common defined with more variables in a diffe…
eugeneepshteyn Oct 21, 2025
dadf669
Moved Scope::FindCommonBlock() and Scope::FindCommonBlockUse() implem…
eugeneepshteyn Oct 21, 2025
c62a62d
Merge branch 'llvm:main' into 2-acc-common
eugeneepshteyn Oct 21, 2025
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
34 changes: 31 additions & 3 deletions flang/include/flang/Semantics/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class Scope {
CHECK(parent_ != this);
return *parent_;
}

mapType &commonBlocks() { return commonBlocks_; }
const mapType &commonBlocks() const { return commonBlocks_; }

mapType &commonBlockUses() { return commonBlockUses_; }
const mapType &commonBlockUses() const { return commonBlockUses_; }

Kind kind() const { return kind_; }
bool IsGlobal() const { return kind_ == Kind::Global; }
bool IsIntrinsicModules() const { return kind_ == Kind::IntrinsicModules; }
Expand Down Expand Up @@ -186,10 +193,30 @@ class Scope {
// Cray pointers are saved as map of pointee name -> pointer symbol
const mapType &crayPointers() const { return crayPointers_; }
void add_crayPointer(const SourceName &, Symbol &);
mapType &commonBlocks() { return commonBlocks_; }
const mapType &commonBlocks() const { return commonBlocks_; }
Symbol &MakeCommonBlock(SourceName, SourceName location);
Symbol *FindCommonBlock(const SourceName &) const;
bool AddCommonBlockUse(
const SourceName &name, Attrs attrs, Symbol &cbUltimate);

// Find COMMON block that is declared in the current scope
Symbol *FindCommonBlock(const SourceName &name) const {
if (const auto it{commonBlocks_.find(name)}; it != commonBlocks_.end()) {
return &*it->second;
}
return nullptr;
}

// Find USE-associated COMMON block in the current scope
Symbol *FindCommonBlockUse(const SourceName &name) const {
if (const auto it{commonBlockUses_.find(name)};
it != commonBlockUses_.end()) {
return &*it->second;
}
return nullptr;
}

// Find COMMON block in current and surrounding scopes, follow USE
// associations
Symbol *FindCommonBlockInVisibleScopes(const SourceName &) const;

/// Make a Symbol but don't add it to the scope.
template <typename D>
Expand Down Expand Up @@ -283,6 +310,7 @@ class Scope {
std::list<Scope> children_;
mapType symbols_;
mapType commonBlocks_;
mapType commonBlockUses_; // USE-assocated COMMON blocks
std::list<EquivalenceSet> equivalenceSets_;
mapType crayPointers_;
std::map<SourceName, common::Reference<Scope>> submodules_;
Expand Down
21 changes: 8 additions & 13 deletions flang/lib/Semantics/resolve-directives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,17 +1730,12 @@ void AccAttributeVisitor::Post(const parser::Name &name) {

Symbol *AccAttributeVisitor::ResolveAccCommonBlockName(
const parser::Name *name) {
if (auto *prev{name
? GetContext().scope.parent().FindCommonBlock(name->source)
: nullptr}) {
name->symbol = prev;
return prev;
}
// Check if the Common Block is declared in the current scope
if (auto *commonBlockSymbol{
name ? GetContext().scope.FindCommonBlock(name->source) : nullptr}) {
name->symbol = commonBlockSymbol;
return commonBlockSymbol;
if (name) {
if (Symbol *
cb{GetContext().scope.FindCommonBlockInVisibleScopes(name->source)}) {
name->symbol = cb;
return cb;
}
}
return nullptr;
}
Expand Down Expand Up @@ -1790,8 +1785,8 @@ void AccAttributeVisitor::ResolveAccObject(
}
} else {
context_.Say(name.source,
"COMMON block must be declared in the same scoping unit "
"in which the OpenACC directive or clause appears"_err_en_US);
"Could not find COMMON block '%s' used in OpenACC directive"_err_en_US,
name.ToString());
}
},
},
Expand Down
14 changes: 14 additions & 0 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3627,6 +3627,20 @@ void ModuleVisitor::Post(const parser::UseStmt &x) {
}
}
}
// Go through the list of COMMON block symbols in the module scope and add
// their USE association to the current scope's USE-associated COMMON blocks.
for (const auto &[name, symbol] : useModuleScope_->commonBlocks()) {
if (!currScope().FindCommonBlockInVisibleScopes(name)) {
currScope().AddCommonBlockUse(
name, symbol->attrs(), symbol->GetUltimate());
}
}
// Go through the list of USE-associated COMMON block symbols in the module
// scope and add USE associations to their ultimate symbols to the current
// scope's USE-associated COMMON blocks.
for (const auto &[name, symbol] : useModuleScope_->commonBlockUses()) {
currScope().AddCommonBlockUse(name, symbol->attrs(), symbol->GetUltimate());
}
useModuleScope_ = nullptr;
}

Expand Down
37 changes: 31 additions & 6 deletions flang/lib/Semantics/scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,34 @@ void Scope::add_crayPointer(const SourceName &name, Symbol &pointer) {
}

Symbol &Scope::MakeCommonBlock(SourceName name, SourceName location) {
const auto it{commonBlocks_.find(name)};
if (it != commonBlocks_.end()) {
return *it->second;
if (auto *cb{FindCommonBlock(name)}) {
return *cb;
} else {
Symbol &symbol{MakeSymbol(
name, Attrs{}, CommonBlockDetails{name.empty() ? location : name})};
commonBlocks_.emplace(name, symbol);
return symbol;
}
}
Symbol *Scope::FindCommonBlock(const SourceName &name) const {
const auto it{commonBlocks_.find(name)};
return it != commonBlocks_.end() ? &*it->second : nullptr;

Symbol *Scope::FindCommonBlockInVisibleScopes(const SourceName &name) const {
if (Symbol * cb{FindCommonBlock(name)}) {
return cb;
} else if (Symbol * cb{FindCommonBlockUse(name)}) {
return &cb->GetUltimate();
} else if (IsSubmodule()) {
if (const Scope *parent{
symbol_ ? symbol_->get<ModuleDetails>().parent() : nullptr}) {
if (auto *cb{parent->FindCommonBlockInVisibleScopes(name)}) {
return cb;
}
}
} else if (!IsTopLevel() && parent_) {
if (auto *cb{parent_->FindCommonBlockInVisibleScopes(name)}) {
return cb;
}
}
return nullptr;
}

Scope *Scope::FindSubmodule(const SourceName &name) const {
Expand All @@ -167,6 +182,16 @@ Scope *Scope::FindSubmodule(const SourceName &name) const {
return &*it->second;
}
}

bool Scope::AddCommonBlockUse(
const SourceName &name, Attrs attrs, Symbol &cbUltimate) {
CHECK(cbUltimate.has<CommonBlockDetails>());
// Make a symbol, but don't add it to the Scope, since it needs to
// be added to the USE-associated COMMON blocks
Symbol &useCB{MakeSymbol(name, attrs, UseDetails{name, cbUltimate})};
return commonBlockUses_.emplace(name, useCB).second;
}

bool Scope::AddSubmodule(const SourceName &name, Scope &submodule) {
return submodules_.emplace(name, submodule).second;
}
Expand Down
41 changes: 41 additions & 0 deletions flang/test/Semantics/OpenACC/acc-common.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
! RUN: %python %S/../test_errors.py %s %flang -fopenacc
module acc_common_decl
implicit none
integer a
common /a_common/ a
!$acc declare create (/a_common/)
data a/42/
end module acc_common_decl

module acc_common_another
implicit none
integer c, d
common /a_common/ c, d
!$acc declare create (/a_common/)
end module acc_common_another

module acc_common_intermediate
use acc_common_decl
implicit none
integer b
common /b_common/ b
!$acc declare create (/b_common/)
end module acc_common_intermediate

program acc_decl_test
use acc_common_intermediate
use acc_common_another
implicit none

a = 1
b = 10
!$acc update device (/a_common/)
a = 2
!$acc update device (/b_common/)
b = 20
!$acc update device (/a_common/)
c = 3
d = 30
!ERROR: Could not find COMMON block 'a_common_bad' used in OpenACC directive
!$acc update device (/a_common_bad/)
end program
Loading