13#ifndef FORTRAN_SEMANTICS_OPENMP_UTILS_H
14#define FORTRAN_SEMANTICS_OPENMP_UTILS_H
16#include "flang/Evaluate/type.h"
17#include "flang/Parser/char-block.h"
18#include "flang/Parser/message.h"
19#include "flang/Parser/openmp-utils.h"
20#include "flang/Parser/parse-tree.h"
21#include "flang/Parser/tools.h"
22#include "flang/Semantics/tools.h"
24#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/ArrayRef.h"
26#include "llvm/Frontend/OpenMP/OMPContext.h"
35namespace Fortran::semantics {
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;
48template <
typename T,
typename U = std::remove_const_t<T>> U AsRvalue(T &t) {
52template <
typename T> T &&AsRvalue(T &&t) {
return std::move(t); }
54const Scope &GetScopingUnit(
const Scope &scope);
55const Scope &GetProgramUnit(
const Scope &scope);
57template <
typename T>
struct WithSource {
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;
66 WithSource &operator=(
const WithSource<T> &) =
default;
67 WithSource &operator=(WithSource<T> &&) =
default;
78 using WithSource<value_type>::WithSource;
79 value_type stmt()
const {
return value; }
80 operator bool()
const {
return stmt() !=
nullptr; }
86std::string ThisVersion(
unsigned version);
87std::string TryVersion(
unsigned version);
89const Symbol *GetObjectSymbol(
91const Symbol *GetArgumentSymbol(
94bool IsCommonBlock(
const Symbol &sym);
95bool IsExtendedListItem(
const Symbol &sym);
96bool IsVariableListItem(
const Symbol &sym);
97bool IsTypeParamInquiry(
const Symbol &sym);
98bool IsComplexPart(
const Symbol &sym);
99bool IsStructureComponent(
const Symbol &sym);
100bool IsPrivatizable(
const Symbol &sym);
101bool IsVarOrFunctionRef(
const MaybeExpr &expr);
105bool IsExtendedListItem(
107bool IsLocatorListItem(
109bool IsVariableListItem(
117bool IsMapEnteringType(parser::OmpMapType::Value type);
118bool IsMapExitingType(parser::OmpMapType::Value type);
124bool HasTemporaryStackDescriptor(
const Symbol &symbol);
126MaybeExpr GetEvaluateExpr(
const parser::Expr &parserExpr);
127template <
typename T> MaybeExpr GetEvaluateExpr(
const T &inp) {
128 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
131std::optional<evaluate::DynamicType> GetDynamicType(
134std::optional<bool> GetLogicalValue(
const SomeExpr &expr);
135std::optional<int64_t> GetIntValueFromExpr(
139std::optional<int64_t> GetIntValueFromExpr(
141 if (
auto *parserExpr{parser::Unwrap<parser::Expr>(wrappedExpr)}) {
142 return GetIntValueFromExpr(*parserExpr, semaCtx);
151template <
typename ClauseTy>
152std::optional<bool> GetLogicalArgument(
153 const std::optional<ClauseTy> &maybeClause, SemanticsContext &semaCtx) {
156 auto &parserExpr{parser::UnwrapRef<parser::Expr>(*maybeClause)};
157 evaluate::ExpressionAnalyzer ea{semaCtx};
158 if (
auto &&maybeExpr{ea.Analyze(parserExpr)}) {
159 if (
auto v{GetLogicalValue(*maybeExpr)}) {
167std::optional<bool> IsContiguous(
168 SemanticsContext &semaCtx,
const parser::OmpObject &
object);
172 const parser::ScalarExpr *expr;
180enum class UnsupportedSelectorFeature {
187 ClauseOrExtensionProperty,
193UnsupportedSelectorFeature FindUnsupportedSelectorFeature(
205std::optional<DynamicUserCondition> MakeVariantMatchInfo(
206 llvm::omp::VariantMatchInfo &vmi,
213class OmpVariantMatchContext :
public llvm::omp::OMPContext {
215 OmpVariantMatchContext(
bool isDeviceCompilation, llvm::Triple targetTriple,
216 llvm::Triple targetOffloadTriple, std::string targetFeatures,
220 bool matchesISATrait(llvm::StringRef rawString)
const override;
223 std::string features_;
226std::vector<SomeExpr> GetTopLevelDesignators(
const SomeExpr &expr);
227const SomeExpr *HasStorageOverlap(
235enum struct ListItemKind : uint32_t {
238 DirectiveSpecification,
249std::optional<ListItemKind> GetArgumentListItemKind(
250 llvm::omp::Clause clause,
unsigned version);
252bool IsLoopTransforming(llvm::omp::Directive dir);
253bool HasDataEnvironment(llvm::omp::Directive dir);
257inline bool IsDoConcurrentLegal(
unsigned version) {
260 return version >= 60;
264 LoopControl(LoopControl &&x) =
default;
265 LoopControl(
const LoopControl &x) =
default;
266 LoopControl(
const parser::LoopControl::Bounds &x);
281 Reason(Reason &&) =
default;
282 Reason(
const Reason &);
283 Reason &operator=(Reason &&) =
default;
284 Reason &operator=(
const Reason &);
288 template <
typename... Ts> Reason &Say(Ts &&...args) {
289 msgs.Say(std::forward<Ts>(args)...);
293 Reason &Append(
const Reason &other) {
297 operator bool()
const {
return !msgs.empty(); }
300 void CopyFrom(
const Reason &other);
305template <
typename T>
struct WithReason {
306 std::optional<T> value;
309 WithReason() =
default;
310 WithReason(std::optional<T> v,
const Reason &r =
Reason())
311 : value(v), reason(r) {}
312 operator bool()
const {
return value.has_value(); }
327std::pair<WithReason<int64_t>,
bool> GetAffectedNestDepthWithReason(
332std::pair<WithReason<int64_t>,
bool> GetGeneratedNestDepthWithReason(
349std::optional<int64_t> GetMinimumSequenceCount(
350 std::optional<int64_t> first, std::optional<int64_t> count);
351std::optional<int64_t> GetMinimumSequenceCount(
352 std::optional<std::pair<int64_t, int64_t>> range);
359std::optional<std::vector<const parser::DoConstruct *>> CollectAffectedDoLoops(
367 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
368 LoopSequence(
const R &range,
unsigned version,
bool allowAllLoops =
false,
370 : version_(version), allowAllLoops_(allowAllLoops), semaCtx_(semaCtx) {
371 entry_ = std::make_unique<Construct>(range,
nullptr);
372 createChildrenFromRange(entry_->location);
385 bool isNest()
const {
return length_.value == 1; }
388 const Depth &depth()
const {
return depth_; }
389 const std::vector<LoopSequence> &children()
const {
return children_; }
390 const parser::ExecutionPartConstruct *owner()
const {
return entry_->owner; }
392 WithReason<bool> isWellFormedSequence()
const;
393 WithReason<bool> isWellFormedNest()
const;
399 std::vector<LoopControl> getLoopControls()
const;
402 WithReason<bool> isRectangular(
403 const std::vector<const LoopSequence *> &outer)
const;
406 using Construct = ExecutionPartIterator::Construct;
408 LoopSequence(std::unique_ptr<Construct> entry,
unsigned version,
409 bool allowAllLoops, SemanticsContext *semaCtx =
nullptr);
411 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
412 void createChildrenFromRange(
const R &range) {
413 createChildrenFromRange(range.begin(), range.end());
416 std::unique_ptr<Construct> createConstructEntry(
417 const parser::ExecutionPartConstruct &code);
419 void createChildrenFromRange(
420 ExecutionPartIterator::IteratorType begin,
421 ExecutionPartIterator::IteratorType end);
426 WithReason<int64_t> calculateLength()
const;
427 WithReason<int64_t> getNestedLength()
const;
428 Depth calculateDepths()
const;
429 Depth getNestedDepths()
const;
430 WithReason<int64_t> calculateHeight()
const;
435 const parser::ExecutionPartConstruct *invalidIC_{
nullptr};
439 const parser::ExecutionPartConstruct *opaqueIC_{
nullptr};
445 WithReason<int64_t> length_;
454 WithReason<int64_t> height_;
459 std::unique_ptr<Construct> entry_;
460 std::vector<LoopSequence> children_;
461 SemanticsContext *semaCtx_{
nullptr};
470llvm::omp::TraitSet MapTraitSet(parser::OmpTraitSetSelectorName::Value name);
474llvm::omp::TraitSelector MapTraitSelector(
475 const parser::OmpTraitSelectorName &name, llvm::omp::TraitSet set);
478std::optional<bool> EvaluateUserCondition(
479 SemanticsContext &semaCtx,
const parser::ScalarExpr &scalarExpr);
482llvm::APInt *GetTraitScore(
483 const std::optional<parser::OmpTraitSelector::Properties> &props,
484 SemanticsContext &semaCtx, std::optional<llvm::APInt> &scoreStorage);
489void ProcessTraitProperties(llvm::omp::VariantMatchInfo &vmi,
490 llvm::omp::TraitSet set, llvm::omp::TraitSelector selector,
491 const std::optional<parser::OmpTraitSelector::Properties> &props,
492 llvm::APInt *scorePtr);
Definition expression.h:920
Definition char-block.h:26
Definition semantics.h:67
Definition parse-tree.h:500
Definition parse-tree.h:2278
Definition parse-tree.h:2365
Definition parse-tree.h:558
Definition parse-tree.h:1735
Definition parse-tree.h:591
Definition parse-tree.h:5162
Definition parse-tree.h:3620
Definition parse-tree.h:3645
Definition parse-tree.h:5535
Definition parse-tree.h:3742
Definition parse-tree.h:3882
Non-constant user condition expression and source for runtime lowering.
Definition openmp-utils.h:171
Definition openmp-utils.h:376
const LoopSequence * getNestedDoConcurrent() const
Definition openmp-utils.cpp:1736
A representation of a "because" message.
Definition openmp-utils.h:279
Definition openmp-utils.h:77
Definition openmp-utils.h:305
Definition openmp-utils.h:57