3030#include " flang/Semantics/tools.h"
3131#include " llvm/ADT/Sequence.h"
3232#include " llvm/ADT/SmallSet.h"
33+ #include < variant>
3334
3435namespace Fortran {
3536namespace lower {
@@ -44,6 +45,13 @@ bool DataSharingProcessor::OMPConstructSymbolVisitor::isSymbolDefineBy(
4445 [](const auto &functionParserNode) { return false ; }});
4546}
4647
48+ bool DataSharingProcessor::OMPConstructSymbolVisitor::
49+ isSymbolDefineByNestedDeclaration (const semantics::Symbol *symbol) const {
50+ return symDefMap.count (symbol) &&
51+ std::holds_alternative<const parser::DeclarationConstruct *>(
52+ symDefMap.at (symbol));
53+ }
54+
4755static bool isConstructWithTopLevelTarget (lower::pft::Evaluation &eval) {
4856 const auto *ompEval = eval.getIf <parser::OpenMPConstruct>();
4957 if (ompEval) {
@@ -550,17 +558,20 @@ void DataSharingProcessor::collectSymbols(
550558 return false ;
551559 }
552560
553- return sym->test (semantics::Symbol::Flag::OmpImplicit);
554- }
555-
556- if (collectPreDetermined) {
557- // Collect pre-determined symbols only if they are defined by the current
558- // OpenMP evaluation. If `sym` is not defined by the current OpenMP
561+ // Collect implicit symbols only if they are not defined by a nested
562+ // `DeclarationConstruct`. If `sym` is not defined by the current OpenMP
559563 // evaluation then it is defined by a block nested within the OpenMP
560564 // construct. This, in turn, means that the private allocation for the
561565 // symbol will be emitted as part of the nested block and there is no need
562566 // to privatize it within the OpenMP construct.
563- return visitor.isSymbolDefineBy (sym, eval) &&
567+ return !visitor.isSymbolDefineByNestedDeclaration (sym) &&
568+ sym->test (semantics::Symbol::Flag::OmpImplicit);
569+ }
570+
571+ if (collectPreDetermined) {
572+ // Similar to implicit symbols, collect pre-determined symbols only if
573+ // they are not defined by a nested `DeclarationConstruct`
574+ return !visitor.isSymbolDefineByNestedDeclaration (sym) &&
564575 sym->test (semantics::Symbol::Flag::OmpPreDetermined);
565576 }
566577
0 commit comments