Skip to content

[flang][OpenMP] Move extractOmpDirective to Utils.cpp, NFC #148653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 0 additions & 84 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,90 +372,6 @@ extractMappedBaseValues(llvm::ArrayRef<mlir::Value> vars,
});
}

/// Get the directive enumeration value corresponding to the given OpenMP
/// construct PFT node.
llvm::omp::Directive
extractOmpDirective(const parser::OpenMPConstruct &ompConstruct) {
return common::visit(
common::visitors{
[](const parser::OpenMPAllocatorsConstruct &c) {
return llvm::omp::OMPD_allocators;
},
[](const parser::OpenMPAssumeConstruct &c) {
return llvm::omp::OMPD_assume;
},
[](const parser::OpenMPAtomicConstruct &c) {
return llvm::omp::OMPD_atomic;
},
[](const parser::OpenMPBlockConstruct &c) {
return std::get<parser::OmpBlockDirective>(
std::get<parser::OmpBeginBlockDirective>(c.t).t)
.v;
},
[](const parser::OpenMPCriticalConstruct &c) {
return llvm::omp::OMPD_critical;
},
[](const parser::OpenMPDeclarativeAllocate &c) {
return llvm::omp::OMPD_allocate;
},
[](const parser::OpenMPDispatchConstruct &c) {
return llvm::omp::OMPD_dispatch;
},
[](const parser::OpenMPExecutableAllocate &c) {
return llvm::omp::OMPD_allocate;
},
[](const parser::OpenMPLoopConstruct &c) {
return std::get<parser::OmpLoopDirective>(
std::get<parser::OmpBeginLoopDirective>(c.t).t)
.v;
},
[](const parser::OpenMPSectionConstruct &c) {
return llvm::omp::OMPD_section;
},
[](const parser::OpenMPSectionsConstruct &c) {
return std::get<parser::OmpSectionsDirective>(
std::get<parser::OmpBeginSectionsDirective>(c.t).t)
.v;
},
[](const parser::OpenMPStandaloneConstruct &c) {
return common::visit(
common::visitors{
[](const parser::OpenMPSimpleStandaloneConstruct &c) {
return c.v.DirId();
},
[](const parser::OpenMPFlushConstruct &c) {
return llvm::omp::OMPD_flush;
},
[](const parser::OpenMPCancelConstruct &c) {
return llvm::omp::OMPD_cancel;
},
[](const parser::OpenMPCancellationPointConstruct &c) {
return llvm::omp::OMPD_cancellation_point;
},
[](const parser::OmpMetadirectiveDirective &c) {
return llvm::omp::OMPD_metadirective;
},
[](const parser::OpenMPDepobjConstruct &c) {
return llvm::omp::OMPD_depobj;
},
[](const parser::OpenMPInteropConstruct &c) {
return llvm::omp::OMPD_interop;
}},
c.u);
},
[](const parser::OpenMPUtilityConstruct &c) {
return common::visit(
common::visitors{[](const parser::OmpErrorDirective &c) {
return llvm::omp::OMPD_error;
},
[](const parser::OmpNothingDirective &c) {
return llvm::omp::OMPD_nothing;
}},
c.u);
}},
ompConstruct.u);
}

/// Populate the global \see hostEvalInfo after processing clauses for the given
/// \p eval OpenMP target construct, or nested constructs, if these must be
/// evaluated outside of the target region per the spec.
Expand Down
84 changes: 84 additions & 0 deletions flang/lib/Lower/OpenMP/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,90 @@ bool collectLoopRelatedInfo(

return found;
}

/// Get the directive enumeration value corresponding to the given OpenMP
/// construct PFT node.
llvm::omp::Directive
extractOmpDirective(const parser::OpenMPConstruct &ompConstruct) {
return common::visit(
common::visitors{
[](const parser::OpenMPAllocatorsConstruct &c) {
return llvm::omp::OMPD_allocators;
},
[](const parser::OpenMPAssumeConstruct &c) {
return llvm::omp::OMPD_assume;
},
[](const parser::OpenMPAtomicConstruct &c) {
return llvm::omp::OMPD_atomic;
},
[](const parser::OpenMPBlockConstruct &c) {
return std::get<parser::OmpBlockDirective>(
std::get<parser::OmpBeginBlockDirective>(c.t).t)
.v;
},
[](const parser::OpenMPCriticalConstruct &c) {
return llvm::omp::OMPD_critical;
},
[](const parser::OpenMPDeclarativeAllocate &c) {
return llvm::omp::OMPD_allocate;
},
[](const parser::OpenMPDispatchConstruct &c) {
return llvm::omp::OMPD_dispatch;
},
[](const parser::OpenMPExecutableAllocate &c) {
return llvm::omp::OMPD_allocate;
},
[](const parser::OpenMPLoopConstruct &c) {
return std::get<parser::OmpLoopDirective>(
std::get<parser::OmpBeginLoopDirective>(c.t).t)
.v;
},
[](const parser::OpenMPSectionConstruct &c) {
return llvm::omp::OMPD_section;
},
[](const parser::OpenMPSectionsConstruct &c) {
return std::get<parser::OmpSectionsDirective>(
std::get<parser::OmpBeginSectionsDirective>(c.t).t)
.v;
},
[](const parser::OpenMPStandaloneConstruct &c) {
return common::visit(
common::visitors{
[](const parser::OpenMPSimpleStandaloneConstruct &c) {
return c.v.DirId();
},
[](const parser::OpenMPFlushConstruct &c) {
return llvm::omp::OMPD_flush;
},
[](const parser::OpenMPCancelConstruct &c) {
return llvm::omp::OMPD_cancel;
},
[](const parser::OpenMPCancellationPointConstruct &c) {
return llvm::omp::OMPD_cancellation_point;
},
[](const parser::OmpMetadirectiveDirective &c) {
return llvm::omp::OMPD_metadirective;
},
[](const parser::OpenMPDepobjConstruct &c) {
return llvm::omp::OMPD_depobj;
},
[](const parser::OpenMPInteropConstruct &c) {
return llvm::omp::OMPD_interop;
}},
c.u);
},
[](const parser::OpenMPUtilityConstruct &c) {
return common::visit(
common::visitors{[](const parser::OmpErrorDirective &c) {
return llvm::omp::OMPD_error;
},
[](const parser::OmpNothingDirective &c) {
return llvm::omp::OMPD_nothing;
}},
c.u);
}},
ompConstruct.u);
}
} // namespace omp
} // namespace lower
} // namespace Fortran
3 changes: 3 additions & 0 deletions flang/lib/Lower/OpenMP/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ bool collectLoopRelatedInfo(
lower::pft::Evaluation &eval, const omp::List<omp::Clause> &clauses,
mlir::omp::LoopRelatedClauseOps &result,
llvm::SmallVectorImpl<const semantics::Symbol *> &iv);

llvm::omp::Directive
extractOmpDirective(const parser::OpenMPConstruct &ompConstruct);
} // namespace omp
} // namespace lower
} // namespace Fortran
Expand Down
18 changes: 1 addition & 17 deletions llvm/include/llvm/Frontend/OpenMP/ConstructDecompositionT.h
Original file line number Diff line number Diff line change
Expand Up @@ -795,25 +795,9 @@ bool ConstructDecompositionT<C, H>::applyClause(
// assigned to which leaf constructs.

// [5.2:340:33]
auto canMakePrivateCopy = [](llvm::omp::Clause id) {
switch (id) {
// Clauses with "privatization" property:
case llvm::omp::Clause::OMPC_firstprivate:
case llvm::omp::Clause::OMPC_in_reduction:
case llvm::omp::Clause::OMPC_lastprivate:
case llvm::omp::Clause::OMPC_linear:
case llvm::omp::Clause::OMPC_private:
case llvm::omp::Clause::OMPC_reduction:
case llvm::omp::Clause::OMPC_task_reduction:
return true;
default:
return false;
}
};

bool applied = applyIf(node, [&](const auto &leaf) {
return llvm::any_of(leaf.clauses, [&](const ClauseTy *n) {
return canMakePrivateCopy(n->id);
return llvm::omp::isPrivatizingClause(n->id);
});
});

Expand Down
16 changes: 16 additions & 0 deletions llvm/include/llvm/Frontend/OpenMP/OMP.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ static constexpr inline bool canHaveIterator(Clause C) {
}
}

// Can clause C create a private copy of a variable.
static constexpr inline bool isPrivatizingClause(Clause C) {
switch (C) {
case OMPC_firstprivate:
case OMPC_in_reduction:
case OMPC_lastprivate:
case OMPC_linear:
case OMPC_private:
case OMPC_reduction:
case OMPC_task_reduction:
return true;
default:
return false;
}
}

static constexpr unsigned FallbackVersion = 52;
LLVM_ABI ArrayRef<unsigned> getOpenMPVersions();

Expand Down
25 changes: 25 additions & 0 deletions llvm/test/TableGen/directive1.td
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: #include "llvm/ADT/ArrayRef.h"
// CHECK-NEXT: #include "llvm/ADT/BitmaskEnum.h"
// CHECK-NEXT: #include "llvm/ADT/Sequence.h"
// CHECK-NEXT: #include "llvm/ADT/StringRef.h"
// CHECK-NEXT: #include "llvm/Frontend/Directive/Spelling.h"
// CHECK-NEXT: #include "llvm/Support/Compiler.h"
Expand All @@ -66,22 +67,26 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Association {
// CHECK-NEXT: Block,
// CHECK-NEXT: First_ = Block,
// CHECK-NEXT: Declaration,
// CHECK-NEXT: Delimited,
// CHECK-NEXT: Loop,
// CHECK-NEXT: None,
// CHECK-NEXT: Separating,
// CHECK-NEXT: Last_ = Separating,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Association_enumSize = 6;
// CHECK-EMPTY:
// CHECK-NEXT: enum class Category {
// CHECK-NEXT: Declarative,
// CHECK-NEXT: First_ = Declarative,
// CHECK-NEXT: Executable,
// CHECK-NEXT: Informational,
// CHECK-NEXT: Meta,
// CHECK-NEXT: Subsidiary,
// CHECK-NEXT: Utility,
// CHECK-NEXT: Last_ = Utility,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Category_enumSize = 6;
Expand All @@ -96,6 +101,8 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Directive {
// CHECK-NEXT: TDLD_dira,
// CHECK-NEXT: First_ = TDLD_dira,
// CHECK-NEXT: Last_ = TDLD_dira,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Directive_enumSize = 1;
Expand All @@ -104,8 +111,10 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Clause {
// CHECK-NEXT: TDLC_clausea,
// CHECK-NEXT: First_ = TDLC_clausea,
// CHECK-NEXT: TDLC_clauseb,
// CHECK-NEXT: TDLC_clausec,
// CHECK-NEXT: Last_ = TDLC_clausec,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Clause_enumSize = 3;
Expand Down Expand Up @@ -151,6 +160,22 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: LLVM_ABI StringRef getTdlAKindName(AKind x);
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace tdl
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Association> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Category> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Directive> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Clause> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-NEXT: } // namespace llvm
// CHECK-NEXT: #endif // LLVM_Tdl_INC

Expand Down
25 changes: 25 additions & 0 deletions llvm/test/TableGen/directive2.td
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: #define LLVM_Tdl_INC
// CHECK-EMPTY:
// CHECK-NEXT: #include "llvm/ADT/ArrayRef.h"
// CHECK-NEXT: #include "llvm/ADT/Sequence.h"
// CHECK-NEXT: #include "llvm/ADT/StringRef.h"
// CHECK-NEXT: #include "llvm/Frontend/Directive/Spelling.h"
// CHECK-NEXT: #include "llvm/Support/Compiler.h"
Expand All @@ -57,22 +58,26 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Association {
// CHECK-NEXT: Block,
// CHECK-NEXT: First_ = Block,
// CHECK-NEXT: Declaration,
// CHECK-NEXT: Delimited,
// CHECK-NEXT: Loop,
// CHECK-NEXT: None,
// CHECK-NEXT: Separating,
// CHECK-NEXT: Last_ = Separating,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Association_enumSize = 6;
// CHECK-EMPTY:
// CHECK-NEXT: enum class Category {
// CHECK-NEXT: Declarative,
// CHECK-NEXT: First_ = Declarative,
// CHECK-NEXT: Executable,
// CHECK-NEXT: Informational,
// CHECK-NEXT: Meta,
// CHECK-NEXT: Subsidiary,
// CHECK-NEXT: Utility,
// CHECK-NEXT: Last_ = Utility,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Category_enumSize = 6;
Expand All @@ -87,15 +92,19 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Directive {
// CHECK-NEXT: TDLD_dira,
// CHECK-NEXT: First_ = TDLD_dira,
// CHECK-NEXT: Last_ = TDLD_dira,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Directive_enumSize = 1;
// CHECK-EMPTY:
// CHECK-NEXT: enum class Clause {
// CHECK-NEXT: TDLC_clausea,
// CHECK-NEXT: First_ = TDLC_clausea,
// CHECK-NEXT: TDLC_clauseb,
// CHECK-NEXT: TDLC_clausec,
// CHECK-NEXT: TDLC_claused,
// CHECK-NEXT: Last_ = TDLC_claused,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Clause_enumSize = 4;
Expand Down Expand Up @@ -124,6 +133,22 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: LLVM_ABI Category getDirectiveCategory(Directive D);
// CHECK-NEXT: LLVM_ABI SourceLanguage getDirectiveLanguages(Directive D);
// CHECK-NEXT: } // namespace tdl
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Association> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Category> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Directive> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Clause> {
// CHECK-NEXT: static constexpr bool is_iterable = true;
// CHECK-NEXT: };
// CHECK-NEXT: } // namespace llvm
// CHECK-NEXT: #endif // LLVM_Tdl_INC

Expand Down
Loading
Loading