FLANG
Clauses.h
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/Evaluate/type.h"
13#include "flang/Parser/parse-tree.h"
14#include "flang/Semantics/expression.h"
15#include "flang/Semantics/semantics.h"
16#include "flang/Semantics/symbol.h"
17
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/Frontend/OpenMP/ClauseT.h"
20#include "llvm/Frontend/OpenMP/OMP.h.inc"
21
22#include <optional>
23#include <type_traits>
24#include <utility>
25
26namespace Fortran::semantics {
27class Symbol;
28}
29
30namespace Fortran::lower::omp {
31using namespace Fortran;
32using SomeExpr = semantics::SomeExpr;
33using MaybeExpr = semantics::MaybeExpr;
34using TypeTy = evaluate::DynamicType;
35
36template <typename ExprTy>
38 // "symbol" is always non-null for id's of actual objects.
40 std::optional<ExprTy> designator;
41
42 bool operator==(const IdTyTemplate &other) const {
43 // If symbols are different, then the objects are different.
44 if (symbol != other.symbol)
45 return false;
46 if (symbol == nullptr)
47 return true;
48 // Equal symbols don't necessarily indicate identical objects,
49 // for example, a derived object component may use a single symbol,
50 // which will refer to different objects for different designators,
51 // e.g. a%c and b%c.
52 return designator == other.designator;
53 }
54
55 // Defining an "ordering" which allows types derived from this to be
56 // utilised in maps and other containers that require comparison
57 // operators for ordering
58 bool operator<(const IdTyTemplate &other) const {
59 return symbol < other.symbol;
60 }
61
62 operator bool() const { return symbol != nullptr; }
63};
64
65using ExprTy = SomeExpr;
66
67template <typename T>
68using List = tomp::ListT<T>;
69} // namespace Fortran::lower::omp
70
71// Specialization of the ObjectT template
72namespace tomp::type {
73template <>
74struct ObjectT<Fortran::lower::omp::IdTyTemplate<Fortran::lower::omp::ExprTy>,
75 Fortran::lower::omp::ExprTy> {
77 using ExprTy = Fortran::lower::omp::ExprTy;
78
79 IdTy id() const { return identity; }
80 Fortran::semantics::Symbol *sym() const { return identity.symbol; }
81 const std::optional<ExprTy> &ref() const { return identity.designator; }
82
83 bool operator<(const ObjectT<IdTy, ExprTy> &other) const {
84 return identity < other.identity;
85 }
86
87 IdTy identity;
88};
89} // namespace tomp::type
90
91namespace Fortran::lower::omp {
92using IdTy = IdTyTemplate<ExprTy>;
93}
94
95namespace std {
96template <>
97struct hash<Fortran::lower::omp::IdTy> {
98 size_t operator()(const Fortran::lower::omp::IdTy &id) const {
99 return static_cast<size_t>(reinterpret_cast<uintptr_t>(id.symbol));
100 }
101};
102} // namespace std
103
104namespace Fortran::lower::omp {
105using Object = tomp::ObjectT<IdTy, ExprTy>;
106using ObjectList = tomp::ObjectListT<IdTy, ExprTy>;
107using StylizedInstance = tomp::type::StylizedInstanceT<IdTy, ExprTy>;
108
109Object makeObject(const parser::OmpObject &object,
111Object makeObject(const parser::Name &name,
113Object makeObject(const parser::Designator &dsg,
115Object makeObject(const parser::StructureComponent &comp,
117Object makeObject(const parser::EntityDecl &decl,
119
120inline auto makeObjectFn(semantics::SemanticsContext &semaCtx) {
121 return [&](auto &&s) { return makeObject(s, semaCtx); };
122}
123
124template <typename T>
125SomeExpr makeExpr(T &&pftExpr, semantics::SemanticsContext &semaCtx) {
126 auto maybeExpr = evaluate::ExpressionAnalyzer(semaCtx).Analyze(pftExpr);
127 assert(maybeExpr);
128 return std::move(*maybeExpr);
129}
130
131inline auto makeExprFn(semantics::SemanticsContext &semaCtx) {
132 return [&](auto &&s) { return makeExpr(s, semaCtx); };
133}
134
135template <
136 typename ContainerTy, typename FunctionTy,
137 typename ElemTy = typename llvm::remove_cvref_t<ContainerTy>::value_type,
138 typename ResultTy = std::invoke_result_t<FunctionTy, ElemTy>>
139List<ResultTy> makeList(ContainerTy &&container, FunctionTy &&func) {
140 List<ResultTy> v;
141 llvm::transform(container, std::back_inserter(v), func);
142 return v;
143}
144
145inline ObjectList makeObjects(const parser::OmpObjectList &objects,
146 semantics::SemanticsContext &semaCtx) {
147 return makeList(objects.v, makeObjectFn(semaCtx));
148}
149
150ObjectList makeObjects(const parser::OmpArgumentList &objects,
151 semantics::SemanticsContext &semaCtx);
152
153template <typename FuncTy, //
154 typename ArgTy, //
155 typename ResultTy = std::invoke_result_t<FuncTy, ArgTy>>
156std::optional<ResultTy> maybeApply(FuncTy &&func,
157 const std::optional<ArgTy> &arg) {
158 if (!arg)
159 return std::nullopt;
160 return func(*arg);
161}
162
163template < //
164 typename FuncTy, //
165 typename ArgTy, //
166 typename ResultTy =
167 std::invoke_result_t<FuncTy, decltype(std::declval<ArgTy>().v)>>
168std::optional<ResultTy> maybeApplyToV(FuncTy &&func, const ArgTy *arg) {
169 if (!arg)
170 return std::nullopt;
171 return func(arg->v);
172}
173
174std::optional<Object> getBaseObject(const Object &object,
175 semantics::SemanticsContext &semaCtx);
176
177StylizedInstance makeStylizedInstance(const parser::OmpStylizedInstance &inp,
178 semantics::SemanticsContext &semaCtx);
179
180namespace clause {
181using Range = tomp::type::RangeT<ExprTy>;
182using Mapper = tomp::type::MapperT<IdTy, ExprTy>;
183using Iterator = tomp::type::IteratorT<TypeTy, IdTy, ExprTy>;
184using IteratorSpecifier = tomp::type::IteratorSpecifierT<TypeTy, IdTy, ExprTy>;
185using DefinedOperator = tomp::type::DefinedOperatorT<IdTy, ExprTy>;
186using ProcedureDesignator = tomp::type::ProcedureDesignatorT<IdTy, ExprTy>;
187using ReductionOperator = tomp::type::ReductionIdentifierT<IdTy, ExprTy>;
188using ReductionOperatorList = List<ReductionOperator>;
189using DependenceType = tomp::type::DependenceType;
190using Prescriptiveness = tomp::type::Prescriptiveness;
191
192// "Requires" clauses are handled early on, and the aggregated information
193// is stored in the Symbol details of modules, programs, and subprograms.
194// These clauses are still handled here to cover all alternatives in the
195// main clause variant.
196
197using Absent = tomp::clause::AbsentT<TypeTy, IdTy, ExprTy>;
198using AcqRel = tomp::clause::AcqRelT<TypeTy, IdTy, ExprTy>;
199using Acquire = tomp::clause::AcquireT<TypeTy, IdTy, ExprTy>;
200using AdjustArgs = tomp::clause::AdjustArgsT<TypeTy, IdTy, ExprTy>;
201using Affinity = tomp::clause::AffinityT<TypeTy, IdTy, ExprTy>;
202using Aligned = tomp::clause::AlignedT<TypeTy, IdTy, ExprTy>;
203using Align = tomp::clause::AlignT<TypeTy, IdTy, ExprTy>;
204using Allocate = tomp::clause::AllocateT<TypeTy, IdTy, ExprTy>;
205using Allocator = tomp::clause::AllocatorT<TypeTy, IdTy, ExprTy>;
206using AppendArgs = tomp::clause::AppendArgsT<TypeTy, IdTy, ExprTy>;
207using Apply = tomp::clause::ApplyT<TypeTy, IdTy, ExprTy>;
208using At = tomp::clause::AtT<TypeTy, IdTy, ExprTy>;
209using AtomicDefaultMemOrder =
210 tomp::clause::AtomicDefaultMemOrderT<TypeTy, IdTy, ExprTy>;
211using Bind = tomp::clause::BindT<TypeTy, IdTy, ExprTy>;
212using Capture = tomp::clause::CaptureT<TypeTy, IdTy, ExprTy>;
213using Collapse = tomp::clause::CollapseT<TypeTy, IdTy, ExprTy>;
214using Collector = tomp::clause::CollectorT<TypeTy, IdTy, ExprTy>;
215using Combiner = tomp::clause::CombinerT<TypeTy, IdTy, ExprTy>;
216using Compare = tomp::clause::CompareT<TypeTy, IdTy, ExprTy>;
217using Contains = tomp::clause::ContainsT<TypeTy, IdTy, ExprTy>;
218using Copyin = tomp::clause::CopyinT<TypeTy, IdTy, ExprTy>;
219using Copyprivate = tomp::clause::CopyprivateT<TypeTy, IdTy, ExprTy>;
220using Counts = tomp::clause::CountsT<TypeTy, IdTy, ExprTy>;
221using Default = tomp::clause::DefaultT<TypeTy, IdTy, ExprTy>;
222using Defaultmap = tomp::clause::DefaultmapT<TypeTy, IdTy, ExprTy>;
223using Depend = tomp::clause::DependT<TypeTy, IdTy, ExprTy>;
224using Depth = tomp::clause::DepthT<TypeTy, IdTy, ExprTy>;
225using Destroy = tomp::clause::DestroyT<TypeTy, IdTy, ExprTy>;
226using Detach = tomp::clause::DetachT<TypeTy, IdTy, ExprTy>;
227using Device = tomp::clause::DeviceT<TypeTy, IdTy, ExprTy>;
228using DeviceSafesync = tomp::clause::DeviceSafesyncT<TypeTy, IdTy, ExprTy>;
229using DeviceType = tomp::clause::DeviceTypeT<TypeTy, IdTy, ExprTy>;
230using DistSchedule = tomp::clause::DistScheduleT<TypeTy, IdTy, ExprTy>;
231using Doacross = tomp::clause::DoacrossT<TypeTy, IdTy, ExprTy>;
232using DynamicAllocators =
233 tomp::clause::DynamicAllocatorsT<TypeTy, IdTy, ExprTy>;
234using DynGroupprivate = tomp::clause::DynGroupprivateT<TypeTy, IdTy, ExprTy>;
235using Enter = tomp::clause::EnterT<TypeTy, IdTy, ExprTy>;
236using Exclusive = tomp::clause::ExclusiveT<TypeTy, IdTy, ExprTy>;
237using Fail = tomp::clause::FailT<TypeTy, IdTy, ExprTy>;
238using Filter = tomp::clause::FilterT<TypeTy, IdTy, ExprTy>;
239using Final = tomp::clause::FinalT<TypeTy, IdTy, ExprTy>;
240using Firstprivate = tomp::clause::FirstprivateT<TypeTy, IdTy, ExprTy>;
241using From = tomp::clause::FromT<TypeTy, IdTy, ExprTy>;
242using Full = tomp::clause::FullT<TypeTy, IdTy, ExprTy>;
243using Grainsize = tomp::clause::GrainsizeT<TypeTy, IdTy, ExprTy>;
244using GraphId = tomp::clause::GraphIdT<TypeTy, IdTy, ExprTy>;
245using GraphReset = tomp::clause::GraphResetT<TypeTy, IdTy, ExprTy>;
246using HasDeviceAddr = tomp::clause::HasDeviceAddrT<TypeTy, IdTy, ExprTy>;
247using Hint = tomp::clause::HintT<TypeTy, IdTy, ExprTy>;
248using Holds = tomp::clause::HoldsT<TypeTy, IdTy, ExprTy>;
249using If = tomp::clause::IfT<TypeTy, IdTy, ExprTy>;
250using Inbranch = tomp::clause::InbranchT<TypeTy, IdTy, ExprTy>;
251using Inclusive = tomp::clause::InclusiveT<TypeTy, IdTy, ExprTy>;
252using Indirect = tomp::clause::IndirectT<TypeTy, IdTy, ExprTy>;
253using Induction = tomp::clause::InductionT<TypeTy, IdTy, ExprTy>;
254using Inductor = tomp::clause::InductorT<TypeTy, IdTy, ExprTy>;
255using Init = tomp::clause::InitT<TypeTy, IdTy, ExprTy>;
256using InitComplete = tomp::clause::InitCompleteT<TypeTy, IdTy, ExprTy>;
257using Initializer = tomp::clause::InitializerT<TypeTy, IdTy, ExprTy>;
258using InReduction = tomp::clause::InReductionT<TypeTy, IdTy, ExprTy>;
259using Interop = tomp::clause::InteropT<TypeTy, IdTy, ExprTy>;
260using IsDevicePtr = tomp::clause::IsDevicePtrT<TypeTy, IdTy, ExprTy>;
261using Lastprivate = tomp::clause::LastprivateT<TypeTy, IdTy, ExprTy>;
262using Linear = tomp::clause::LinearT<TypeTy, IdTy, ExprTy>;
263using Link = tomp::clause::LinkT<TypeTy, IdTy, ExprTy>;
264using Local = tomp::clause::LocalT<TypeTy, IdTy, ExprTy>;
265using Looprange = tomp::clause::LooprangeT<TypeTy, IdTy, ExprTy>;
266using Map = tomp::clause::MapT<TypeTy, IdTy, ExprTy>;
267using Match = tomp::clause::MatchT<TypeTy, IdTy, ExprTy>;
268using Memscope = tomp::clause::MemscopeT<TypeTy, IdTy, ExprTy>;
269using Mergeable = tomp::clause::MergeableT<TypeTy, IdTy, ExprTy>;
270using Message = tomp::clause::MessageT<TypeTy, IdTy, ExprTy>;
271using Nocontext = tomp::clause::NocontextT<TypeTy, IdTy, ExprTy>;
272using Nogroup = tomp::clause::NogroupT<TypeTy, IdTy, ExprTy>;
273using Nontemporal = tomp::clause::NontemporalT<TypeTy, IdTy, ExprTy>;
274using NoOpenmp = tomp::clause::NoOpenmpT<TypeTy, IdTy, ExprTy>;
275using NoOpenmpConstructs =
276 tomp::clause::NoOpenmpConstructsT<TypeTy, IdTy, ExprTy>;
277using NoOpenmpRoutines = tomp::clause::NoOpenmpRoutinesT<TypeTy, IdTy, ExprTy>;
278using NoParallelism = tomp::clause::NoParallelismT<TypeTy, IdTy, ExprTy>;
279using Notinbranch = tomp::clause::NotinbranchT<TypeTy, IdTy, ExprTy>;
280using Novariants = tomp::clause::NovariantsT<TypeTy, IdTy, ExprTy>;
281using Nowait = tomp::clause::NowaitT<TypeTy, IdTy, ExprTy>;
282using NumTasks = tomp::clause::NumTasksT<TypeTy, IdTy, ExprTy>;
283using NumTeams = tomp::clause::NumTeamsT<TypeTy, IdTy, ExprTy>;
284using NumThreads = tomp::clause::NumThreadsT<TypeTy, IdTy, ExprTy>;
285using OmpxAttribute = tomp::clause::OmpxAttributeT<TypeTy, IdTy, ExprTy>;
286using OmpxBare = tomp::clause::OmpxBareT<TypeTy, IdTy, ExprTy>;
287using OmpxDynCgroupMem = tomp::clause::OmpxDynCgroupMemT<TypeTy, IdTy, ExprTy>;
288using Order = tomp::clause::OrderT<TypeTy, IdTy, ExprTy>;
289using Ordered = tomp::clause::OrderedT<TypeTy, IdTy, ExprTy>;
290using Otherwise = tomp::clause::OtherwiseT<TypeTy, IdTy, ExprTy>;
291using Partial = tomp::clause::PartialT<TypeTy, IdTy, ExprTy>;
292using Permutation = tomp::clause::PermutationT<TypeTy, IdTy, ExprTy>;
293using Priority = tomp::clause::PriorityT<TypeTy, IdTy, ExprTy>;
294using Private = tomp::clause::PrivateT<TypeTy, IdTy, ExprTy>;
295using ProcBind = tomp::clause::ProcBindT<TypeTy, IdTy, ExprTy>;
296using Read = tomp::clause::ReadT<TypeTy, IdTy, ExprTy>;
297using Reduction = tomp::clause::ReductionT<TypeTy, IdTy, ExprTy>;
298using Relaxed = tomp::clause::RelaxedT<TypeTy, IdTy, ExprTy>;
299using Release = tomp::clause::ReleaseT<TypeTy, IdTy, ExprTy>;
300using Replayable = tomp::clause::ReplayableT<TypeTy, IdTy, ExprTy>;
301using ReverseOffload = tomp::clause::ReverseOffloadT<TypeTy, IdTy, ExprTy>;
302using Safelen = tomp::clause::SafelenT<TypeTy, IdTy, ExprTy>;
303using Safesync = tomp::clause::SafesyncT<TypeTy, IdTy, ExprTy>;
304using Schedule = tomp::clause::ScheduleT<TypeTy, IdTy, ExprTy>;
305using SelfMaps = tomp::clause::SelfMapsT<TypeTy, IdTy, ExprTy>;
306using SeqCst = tomp::clause::SeqCstT<TypeTy, IdTy, ExprTy>;
307using Severity = tomp::clause::SeverityT<TypeTy, IdTy, ExprTy>;
308using Shared = tomp::clause::SharedT<TypeTy, IdTy, ExprTy>;
309using Simd = tomp::clause::SimdT<TypeTy, IdTy, ExprTy>;
310using Simdlen = tomp::clause::SimdlenT<TypeTy, IdTy, ExprTy>;
311using Sizes = tomp::clause::SizesT<TypeTy, IdTy, ExprTy>;
312using TaskReduction = tomp::clause::TaskReductionT<TypeTy, IdTy, ExprTy>;
313using ThreadLimit = tomp::clause::ThreadLimitT<TypeTy, IdTy, ExprTy>;
314using Threads = tomp::clause::ThreadsT<TypeTy, IdTy, ExprTy>;
315using Threadset = tomp::clause::ThreadsetT<TypeTy, IdTy, ExprTy>;
316using To = tomp::clause::ToT<TypeTy, IdTy, ExprTy>;
317using Transparent = tomp::clause::TransparentT<TypeTy, IdTy, ExprTy>;
318using UnifiedAddress = tomp::clause::UnifiedAddressT<TypeTy, IdTy, ExprTy>;
319using UnifiedSharedMemory =
320 tomp::clause::UnifiedSharedMemoryT<TypeTy, IdTy, ExprTy>;
321using Uniform = tomp::clause::UniformT<TypeTy, IdTy, ExprTy>;
322using Unknown = tomp::clause::UnknownT<TypeTy, IdTy, ExprTy>;
323using Untied = tomp::clause::UntiedT<TypeTy, IdTy, ExprTy>;
324using Update = tomp::clause::UpdateT<TypeTy, IdTy, ExprTy>;
325using Use = tomp::clause::UseT<TypeTy, IdTy, ExprTy>;
326using UseDeviceAddr = tomp::clause::UseDeviceAddrT<TypeTy, IdTy, ExprTy>;
327using UseDevicePtr = tomp::clause::UseDevicePtrT<TypeTy, IdTy, ExprTy>;
328using UsesAllocators = tomp::clause::UsesAllocatorsT<TypeTy, IdTy, ExprTy>;
329using Weak = tomp::clause::WeakT<TypeTy, IdTy, ExprTy>;
330using When = tomp::clause::WhenT<TypeTy, IdTy, ExprTy>;
331using Write = tomp::clause::WriteT<TypeTy, IdTy, ExprTy>;
332} // namespace clause
333
334using tomp::type::operator==;
335
337 using WrapperTrait = std::true_type;
338 llvm::omp::CancellationConstructType v;
339};
340struct Depobj {
341 using EmptyTrait = std::true_type;
342};
343struct Flush {
344 using EmptyTrait = std::true_type;
345};
347 using EmptyTrait = std::true_type;
348};
350 using EmptyTrait = std::true_type;
351};
353 using EmptyTrait = std::true_type;
354};
355
356using ClauseBase = tomp::ClauseT<TypeTy, IdTy, ExprTy,
357 // Extras...
360
361struct Clause : public ClauseBase {
362 Clause(ClauseBase &&base, const parser::CharBlock source = {})
363 : ClauseBase(std::move(base)), source(source) {}
364 // "source" will be ignored by tomp::type::operator==.
365 parser::CharBlock source;
366};
367
368template <typename Specific>
369Clause makeClause(llvm::omp::Clause id, Specific &&specific,
370 parser::CharBlock source = {}) {
371 return Clause(typename Clause::BaseT{id, specific}, source);
372}
373
374Clause makeClause(const parser::OmpClause &cls,
376
377List<Clause> makeClauses(const parser::OmpClauseList &clauses,
379
380bool transferLocations(const List<Clause> &from, List<Clause> &to);
381} // namespace Fortran::lower::omp
382
383#endif // FORTRAN_LOWER_OPENMP_CLAUSES_H
Definition char-block.h:28
Definition semantics.h:67
Definition symbol.h:825
Definition bit-population-count.h:20
Definition Clauses.h:361
Definition Clauses.h:340
Definition Clauses.h:343
Definition Clauses.h:352
Definition Clauses.h:37
Definition Clauses.h:346
Definition Clauses.h:349
Definition parse-tree.h:1833
Definition parse-tree.h:1380
Definition parse-tree.h:587
Definition parse-tree.h:5023
Definition parse-tree.h:5007
Definition parse-tree.h:3507
Definition parse-tree.h:1863