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/DenseMap.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/Frontend/OpenMP/OMPContext.h"
29#include "llvm/Frontend/OpenMP/OMPVersion.h"
38namespace Fortran::semantics {
46using Fortran::parser::omp::BlockRange;
47using Fortran::parser::omp::ExecutionPartIterator;
48using Fortran::parser::omp::is_range_v;
49using Fortran::parser::omp::LoopNestIterator;
50using Fortran::parser::omp::LoopRange;
52template <
typename T,
typename U = std::remove_const_t<T>> U AsRvalue(T &t) {
56template <
typename T> T &&AsRvalue(T &&t) {
return std::move(t); }
65 llvm::DenseMap<llvm::omp::Clause, llvm::omp::Directives> allowedClauses;
68bool IsClauseAllowedOnDirective(llvm::omp::Clause clauseId,
69 llvm::omp::Directive dirId, llvm::omp::Version ver,
72bool IsClauseAllowedOnDirective(llvm::omp::Clause clauseId,
73 llvm::omp::Directive dirId, llvm::omp::Version version,
76const Scope &GetScopingUnit(
const Scope &scope);
77const Scope &GetProgramUnit(
const Scope &scope);
85 value_type stmt()
const {
return value; }
86 operator bool()
const {
return stmt() !=
nullptr; }
92std::string ThisVersion(llvm::omp::Version version);
93std::string TryVersion(llvm::omp::Version version);
95const Symbol *GetObjectSymbol(
97const Symbol *GetArgumentSymbol(
100bool IsCommonBlock(
const Symbol &sym);
101bool IsExtendedListItem(
const Symbol &sym);
102bool IsVariableListItem(
const Symbol &sym);
103bool IsTypeParamInquiry(
const Symbol &sym);
104bool IsComplexPart(
const Symbol &sym);
105bool IsStructureComponent(
const Symbol &sym);
106bool IsPrivatizable(
const Symbol &sym);
107bool IsVarOrFunctionRef(
const MaybeExpr &expr);
111bool IsExtendedListItem(
113bool IsLocatorListItem(
115bool IsVariableListItem(
121std::optional<bool> IsSubstringAndValidate(
138const Symbol *FindUserReductionSymbol(
const Scope &scope,
140 bool *ambiguous =
nullptr);
149const Symbol *FindOperatorUserReductionSymbol(
const Scope &scope,
152bool IsMapEnteringType(parser::OmpMapType::Value type);
153bool IsMapExitingType(parser::OmpMapType::Value type);
159bool HasTemporaryStackDescriptor(
const Symbol &symbol);
161MaybeExpr GetEvaluateExpr(
const parser::Expr &parserExpr);
162template <
typename T> MaybeExpr GetEvaluateExpr(
const T &inp) {
163 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
166std::optional<evaluate::DynamicType> GetDynamicType(
169std::optional<bool> GetLogicalValue(
const SomeExpr &expr);
170std::optional<int64_t> GetIntValueFromExpr(
174std::optional<int64_t> GetIntValueFromExpr(
176 if (
auto *parserExpr{parser::Unwrap<parser::Expr>(wrappedExpr)}) {
177 return GetIntValueFromExpr(*parserExpr, semaCtx);
186template <
typename ClauseTy>
187std::optional<bool> GetLogicalArgument(
188 const std::optional<ClauseTy> &maybeClause, SemanticsContext &semaCtx) {
191 auto &parserExpr{parser::UnwrapRef<parser::Expr>(*maybeClause)};
192 evaluate::ExpressionAnalyzer ea{semaCtx};
193 if (
auto &&maybeExpr{ea.Analyze(parserExpr)}) {
194 if (
auto v{GetLogicalValue(*maybeExpr)}) {
202std::optional<bool> IsContiguous(
203 SemanticsContext &semaCtx,
const parser::OmpObject &
object);
207 const parser::ScalarExpr *expr;
215enum class UnsupportedSelectorFeature {
222 ClauseOrExtensionProperty,
228UnsupportedSelectorFeature FindUnsupportedSelectorFeature(
240std::optional<DynamicUserCondition> MakeVariantMatchInfo(
241 llvm::omp::VariantMatchInfo &vmi,
248class OmpVariantMatchContext :
public llvm::omp::OMPContext {
250 OmpVariantMatchContext(
bool isDeviceCompilation, llvm::Triple targetTriple,
251 llvm::Triple targetOffloadTriple, std::string targetFeatures,
255 bool matchesISATrait(llvm::StringRef rawString)
const override;
258 std::string features_;
261struct MetadirectiveCandidate {
263 llvm::omp::VariantMatchInfo vmi,
bool isExplicit,
264 std::optional<DynamicUserCondition> dynamicCondition = std::nullopt,
265 bool conditionShouldBeTrue =
true)
266 : spec{spec}, vmi{std::move(vmi)}, isExplicit{isExplicit},
267 dynamicCondition{dynamicCondition},
268 conditionShouldBeTrue{conditionShouldBeTrue} {}
271 llvm::omp::VariantMatchInfo vmi;
272 bool isExplicit{
false};
273 std::optional<DynamicUserCondition> dynamicCondition;
274 bool conditionShouldBeTrue{
true};
283const parser::OmpContextSelector *GetContextSelector(
292std::optional<MetadirectiveCandidateSet> BuildMetadirectiveCandidateSet(
296std::optional<unsigned> SelectBestMetadirectiveCandidate(
303bool IsRepeatableMetadirectiveCondition(
307bool AreSameRepeatableMetadirectiveCondition(
const parser::ScalarExpr &left,
329bool MayVariantBeSelected(
333std::vector<SomeExpr> GetTopLevelDesignators(
const SomeExpr &expr);
334const SomeExpr *HasStorageOverlap(
342enum struct ListItemKind : uint32_t {
345 DirectiveSpecification,
356std::optional<ListItemKind> GetArgumentListItemKind(
357 llvm::omp::Clause clause, llvm::omp::Version version);
359bool IsLoopTransforming(llvm::omp::Directive dir);
360bool HasDataEnvironment(llvm::omp::Directive dir);
368 parser::OmpAtClause::ActionTime at{
369 parser::OmpAtClause::ActionTime::Compilation};
370 parser::OmpSeverityClause::SevLevel severity{
371 parser::OmpSeverityClause::SevLevel::Fatal};
381inline bool IsDoConcurrentLegal(llvm::omp::Version version) {
384 return version >= 60;
388 LoopControl(LoopControl &&x) =
default;
389 LoopControl(
const LoopControl &x) =
default;
390 LoopControl(
const parser::LoopControl::Bounds &x);
406 Reason(Reason &&) =
default;
407 Reason(
const Reason &);
408 Reason &operator=(Reason &&) =
default;
409 Reason &operator=(
const Reason &);
413 template <
typename... Ts> Reason &Say(Ts &&...args) {
414 msgs.Say(std::forward<Ts>(args)...);
418 Reason &Append(
const Reason &other) {
422 operator bool()
const {
return !msgs.empty(); }
425 void CopyFrom(
const Reason &other);
430template <
typename T>
struct WithReason {
431 std::optional<T> value;
434 WithReason() =
default;
435 WithReason(std::optional<T> v,
const Reason &r =
Reason())
436 : value(v), reason(r) {}
437 operator bool()
const {
return value.has_value(); }
452std::pair<WithReason<int64_t>,
bool> GetAffectedNestDepthWithReason(
457std::pair<WithReason<int64_t>,
bool> GetGeneratedNestDepthWithReason(
474std::optional<int64_t> GetMinimumSequenceCount(
475 std::optional<int64_t> first, std::optional<int64_t> count);
476std::optional<int64_t> GetMinimumSequenceCount(
477 std::optional<std::pair<int64_t, int64_t>> range);
484std::optional<std::vector<const parser::DoConstruct *>> CollectAffectedDoLoops(
496 llvm::omp::Version version,
bool allowAllLoops =
false,
499 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
500 LoopSequence(
const R &range, llvm::omp::Version version,
502 : version_(version), allowAllLoops_(allowAllLoops), semaCtx_(semaCtx) {
503 entry_ = std::make_unique<Construct>(range,
nullptr);
504 createChildrenFromRange(entry_->location);
517 bool isNest()
const {
return length_.value == 1; }
520 const Depth &depth()
const {
return depth_; }
521 const std::vector<LoopSequence> &children()
const {
return children_; }
522 const parser::ExecutionPartConstruct *owner()
const {
return entry_->owner; }
524 WithReason<bool> isWellFormedSequence()
const;
525 WithReason<bool> isWellFormedNest()
const;
531 std::vector<LoopControl> getLoopControls()
const;
534 WithReason<bool> isRectangular(
535 const std::vector<const LoopSequence *> &outer)
const;
538 using Construct = ExecutionPartIterator::Construct;
540 LoopSequence(std::unique_ptr<Construct> entry, llvm::omp::Version version,
541 bool allowAllLoops, SemanticsContext *semaCtx =
nullptr);
543 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
544 void createChildrenFromRange(
const R &range) {
545 createChildrenFromRange(range.begin(), range.end());
548 std::unique_ptr<Construct> createConstructEntry(
549 const parser::ExecutionPartConstruct &code);
551 void createChildrenFromRange(
552 ExecutionPartIterator::IteratorType begin,
553 ExecutionPartIterator::IteratorType end);
558 WithReason<int64_t> calculateLength()
const;
559 WithReason<int64_t> getNestedLength()
const;
560 Depth calculateDepths()
const;
561 Depth getNestedDepths()
const;
562 WithReason<int64_t> calculateHeight()
const;
567 const parser::ExecutionPartConstruct *invalidIC_{
nullptr};
571 const parser::ExecutionPartConstruct *opaqueIC_{
nullptr};
577 WithReason<int64_t> length_;
586 WithReason<int64_t> height_;
589 llvm::omp::Version version_;
591 std::unique_ptr<Construct> entry_;
592 std::vector<LoopSequence> children_;
593 SemanticsContext *semaCtx_{
nullptr};
602llvm::omp::TraitSet MapTraitSet(parser::OmpTraitSetSelectorName::Value name);
606llvm::omp::TraitSelector MapTraitSelector(
607 const parser::OmpTraitSelectorName &name, llvm::omp::TraitSet set);
610std::optional<bool> EvaluateUserCondition(
611 SemanticsContext &semaCtx,
const parser::ScalarExpr &scalarExpr);
614llvm::APInt *GetTraitScore(
615 const std::optional<parser::OmpTraitSelector::Properties> &props,
616 SemanticsContext &semaCtx, std::optional<llvm::APInt> &scoreStorage);
621void ProcessTraitProperties(llvm::omp::VariantMatchInfo &vmi,
622 llvm::omp::TraitSet set, llvm::omp::TraitSelector selector,
623 const std::optional<parser::OmpTraitSelector::Properties> &props,
624 llvm::APInt *scorePtr);
Definition expression.h:960
Definition char-block.h:27
Definition semantics.h:71
Definition openmp-utils.h:248
Definition parse-tree.h:501
Definition parse-tree.h:2292
Definition parse-tree.h:2379
Definition parse-tree.h:559
Definition parse-tree.h:1749
Definition parse-tree.h:592
Definition parse-tree.h:5288
Definition parse-tree.h:5272
Definition parse-tree.h:5295
Definition parse-tree.h:5362
Definition parse-tree.h:3634
Definition parse-tree.h:3659
Definition parse-tree.h:5668
Definition parse-tree.h:3757
Definition openmp-utils.h:114
Definition parse-tree.h:3897
Non-constant user condition expression and source for runtime lowering.
Definition openmp-utils.h:206
Definition openmp-utils.h:508
const LoopSequence * getNestedDoConcurrent() const
Definition openmp-utils.cpp:1883
Definition openmp-utils.h:367
A representation of a "because" message.
Definition openmp-utils.h:404
Definition openmp-utils.h:58
Definition openmp-utils.h:83
Definition openmp-utils.h:430