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"
28#include "llvm/Frontend/OpenMP/OMPVersion.h"
37namespace Fortran::semantics {
45using Fortran::parser::omp::BlockRange;
46using Fortran::parser::omp::ExecutionPartIterator;
47using Fortran::parser::omp::is_range_v;
48using Fortran::parser::omp::LoopNestIterator;
49using Fortran::parser::omp::LoopRange;
51template <
typename T,
typename U = std::remove_const_t<T>> U AsRvalue(T &t) {
55template <
typename T> T &&AsRvalue(T &&t) {
return std::move(t); }
57const Scope &GetScopingUnit(
const Scope &scope);
58const Scope &GetProgramUnit(
const Scope &scope);
66 value_type stmt()
const {
return value; }
67 operator bool()
const {
return stmt() !=
nullptr; }
73std::string ThisVersion(llvm::omp::Version version);
74std::string TryVersion(llvm::omp::Version version);
76const Symbol *GetObjectSymbol(
78const Symbol *GetArgumentSymbol(
81bool IsCommonBlock(
const Symbol &sym);
82bool IsExtendedListItem(
const Symbol &sym);
83bool IsVariableListItem(
const Symbol &sym);
84bool IsTypeParamInquiry(
const Symbol &sym);
85bool IsComplexPart(
const Symbol &sym);
86bool IsStructureComponent(
const Symbol &sym);
87bool IsPrivatizable(
const Symbol &sym);
88bool IsVarOrFunctionRef(
const MaybeExpr &expr);
92bool IsExtendedListItem(
94bool IsLocatorListItem(
96bool IsVariableListItem(
115const Symbol *FindUserReductionSymbol(
const Scope &scope,
117 bool *ambiguous =
nullptr);
126const Symbol *FindOperatorUserReductionSymbol(
const Scope &scope,
136bool IsMapEnteringType(parser::OmpMapType::Value type);
137bool IsMapExitingType(parser::OmpMapType::Value type);
143bool HasTemporaryStackDescriptor(
const Symbol &symbol);
145MaybeExpr GetEvaluateExpr(
const parser::Expr &parserExpr);
146template <
typename T> MaybeExpr GetEvaluateExpr(
const T &inp) {
147 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
150std::optional<evaluate::DynamicType> GetDynamicType(
153std::optional<bool> GetLogicalValue(
const SomeExpr &expr);
154std::optional<int64_t> GetIntValueFromExpr(
158std::optional<int64_t> GetIntValueFromExpr(
160 if (
auto *parserExpr{parser::Unwrap<parser::Expr>(wrappedExpr)}) {
161 return GetIntValueFromExpr(*parserExpr, semaCtx);
170template <
typename ClauseTy>
171std::optional<bool> GetLogicalArgument(
172 const std::optional<ClauseTy> &maybeClause, SemanticsContext &semaCtx) {
175 auto &parserExpr{parser::UnwrapRef<parser::Expr>(*maybeClause)};
176 evaluate::ExpressionAnalyzer ea{semaCtx};
177 if (
auto &&maybeExpr{ea.Analyze(parserExpr)}) {
178 if (
auto v{GetLogicalValue(*maybeExpr)}) {
186std::optional<bool> IsContiguous(
187 SemanticsContext &semaCtx,
const parser::OmpObject &
object);
191 const parser::ScalarExpr *expr;
199enum class UnsupportedSelectorFeature {
206 ClauseOrExtensionProperty,
212UnsupportedSelectorFeature FindUnsupportedSelectorFeature(
224std::optional<DynamicUserCondition> MakeVariantMatchInfo(
225 llvm::omp::VariantMatchInfo &vmi,
232class OmpVariantMatchContext :
public llvm::omp::OMPContext {
234 OmpVariantMatchContext(
bool isDeviceCompilation, llvm::Triple targetTriple,
235 llvm::Triple targetOffloadTriple, std::string targetFeatures,
239 bool matchesISATrait(llvm::StringRef rawString)
const override;
242 std::string features_;
245struct MetadirectiveCandidate {
247 llvm::omp::VariantMatchInfo vmi,
bool isExplicit,
248 std::optional<DynamicUserCondition> dynamicCondition = std::nullopt,
249 bool conditionShouldBeTrue =
true)
250 : spec{spec}, vmi{std::move(vmi)}, isExplicit{isExplicit},
251 dynamicCondition{dynamicCondition},
252 conditionShouldBeTrue{conditionShouldBeTrue} {}
255 llvm::omp::VariantMatchInfo vmi;
256 bool isExplicit{
false};
257 std::optional<DynamicUserCondition> dynamicCondition;
258 bool conditionShouldBeTrue{
true};
271std::optional<MetadirectiveCandidateSet> BuildMetadirectiveCandidateSet(
275std::optional<unsigned> SelectBestMetadirectiveCandidate(
282bool IsRepeatableMetadirectiveCondition(
286bool AreSameRepeatableMetadirectiveCondition(
const parser::ScalarExpr &left,
308bool MayVariantBeSelected(
312std::vector<SomeExpr> GetTopLevelDesignators(
const SomeExpr &expr);
313const SomeExpr *HasStorageOverlap(
321enum struct ListItemKind : uint32_t {
324 DirectiveSpecification,
335std::optional<ListItemKind> GetArgumentListItemKind(
336 llvm::omp::Clause clause, llvm::omp::Version version);
338bool IsLoopTransforming(llvm::omp::Directive dir);
339bool HasDataEnvironment(llvm::omp::Directive dir);
347 parser::OmpAtClause::ActionTime at{
348 parser::OmpAtClause::ActionTime::Compilation};
349 parser::OmpSeverityClause::SevLevel severity{
350 parser::OmpSeverityClause::SevLevel::Fatal};
360inline bool IsDoConcurrentLegal(llvm::omp::Version version) {
363 return version >= 60;
367 LoopControl(LoopControl &&x) =
default;
368 LoopControl(
const LoopControl &x) =
default;
369 LoopControl(
const parser::LoopControl::Bounds &x);
385 Reason(Reason &&) =
default;
386 Reason(
const Reason &);
387 Reason &operator=(Reason &&) =
default;
388 Reason &operator=(
const Reason &);
392 template <
typename... Ts> Reason &Say(Ts &&...args) {
393 msgs.Say(std::forward<Ts>(args)...);
397 Reason &Append(
const Reason &other) {
401 operator bool()
const {
return !msgs.empty(); }
404 void CopyFrom(
const Reason &other);
409template <
typename T>
struct WithReason {
410 std::optional<T> value;
413 WithReason() =
default;
414 WithReason(std::optional<T> v,
const Reason &r =
Reason())
415 : value(v), reason(r) {}
416 operator bool()
const {
return value.has_value(); }
431std::pair<WithReason<int64_t>,
bool> GetAffectedNestDepthWithReason(
436std::pair<WithReason<int64_t>,
bool> GetGeneratedNestDepthWithReason(
453std::optional<int64_t> GetMinimumSequenceCount(
454 std::optional<int64_t> first, std::optional<int64_t> count);
455std::optional<int64_t> GetMinimumSequenceCount(
456 std::optional<std::pair<int64_t, int64_t>> range);
463std::optional<std::vector<const parser::DoConstruct *>> CollectAffectedDoLoops(
475 llvm::omp::Version version,
bool allowAllLoops =
false,
478 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
479 LoopSequence(
const R &range, llvm::omp::Version version,
481 : version_(version), allowAllLoops_(allowAllLoops), semaCtx_(semaCtx) {
482 entry_ = std::make_unique<Construct>(range,
nullptr);
483 createChildrenFromRange(entry_->location);
496 bool isNest()
const {
return length_.value == 1; }
499 const Depth &depth()
const {
return depth_; }
500 const std::vector<LoopSequence> &children()
const {
return children_; }
501 const parser::ExecutionPartConstruct *owner()
const {
return entry_->owner; }
503 WithReason<bool> isWellFormedSequence()
const;
504 WithReason<bool> isWellFormedNest()
const;
510 std::vector<LoopControl> getLoopControls()
const;
513 WithReason<bool> isRectangular(
514 const std::vector<const LoopSequence *> &outer)
const;
517 using Construct = ExecutionPartIterator::Construct;
519 LoopSequence(std::unique_ptr<Construct> entry, llvm::omp::Version version,
520 bool allowAllLoops, SemanticsContext *semaCtx =
nullptr);
522 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
523 void createChildrenFromRange(
const R &range) {
524 createChildrenFromRange(range.begin(), range.end());
527 std::unique_ptr<Construct> createConstructEntry(
528 const parser::ExecutionPartConstruct &code);
530 void createChildrenFromRange(
531 ExecutionPartIterator::IteratorType begin,
532 ExecutionPartIterator::IteratorType end);
537 WithReason<int64_t> calculateLength()
const;
538 WithReason<int64_t> getNestedLength()
const;
539 Depth calculateDepths()
const;
540 Depth getNestedDepths()
const;
541 WithReason<int64_t> calculateHeight()
const;
546 const parser::ExecutionPartConstruct *invalidIC_{
nullptr};
550 const parser::ExecutionPartConstruct *opaqueIC_{
nullptr};
556 WithReason<int64_t> length_;
565 WithReason<int64_t> height_;
568 llvm::omp::Version version_;
570 std::unique_ptr<Construct> entry_;
571 std::vector<LoopSequence> children_;
572 SemanticsContext *semaCtx_{
nullptr};
581llvm::omp::TraitSet MapTraitSet(parser::OmpTraitSetSelectorName::Value name);
585llvm::omp::TraitSelector MapTraitSelector(
586 const parser::OmpTraitSelectorName &name, llvm::omp::TraitSet set);
589std::optional<bool> EvaluateUserCondition(
590 SemanticsContext &semaCtx,
const parser::ScalarExpr &scalarExpr);
593llvm::APInt *GetTraitScore(
594 const std::optional<parser::OmpTraitSelector::Properties> &props,
595 SemanticsContext &semaCtx, std::optional<llvm::APInt> &scoreStorage);
600void ProcessTraitProperties(llvm::omp::VariantMatchInfo &vmi,
601 llvm::omp::TraitSet set, llvm::omp::TraitSelector selector,
602 const std::optional<parser::OmpTraitSelector::Properties> &props,
603 llvm::APInt *scorePtr);
Definition expression.h:923
Definition char-block.h:26
Definition semantics.h:67
Definition openmp-utils.h:232
Definition parse-tree.h:501
Definition parse-tree.h:2281
Definition parse-tree.h:2368
Definition parse-tree.h:559
Definition parse-tree.h:1738
Definition parse-tree.h:592
Definition parse-tree.h:5276
Definition parse-tree.h:5283
Definition parse-tree.h:5350
Definition parse-tree.h:3623
Definition parse-tree.h:3648
Definition parse-tree.h:5656
Definition parse-tree.h:3746
Definition openmp-utils.h:55
Definition parse-tree.h:3886
Non-constant user condition expression and source for runtime lowering.
Definition openmp-utils.h:190
Definition openmp-utils.h:487
const LoopSequence * getNestedDoConcurrent() const
Definition openmp-utils.cpp:1844
Definition openmp-utils.h:346
A representation of a "because" message.
Definition openmp-utils.h:383
Definition openmp-utils.h:64
Definition openmp-utils.h:409