Skip to content

Commit efc37fe

Browse files
committed
[flang][OpenMP] Try to find a proper solution for symbol scoping in DSP
1 parent ad7f194 commit efc37fe

File tree

2 files changed

+68
-37
lines changed

2 files changed

+68
-37
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@
2222
namespace Fortran {
2323
namespace lower {
2424
namespace omp {
25+
bool DataSharingProcessor::OMPConstructSymbolVisitor::isSymbolDefineBy(
26+
const semantics::Symbol *symbol, lower::pft::Evaluation &eval) const {
27+
return eval.visit(
28+
common::visitors{[&](const parser::OpenMPConstruct &functionParserNode) {
29+
return symDefMap.count(symbol) &&
30+
symDefMap.at(symbol) == &functionParserNode;
31+
},
32+
[](const auto &functionParserNode) { return false; }});
33+
}
34+
35+
DataSharingProcessor::DataSharingProcessor(
36+
lower::AbstractConverter &converter, semantics::SemanticsContext &semaCtx,
37+
const List<Clause> &clauses, lower::pft::Evaluation &eval,
38+
bool shouldCollectPreDeterminedSymbols, bool useDelayedPrivatization,
39+
lower::SymMap *symTable)
40+
: hasLastPrivateOp(false), converter(converter), semaCtx(semaCtx),
41+
firOpBuilder(converter.getFirOpBuilder()), clauses(clauses), eval(eval),
42+
shouldCollectPreDeterminedSymbols(shouldCollectPreDeterminedSymbols),
43+
useDelayedPrivatization(useDelayedPrivatization), symTable(symTable),
44+
visitor() {
45+
eval.visit([&](const auto &functionParserNode) {
46+
parser::Walk(functionParserNode, visitor);
47+
});
48+
}
2549

2650
void DataSharingProcessor::processStep1(
2751
mlir::omp::PrivateClauseOps *clauseOps,
@@ -285,38 +309,9 @@ void DataSharingProcessor::collectSymbolsInNestedRegions(
285309
// Recursively look for OpenMP constructs within `nestedEval`'s region
286310
collectSymbolsInNestedRegions(nestedEval, flag, symbolsInNestedRegions);
287311
else {
288-
bool isOrderedConstruct = [&]() {
289-
if (auto *ompConstruct =
290-
nestedEval.getIf<parser::OpenMPConstruct>()) {
291-
if (auto *ompBlockConstruct =
292-
std::get_if<parser::OpenMPBlockConstruct>(
293-
&ompConstruct->u)) {
294-
const auto &beginBlockDirective =
295-
std::get<parser::OmpBeginBlockDirective>(
296-
ompBlockConstruct->t);
297-
const auto origDirective =
298-
std::get<parser::OmpBlockDirective>(beginBlockDirective.t).v;
299-
300-
return origDirective == llvm::omp::Directive::OMPD_ordered;
301-
}
302-
}
303-
304-
return false;
305-
}();
306-
307-
bool isCriticalConstruct = [&]() {
308-
if (auto *ompConstruct =
309-
nestedEval.getIf<parser::OpenMPConstruct>()) {
310-
return std::get_if<parser::OpenMPCriticalConstruct>(
311-
&ompConstruct->u) != nullptr;
312-
}
313-
return false;
314-
}();
315-
316-
if (!isOrderedConstruct && !isCriticalConstruct)
317-
converter.collectSymbolSet(nestedEval, symbolsInNestedRegions, flag,
318-
/*collectSymbols=*/true,
319-
/*collectHostAssociatedSymbols=*/false);
312+
converter.collectSymbolSet(nestedEval, symbolsInNestedRegions, flag,
313+
/*collectSymbols=*/true,
314+
/*collectHostAssociatedSymbols=*/false);
320315
}
321316
}
322317
}
@@ -356,6 +351,11 @@ void DataSharingProcessor::collectSymbols(
356351

357352
llvm::SetVector<const semantics::Symbol *> symbolsInNestedRegions;
358353
collectSymbolsInNestedRegions(eval, flag, symbolsInNestedRegions);
354+
355+
for (auto *symbol : allSymbols)
356+
if (visitor.isSymbolDefineBy(symbol, eval))
357+
symbolsInNestedRegions.remove(symbol);
358+
359359
// Filter-out symbols that must not be privatized.
360360
bool collectImplicit = flag == semantics::Symbol::Flag::OmpImplicit;
361361
bool collectPreDetermined = flag == semantics::Symbol::Flag::OmpPreDetermined;

flang/lib/Lower/OpenMP/DataSharingProcessor.h

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,40 @@ namespace omp {
3232

3333
class DataSharingProcessor {
3434
private:
35+
/// A symbol visitor that keeps track of the currently active OpenMPConstruct
36+
/// at any point in time. This is used to track Symbol definition scopes in
37+
/// order to tell which OMP scope defined vs. references a certain Symbol.
38+
struct OMPConstructSymbolVisitor {
39+
template <typename T>
40+
bool Pre(const T &) {
41+
return true;
42+
}
43+
template <typename T>
44+
void Post(const T &) {}
45+
46+
bool Pre(const parser::OpenMPConstruct &omp) {
47+
currentConstruct = &omp;
48+
return true;
49+
}
50+
51+
void Post(const parser::OpenMPConstruct &omp) {
52+
currentConstruct = nullptr;
53+
}
54+
55+
void Post(const parser::Name &name) {
56+
symDefMap.try_emplace(name.symbol, currentConstruct);
57+
}
58+
59+
const parser::OpenMPConstruct *currentConstruct = nullptr;
60+
llvm::DenseMap<semantics::Symbol *, const parser::OpenMPConstruct *>
61+
symDefMap;
62+
63+
/// Given a \p symbol and an \p eval, returns true if eval is the OMP
64+
/// construct that defines symbol.
65+
bool isSymbolDefineBy(const semantics::Symbol *symbol,
66+
lower::pft::Evaluation &eval) const;
67+
};
68+
3569
bool hasLastPrivateOp;
3670
mlir::OpBuilder::InsertPoint lastPrivIP;
3771
mlir::OpBuilder::InsertPoint insPt;
@@ -53,6 +87,7 @@ class DataSharingProcessor {
5387
bool shouldCollectPreDeterminedSymbols;
5488
bool useDelayedPrivatization;
5589
lower::SymMap *symTable;
90+
OMPConstructSymbolVisitor visitor;
5691

5792
bool needBarrier();
5893
void collectSymbols(semantics::Symbol::Flag flag,
@@ -97,11 +132,7 @@ class DataSharingProcessor {
97132
lower::pft::Evaluation &eval,
98133
bool shouldCollectPreDeterminedSymbols,
99134
bool useDelayedPrivatization = false,
100-
lower::SymMap *symTable = nullptr)
101-
: hasLastPrivateOp(false), converter(converter), semaCtx(semaCtx),
102-
firOpBuilder(converter.getFirOpBuilder()), clauses(clauses), eval(eval),
103-
shouldCollectPreDeterminedSymbols(shouldCollectPreDeterminedSymbols),
104-
useDelayedPrivatization(useDelayedPrivatization), symTable(symTable) {}
135+
lower::SymMap *symTable = nullptr);
105136

106137
// Privatisation is split into two steps.
107138
// Step1 performs cloning of all privatisation clauses and copying for

0 commit comments

Comments
 (0)