FLANG
openmp-utils.h
1//===-- lib/Semantics/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 utilities used in OpenMP semantic checks.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_SEMANTICS_OPENMP_UTILS_H
14#define FORTRAN_SEMANTICS_OPENMP_UTILS_H
15
16#include "flang/Common/indirection.h"
17#include "flang/Evaluate/type.h"
18#include "flang/Parser/char-block.h"
19#include "flang/Parser/message.h"
20#include "flang/Parser/openmp-utils.h"
21#include "flang/Parser/parse-tree.h"
22#include "flang/Parser/tools.h"
23#include "flang/Semantics/tools.h"
24
25#include "llvm/ADT/ArrayRef.h"
26
27#include <memory>
28#include <optional>
29#include <string>
30#include <tuple>
31#include <type_traits>
32#include <utility>
33#include <vector>
34
35namespace Fortran::semantics {
36class Scope;
38class Symbol;
39
40// Add this namespace to avoid potential conflicts
41namespace omp {
42using Fortran::parser::omp::BlockRange;
43using Fortran::parser::omp::ExecutionPartIterator;
44using Fortran::parser::omp::is_range_v;
45using Fortran::parser::omp::LoopNestIterator;
46using Fortran::parser::omp::LoopRange;
47
48template <typename T, typename U = std::remove_const_t<T>> U AsRvalue(T &t) {
49 return U(t);
50}
51
52template <typename T> T &&AsRvalue(T &&t) { return std::move(t); }
53
54const Scope &GetScopingUnit(const Scope &scope);
55const Scope &GetProgramUnit(const Scope &scope);
56
57template <typename T> struct WithSource {
58 template < //
59 typename U = std::remove_reference_t<T>,
60 typename = std::enable_if_t<std::is_default_constructible_v<U>>>
61 WithSource() : value(), source() {}
62 WithSource(const WithSource<T> &) = default;
63 WithSource(WithSource<T> &&) = default;
64 WithSource(const T &t, parser::CharBlock s) : value(t), source(s) {}
65 WithSource(T &&t, parser::CharBlock s) : value(std::move(t)), source(s) {}
66 WithSource &operator=(const WithSource<T> &) = default;
67 WithSource &operator=(WithSource<T> &&) = default;
68
69 using value_type = T;
70 T value;
71 parser::CharBlock source;
72};
73
74// There is no consistent way to get the source of an ActionStmt, but there
75// is "source" in Statement<T>. This structure keeps the ActionStmt with the
76// extracted source for further use.
77struct SourcedActionStmt : public WithSource<const parser::ActionStmt *> {
78 using WithSource<value_type>::WithSource;
79 value_type stmt() const { return value; }
80 operator bool() const { return stmt() != nullptr; }
81};
82
84SourcedActionStmt GetActionStmt(const parser::Block &block);
85
86std::string ThisVersion(unsigned version);
87std::string TryVersion(unsigned version);
88
89const Symbol *GetObjectSymbol(
90 const parser::OmpObject &object, bool ultimate = false);
91const Symbol *GetArgumentSymbol(
92 const parser::OmpArgument &argument, bool ultimate = false);
93
94bool IsCommonBlock(const Symbol &sym);
95bool IsExtendedListItem(const Symbol &sym);
96bool IsVariableListItem(const Symbol &sym);
97bool IsTypeParamInquiry(const Symbol &sym);
98bool IsStructureComponent(const Symbol &sym);
99bool IsPrivatizable(const Symbol &sym);
100bool IsVarOrFunctionRef(const MaybeExpr &expr);
101
102bool IsWholeAssumedSizeArray(const parser::OmpObject &object);
103
104const Symbol *GetHostSymbol(const Symbol &sym);
105
106bool IsMapEnteringType(parser::OmpMapType::Value type);
107bool IsMapExitingType(parser::OmpMapType::Value type);
108
109MaybeExpr GetEvaluateExpr(const parser::Expr &parserExpr);
110template <typename T> MaybeExpr GetEvaluateExpr(const T &inp) {
111 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
112}
113
114std::optional<evaluate::DynamicType> GetDynamicType(
115 const parser::Expr &parserExpr);
116
117std::optional<bool> GetLogicalValue(const SomeExpr &expr);
118std::optional<int64_t> GetIntValueFromExpr(
119 const parser::Expr &parserExpr, SemanticsContext *semaCtx = nullptr);
120
121template <typename T>
122std::optional<int64_t> GetIntValueFromExpr(
123 const T &wrappedExpr, SemanticsContext *semaCtx = nullptr) {
124 if (auto *parserExpr{parser::Unwrap<parser::Expr>(wrappedExpr)}) {
125 return GetIntValueFromExpr(*parserExpr, semaCtx);
126 }
127 return std::nullopt;
128}
129
130std::optional<bool> IsContiguous(
131 SemanticsContext &semaCtx, const parser::OmpObject &object);
132
133std::vector<SomeExpr> GetTopLevelDesignators(const SomeExpr &expr);
134const SomeExpr *HasStorageOverlap(
135 const SomeExpr &base, llvm::ArrayRef<SomeExpr> exprs);
136
137bool IsAssignment(const parser::ActionStmt *x);
138bool IsPointerAssignment(const evaluate::Assignment &x);
139
140MaybeExpr MakeEvaluateExpr(const parser::OmpStylizedInstance &inp);
141
142bool IsLoopTransforming(llvm::omp::Directive dir);
143bool HasDataEnvironment(llvm::omp::Directive dir);
144
145bool IsFullUnroll(const parser::OmpDirectiveSpecification &spec);
146
147inline bool IsDoConcurrentLegal(unsigned version) {
148 // DO CONCURRENT is allowed (as an alternative to a Canonical Loop Nest)
149 // in OpenMP 6.0+.
150 return version >= 60;
151}
152
153struct LoopControl {
154 LoopControl(LoopControl &&x) = default;
155 LoopControl(const LoopControl &x) = default;
156 LoopControl(const parser::LoopControl::Bounds &x);
157 LoopControl(const parser::ConcurrentControl &x);
158
159 const parser::Name &iv;
160 WithSource<MaybeExpr> lbound, ubound, step;
161
162private:
163 static WithSource<MaybeExpr> fromParserExpr(const parser::Expr &x);
164};
165
166std::vector<LoopControl> GetLoopControls(const parser::DoConstruct &x);
167
169struct Reason {
170 Reason() = default;
171 Reason(Reason &&) = default;
172 Reason(const Reason &);
173 Reason &operator=(Reason &&) = default;
174 Reason &operator=(const Reason &);
175
176 parser::Messages msgs;
177
178 template <typename... Ts> Reason &Say(Ts &&...args) {
179 msgs.Say(std::forward<Ts>(args)...);
180 return *this;
181 }
182 parser::Message &AttachTo(parser::Message &msg);
183 Reason &Append(const Reason &other) {
184 CopyFrom(other);
185 return *this;
186 }
187 operator bool() const { return !msgs.empty(); }
188
189private:
190 void CopyFrom(const Reason &other);
191};
192
193// A property with an explanation of its value. Both, the property and the
194// reason are optional (the reason can have no messages in it).
195template <typename T> struct WithReason {
196 std::optional<T> value;
197 Reason reason;
198
199 WithReason() = default;
200 WithReason(std::optional<T> v, const Reason &r = Reason())
201 : value(v), reason(r) {}
202 operator bool() const { return value.has_value(); }
203};
204
205WithReason<int64_t> GetArgumentValueWithReason(
206 const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
207 unsigned version, SemanticsContext *semaCtx = nullptr);
208WithReason<int64_t> GetNumArgumentsWithReason(
209 const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
210 unsigned version, SemanticsContext *semaCtx = nullptr);
211WithReason<int64_t> GetHeightWithReason(
212 const parser::OmpDirectiveSpecification &spec, unsigned version,
213 SemanticsContext *semaCtx = nullptr);
214
217std::pair<WithReason<int64_t>, bool> GetAffectedNestDepthWithReason(
218 const parser::OmpDirectiveSpecification &spec, unsigned version,
219 SemanticsContext *semaCtx = nullptr);
222std::pair<WithReason<int64_t>, bool> GetGeneratedNestDepthWithReason(
223 const parser::OmpDirectiveSpecification &spec, unsigned version,
224 SemanticsContext *semaCtx = nullptr);
228WithReason<std::pair<int64_t, int64_t>> GetAffectedLoopRangeWithReason(
229 const parser::OmpDirectiveSpecification &spec, unsigned version,
230 SemanticsContext *semaCtx = nullptr);
232WithReason<int64_t> GetRectangularNestDepthWithReason(
233 const parser::OmpDirectiveSpecification &spec, unsigned version,
234 SemanticsContext *semaCtx = nullptr);
235
239std::optional<int64_t> GetMinimumSequenceCount(
240 std::optional<int64_t> first, std::optional<int64_t> count);
241std::optional<int64_t> GetMinimumSequenceCount(
242 std::optional<std::pair<int64_t, int64_t>> range);
243
249std::optional<std::vector<const parser::DoConstruct *>> CollectAffectedDoLoops(
250 const parser::OpenMPLoopConstruct &x, unsigned version,
251 SemanticsContext *semaCtx = nullptr);
252
253struct LoopSequence {
254 LoopSequence(const parser::ExecutionPartConstruct &root, unsigned version,
255 bool allowAllLoops = false, SemanticsContext *semaCtx = nullptr);
256
257 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
258 LoopSequence(const R &range, unsigned version, bool allowAllLoops = false,
259 SemanticsContext *semaCtx = nullptr)
260 : version_(version), allowAllLoops_(allowAllLoops), semaCtx_(semaCtx) {
261 entry_ = std::make_unique<Construct>(range, nullptr);
262 createChildrenFromRange(entry_->location);
263 precalculate();
264 }
265
266 struct Depth {
267 // If this sequence is a nest, the depth of the Canonical Loop Nest rooted
268 // at this sequence. Otherwise unspecified.
269 WithReason<int64_t> semantic;
270 // If this sequence is a nest, the depth of the perfect Canonical Loop Nest
271 // rooted at this sequence. Otherwise unspecified.
272 WithReason<int64_t> perfect;
273 };
274
275 bool isNest() const { return length_.value == 1; }
276 const WithReason<int64_t> &length() const { return length_; }
277 const WithReason<int64_t> &height() const { return height_; }
278 const Depth &depth() const { return depth_; }
279 const std::vector<LoopSequence> &children() const { return children_; }
280 const parser::ExecutionPartConstruct *owner() const { return entry_->owner; }
281
282 WithReason<bool> isWellFormedSequence() const;
283 WithReason<bool> isWellFormedNest() const;
284
287 const LoopSequence *getNestedDoConcurrent() const;
288
289 std::vector<LoopControl> getLoopControls() const;
290 // Check if this loop's bounds are invariant in each of the `outer`
291 // constructs.
292 WithReason<bool> isRectangular(
293 const std::vector<const LoopSequence *> &outer) const;
294
295private:
296 using Construct = ExecutionPartIterator::Construct;
297
298 LoopSequence(std::unique_ptr<Construct> entry, unsigned version,
299 bool allowAllLoops, SemanticsContext *semaCtx = nullptr);
300
301 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
302 void createChildrenFromRange(const R &range) {
303 createChildrenFromRange(range.begin(), range.end());
304 }
305
306 std::unique_ptr<Construct> createConstructEntry(
307 const parser::ExecutionPartConstruct &code);
308
309 void createChildrenFromRange( //
310 ExecutionPartIterator::IteratorType begin,
311 ExecutionPartIterator::IteratorType end);
312
314 void precalculate();
315
316 WithReason<int64_t> calculateLength() const;
317 WithReason<int64_t> getNestedLength() const;
318 Depth calculateDepths() const;
319 Depth getNestedDepths() const;
320 WithReason<int64_t> calculateHeight() const;
321
325 const parser::ExecutionPartConstruct *invalidIC_{nullptr};
329 const parser::ExecutionPartConstruct *opaqueIC_{nullptr};
330
335 WithReason<int64_t> length_;
337 Depth depth_;
344 WithReason<int64_t> height_;
345
346 // The core structure of the class:
347 unsigned version_; // Needed for GetXyzWithReason
348 bool allowAllLoops_;
349 std::unique_ptr<Construct> entry_;
350 std::vector<LoopSequence> children_;
351 SemanticsContext *semaCtx_{nullptr};
352};
353} // namespace omp
354} // namespace Fortran::semantics
355
356#endif // FORTRAN_SEMANTICS_OPENMP_UTILS_H
Definition char-block.h:28
Definition message.h:200
Definition message.h:332
Definition scope.h:68
Definition semantics.h:68
Definition symbol.h:832
Definition parse-tree.h:2241
Definition parse-tree.h:2328
Definition parse-tree.h:556
Definition parse-tree.h:1698
Definition parse-tree.h:589
Definition parse-tree.h:5091
Definition parse-tree.h:3568
Definition parse-tree.h:5464
Definition parse-tree.h:3699
const LoopSequence * getNestedDoConcurrent() const
Definition openmp-utils.cpp:1429
A representation of a "because" message.
Definition openmp-utils.h:169
Definition openmp-utils.h:77
Definition openmp-utils.h:195
Definition openmp-utils.h:57