FLANG
openmp-utils.h
1//===-- flang/Parser/openmp-utils.h ---------------------------------------===//
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// Common OpenMP utilities.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_PARSER_OPENMP_UTILS_H
14#define FORTRAN_PARSER_OPENMP_UTILS_H
15
16#include "flang/Common/indirection.h"
17#include "flang/Parser/char-block.h"
18#include "flang/Parser/parse-tree.h"
19#include "llvm/ADT/iterator_range.h"
20#include "llvm/Frontend/OpenMP/OMP.h"
21
22#include <cassert>
23#include <iterator>
24#include <tuple>
25#include <type_traits>
26#include <utility>
27#include <variant>
28#include <vector>
29
30namespace Fortran::parser::omp {
31
32template <typename T> constexpr auto addr_if(std::optional<T> &x) {
33 return x ? &*x : nullptr;
34}
35template <typename T> constexpr auto addr_if(const std::optional<T> &x) {
36 return x ? &*x : nullptr;
37}
38
39const parser::Designator *GetDesignatorFromObj(const parser::OmpObject &object);
40const parser::DataRef *GetDataRefFromObj(const parser::OmpObject &object);
41const parser::OmpLocator *GetLocatorFromObj(const parser::OmpObject &object);
42const parser::Name *GetCommonBlockFromObj(const parser::OmpObject &object);
43
44const parser::ArrayElement *GetArrayElementFromObj(
45 const parser::OmpObject &object);
46std::optional<parser::CharBlock> GetObjectSource(
47 const parser::OmpObject &object);
48const parser::OmpObject *GetArgumentObject(const parser::OmpArgument &argument);
49
50const OmpDirectiveSpecification &GetOmpDirectiveSpecification(
51 const OpenMPConstruct &x);
52const OmpDirectiveSpecification &GetOmpDirectiveSpecification(
53 const OpenMPDeclarativeConstruct &x);
54
55template <typename T> struct WithSource {
56 template < //
57 typename U = std::remove_reference_t<T>,
58 typename = std::enable_if_t<std::is_default_constructible_v<U>>>
59 WithSource() : value(), source() {}
60 WithSource(const WithSource<T> &) = default;
61 WithSource(WithSource<T> &&) = default;
62 WithSource(const T &t, parser::CharBlock s) : value(t), source(s) {}
63 WithSource(T &&t, parser::CharBlock s) : value(std::move(t)), source(s) {}
64 WithSource &operator=(const WithSource<T> &) = default;
65 WithSource &operator=(WithSource<T> &&) = default;
66
67 using value_type = T;
68 T value;
69 parser::CharBlock source;
70};
71
72namespace detail {
74 static OmpDirectiveName MakeName(CharBlock source = {},
75 llvm::omp::Directive id = llvm::omp::Directive::OMPD_unknown) {
77 name.source = source;
78 name.v = id;
79 return name;
80 }
81
82 static OmpDirectiveName GetOmpDirectiveName(const OmpDirectiveName &x) {
83 return x;
84 }
85
86 static OmpDirectiveName GetOmpDirectiveName(const OmpSectionDirective &x) {
87 if (auto &spec{std::get<std::optional<OmpDirectiveSpecification>>(x.t)}) {
88 return spec->DirName();
89 } else {
90 return MakeName({}, llvm::omp::Directive::OMPD_section);
91 }
92 }
93
94 static OmpDirectiveName GetOmpDirectiveName(
96 return x.DirName();
97 }
98
99 template <typename T>
100 static OmpDirectiveName GetOmpDirectiveName(const T &x) {
101 if constexpr (WrapperTrait<T>) {
102 return GetOmpDirectiveName(x.v);
103 } else if constexpr (TupleTrait<T>) {
104 if constexpr (std::is_base_of_v<OmpBlockConstruct, T>) {
105 return std::get<OmpBeginDirective>(x.t).DirName();
106 } else {
107 return GetFromTuple(
108 x.t, std::make_index_sequence<std::tuple_size_v<decltype(x.t)>>{});
109 }
110 } else if constexpr (UnionTrait<T>) {
111 return common::visit(
112 [](auto &&s) { return GetOmpDirectiveName(s); }, x.u);
113 } else {
114 return MakeName();
115 }
116 }
117
118 template <typename... Ts, size_t... Is>
119 static OmpDirectiveName GetFromTuple(
120 const std::tuple<Ts...> &t, std::index_sequence<Is...>) {
121 OmpDirectiveName name = MakeName();
122 auto accumulate = [&](const OmpDirectiveName &n) {
123 if (name.v == llvm::omp::Directive::OMPD_unknown) {
124 name = n;
125 } else {
126 assert(
127 n.v == llvm::omp::Directive::OMPD_unknown && "Conflicting names");
128 }
129 };
130 (accumulate(GetOmpDirectiveName(std::get<Is>(t))), ...);
131 return name;
132 }
133
134 template <typename T>
135 static OmpDirectiveName GetOmpDirectiveName(const common::Indirection<T> &x) {
136 return GetOmpDirectiveName(x.value());
137 }
138};
139} // namespace detail
140
141template <typename T> OmpDirectiveName GetOmpDirectiveName(const T &x) {
142 return detail::DirectiveNameScope::GetOmpDirectiveName(x);
143}
144
145std::string GetUpperName(llvm::omp::Clause id, llvm::omp::Version version);
146std::string GetUpperName(llvm::omp::Directive id, llvm::omp::Version version);
147
149const OpenMPConstruct *GetOmp(const ExecutionPartConstruct &x);
150
151const OpenMPLoopConstruct *GetOmpLoop(const ExecutionPartConstruct &x);
152const DoConstruct *GetDoConstruct(const ExecutionPartConstruct &x);
153
154namespace detail {
156 template <typename T> static const OmpObjectList *Get(const T &x) {
157 if constexpr (std::is_same_v<OmpObjectList, T>) {
158 return &x;
159 } else if constexpr (WrapperTrait<T>) {
160 return Get(x.v);
161 } else if constexpr (UnionTrait<T>) {
162 return std::visit([](auto &&s) { return Get(s); }, x.u);
163 } else if constexpr (TupleTrait<T>) {
164 return GetFromTuple(
165 x.t, std::make_index_sequence<std::tuple_size_v<decltype(x.t)>>{});
166 } else if constexpr (ConstraintTrait<T>) {
167 return Get(x.thing);
168 } else {
169 return nullptr;
170 }
171 }
172
173 template <typename T>
174 static const OmpObjectList *Get(const common::Indirection<T> &x) {
175 return Get(x.value());
176 }
177
178 template <typename... Ts, size_t... Is>
179 static const OmpObjectList *GetFromTuple(
180 const std::tuple<Ts...> &t, std::index_sequence<Is...>) {
181 const OmpObjectList *objects{nullptr};
182 ((objects = objects ? objects : Get(std::get<Is>(t))), ...);
183 return objects;
184 }
185};
186} // namespace detail
187
188template <typename T> const OmpObjectList *GetOmpObjectList(const T &clause) {
189 static_assert(std::is_class_v<T>, "Unexpected argument type");
190 return detail::OmpObjectListScope::Get(clause);
191}
192
193template <typename T>
194const T *GetFirstArgument(const OmpDirectiveSpecification &spec) {
195 for (const OmpArgument &arg : spec.Arguments().v) {
196 if (auto *t{std::get_if<T>(&arg.u)}) {
197 return t;
198 }
199 }
200 return nullptr;
201}
202
203const OmpClause *FindClause(
204 const OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId);
205
206const BlockConstruct *GetFortranBlockConstruct(
207 const ExecutionPartConstruct &epc);
208const Block &GetInnermostExecPart(const Block &block);
209bool IsStrictlyStructuredBlock(const Block &block);
210
211const OmpCombinerExpression *GetCombinerExpr(const OmpReductionSpecifier &x);
212const OmpCombinerExpression *GetCombinerExpr(const OmpClause &x);
213const OmpInitializerExpression *GetInitializerExpr(const OmpClause &x);
214
216 std::vector<const OmpAllocateDirective *> dirs;
217 const ExecutionPartConstruct *body{nullptr};
218};
219
220OmpAllocateInfo SplitOmpAllocate(const OmpAllocateDirective &x);
221
222namespace detail {
223template <typename ClauseTy, typename VoidTy = void> struct HasModifierImpl {
224 static constexpr bool value{false};
225};
226template <typename ClauseTy>
227struct HasModifierImpl<ClauseTy, std::void_t<typename ClauseTy::Modifier>> {
228 static constexpr bool value{true};
229};
230} // namespace detail
231template <typename ClauseTy>
232static constexpr bool HasModifier = detail::HasModifierImpl<ClauseTy>::value;
233
234template <typename R, typename = void, typename = void> struct is_range {
235 static constexpr bool value{false};
236};
237
238template <typename R>
239struct is_range<R, //
240 std::void_t<decltype(std::declval<R>().begin())>,
241 std::void_t<decltype(std::declval<R>().end())>> {
242 static constexpr bool value{true};
243};
244
245template <typename R> constexpr bool is_range_v = is_range<R>::value;
246
247// Iterate over a range of parser::Block::const_iterator's. When the end
248// of the range is reached, the iterator becomes invalid.
249// Treat BLOCK constructs as if they were transparent, i.e. as if the
250// BLOCK/ENDBLOCK statements, and the specification part contained within
251// were removed. The stepping determines whether the iterator steps "into"
252// DO loops and OpenMP loop constructs, or steps "over" them.
253//
254// Example: consecutive locations of the iterator:
255//
256// Step::Into Step::Over
257// block block
258// 1 => stmt1 1 => stmt1
259// block block
260// integer :: x integer :: x
261// 2 => stmt2 2 => stmt2
262// block block
263// end block end block
264// end block end block
265// 3 => do i = 1, n 3 => do i = 1, n
266// 4 => continue continue
267// end do end do
268// 5 => stmt3 4 => stmt3
269// end block end block
270//
271// 6 => <invalid> 5 => <invalid>
272//
273// The iterator is in a legal state (position) if it's at an
274// ExecutionPartConstruct that is not a BlockConstruct, or is invalid.
275struct ExecutionPartIterator {
276 enum class Step {
277 Into,
278 Over,
279 Default = Into,
280 };
281
282 using IteratorType = Block::const_iterator;
283 using IteratorRange = llvm::iterator_range<IteratorType>;
284
285 // An iterator range with a third iterator indicating a position inside
286 // the range.
287 struct IteratorGauge : public IteratorRange {
288 IteratorGauge(IteratorType b, IteratorType e)
289 : IteratorRange(b, e), at(b) {}
290 IteratorGauge(IteratorRange r) : IteratorRange(r), at(r.begin()) {}
291
292 bool atEnd() const { return at == end(); }
293 IteratorType at;
294 };
295
296 struct Construct {
297 Construct(IteratorType b, IteratorType e, const ExecutionPartConstruct *c)
298 : location(b, e), owner(c) {}
299 template <typename R>
300 Construct(const R &r, const ExecutionPartConstruct *c)
301 : location(r), owner(c) {}
302 Construct(const Construct &c) = default;
303 // The original range of the construct with the current position in it.
304 // The location.at is the construct currently being pointed at, or
305 // stepped into.
306 IteratorGauge location;
307 const ExecutionPartConstruct *owner;
308 };
309
310 ExecutionPartIterator() = default;
311
312 ExecutionPartIterator(IteratorType b, IteratorType e, Step s = Step::Default,
313 const ExecutionPartConstruct *c = nullptr)
314 : stepping_(s) {
315 stack_.emplace_back(b, e, c);
316 adjust();
317 }
318 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
319 ExecutionPartIterator(const R &range, Step stepping = Step::Default,
320 const ExecutionPartConstruct *construct = nullptr)
321 : ExecutionPartIterator(range.begin(), range.end(), stepping, construct) {
322 }
323
324 // Advance the iterator to the next legal position. If the current position
325 // is a DO-loop or a loop construct, step into the contained Block.
326 void step();
327
328 // Advance the iterator to the next legal position. If the current position
329 // is a DO-loop or a loop construct, step to the next legal position following
330 // the DO-loop or loop construct.
331 void next();
332
333 bool valid() const { return !stack_.empty(); }
334
335 const std::vector<Construct> &stack() const { return stack_; }
336 decltype(auto) operator*() const { return *at(); }
337 bool operator==(const ExecutionPartIterator &other) const {
338 if (valid() != other.valid()) {
339 return false;
340 }
341 // Invalid iterators are considered equal.
342 return !valid() ||
343 stack_.back().location.at == other.stack_.back().location.at;
344 }
345 bool operator!=(const ExecutionPartIterator &other) const {
346 return !(*this == other);
347 }
348
349 ExecutionPartIterator &operator++() {
350 if (stepping_ == Step::Into) {
351 step();
352 } else {
353 assert(stepping_ == Step::Over && "Unexpected stepping");
354 next();
355 }
356 return *this;
357 }
358
359 ExecutionPartIterator operator++(int) {
360 ExecutionPartIterator copy{*this};
361 operator++();
362 return copy;
363 }
364
365 using difference_type = IteratorType::difference_type;
366 using value_type = IteratorType::value_type;
367 using reference = IteratorType::reference;
368 using pointer = IteratorType::pointer;
369 using iterator_category = std::forward_iterator_tag;
370
371private:
372 IteratorType at() const { return stack_.back().location.at; };
373
374 // If the iterator is not at a legal location, keep advancing it until
375 // it lands at a legal location or becomes invalid.
376 void adjust();
377
378 const Step stepping_ = Step::Default;
379 std::vector<Construct> stack_;
380};
381
382template <typename Iterator = ExecutionPartIterator> struct ExecutionPartRange {
383 using Step = typename Iterator::Step;
384
385 ExecutionPartRange(Block::const_iterator begin, Block::const_iterator end,
386 Step stepping = Step::Default,
387 const ExecutionPartConstruct *owner = nullptr)
388 : begin_(begin, end, stepping, owner), end_() {}
389 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
390 ExecutionPartRange(const R &range, Step stepping = Step::Default,
391 const ExecutionPartConstruct *owner = nullptr)
392 : ExecutionPartRange(range.begin(), range.end(), stepping, owner) {}
393
394 Iterator begin() const { return begin_; }
395 Iterator end() const { return end_; }
396
397private:
398 Iterator begin_, end_;
399};
400
401struct LoopNestIterator : public ExecutionPartIterator {
402 LoopNestIterator() = default;
403
404 LoopNestIterator(IteratorType b, IteratorType e, Step s = Step::Default,
405 const ExecutionPartConstruct *c = nullptr)
406 : ExecutionPartIterator(b, e, s, c) {
407 adjust();
408 }
409 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
410 LoopNestIterator(const R &range, Step stepping = Step::Default,
411 const ExecutionPartConstruct *construct = nullptr)
412 : LoopNestIterator(range.begin(), range.end(), stepping, construct) {}
413
414 LoopNestIterator &operator++() {
415 ExecutionPartIterator::operator++();
416 adjust();
417 return *this;
418 }
419
420 LoopNestIterator operator++(int) {
421 LoopNestIterator copy{*this};
422 operator++();
423 return copy;
424 }
425
426private:
427 static bool isLoop(const ExecutionPartConstruct &c);
428
429 void adjust() {
430 while (valid() && !isLoop(**this)) {
431 ExecutionPartIterator::operator++();
432 }
433 }
434};
435
438
439} // namespace Fortran::parser::omp
440
441#endif // FORTRAN_PARSER_OPENMP_UTILS_H
Definition indirection.h:31
Definition char-block.h:26
Definition parse-tree.h:442
Definition parse-tree.h:2368
Definition parse-tree.h:559
Definition parse-tree.h:5518
Definition parse-tree.h:5382
Definition parse-tree.h:3580
Definition parse-tree.h:5283
Definition parse-tree.h:3635
Definition parse-tree.h:5396
Definition parse-tree.h:5670
Definition parse-tree.h:5656
Definition parse-tree.h:3746
Definition openmp-utils.h:275
Definition openmp-utils.h:382
Definition openmp-utils.h:215
Definition openmp-utils.h:234