Debian Codegraph @

flang/lib/Lower/OpenMP/Clauses.h

raw
1 //===-- Clauses.h -- OpenMP clause handling -------------------------------===//
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 #ifndef FORTRAN_LOWER_OPENMP_CLAUSES_H
9 #define FORTRAN_LOWER_OPENMP_CLAUSES_H
10
11 #include "flang/Evaluate/expression.h"
12 #include "flang/Parser/parse-tree.h"
13 #include "flang/Semantics/expression.h"
14 #include "flang/Semantics/semantics.h"
15 #include "flang/Semantics/symbol.h"
16
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/Frontend/OpenMP/ClauseT.h"
19
20 #include <optional>
21 #include <type_traits>
22 #include <utility>
23
24 namespace Fortran::semantics {
25 class Symbol;
26 }
27
28 namespace Fortran::lower::omp {
29 using namespace Fortran;
30 using SomeExpr = semantics::SomeExpr;
31 using MaybeExpr = semantics::MaybeExpr;
32
33 // evaluate::SomeType doesn't provide == operation. It's not really used in
34 // flang's clauses so far, so a trivial implementation is sufficient.
35 struct TypeTy : public evaluate::SomeType {
36 bool operator==(const TypeTy &t) const { return true; }
37 };
38
39 template <typename ExprTy>
40 struct IdTyTemplate {
41 // "symbol" is always non-null for id's of actual objects.
42 Fortran::semantics::Symbol *symbol;
43 std::optional<ExprTy> designator;
44
45 bool operator==(const IdTyTemplate &other) const {
46 // If symbols are different, then the objects are different.
47 if (symbol != other.symbol)
48 return false;
49 if (symbol == nullptr)
50 return true;
51 // Equal symbols don't necessarily indicate identical objects,
52 // for example, a derived object component may use a single symbol,
53 // which will refer to different objects for different designators,
54 // e.g. a%c and b%c.
55 return designator == other.designator;
56 }
57
58 operator bool() const { return symbol != nullptr; }
59 };
60
61 using ExprTy = SomeExpr;
62
63 template <typename T>
64 using List = tomp::ListT<T>;
65 } // namespace Fortran::lower::omp
66
67 // Specialization of the ObjectT template
68 namespace tomp::type {
69 template <>
70 struct ObjectT<Fortran::lower::omp::IdTyTemplate<Fortran::lower::omp::ExprTy>,
71 Fortran::lower::omp::ExprTy> {
72 using IdTy = Fortran::lower::omp::IdTyTemplate<Fortran::lower::omp::ExprTy>;
73 using ExprTy = Fortran::lower::omp::ExprTy;
74
75 IdTy id() const { return identity; }
76 Fortran::semantics::Symbol *sym() const { return identity.symbol; }
77 const std::optional<ExprTy> &ref() const { return identity.designator; }
78
79 IdTy identity;
80 };
81 } // namespace tomp::type
82
83 namespace Fortran::lower::omp {
84 using IdTy = IdTyTemplate<ExprTy>;
85 }
86
87 namespace std {
88 template <>
89 struct hash<Fortran::lower::omp::IdTy> {
90 size_t operator()(const Fortran::lower::omp::IdTy &id) const {
91 return static_cast<size_t>(reinterpret_cast<uintptr_t>(id.symbol));
92 }
93 };
94 } // namespace std
95
96 namespace Fortran::lower::omp {
97 using Object = tomp::ObjectT<IdTy, ExprTy>;
98 using ObjectList = tomp::ObjectListT<IdTy, ExprTy>;
99
100 Object makeObject(const parser::OmpObject &object,
101 semantics::SemanticsContext &semaCtx);
102 Object makeObject(const parser::Name &name,
103 semantics::SemanticsContext &semaCtx);
104 Object makeObject(const parser::Designator &dsg,
105 semantics::SemanticsContext &semaCtx);
106 Object makeObject(const parser::StructureComponent &comp,
107 semantics::SemanticsContext &semaCtx);
108
109 inline auto makeObjectFn(semantics::SemanticsContext &semaCtx) {
110 return [&](auto &&s) { return makeObject(s, semaCtx); };
111 }
112
113 template <typename T>
114 SomeExpr makeExpr(T &&pftExpr, semantics::SemanticsContext &semaCtx) {
115 auto maybeExpr = evaluate::ExpressionAnalyzer(semaCtx).Analyze(pftExpr);
116 assert(maybeExpr);
117 return std::move(*maybeExpr);
118 }
119
120 inline auto makeExprFn(semantics::SemanticsContext &semaCtx) {
121 return [&](auto &&s) { return makeExpr(s, semaCtx); };
122 }
123
124 template <
125 typename ContainerTy, typename FunctionTy,
126 typename ElemTy = typename llvm::remove_cvref_t<ContainerTy>::value_type,
127 typename ResultTy = std::invoke_result_t<FunctionTy, ElemTy>>
128 List<ResultTy> makeList(ContainerTy &&container, FunctionTy &&func) {
129 List<ResultTy> v;
130 llvm::transform(container, std::back_inserter(v), func);
131 return v;
132 }
133
134 inline ObjectList makeObjects(const parser::OmpObjectList &objects,
135 semantics::SemanticsContext &semaCtx) {
136 return makeList(objects.v, makeObjectFn(semaCtx));
137 }
138
139 template <typename FuncTy, //
140 typename ArgTy, //
141 typename ResultTy = std::invoke_result_t<FuncTy, ArgTy>>
142 std::optional<ResultTy> maybeApply(FuncTy &&func,
143 const std::optional<ArgTy> &arg) {
144 if (!arg)
145 return std::nullopt;
146 return std::move(func(*arg));
147 }
148
149 std::optional<Object> getBaseObject(const Object &object,
150 semantics::SemanticsContext &semaCtx);
151
152 namespace clause {
153 using DefinedOperator = tomp::type::DefinedOperatorT<IdTy, ExprTy>;
154 using ProcedureDesignator = tomp::type::ProcedureDesignatorT<IdTy, ExprTy>;
155 using ReductionOperator = tomp::type::ReductionIdentifierT<IdTy, ExprTy>;
156
157 // "Requires" clauses are handled early on, and the aggregated information
158 // is stored in the Symbol details of modules, programs, and subprograms.
159 // These clauses are still handled here to cover all alternatives in the
160 // main clause variant.
161
162 using AcqRel = tomp::clause::AcqRelT<TypeTy, IdTy, ExprTy>;
163 using Acquire = tomp::clause::AcquireT<TypeTy, IdTy, ExprTy>;
164 using AdjustArgs = tomp::clause::AdjustArgsT<TypeTy, IdTy, ExprTy>;
165 using Affinity = tomp::clause::AffinityT<TypeTy, IdTy, ExprTy>;
166 using Aligned = tomp::clause::AlignedT<TypeTy, IdTy, ExprTy>;
167 using Align = tomp::clause::AlignT<TypeTy, IdTy, ExprTy>;
168 using Allocate = tomp::clause::AllocateT<TypeTy, IdTy, ExprTy>;
169 using Allocator = tomp::clause::AllocatorT<TypeTy, IdTy, ExprTy>;
170 using AppendArgs = tomp::clause::AppendArgsT<TypeTy, IdTy, ExprTy>;
171 using AtomicDefaultMemOrder =
172 tomp::clause::AtomicDefaultMemOrderT<TypeTy, IdTy, ExprTy>;
173 using At = tomp::clause::AtT<TypeTy, IdTy, ExprTy>;
174 using Bind = tomp::clause::BindT<TypeTy, IdTy, ExprTy>;
175 using Capture = tomp::clause::CaptureT<TypeTy, IdTy, ExprTy>;
176 using Collapse = tomp::clause::CollapseT<TypeTy, IdTy, ExprTy>;
177 using Compare = tomp::clause::CompareT<TypeTy, IdTy, ExprTy>;
178 using Copyin = tomp::clause::CopyinT<TypeTy, IdTy, ExprTy>;
179 using Copyprivate = tomp::clause::CopyprivateT<TypeTy, IdTy, ExprTy>;
180 using Defaultmap = tomp::clause::DefaultmapT<TypeTy, IdTy, ExprTy>;
181 using Default = tomp::clause::DefaultT<TypeTy, IdTy, ExprTy>;
182 using Depend = tomp::clause::DependT<TypeTy, IdTy, ExprTy>;
183 using Destroy = tomp::clause::DestroyT<TypeTy, IdTy, ExprTy>;
184 using Detach = tomp::clause::DetachT<TypeTy, IdTy, ExprTy>;
185 using Device = tomp::clause::DeviceT<TypeTy, IdTy, ExprTy>;
186 using DeviceType = tomp::clause::DeviceTypeT<TypeTy, IdTy, ExprTy>;
187 using DistSchedule = tomp::clause::DistScheduleT<TypeTy, IdTy, ExprTy>;
188 using Doacross = tomp::clause::DoacrossT<TypeTy, IdTy, ExprTy>;
189 using DynamicAllocators =
190 tomp::clause::DynamicAllocatorsT<TypeTy, IdTy, ExprTy>;
191 using Enter = tomp::clause::EnterT<TypeTy, IdTy, ExprTy>;
192 using Exclusive = tomp::clause::ExclusiveT<TypeTy, IdTy, ExprTy>;
193 using Fail = tomp::clause::FailT<TypeTy, IdTy, ExprTy>;
194 using Filter = tomp::clause::FilterT<TypeTy, IdTy, ExprTy>;
195 using Final = tomp::clause::FinalT<TypeTy, IdTy, ExprTy>;
196 using Firstprivate = tomp::clause::FirstprivateT<TypeTy, IdTy, ExprTy>;
197 using From = tomp::clause::FromT<TypeTy, IdTy, ExprTy>;
198 using Full = tomp::clause::FullT<TypeTy, IdTy, ExprTy>;
199 using Grainsize = tomp::clause::GrainsizeT<TypeTy, IdTy, ExprTy>;
200 using HasDeviceAddr = tomp::clause::HasDeviceAddrT<TypeTy, IdTy, ExprTy>;
201 using Hint = tomp::clause::HintT<TypeTy, IdTy, ExprTy>;
202 using If = tomp::clause::IfT<TypeTy, IdTy, ExprTy>;
203 using Inbranch = tomp::clause::InbranchT<TypeTy, IdTy, ExprTy>;
204 using Inclusive = tomp::clause::InclusiveT<TypeTy, IdTy, ExprTy>;
205 using Indirect = tomp::clause::IndirectT<TypeTy, IdTy, ExprTy>;
206 using Init = tomp::clause::InitT<TypeTy, IdTy, ExprTy>;
207 using InReduction = tomp::clause::InReductionT<TypeTy, IdTy, ExprTy>;
208 using IsDevicePtr = tomp::clause::IsDevicePtrT<TypeTy, IdTy, ExprTy>;
209 using Lastprivate = tomp::clause::LastprivateT<TypeTy, IdTy, ExprTy>;
210 using Linear = tomp::clause::LinearT<TypeTy, IdTy, ExprTy>;
211 using Link = tomp::clause::LinkT<TypeTy, IdTy, ExprTy>;
212 using Map = tomp::clause::MapT<TypeTy, IdTy, ExprTy>;
213 using Match = tomp::clause::MatchT<TypeTy, IdTy, ExprTy>;
214 using Mergeable = tomp::clause::MergeableT<TypeTy, IdTy, ExprTy>;
215 using Message = tomp::clause::MessageT<TypeTy, IdTy, ExprTy>;
216 using Nocontext = tomp::clause::NocontextT<TypeTy, IdTy, ExprTy>;
217 using Nogroup = tomp::clause::NogroupT<TypeTy, IdTy, ExprTy>;
218 using Nontemporal = tomp::clause::NontemporalT<TypeTy, IdTy, ExprTy>;
219 using Notinbranch = tomp::clause::NotinbranchT<TypeTy, IdTy, ExprTy>;
220 using Novariants = tomp::clause::NovariantsT<TypeTy, IdTy, ExprTy>;
221 using Nowait = tomp::clause::NowaitT<TypeTy, IdTy, ExprTy>;
222 using NumTasks = tomp::clause::NumTasksT<TypeTy, IdTy, ExprTy>;
223 using NumTeams = tomp::clause::NumTeamsT<TypeTy, IdTy, ExprTy>;
224 using NumThreads = tomp::clause::NumThreadsT<TypeTy, IdTy, ExprTy>;
225 using OmpxAttribute = tomp::clause::OmpxAttributeT<TypeTy, IdTy, ExprTy>;
226 using OmpxBare = tomp::clause::OmpxBareT<TypeTy, IdTy, ExprTy>;
227 using OmpxDynCgroupMem = tomp::clause::OmpxDynCgroupMemT<TypeTy, IdTy, ExprTy>;
228 using Ordered = tomp::clause::OrderedT<TypeTy, IdTy, ExprTy>;
229 using Order = tomp::clause::OrderT<TypeTy, IdTy, ExprTy>;
230 using Partial = tomp::clause::PartialT<TypeTy, IdTy, ExprTy>;
231 using Priority = tomp::clause::PriorityT<TypeTy, IdTy, ExprTy>;
232 using Private = tomp::clause::PrivateT<TypeTy, IdTy, ExprTy>;
233 using ProcBind = tomp::clause::ProcBindT<TypeTy, IdTy, ExprTy>;
234 using Read = tomp::clause::ReadT<TypeTy, IdTy, ExprTy>;
235 using Reduction = tomp::clause::ReductionT<TypeTy, IdTy, ExprTy>;
236 using Relaxed = tomp::clause::RelaxedT<TypeTy, IdTy, ExprTy>;
237 using Release = tomp::clause::ReleaseT<TypeTy, IdTy, ExprTy>;
238 using ReverseOffload = tomp::clause::ReverseOffloadT<TypeTy, IdTy, ExprTy>;
239 using Safelen = tomp::clause::SafelenT<TypeTy, IdTy, ExprTy>;
240 using Schedule = tomp::clause::ScheduleT<TypeTy, IdTy, ExprTy>;
241 using SeqCst = tomp::clause::SeqCstT<TypeTy, IdTy, ExprTy>;
242 using Severity = tomp::clause::SeverityT<TypeTy, IdTy, ExprTy>;
243 using Shared = tomp::clause::SharedT<TypeTy, IdTy, ExprTy>;
244 using Simdlen = tomp::clause::SimdlenT<TypeTy, IdTy, ExprTy>;
245 using Simd = tomp::clause::SimdT<TypeTy, IdTy, ExprTy>;
246 using Sizes = tomp::clause::SizesT<TypeTy, IdTy, ExprTy>;
247 using TaskReduction = tomp::clause::TaskReductionT<TypeTy, IdTy, ExprTy>;
248 using ThreadLimit = tomp::clause::ThreadLimitT<TypeTy, IdTy, ExprTy>;
249 using Threads = tomp::clause::ThreadsT<TypeTy, IdTy, ExprTy>;
250 using To = tomp::clause::ToT<TypeTy, IdTy, ExprTy>;
251 using UnifiedAddress = tomp::clause::UnifiedAddressT<TypeTy, IdTy, ExprTy>;
252 using UnifiedSharedMemory =
253 tomp::clause::UnifiedSharedMemoryT<TypeTy, IdTy, ExprTy>;
254 using Uniform = tomp::clause::UniformT<TypeTy, IdTy, ExprTy>;
255 using Unknown = tomp::clause::UnknownT<TypeTy, IdTy, ExprTy>;
256 using Untied = tomp::clause::UntiedT<TypeTy, IdTy, ExprTy>;
257 using Update = tomp::clause::UpdateT<TypeTy, IdTy, ExprTy>;
258 using UseDeviceAddr = tomp::clause::UseDeviceAddrT<TypeTy, IdTy, ExprTy>;
259 using UseDevicePtr = tomp::clause::UseDevicePtrT<TypeTy, IdTy, ExprTy>;
260 using UsesAllocators = tomp::clause::UsesAllocatorsT<TypeTy, IdTy, ExprTy>;
261 using Use = tomp::clause::UseT<TypeTy, IdTy, ExprTy>;
262 using Weak = tomp::clause::WeakT<TypeTy, IdTy, ExprTy>;
263 using When = tomp::clause::WhenT<TypeTy, IdTy, ExprTy>;
264 using Write = tomp::clause::WriteT<TypeTy, IdTy, ExprTy>;
265 } // namespace clause
266
267 using tomp::type::operator==;
268
269 struct CancellationConstructType {
270 using EmptyTrait = std::true_type;
271 };
272 struct Depobj {
273 using EmptyTrait = std::true_type;
274 };
275 struct Flush {
276 using EmptyTrait = std::true_type;
277 };
278 struct MemoryOrder {
279 using EmptyTrait = std::true_type;
280 };
281 struct Threadprivate {
282 using EmptyTrait = std::true_type;
283 };
284
285 using ClauseBase = tomp::ClauseT<TypeTy, IdTy, ExprTy,
286 // Extras...
287 CancellationConstructType, Depobj, Flush,
288 MemoryOrder, Threadprivate>;
289
290 struct Clause : public ClauseBase {
291 Clause(ClauseBase &&base, const parser::CharBlock source = {})
292 : ClauseBase(std::move(base)), source(source) {}
293 // "source" will be ignored by tomp::type::operator==.
294 parser::CharBlock source;
295 };
296
297 template <typename Specific>
298 Clause makeClause(llvm::omp::Clause id, Specific &&specific,
299 parser::CharBlock source = {}) {
300 return Clause(typename Clause::BaseT{id, specific}, source);
301 }
302
303 Clause makeClause(const parser::OmpClause &cls,
304 semantics::SemanticsContext &semaCtx);
305
306 List<Clause> makeClauses(const parser::OmpClauseList &clauses,
307 semantics::SemanticsContext &semaCtx);
308
309 bool transferLocations(const List<Clause> &from, List<Clause> &to);
310 } // namespace Fortran::lower::omp
311
312 #endif // FORTRAN_LOWER_OPENMP_CLAUSES_H