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 parser::Designator *GetDesignatorFromObj(const parser::OmpObject &object);
90const parser::DataRef *GetDataRefFromObj(const parser::OmpObject &object);
91const parser::ArrayElement *GetArrayElementFromObj(
92 const parser::OmpObject &object);
93const Symbol *GetObjectSymbol(const parser::OmpObject &object);
94std::optional<parser::CharBlock> GetObjectSource(
95 const parser::OmpObject &object);
96const Symbol *GetArgumentSymbol(const parser::OmpArgument &argument);
97const parser::OmpObject *GetArgumentObject(const parser::OmpArgument &argument);
98
99bool IsCommonBlock(const Symbol &sym);
100bool IsExtendedListItem(const Symbol &sym);
101bool IsVariableListItem(const Symbol &sym);
102bool IsTypeParamInquiry(const Symbol &sym);
103bool IsStructureComponent(const Symbol &sym);
104bool IsPrivatizable(const Symbol &sym);
105bool IsVarOrFunctionRef(const MaybeExpr &expr);
106
107bool IsWholeAssumedSizeArray(const parser::OmpObject &object);
108
109bool IsMapEnteringType(parser::OmpMapType::Value type);
110bool IsMapExitingType(parser::OmpMapType::Value type);
111
112MaybeExpr GetEvaluateExpr(const parser::Expr &parserExpr);
113template <typename T> MaybeExpr GetEvaluateExpr(const T &inp) {
114 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
115}
116
117std::optional<evaluate::DynamicType> GetDynamicType(
118 const parser::Expr &parserExpr);
119
120std::optional<bool> GetLogicalValue(const SomeExpr &expr);
121std::optional<int64_t> GetIntValueFromExpr(
122 const parser::Expr &parserExpr, SemanticsContext *semaCtx = nullptr);
123
124template <typename T>
125std::optional<int64_t> GetIntValueFromExpr(
126 const T &wrappedExpr, SemanticsContext *semaCtx = nullptr) {
127 if (auto *parserExpr{parser::Unwrap<parser::Expr>(wrappedExpr)}) {
128 return GetIntValueFromExpr(*parserExpr, semaCtx);
129 }
130 return std::nullopt;
131}
132
133std::optional<bool> IsContiguous(
134 SemanticsContext &semaCtx, const parser::OmpObject &object);
135
136std::vector<SomeExpr> GetTopLevelDesignators(const SomeExpr &expr);
137const SomeExpr *HasStorageOverlap(
138 const SomeExpr &base, llvm::ArrayRef<SomeExpr> exprs);
139
140bool IsAssignment(const parser::ActionStmt *x);
141bool IsPointerAssignment(const evaluate::Assignment &x);
142
143MaybeExpr MakeEvaluateExpr(const parser::OmpStylizedInstance &inp);
144
145bool IsLoopTransforming(llvm::omp::Directive dir);
146bool IsFullUnroll(const parser::OmpDirectiveSpecification &spec);
147
148inline bool IsDoConcurrentLegal(unsigned version) {
149 // DO CONCURRENT is allowed (as an alternative to a Canonical Loop Nest)
150 // in OpenMP 6.0+.
151 return version >= 60;
152}
153
154struct LoopControl {
155 LoopControl(LoopControl &&x) = default;
156 LoopControl(const LoopControl &x) = default;
157 LoopControl(const parser::LoopControl::Bounds &x);
158 LoopControl(const parser::ConcurrentControl &x);
159
160 const Symbol *iv{nullptr};
161 WithSource<MaybeExpr> lbound, ubound, step;
162
163private:
164 static WithSource<MaybeExpr> fromParserExpr(const parser::Expr &x);
165};
166
167std::vector<LoopControl> GetLoopControls(const parser::DoConstruct &x);
168
170struct Reason {
171 Reason() = default;
172 Reason(Reason &&) = default;
173 Reason(const Reason &);
174 Reason &operator=(Reason &&) = default;
175 Reason &operator=(const Reason &);
176
177 parser::Messages msgs;
178
179 template <typename... Ts> Reason &Say(Ts &&...args) {
180 msgs.Say(std::forward<Ts>(args)...);
181 return *this;
182 }
183 parser::Message &AttachTo(parser::Message &msg);
184 Reason &Append(const Reason &other) {
185 CopyFrom(other);
186 return *this;
187 }
188 operator bool() const { return !msgs.empty(); }
189
190private:
191 void CopyFrom(const Reason &other);
192};
193
194// A property with an explanation of its value. Both, the property and the
195// reason are optional (the reason can have no messages in it).
196template <typename T> struct WithReason {
197 std::optional<T> value;
198 Reason reason;
199
200 WithReason() = default;
201 WithReason(std::optional<T> v, const Reason &r = Reason())
202 : value(v), reason(r) {}
203 operator bool() const { return value.has_value(); }
204};
205
206WithReason<int64_t> GetArgumentValueWithReason(
207 const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
208 unsigned version, SemanticsContext *semaCtx = nullptr);
209WithReason<int64_t> GetNumArgumentsWithReason(
210 const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
211 unsigned version, SemanticsContext *semaCtx = nullptr);
212WithReason<int64_t> GetHeightWithReason(
213 const parser::OmpDirectiveSpecification &spec, unsigned version,
214 SemanticsContext *semaCtx = nullptr);
215
218std::pair<WithReason<int64_t>, bool> GetAffectedNestDepthWithReason(
219 const parser::OmpDirectiveSpecification &spec, unsigned version,
220 SemanticsContext *semaCtx = nullptr);
223std::pair<WithReason<int64_t>, bool> GetGeneratedNestDepthWithReason(
224 const parser::OmpDirectiveSpecification &spec, unsigned version,
225 SemanticsContext *semaCtx = nullptr);
229WithReason<std::pair<int64_t, int64_t>> GetAffectedLoopRangeWithReason(
230 const parser::OmpDirectiveSpecification &spec, unsigned version,
231 SemanticsContext *semaCtx = nullptr);
233WithReason<int64_t> GetRectangularNestDepthWithReason(
234 const parser::OmpDirectiveSpecification &spec, unsigned version,
235 SemanticsContext *semaCtx = nullptr);
236
240std::optional<int64_t> GetMinimumSequenceCount(
241 std::optional<int64_t> first, std::optional<int64_t> count);
242std::optional<int64_t> GetMinimumSequenceCount(
243 std::optional<std::pair<int64_t, int64_t>> range);
244
250std::optional<std::vector<const parser::DoConstruct *>> CollectAffectedDoLoops(
251 const parser::OpenMPLoopConstruct &x, unsigned version,
252 SemanticsContext *semaCtx = nullptr);
253
254struct LoopSequence {
255 LoopSequence(const parser::ExecutionPartConstruct &root, unsigned version,
256 bool allowAllLoops = false, SemanticsContext *semaCtx = nullptr);
257
258 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
259 LoopSequence(const R &range, unsigned version, bool allowAllLoops = false,
260 SemanticsContext *semaCtx = nullptr)
261 : version_(version), allowAllLoops_(allowAllLoops), semaCtx_(semaCtx) {
262 entry_ = std::make_unique<Construct>(range, nullptr);
263 createChildrenFromRange(entry_->location);
264 precalculate();
265 }
266
267 struct Depth {
268 // If this sequence is a nest, the depth of the Canonical Loop Nest rooted
269 // at this sequence. Otherwise unspecified.
270 WithReason<int64_t> semantic;
271 // If this sequence is a nest, the depth of the perfect Canonical Loop Nest
272 // rooted at this sequence. Otherwise unspecified.
273 WithReason<int64_t> perfect;
274 };
275
276 bool isNest() const { return length_.value == 1; }
277 const WithReason<int64_t> &length() const { return length_; }
278 const WithReason<int64_t> &height() const { return height_; }
279 const Depth &depth() const { return depth_; }
280 const std::vector<LoopSequence> &children() const { return children_; }
281 const parser::ExecutionPartConstruct *owner() const { return entry_->owner; }
282
283 WithReason<bool> isWellFormedSequence() const;
284 WithReason<bool> isWellFormedNest() const;
285
288 const LoopSequence *getNestedDoConcurrent() const;
289
290 std::vector<LoopControl> getLoopControls() const;
291 // Check if this loop's bounds are invariant in each of the `outer`
292 // constructs.
293 WithReason<bool> isRectangular(
294 const std::vector<const LoopSequence *> &outer) const;
295
296private:
297 using Construct = ExecutionPartIterator::Construct;
298
299 LoopSequence(std::unique_ptr<Construct> entry, unsigned version,
300 bool allowAllLoops, SemanticsContext *semaCtx = nullptr);
301
302 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
303 void createChildrenFromRange(const R &range) {
304 createChildrenFromRange(range.begin(), range.end());
305 }
306
307 std::unique_ptr<Construct> createConstructEntry(
308 const parser::ExecutionPartConstruct &code);
309
310 void createChildrenFromRange( //
311 ExecutionPartIterator::IteratorType begin,
312 ExecutionPartIterator::IteratorType end);
313
315 void precalculate();
316
317 WithReason<int64_t> calculateLength() const;
318 WithReason<int64_t> getNestedLength() const;
319 Depth calculateDepths() const;
320 Depth getNestedDepths() const;
321 WithReason<int64_t> calculateHeight() const;
322
326 const parser::ExecutionPartConstruct *invalidIC_{nullptr};
330 const parser::ExecutionPartConstruct *opaqueIC_{nullptr};
331
336 WithReason<int64_t> length_;
338 Depth depth_;
345 WithReason<int64_t> height_;
346
347 // The core structure of the class:
348 unsigned version_; // Needed for GetXyzWithReason
349 bool allowAllLoops_;
350 std::unique_ptr<Construct> entry_;
351 std::vector<LoopSequence> children_;
352 SemanticsContext *semaCtx_{nullptr};
353};
354} // namespace omp
355} // namespace Fortran::semantics
356
357#endif // FORTRAN_SEMANTICS_OPENMP_UTILS_H
Definition char-block.h:28
Definition message.h:200
Definition message.h:332
Definition scope.h:67
Definition semantics.h:67
Definition symbol.h:832
Definition parse-tree.h:1896
Definition parse-tree.h:2236
Definition parse-tree.h:1806
Definition parse-tree.h:1845
Definition parse-tree.h:2323
Definition parse-tree.h:554
Definition parse-tree.h:1693
Definition parse-tree.h:5048
Definition parse-tree.h:3525
Definition parse-tree.h:5424
Definition parse-tree.h:3656
const LoopSequence * getNestedDoConcurrent() const
Definition openmp-utils.cpp:1421
A representation of a "because" message.
Definition openmp-utils.h:170
Definition openmp-utils.h:77
Definition openmp-utils.h:196
Definition openmp-utils.h:57