Skip to content

Commit abd2f40

Browse files
[AddOrUpdateCommonBlockUse] Implemented selection of largest COMMON block in case of uses from multiple modules, except that it doesn't work: COMMON block sizes are computed after the name resolution
1 parent acde775 commit abd2f40

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

flang/include/flang/Semantics/scope.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Scope {
194194
const mapType &crayPointers() const { return crayPointers_; }
195195
void add_crayPointer(const SourceName &, Symbol &);
196196
Symbol &MakeCommonBlock(SourceName, SourceName location);
197-
bool AddCommonBlockUse(const SourceName &name, Symbol &cbSymbol);
197+
bool AddOrUpdateCommonBlockUse(const SourceName &name, Attrs attrs, Symbol& cbUltimate);
198198

199199
// Find COMMON block that is declared in the current scope
200200
Symbol *FindCommonBlock(const SourceName &name) const {

flang/lib/Semantics/resolve-names.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3631,22 +3631,14 @@ void ModuleVisitor::Post(const parser::UseStmt &x) {
36313631
// their USE association to the current scope's USE-associated COMMON blocks.
36323632
for (const auto &[name, symbol] : useModuleScope_->commonBlocks()) {
36333633
if (!currScope().FindCommonBlockInVisibleScopes(name)) {
3634-
// Make a symbol, but don't add it to the Scope, since it needs to
3635-
// be added to the USE-associated COMMON blocks
3636-
Symbol &localCB{currScope().MakeSymbol(
3637-
name, symbol->attrs(), UseDetails{name, symbol->GetUltimate()})};
3638-
currScope().AddCommonBlockUse(name, localCB);
3634+
currScope().AddOrUpdateCommonBlockUse(name, symbol->attrs(), symbol->GetUltimate());
36393635
}
36403636
}
36413637
// Go through the list of USE-associated COMMON block symbols in the module
36423638
// scope and add USE associations to their ultimate symbols to the current
36433639
// scope's USE-associated COMMON blocks.
36443640
for (const auto &[name, symbol] : useModuleScope_->commonBlockUses()) {
3645-
// Make a symbol, but don't add it to the Scope, since it needs to
3646-
// be added to the USE-associated COMMON blocks
3647-
Symbol &localCB{currScope().MakeSymbol(
3648-
name, symbol->attrs(), UseDetails{name, symbol->GetUltimate()})};
3649-
currScope().AddCommonBlockUse(name, localCB);
3641+
currScope().AddOrUpdateCommonBlockUse(name, symbol->attrs(), symbol->GetUltimate());
36503642
}
36513643
useModuleScope_ = nullptr;
36523644
}

flang/lib/Semantics/scope.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,24 @@ Scope *Scope::FindSubmodule(const SourceName &name) const {
183183
}
184184
}
185185

186-
bool Scope::AddCommonBlockUse(const SourceName &name, Symbol &cbSymbol) {
187-
return commonBlockUses_.emplace(name, cbSymbol).second;
186+
bool Scope::AddOrUpdateCommonBlockUse(const SourceName &name, Attrs attrs, Symbol& cbUltimate) {
187+
CHECK(cbUltimate.has<CommonBlockDetails>());
188+
if (Symbol *cb{FindCommonBlockUse(name)}) {
189+
// We already stored UseDetails from the previous encounter with the
190+
// same named COMMON block (perhaps it came from a different module).
191+
// Ensure that we store USE association to the largest COMMON block,
192+
// which is presumably is better defined.
193+
Symbol &cbExistingUltimate{cb->GetUltimate()};
194+
if (cbExistingUltimate.size() < cbUltimate.size()) {
195+
cb->set_details(UseDetails{name, cbUltimate});
196+
}
197+
return true;
198+
} else {
199+
// Make a symbol, but don't add it to the Scope, since it needs to
200+
// be added to the USE-associated COMMON blocks
201+
Symbol &useCB{MakeSymbol(name, attrs, UseDetails{name, cbUltimate})};
202+
return commonBlockUses_.emplace(name, useCB).second;
203+
}
188204
}
189205

190206
bool Scope::AddSubmodule(const SourceName &name, Scope &submodule) {

flang/lib/Semantics/symbol.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,16 @@ bool Symbol::CanReplaceDetails(const Details &details) const {
373373
},
374374
[&](const UseDetails &x) {
375375
const auto *use{this->detailsIf<UseDetails>()};
376-
return use && use->symbol() == x.symbol();
376+
if (!use) {
377+
return false;
378+
} else if (use->symbol().detailsIf<CommonBlockDetails>() &&
379+
x.symbol().detailsIf<CommonBlockDetails>()) {
380+
// Allow to replace UseDetails, if they both refer to COMMON
381+
// symbol with the same name.
382+
return use->symbol().name() == x.symbol().name();
383+
} else {
384+
return use->symbol() == x.symbol();
385+
}
377386
},
378387
[&](const HostAssocDetails &) { return has<HostAssocDetails>(); },
379388
[&](const UserReductionDetails &) {

0 commit comments

Comments
 (0)