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 bool forceHeapAllocationForPrivateDynamicArrays = false;
109 llvm::SmallPtrSet<const semantics::Symbol *, 16> mightHaveReadHostSym;
110 lower::SymMap &symTable;
111 bool isTargetPrivatization;
112 OMPConstructSymbolVisitor visitor;
113
114 bool needBarrier();
115 void collectPrivatizedSymbols(
116 std::optional<semantics::Symbol::Flag> flag,
117 const llvm::SetVector<const semantics::Symbol *> &allSymbols,
118 const llvm::SetVector<const semantics::Symbol *> &symbolsInNestedRegions,
119 llvm::SetVector<const semantics::Symbol *> *symbols = nullptr);
120 void
121 collectSymbols(semantics::Symbol::Flag flag,
122 llvm::SetVector<const semantics::Symbol *> *symbols = nullptr);
123 void collectSymbolsInNestedRegions(
124 lower::pft::Evaluation &eval, semantics::Symbol::Flag flag,
125 llvm::SetVector<const semantics::Symbol *> &symbolsInNestedRegions);
126 void collectOmpObjectListSymbol(
127 const omp::ObjectList &objects,
128 llvm::SetVector<const semantics::Symbol *> &symbolSet);
129 void collectSymbolsForPrivatization();
130 void insertBarrier(mlir::omp::PrivateClauseOps *clauseOps);
131 void collectDefaultSymbols();
132 void collectImplicitSymbols();
133 void collectPreDeterminedSymbols();
134 void collectIndirectReferences();
135 void privatize(mlir::omp::PrivateClauseOps *clauseOps,
136 std::optional<llvm::omp::Directive> dir = std::nullopt);
137 void copyLastPrivatize(mlir::Operation *op);
138 void insertLastPrivateCompare(mlir::Operation *op);
139 void cloneSymbol(const semantics::Symbol *sym);
140 void
141 copyFirstPrivateSymbol(const semantics::Symbol *sym,
142 mlir::OpBuilder::InsertPoint *copyAssignIP = nullptr);
143 void copyLastPrivateSymbol(const semantics::Symbol *sym,
144 mlir::OpBuilder::InsertPoint *lastPrivIP);
145 void insertDeallocs();
146
147 static bool isOpenMPPrivatizingConstruct(const parser::OpenMPConstruct &omp,
148 unsigned version);
149 bool isOpenMPPrivatizingEvaluation(const pft::Evaluation &eval) const;
150
151public:
152 DataSharingProcessor(lower::AbstractConverter &converter,
154 const List<Clause> &clauses,
156 bool shouldCollectPreDeterminedSymbols,
157 bool useDelayedPrivatization, lower::SymMap &symTable,
158 bool isTargetPrivatization = false);
159
160 DataSharingProcessor(lower::AbstractConverter &converter,
163 bool useDelayedPrivatization, lower::SymMap &symTable,
164 bool isTargetPrivatization = false);
165
166 // Privatisation is split into two steps.
167 // Step1 performs cloning of all privatisation clauses and copying for
168 // firstprivates. Step1 is performed at the place where process/processStep1
169 // is called. This is usually inside the Operation corresponding to the OpenMP
170 // construct, for looping constructs this is just before the Operation. The
171 // split into two steps was performed basically to be able to call
172 // privatisation for looping constructs before the operation is created since
173 // the bounds of the MLIR OpenMP operation can be privatised.
174 // Step2 performs the copying for lastprivates and requires knowledge of the
175 // MLIR operation to insert the last private update. Step2 adds
176 // dealocation code as well.
177 void processStep1(mlir::omp::PrivateClauseOps *clauseOps = nullptr,
178 std::optional<llvm::omp::Directive> dir = std::nullopt);
179 void processStep2(mlir::Operation *op, bool isLoop);
180
181 void pushLoopIV(mlir::Value iv) { loopIVs.push_back(iv); }
182
183 void setForceHeapAllocationForPrivateDynamicArrays(bool value = true) {
184 forceHeapAllocationForPrivateDynamicArrays = value;
185 }
186
187 const llvm::SetVector<const semantics::Symbol *> &
188 getAllSymbolsToPrivatize() const {
189 return allPrivatizedSymbols;
190 }
191
192 llvm::ArrayRef<const semantics::Symbol *> getDelayedPrivSymbols() const {
193 return useDelayedPrivatization
194 ? allPrivatizedSymbols.getArrayRef()
196 }
197
198 void privatizeSymbol(const semantics::Symbol *symToPrivatize,
199 mlir::omp::PrivateClauseOps *clauseOps,
200 std::optional<llvm::omp::Directive> dir = std::nullopt);
201};
202
203} // namespace omp
204} // namespace lower
205} // namespace Fortran
206
207#endif // FORTRAN_LOWER_DATASHARINGPROCESSOR_H
Definition AbstractConverter.h:87
Definition SymbolMap.h:182
Definition semantics.h:68
Definition symbol.h:881
Definition FIRBuilder.h:59
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:442
Definition parse-tree.h:592
Definition parse-tree.h:5516