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 <optional>
28#include <string>
29#include <tuple>
30#include <type_traits>
31#include <utility>
32#include <vector>
33
34namespace Fortran::semantics {
35class Scope;
37class Symbol;
38
39// Add this namespace to avoid potential conflicts
40namespace omp {
41using Fortran::parser::omp::BlockRange;
42using Fortran::parser::omp::ExecutionPartIterator;
43using Fortran::parser::omp::is_range_v;
44using Fortran::parser::omp::LoopNestIterator;
45using Fortran::parser::omp::LoopRange;
46
47template <typename T, typename U = std::remove_const_t<T>> U AsRvalue(T &t) {
48 return U(t);
49}
50
51template <typename T> T &&AsRvalue(T &&t) { return std::move(t); }
52
53const Scope &GetScopingUnit(const Scope &scope);
54const Scope &GetProgramUnit(const Scope &scope);
55
56// There is no consistent way to get the source of an ActionStmt, but there
57// is "source" in Statement<T>. This structure keeps the ActionStmt with the
58// extracted source for further use.
60 const parser::ActionStmt *stmt{nullptr};
61 parser::CharBlock source;
62
63 operator bool() const { return stmt != nullptr; }
64};
65
67SourcedActionStmt GetActionStmt(const parser::Block &block);
68
69std::string ThisVersion(unsigned version);
70std::string TryVersion(unsigned version);
71
72const parser::Designator *GetDesignatorFromObj(const parser::OmpObject &object);
73const parser::DataRef *GetDataRefFromObj(const parser::OmpObject &object);
74const parser::ArrayElement *GetArrayElementFromObj(
75 const parser::OmpObject &object);
76const Symbol *GetObjectSymbol(const parser::OmpObject &object);
77std::optional<parser::CharBlock> GetObjectSource(
78 const parser::OmpObject &object);
79const Symbol *GetArgumentSymbol(const parser::OmpArgument &argument);
80const parser::OmpObject *GetArgumentObject(const parser::OmpArgument &argument);
81
82bool IsCommonBlock(const Symbol &sym);
83bool IsExtendedListItem(const Symbol &sym);
84bool IsVariableListItem(const Symbol &sym);
85bool IsTypeParamInquiry(const Symbol &sym);
86bool IsStructureComponent(const Symbol &sym);
87bool IsPrivatizable(const Symbol &sym);
88bool IsVarOrFunctionRef(const MaybeExpr &expr);
89
90bool IsWholeAssumedSizeArray(const parser::OmpObject &object);
91
92bool IsMapEnteringType(parser::OmpMapType::Value type);
93bool IsMapExitingType(parser::OmpMapType::Value type);
94
95MaybeExpr GetEvaluateExpr(const parser::Expr &parserExpr);
96template <typename T> MaybeExpr GetEvaluateExpr(const T &inp) {
97 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
98}
99
100std::optional<evaluate::DynamicType> GetDynamicType(
101 const parser::Expr &parserExpr);
102
103std::optional<bool> GetLogicalValue(const SomeExpr &expr);
104
105std::optional<bool> IsContiguous(
106 SemanticsContext &semaCtx, const parser::OmpObject &object);
107
108std::vector<SomeExpr> GetTopLevelDesignators(const SomeExpr &expr);
109const SomeExpr *HasStorageOverlap(
110 const SomeExpr &base, llvm::ArrayRef<SomeExpr> exprs);
111bool IsAssignment(const parser::ActionStmt *x);
112bool IsPointerAssignment(const evaluate::Assignment &x);
113
114MaybeExpr MakeEvaluateExpr(const parser::OmpStylizedInstance &inp);
115
117struct Reason {
118 Reason() = default;
119 Reason(Reason &&) = default;
120 Reason(const Reason &);
121 Reason &operator=(Reason &&) = default;
122 Reason &operator=(const Reason &);
123
124 parser::Messages msgs;
125
126 template <typename... Ts> Reason &Say(Ts &&...args) {
127 msgs.Say(std::forward<Ts>(args)...);
128 return *this;
129 }
130 parser::Message &AttachTo(parser::Message &msg);
131 Reason &Append(const Reason &other) {
132 CopyFrom(other);
133 return *this;
134 }
135 operator bool() const { return !msgs.empty(); }
136
137private:
138 void CopyFrom(const Reason &other);
139};
140
141// A property with an explanation of its value. Both, the property and the
142// reason are optional (the reason can have no messages in it).
143template <typename T> struct WithReason {
144 std::optional<T> value;
145 Reason reason;
146
147 WithReason() = default;
148 WithReason(std::optional<T> v, const Reason &r = Reason())
149 : value(v), reason(r) {}
150 operator bool() const { return value.has_value(); }
151};
152
153WithReason<int64_t> GetArgumentValueWithReason(
154 const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
155 unsigned version);
156WithReason<int64_t> GetNumArgumentsWithReason(
157 const parser::OmpDirectiveSpecification &spec, llvm::omp::Clause clauseId,
158 unsigned version);
159
160bool IsLoopTransforming(llvm::omp::Directive dir);
161bool IsFullUnroll(const parser::OpenMPLoopConstruct &x);
162
163// Return the depth of the affected nests:
164// {affected-depth, reason, must-be-perfect-nest}.
165std::pair<WithReason<int64_t>, bool> GetAffectedNestDepthWithReason(
166 const parser::OmpDirectiveSpecification &spec, unsigned version);
167// Return the range of the affected nests in the sequence:
168// {first, count, reason}.
169// If the range is "the whole sequence", the return value will be {1, -1, ...}.
170WithReason<std::pair<int64_t, int64_t>> GetAffectedLoopRangeWithReason(
171 const parser::OmpDirectiveSpecification &spec, unsigned version);
172
173// Count the required loop count from range. If count == -1, return -1,
174// indicating all loops in the sequence.
175std::optional<int64_t> GetRequiredCount(
176 std::optional<int64_t> first, std::optional<int64_t> count);
177std::optional<int64_t> GetRequiredCount(
178 std::optional<std::pair<int64_t, int64_t>> range);
179
180struct LoopSequence {
181 LoopSequence(const parser::ExecutionPartConstruct &root, unsigned version,
182 bool allowAllLoops = false);
183
184 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
185 LoopSequence(const R &range, unsigned version, bool allowAllLoops = false)
186 : version_(version), allowAllLoops_(allowAllLoops) {
187 entry_ = std::make_unique<Construct>(range, nullptr);
188 createChildrenFromRange(entry_->location);
189 precalculate();
190 }
191
192 struct Depth {
193 // If this sequence is a nest, the depth of the Canonical Loop Nest rooted
194 // at this sequence. Otherwise unspecified.
195 WithReason<int64_t> semantic;
196 // If this sequence is a nest, the depth of the perfect Canonical Loop Nest
197 // rooted at this sequence. Otherwise unspecified.
198 WithReason<int64_t> perfect;
199 };
200
201 bool isNest() const { return length_.value == 1; }
202 const WithReason<int64_t> &length() const { return length_; }
203 const Depth &depth() const { return depth_; }
204 const std::vector<LoopSequence> &children() const { return children_; }
205
206 WithReason<bool> isWellFormedSequence() const;
207 WithReason<bool> isWellFormedNest() const;
208
209private:
210 using Construct = ExecutionPartIterator::Construct;
211
212 LoopSequence(
213 std::unique_ptr<Construct> entry, unsigned version, bool allowAllLoops);
214
215 template <typename R, typename = std::enable_if_t<is_range_v<R>>>
216 void createChildrenFromRange(const R &range) {
217 createChildrenFromRange(range.begin(), range.end());
218 }
219
220 std::unique_ptr<Construct> createConstructEntry(
221 const parser::ExecutionPartConstruct &code);
222
223 void createChildrenFromRange( //
224 ExecutionPartIterator::IteratorType begin,
225 ExecutionPartIterator::IteratorType end);
226
228 void precalculate();
229
230 WithReason<int64_t> calculateLength() const;
231 WithReason<int64_t> getNestedLength() const;
232 Depth calculateDepths() const;
233 Depth getNestedDepths() const;
234
238 const parser::ExecutionPartConstruct *invalidIC_{nullptr};
242 const parser::ExecutionPartConstruct *opaqueIC_{nullptr};
243
248 WithReason<int64_t> length_;
250 Depth depth_;
251
252 // The core structure of the class:
253 unsigned version_; // Needed for GetXyzWithReason
254 bool allowAllLoops_;
255 std::unique_ptr<Construct> entry_;
256 std::vector<LoopSequence> children_;
257};
258} // namespace omp
259} // namespace Fortran::semantics
260
261#endif // FORTRAN_SEMANTICS_OPENMP_UTILS_H
Definition expression.h:878
Definition char-block.h:28
Definition message.h:200
Definition message.h:332
Definition scope.h:67
Definition semantics.h:67
Definition symbol.h:825
Definition FIRType.h:103
Definition parse-tree.h:496
Definition parse-tree.h:1884
Definition parse-tree.h:1794
Definition parse-tree.h:1833
Definition parse-tree.h:554
Definition parse-tree.h:1682
Definition parse-tree.h:5036
Definition parse-tree.h:3513
Definition parse-tree.h:3538
Definition parse-tree.h:5412
Definition parse-tree.h:3644
A representation of a "because" message.
Definition openmp-utils.h:117
Definition openmp-utils.h:59
Definition openmp-utils.h:143