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);
113bool IsExtendedListItem(
115bool IsLocatorListItem(
117bool IsVariableListItem(
123std::optional<bool> IsSubstringAndValidate(
144const Symbol *FindUserReductionSymbol(
const Scope &scope,
146 bool *ambiguous =
nullptr);
155const Symbol *FindOperatorUserReductionSymbol(
const Scope &scope,
158bool IsMapEnteringType(parser::OmpMapType::Value type);
159bool IsMapExitingType(parser::OmpMapType::Value type);
165bool HasTemporaryStackDescriptor(
const Symbol &symbol);
167MaybeExpr GetEvaluateExpr(
const parser::Expr &parserExpr);
168template <
typename T> MaybeExpr GetEvaluateExpr(
const T &inp) {
169 return GetEvaluateExpr(parser::UnwrapRef<parser::Expr>(inp));
172std::optional<evaluate::DynamicType> GetDynamicType(
175std::optional<bool> GetLogicalValue(
const SomeExpr &expr);
176std::optional<int64_t> GetIntValueFromExpr(
180std::optional<int64_t> GetIntValueFromExpr(
182 if (
auto *parserExpr{parser::Unwrap<parser::Expr>(wrappedExpr)}) {
183 return GetIntValueFromExpr(*parserExpr, semaCtx);
192template <
typename ClauseTy>
193std::optional<bool> GetLogicalArgument(
194 const std::optional<ClauseTy> &maybeClause, SemanticsContext &semaCtx) {
197 auto &parserExpr{parser::UnwrapRef<parser::Expr>(*maybeClause)};
198 evaluate::ExpressionAnalyzer ea{semaCtx};
199 if (
auto &&maybeExpr{ea.Analyze(parserExpr)}) {
200 if (
auto v{GetLogicalValue(*maybeExpr)}) {
208std::optional<bool> IsContiguous(
209 SemanticsContext &semaCtx,
const parser::OmpObject &
object);
213 const parser::ScalarExpr *expr;
221enum class UnsupportedSelectorFeature {
228 ClauseOrExtensionProperty,
234UnsupportedSelectorFeature FindUnsupportedSelectorFeature(
246std::optional<DynamicUserCondition> MakeVariantMatchInfo(
247 llvm::omp::VariantMatchInfo &vmi,
254class OmpVariantMatchContext :
public llvm::omp::OMPContext {
256 OmpVariantMatchContext(
bool isDeviceCompilation, llvm::Triple targetTriple,
257 llvm::Triple targetOffloadTriple, std::string targetFeatures,
261 bool matchesISATrait(llvm::StringRef rawString)
const override;
264 std::string features_;
267struct MetadirectiveCandidate {
269 llvm::omp::VariantMatchInfo vmi,
bool isExplicit,
270 std::optional<DynamicUserCondition> dynamicCondition = std::nullopt,
271 bool conditionShouldBeTrue =
true)
272 : spec{spec}, vmi{std::move(vmi)}, isExplicit{isExplicit},
273 dynamicCondition{dynamicCondition},
274 conditionShouldBeTrue{conditionShouldBeTrue} {}
277 llvm::omp::VariantMatchInfo vmi;
278 bool isExplicit{
false};
279 std::optional<DynamicUserCondition> dynamicCondition;
280 bool conditionShouldBeTrue{
true};
289const parser::OmpContextSelector *GetContextSelector(
298std::optional<MetadirectiveCandidateSet> BuildMetadirectiveCandidateSet(
302std::optional<unsigned> SelectBestMetadirectiveCandidate(
309bool IsRepeatableMetadirectiveCondition(
313bool AreSameRepeatableMetadirectiveCondition(
const parser::ScalarExpr &left,
335bool MayVariantBeSelected(
339std::vector<SomeExpr> GetTopLevelDesignators(
const SomeExpr &expr);
340const SomeExpr *HasStorageOverlap(
348enum struct ListItemKind : uint32_t {
351 DirectiveSpecification,
362std::optional<ListItemKind> GetArgumentListItemKind(
363 llvm::omp::Clause clause, llvm::omp::Version version);
365bool IsLoopTransforming(llvm::omp::Directive dir);
366bool HasDataEnvironment(llvm::omp::Directive dir);
374 parser::OmpAtClause::ActionTime at{
375 parser::OmpAtClause::ActionTime::Compilation};
376 parser::OmpSeverityClause::SevLevel severity{
377 parser::OmpSeverityClause::SevLevel::Fatal};
387inline bool IsDoConcurrentLegal(llvm::omp::Version version) {
390 return version >= 60;
394 LoopControl(LoopControl &&x) =
default;
395 LoopControl(
const LoopControl &x) =
default;
396 LoopControl(
const parser::LoopControl::Bounds &x);
412 Reason(Reason &&) =
default;
413 Reason(
const Reason &);
414 Reason &operator=(Reason &&) =
default;
415 Reason &operator=(
const Reason &);
419 template <
typename... Ts> Reason &Say(Ts &&...args) {
420 msgs.Say(std::forward<Ts>(args)...);
424 Reason &Append(
const Reason &other) {
428 operator bool()
const {
return !msgs.empty(); }
431 void CopyFrom(
const Reason &other);
436template <
typename T>
struct WithReason {
437 std::optional<T> value;
440 WithReason() =
default;
441 WithReason(std::optional<T> v,
const Reason &r =
Reason())
442 : value(v), reason(r) {}
443 operator bool()
const {
return value.has_value(); }
458std::pair<WithReason<int64_t>,
bool> GetAffectedNestDepthWithReason(
463std::pair<WithReason<int64_t>,
bool> GetGeneratedNestDepthWithReason(
480std::optional<int64_t> GetMinimumSequenceCount(
481 std::optional<int64_t> first, std::optional<int64_t> count);
482std::optional<int64_t> GetMinimumSequenceCount(
483 std::optional<std::pair<int64_t, int64_t>> range);
490std::optional<std::vector<const parser::DoConstruct *>> CollectAffectedDoLoops(
502 llvm::omp::Version version,
bool allowAllLoops =
false,
505 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
506 LoopSequence(
const R &range, llvm::omp::Version version,
508 : version_(version), allowAllLoops_(allowAllLoops), semaCtx_(semaCtx) {
509 entry_ = std::make_unique<Construct>(range,
nullptr);
510 createChildrenFromRange(entry_->location);
523 bool isNest()
const {
return length_.value == 1; }
526 const Depth &depth()
const {
return depth_; }
527 const std::vector<LoopSequence> &children()
const {
return children_; }
528 const parser::ExecutionPartConstruct *owner()
const {
return entry_->owner; }
530 WithReason<bool> isWellFormedSequence()
const;
531 WithReason<bool> isWellFormedNest()
const;
537 std::vector<LoopControl> getLoopControls()
const;
540 WithReason<bool> isRectangular(
541 const std::vector<const LoopSequence *> &outer)
const;
544 using Construct = ExecutionPartIterator::Construct;
546 LoopSequence(std::unique_ptr<Construct> entry, llvm::omp::Version version,
547 bool allowAllLoops, SemanticsContext *semaCtx =
nullptr);
549 template <
typename R,
typename = std::enable_if_t<is_range_v<R>>>
550 void createChildrenFromRange(
const R &range) {
551 createChildrenFromRange(range.begin(), range.end());
554 std::unique_ptr<Construct> createConstructEntry(
555 const parser::ExecutionPartConstruct &code);
557 void createChildrenFromRange(
558 ExecutionPartIterator::IteratorType begin,
559 ExecutionPartIterator::IteratorType end);
564 WithReason<int64_t> calculateLength()
const;
565 WithReason<int64_t> getNestedLength()
const;
566 Depth calculateDepths()
const;
567 Depth getNestedDepths()
const;
568 WithReason<int64_t> calculateHeight()
const;
573 const parser::ExecutionPartConstruct *invalidIC_{
nullptr};
577 const parser::ExecutionPartConstruct *opaqueIC_{
nullptr};
583 WithReason<int64_t> length_;
592 WithReason<int64_t> height_;
595 llvm::omp::Version version_;
597 std::unique_ptr<Construct> entry_;
598 std::vector<LoopSequence> children_;
599 SemanticsContext *semaCtx_{
nullptr};
608llvm::omp::TraitSet MapTraitSet(parser::OmpTraitSetSelectorName::Value name);
612llvm::omp::TraitSelector MapTraitSelector(
613 const parser::OmpTraitSelectorName &name, llvm::omp::TraitSet set);
616std::optional<bool> EvaluateUserCondition(
617 SemanticsContext &semaCtx,
const parser::ScalarExpr &scalarExpr);
620llvm::APInt *GetTraitScore(
621 const std::optional<parser::OmpTraitSelector::Properties> &props,
622 SemanticsContext &semaCtx, std::optional<llvm::APInt> &scoreStorage);
627void ProcessTraitProperties(llvm::omp::VariantMatchInfo &vmi,
628 llvm::omp::TraitSet set, llvm::omp::TraitSelector selector,
629 const std::optional<parser::OmpTraitSelector::Properties> &props,
630 llvm::APInt *scorePtr);
Definition expression.h:960
Definition char-block.h:27
Definition semantics.h:71
Definition openmp-utils.h:254
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:5298
Definition parse-tree.h:5282
Definition parse-tree.h:5305
Definition parse-tree.h:5372
Definition parse-tree.h:3642
Definition parse-tree.h:3667
Definition parse-tree.h:5678
Definition parse-tree.h:3765
Definition openmp-utils.h:114
Definition parse-tree.h:3905
Non-constant user condition expression and source for runtime lowering.
Definition openmp-utils.h:212
Definition openmp-utils.h:514
const LoopSequence * getNestedDoConcurrent() const
Definition openmp-utils.cpp:1897
Definition openmp-utils.h:373
A representation of a "because" message.
Definition openmp-utils.h:410
Definition openmp-utils.h:58
Definition openmp-utils.h:83
Definition openmp-utils.h:436