Debian Codegraph @

flang/lib/Lower/OpenMP/DataSharingProcessor.h

raw
1 //===-- Lower/OpenMP/DataSharingProcessor.h ---------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10 //
11 //===----------------------------------------------------------------------===//
12 #ifndef FORTRAN_LOWER_DATASHARINGPROCESSOR_H
13 #define FORTRAN_LOWER_DATASHARINGPROCESSOR_H
14
15 #include "Clauses.h"
16 #include "flang/Lower/AbstractConverter.h"
17 #include "flang/Lower/OpenMP.h"
18 #include "flang/Optimizer/Builder/FIRBuilder.h"
19 #include "flang/Parser/parse-tree.h"
20 #include "flang/Semantics/symbol.h"
21 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
22
23 namespace mlir {
24 namespace omp {
25 struct PrivateClauseOps;
26 } // namespace omp
27 } // namespace mlir
28
29 namespace Fortran {
30 namespace lower {
31 namespace omp {
32
33 class DataSharingProcessor {
34 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 // Skip constructs that may not have privatizations.
48 if (!std::holds_alternative<parser::OpenMPCriticalConstruct>(omp.u))
49 currentConstruct = &omp;
50 return true;
51 }
52
53 void Post(const parser::OpenMPConstruct &omp) {
54 currentConstruct = nullptr;
55 }
56
57 void Post(const parser::Name &name) {
58 symDefMap.try_emplace(name.symbol, currentConstruct);
59 }
60
61 const parser::OpenMPConstruct *currentConstruct = nullptr;
62 llvm::DenseMap<semantics::Symbol *, const parser::OpenMPConstruct *>
63 symDefMap;
64
65 /// Given a \p symbol and an \p eval, returns true if eval is the OMP
66 /// construct that defines symbol.
67 bool isSymbolDefineBy(const semantics::Symbol *symbol,
68 lower::pft::Evaluation &eval) const;
69 };
70
71 bool hasLastPrivateOp;
72 mlir::OpBuilder::InsertPoint lastPrivIP;
73 mlir::OpBuilder::InsertPoint insPt;
74 llvm::SmallVector<mlir::Value> loopIVs;
75 // Symbols in private, firstprivate, and/or lastprivate clauses.
76 llvm::SetVector<const semantics::Symbol *> explicitlyPrivatizedSymbols;
77 llvm::SetVector<const semantics::Symbol *> defaultSymbols;
78 llvm::SetVector<const semantics::Symbol *> implicitSymbols;
79 llvm::SetVector<const semantics::Symbol *> preDeterminedSymbols;
80 llvm::SetVector<const semantics::Symbol *> allPrivatizedSymbols;
81
82 llvm::DenseMap<const semantics::Symbol *, mlir::omp::PrivateClauseOp>
83 symToPrivatizer;
84 lower::AbstractConverter &converter;
85 semantics::SemanticsContext &semaCtx;
86 fir::FirOpBuilder &firOpBuilder;
87 omp::List<omp::Clause> clauses;
88 lower::pft::Evaluation &eval;
89 bool shouldCollectPreDeterminedSymbols;
90 bool useDelayedPrivatization;
91 lower::SymMap *symTable;
92 OMPConstructSymbolVisitor visitor;
93
94 bool needBarrier();
95 void collectSymbols(semantics::Symbol::Flag flag,
96 llvm::SetVector<const semantics::Symbol *> &symbols);
97 void collectSymbolsInNestedRegions(
98 lower::pft::Evaluation &eval, semantics::Symbol::Flag flag,
99 llvm::SetVector<const semantics::Symbol *> &symbolsInNestedRegions);
100 void collectOmpObjectListSymbol(
101 const omp::ObjectList &objects,
102 llvm::SetVector<const semantics::Symbol *> &symbolSet);
103 void collectSymbolsForPrivatization();
104 void insertBarrier();
105 void collectDefaultSymbols();
106 void collectImplicitSymbols();
107 void collectPreDeterminedSymbols();
108 void privatize(mlir::omp::PrivateClauseOps *clauseOps);
109 void defaultPrivatize(
110 mlir::omp::PrivateClauseOps *clauseOps,
111 llvm::SmallVectorImpl<const semantics::Symbol *> *privateSyms);
112 void implicitPrivatize(
113 mlir::omp::PrivateClauseOps *clauseOps,
114 llvm::SmallVectorImpl<const semantics::Symbol *> *privateSyms);
115 void doPrivatize(const semantics::Symbol *sym,
116 mlir::omp::PrivateClauseOps *clauseOps);
117 void copyLastPrivatize(mlir::Operation *op);
118 void insertLastPrivateCompare(mlir::Operation *op);
119 void cloneSymbol(const semantics::Symbol *sym);
120 void
121 copyFirstPrivateSymbol(const semantics::Symbol *sym,
122 mlir::OpBuilder::InsertPoint *copyAssignIP = nullptr);
123 void copyLastPrivateSymbol(const semantics::Symbol *sym,
124 mlir::OpBuilder::InsertPoint *lastPrivIP);
125 void insertDeallocs();
126
127 public:
128 DataSharingProcessor(lower::AbstractConverter &converter,
129 semantics::SemanticsContext &semaCtx,
130 const List<Clause> &clauses,
131 lower::pft::Evaluation &eval,
132 bool shouldCollectPreDeterminedSymbols,
133 bool useDelayedPrivatization = false,
134 lower::SymMap *symTable = nullptr);
135
136 // Privatisation is split into two steps.
137 // Step1 performs cloning of all privatisation clauses and copying for
138 // firstprivates. Step1 is performed at the place where process/processStep1
139 // is called. This is usually inside the Operation corresponding to the OpenMP
140 // construct, for looping constructs this is just before the Operation. The
141 // split into two steps was performed basically to be able to call
142 // privatisation for looping constructs before the operation is created since
143 // the bounds of the MLIR OpenMP operation can be privatised.
144 // Step2 performs the copying for lastprivates and requires knowledge of the
145 // MLIR operation to insert the last private update. Step2 adds
146 // dealocation code as well.
147 void processStep1(mlir::omp::PrivateClauseOps *clauseOps = nullptr);
148 void processStep2(mlir::Operation *op, bool isLoop);
149
150 void pushLoopIV(mlir::Value iv) { loopIVs.push_back(iv); }
151
152 const llvm::SetVector<const semantics::Symbol *> &
153 getAllSymbolsToPrivatize() const {
154 return allPrivatizedSymbols;
155 }
156 };
157
158 } // namespace omp
159 } // namespace lower
160 } // namespace Fortran
161
162 #endif // FORTRAN_LOWER_DATASHARINGPROCESSOR_H