FLANG
DataSharingProcessor.h
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 "flang/Lower/AbstractConverter.h"
16#include "flang/Lower/OpenMP.h"
17#include "flang/Lower/OpenMP/Clauses.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#include <variant>
23
24namespace mlir {
25namespace omp {
26struct PrivateClauseOps;
27} // namespace omp
28} // namespace mlir
29
30namespace Fortran {
31namespace lower {
32namespace omp {
33
34class DataSharingProcessor {
35private:
39 struct OMPConstructSymbolVisitor {
40 OMPConstructSymbolVisitor(semantics::SemanticsContext &ctx)
41 : version(ctx.langOptions().OpenMPVersion) {}
42 template <typename T>
43 bool Pre(const T &) {
44 return true;
45 }
46 template <typename T>
47 void Post(const T &) {}
48
49 bool Pre(const parser::OpenMPConstruct &omp) {
50 // Skip constructs that may not have privatizations.
51 if (isOpenMPPrivatizingConstruct(omp, version))
52 constructs.push_back(&omp);
53 return true;
54 }
55
56 void Post(const parser::OpenMPConstruct &omp) {
57 if (isOpenMPPrivatizingConstruct(omp, version))
58 constructs.pop_back();
59 }
60
61 void Post(const parser::Name &name) {
62 auto current = !constructs.empty() ? constructs.back() : ConstructPtr();
63 symDefMap.try_emplace(name.symbol, current);
64 }
65
66 bool Pre(const parser::DeclarationConstruct &decl) {
67 constructs.push_back(&decl);
68 return true;
69 }
70
71 void Post(const parser::DeclarationConstruct &decl) {
72 constructs.pop_back();
73 }
74
77 bool isSymbolDefineBy(const semantics::Symbol *symbol,
78 lower::pft::Evaluation &eval) const;
79
80 // Given a \p symbol, returns true if it is defined by a nested
81 // `DeclarationConstruct`.
82 bool
83 isSymbolDefineByNestedDeclaration(const semantics::Symbol *symbol) const;
84
85 private:
86 using ConstructPtr = std::variant<const parser::OpenMPConstruct *,
89 llvm::DenseMap<semantics::Symbol *, ConstructPtr> symDefMap;
90
91 unsigned version;
92 };
93
94 mlir::OpBuilder::InsertPoint lastPrivIP;
96 // Symbols in private, firstprivate, and/or lastprivate clauses.
97 llvm::SetVector<const semantics::Symbol *> explicitlyPrivatizedSymbols;
98 llvm::SetVector<const semantics::Symbol *> defaultSymbols;
99 llvm::SetVector<const semantics::Symbol *> allPrivatizedSymbols;
100
101 lower::AbstractConverter &converter;
103 fir::FirOpBuilder &firOpBuilder;
104 omp::List<omp::Clause> clauses;
106 bool shouldCollectPreDeterminedSymbols;
107 bool useDelayedPrivatization;
108 llvm::SmallPtrSet<const semantics::Symbol *, 16> mightHaveReadHostSym;
109 lower::SymMap &symTable;
110 bool isTargetPrivatization;
111 OMPConstructSymbolVisitor visitor;
112
113 bool needBarrier();
114 void collectPrivatizedSymbols(
115 std::optional<semantics::Symbol::Flag> flag,
116 const llvm::SetVector<const semantics::Symbol *> &allSymbols,
117 const llvm::SetVector<const semantics::Symbol *> &symbolsInNestedRegions,
118 llvm::SetVector<const semantics::Symbol *> *symbols = nullptr);
119 void
120 collectSymbols(semantics::Symbol::Flag flag,
121 llvm::SetVector<const semantics::Symbol *> *symbols = nullptr);
122 void collectSymbolsInNestedRegions(
123 lower::pft::Evaluation &eval, semantics::Symbol::Flag flag,
124 llvm::SetVector<const semantics::Symbol *> &symbolsInNestedRegions);
125 void collectOmpObjectListSymbol(
126 const omp::ObjectList &objects,
127 llvm::SetVector<const semantics::Symbol *> &symbolSet);
128 void collectSymbolsForPrivatization();
129 void insertBarrier(mlir::omp::PrivateClauseOps *clauseOps);
130 void collectDefaultSymbols();
131 void collectImplicitSymbols();
132 void collectPreDeterminedSymbols();
133 void collectIndirectReferences();
134 void privatize(mlir::omp::PrivateClauseOps *clauseOps,
135 std::optional<llvm::omp::Directive> dir = std::nullopt);
136 void copyLastPrivatize(mlir::Operation *op);
137 void insertLastPrivateCompare(mlir::Operation *op);
138 void cloneSymbol(const semantics::Symbol *sym);
139 void
140 copyFirstPrivateSymbol(const semantics::Symbol *sym,
141 mlir::OpBuilder::InsertPoint *copyAssignIP = nullptr);
142 void copyLastPrivateSymbol(const semantics::Symbol *sym,
143 mlir::OpBuilder::InsertPoint *lastPrivIP);
144 void insertDeallocs();
145
146 static bool isOpenMPPrivatizingConstruct(const parser::OpenMPConstruct &omp,
147 unsigned version);
148 bool isOpenMPPrivatizingEvaluation(const pft::Evaluation &eval) const;
149
150public:
151 DataSharingProcessor(lower::AbstractConverter &converter,
153 const List<Clause> &clauses,
155 bool shouldCollectPreDeterminedSymbols,
156 bool useDelayedPrivatization, lower::SymMap &symTable,
157 bool isTargetPrivatization = false);
158
159 DataSharingProcessor(lower::AbstractConverter &converter,
162 bool useDelayedPrivatization, lower::SymMap &symTable,
163 bool isTargetPrivatization = false);
164
165 // Privatisation is split into two steps.
166 // Step1 performs cloning of all privatisation clauses and copying for
167 // firstprivates. Step1 is performed at the place where process/processStep1
168 // is called. This is usually inside the Operation corresponding to the OpenMP
169 // construct, for looping constructs this is just before the Operation. The
170 // split into two steps was performed basically to be able to call
171 // privatisation for looping constructs before the operation is created since
172 // the bounds of the MLIR OpenMP operation can be privatised.
173 // Step2 performs the copying for lastprivates and requires knowledge of the
174 // MLIR operation to insert the last private update. Step2 adds
175 // dealocation code as well.
176 void processStep1(mlir::omp::PrivateClauseOps *clauseOps = nullptr,
177 std::optional<llvm::omp::Directive> dir = std::nullopt);
178 void processStep2(mlir::Operation *op, bool isLoop);
179
180 void pushLoopIV(mlir::Value iv) { loopIVs.push_back(iv); }
181
182 const llvm::SetVector<const semantics::Symbol *> &
183 getAllSymbolsToPrivatize() const {
184 return allPrivatizedSymbols;
185 }
186
187 llvm::ArrayRef<const semantics::Symbol *> getDelayedPrivSymbols() const {
188 return useDelayedPrivatization
189 ? allPrivatizedSymbols.getArrayRef()
191 }
192
193 void privatizeSymbol(const semantics::Symbol *symToPrivatize,
194 mlir::omp::PrivateClauseOps *clauseOps,
195 std::optional<llvm::omp::Directive> dir = std::nullopt);
196};
197
198} // namespace omp
199} // namespace lower
200} // namespace Fortran
201
202#endif // FORTRAN_LOWER_DATASHARINGPROCESSOR_H
Definition AbstractConverter.h:87
Definition SymbolMap.h:182
Definition semantics.h:67
Definition symbol.h:825
Definition FIRBuilder.h:56
Definition FIRType.h:103
Definition OpenACC.h:20
Definition ParserActions.h:24
Definition bit-population-count.h:20
Definition AbstractConverter.h:32
Definition PFTBuilder.h:221
Definition parse-tree.h:437
Definition parse-tree.h:587
Definition parse-tree.h:5450