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/ADT/SmallVector.h"
27#include "llvm/Frontend/OpenMP/OMPContext.h"
36namespace Fortran::semantics {
44using Fortran::parser::omp::BlockRange;
45using Fortran::parser::omp::ExecutionPartIterator;
46using Fortran::parser::omp::is_range_v;
47using Fortran::parser::omp::LoopNestIterator;
48using Fortran::parser::omp::LoopRange;
50template <
typename T,
typename U = std::remove_const_t<T>> U AsRvalue(T &t) {
54template <
typename T> T &&AsRvalue(T &&t) {
return std::move(t); }
56const Scope &GetScopingUnit(
const Scope &scope);
57const Scope &GetProgramUnit(
const Scope &scope);
59template <
typename T>
struct WithSource {
61 typename U = std::remove_reference_t<T>,
62 typename = std::enable_if_t<std::is_default_constructible_v<U>>>
63 WithSource() : value(), source() {}
64 WithSource(
const WithSource<T> &) =
default;
65 WithSource(WithSource<T> &&) =
default;
68 WithSource &operator=(
const WithSource<T> &) =
default;
69 WithSource &operator=(WithSource<T> &&) =
default;
80 using WithSource<value_type>::WithSource;
81 value_type stmt()
const {
return value; }
82 operator bool()
const {
return stmt() !=
nullptr; }
88std::string ThisVersion(
unsigned version);
89std::string TryVersion(
unsigned version);
91const Symbol *GetObjectSymbol(
93const Symbol *GetArgumentSymbol(
96bool IsCommonBlock(
const Symbol &sym);
97bool IsExtendedListItem(
const Symbol &sym);
98bool IsVariableListItem(
const Symbol &sym);
99bool IsTypeParamInquiry(
const Symbol &sym);
100bool IsComplexPart(
const Symbol &sym);
101bool IsStructureComponent(
const Symbol &sym);
102bool IsPrivatizable(
const Symbol &sym);
103bool IsVarOrFunctionRef(
const MaybeExpr &expr);
107bool IsExtendedListItem(
109bool IsLocatorListItem(
111bool IsVariableListItem(
130const Symbol *FindUserReductionSymbol(
const Scope &scope,
132 bool *ambiguous =
nullptr);
141const Symbol *FindOperatorUserReductionSymbol(
const Scope &scope,
151bool IsMapEnteringType(parser::OmpMapType::Value type);
152bool IsMapExitingType(parser::OmpMapType::Value type);
158bool HasTemporaryStackDescriptor(
const Symbol &symbol);
160MaybeExpr GetEvaluateExpr(
const parser::Expr &parserExpr);
161template <
typename T> MaybeExpr GetEvaluateExpr(
const T &inp) {
162 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
165std::optional<evaluate::DynamicType> GetDynamicType(
168std::optional<bool> GetLogicalValue(
const SomeExpr &expr);
169std::optional<int64_t> GetIntValueFromExpr(
173std::optional<int64_t> GetIntValueFromExpr(
175 if (
auto *parserExpr{parser::Unwrap<parser::Expr>(wrappedExpr)}) {
176 return GetIntValueFromExpr(*parserExpr, semaCtx);
185template <
typename ClauseTy>
186std::optional<bool> GetLogicalArgument(
187 const std::optional<ClauseTy> &maybeClause, SemanticsContext &semaCtx) {
190 auto &parserExpr{parser::UnwrapRef<parser::Expr>(*maybeClause)};
191 evaluate::ExpressionAnalyzer ea{semaCtx};
192 if (
auto &&maybeExpr{ea.Analyze(parserExpr)}) {
193 if (
auto v{GetLogicalValue(*maybeExpr)}) {
201std::optional<bool> IsContiguous(
202 SemanticsContext &semaCtx,
const parser::OmpObject &
object);
206 const parser::ScalarExpr *expr;
214enum class UnsupportedSelectorFeature {
221 ClauseOrExtensionProperty,
227UnsupportedSelectorFeature FindUnsupportedSelectorFeature(
239std::optional<DynamicUserCondition> MakeVariantMatchInfo(
240 llvm::omp::VariantMatchInfo &vmi,
247class OmpVariantMatchContext :
public llvm::omp::OMPContext {
249 OmpVariantMatchContext(
bool isDeviceCompilation, llvm::Triple targetTriple,
250 llvm::Triple targetOffloadTriple, std::string targetFeatures,
254 bool matchesISATrait(llvm::StringRef rawString)
const override;
257 std::string features_;
266bool MayVariantBeSelected(
270std::vector<SomeExpr> GetTopLevelDesignators(
const SomeExpr &expr);
271const SomeExpr *HasStorageOverlap(
279enum struct ListItemKind : uint32_t {
282 DirectiveSpecification,
293std::optional<ListItemKind> GetArgumentListItemKind(
294 llvm::omp::Clause clause,
unsigned version);
296bool IsLoopTransforming(llvm::omp::Directive dir);
297bool HasDataEnvironment(llvm::omp::Directive dir);
305 parser::OmpAtClause::ActionTime at{
306 parser::OmpAtClause::ActionTime::Compilation};
307 parser::OmpSeverityClause::SevLevel severity{
308 parser::OmpSeverityClause::SevLevel::Fatal};
316inline bool IsDoConcurrentLegal(
unsigned version) {
319 return version >= 60;
323 LoopControl(LoopControl &&x) =
default;
324 LoopControl(
const LoopControl &x) =
default;
325 LoopControl(
const parser::LoopControl::Bounds &x);
340 Reason(Reason &&) =
default;
341 Reason(
const Reason &);
342 Reason &operator=(Reason &&) =
default;
343 Reason &operator=(
const Reason &);
347 template <
typename... Ts> Reason &Say(Ts &&...args) {
348 msgs.Say(std::forward<Ts>(args)...);
352 Reason &Append(
const Reason &other) {
356 operator bool()
const {
return !msgs.empty(); }
359 void CopyFrom(
const Reason &other);
364template <
typename T>
struct WithReason {
365 std::optional<T> value;
368 WithReason() =
default;
369 WithReason(std::optional<T> v,
const Reason &r =
Reason())
370 : value(v), reason(r) {}
371 operator bool()
const {
return value.has_value(); }
386std::pair<WithReason<int64_t>,
bool> GetAffectedNestDepthWithReason(
391std::pair<WithReason<int64_t>,
bool> GetGeneratedNestDepthWithReason(
408std::optional<int64_t> GetMinimumSequenceCount(
409 std::optional<int64_t> first, std::optional<int64_t> count);
410std::optional<int64_t> GetMinimumSequenceCount(
411 std::optional<std::pair<int64_t, int64_t>> range);
418std::optional<std::vector<const parser::DoConstruct *>> CollectAffectedDoLoops(
432 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
433 LoopSequence(
const R &range,
unsigned version,
bool allowAllLoops =
false,
435 : version_(version), allowAllLoops_(allowAllLoops), semaCtx_(semaCtx) {
436 entry_ = std::make_unique<Construct>(range,
nullptr);
437 createChildrenFromRange(entry_->location);
450 bool isNest()
const {
return length_.value == 1; }
453 const Depth &depth()
const {
return depth_; }
454 const std::vector<LoopSequence> &children()
const {
return children_; }
455 const parser::ExecutionPartConstruct *owner()
const {
return entry_->owner; }
457 WithReason<bool> isWellFormedSequence()
const;
458 WithReason<bool> isWellFormedNest()
const;
464 std::vector<LoopControl> getLoopControls()
const;
467 WithReason<bool> isRectangular(
468 const std::vector<const LoopSequence *> &outer)
const;
471 using Construct = ExecutionPartIterator::Construct;
473 LoopSequence(std::unique_ptr<Construct> entry,
unsigned version,
474 bool allowAllLoops, SemanticsContext *semaCtx =
nullptr);
476 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
477 void createChildrenFromRange(
const R &range) {
478 createChildrenFromRange(range.begin(), range.end());
481 std::unique_ptr<Construct> createConstructEntry(
482 const parser::ExecutionPartConstruct &code);
484 void createChildrenFromRange(
485 ExecutionPartIterator::IteratorType begin,
486 ExecutionPartIterator::IteratorType end);
491 WithReason<int64_t> calculateLength()
const;
492 WithReason<int64_t> getNestedLength()
const;
493 Depth calculateDepths()
const;
494 Depth getNestedDepths()
const;
495 WithReason<int64_t> calculateHeight()
const;
500 const parser::ExecutionPartConstruct *invalidIC_{
nullptr};
504 const parser::ExecutionPartConstruct *opaqueIC_{
nullptr};
510 WithReason<int64_t> length_;
519 WithReason<int64_t> height_;
524 std::unique_ptr<Construct> entry_;
525 std::vector<LoopSequence> children_;
526 SemanticsContext *semaCtx_{
nullptr};
535llvm::omp::TraitSet MapTraitSet(parser::OmpTraitSetSelectorName::Value name);
539llvm::omp::TraitSelector MapTraitSelector(
540 const parser::OmpTraitSelectorName &name, llvm::omp::TraitSet set);
543std::optional<bool> EvaluateUserCondition(
544 SemanticsContext &semaCtx,
const parser::ScalarExpr &scalarExpr);
547llvm::APInt *GetTraitScore(
548 const std::optional<parser::OmpTraitSelector::Properties> &props,
549 SemanticsContext &semaCtx, std::optional<llvm::APInt> &scoreStorage);
554void ProcessTraitProperties(llvm::omp::VariantMatchInfo &vmi,
555 llvm::omp::TraitSet set, llvm::omp::TraitSelector selector,
556 const std::optional<parser::OmpTraitSelector::Properties> &props,
557 llvm::APInt *scorePtr);
Definition expression.h:920
Definition char-block.h:26
Definition semantics.h:67
Definition openmp-utils.h:247
Definition parse-tree.h:500
Definition parse-tree.h:2280
Definition parse-tree.h:2367
Definition parse-tree.h:558
Definition parse-tree.h:1737
Definition parse-tree.h:591
Definition parse-tree.h:5166
Definition parse-tree.h:5233
Definition parse-tree.h:3622
Definition parse-tree.h:3647
Definition parse-tree.h:5539
Definition parse-tree.h:3744
Definition parse-tree.h:3884
Non-constant user condition expression and source for runtime lowering.
Definition openmp-utils.h:205
Definition openmp-utils.h:441
const LoopSequence * getNestedDoConcurrent() const
Definition openmp-utils.cpp:1837
Definition openmp-utils.h:304
A representation of a "because" message.
Definition openmp-utils.h:338
Definition openmp-utils.h:79
Definition openmp-utils.h:364
Definition openmp-utils.h:59