|
| 1 | +//===-- flang/Parser/openmp-utils.h ---------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Common OpenMP utilities. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef FORTRAN_PARSER_OPENMP_UTILS_H |
| 14 | +#define FORTRAN_PARSER_OPENMP_UTILS_H |
| 15 | + |
| 16 | +#include "flang/Common/indirection.h" |
| 17 | +#include "flang/Parser/parse-tree.h" |
| 18 | +#include "llvm/Frontend/OpenMP/OMP.h" |
| 19 | + |
| 20 | +#include <cassert> |
| 21 | +#include <tuple> |
| 22 | +#include <type_traits> |
| 23 | +#include <utility> |
| 24 | +#include <variant> |
| 25 | + |
| 26 | +namespace Fortran::parser::omp { |
| 27 | + |
| 28 | +namespace detail { |
| 29 | +using D = llvm::omp::Directive; |
| 30 | + |
| 31 | +template <typename Construct> // |
| 32 | +struct ConstructId { |
| 33 | + static constexpr llvm::omp::Directive id{D::OMPD_unknown}; |
| 34 | +}; |
| 35 | + |
| 36 | +#define MAKE_CONSTR_ID(Construct, Id) \ |
| 37 | + template <> struct ConstructId<Construct> { \ |
| 38 | + static constexpr llvm::omp::Directive id{Id}; \ |
| 39 | + } |
| 40 | + |
| 41 | +MAKE_CONSTR_ID(OmpAssumeDirective, D::OMPD_assume); |
| 42 | +MAKE_CONSTR_ID(OmpCriticalDirective, D::OMPD_critical); |
| 43 | +MAKE_CONSTR_ID(OmpDeclareVariantDirective, D::OMPD_declare_variant); |
| 44 | +MAKE_CONSTR_ID(OmpErrorDirective, D::OMPD_error); |
| 45 | +MAKE_CONSTR_ID(OmpMetadirectiveDirective, D::OMPD_metadirective); |
| 46 | +MAKE_CONSTR_ID(OpenMPDeclarativeAllocate, D::OMPD_allocate); |
| 47 | +MAKE_CONSTR_ID(OpenMPDeclarativeAssumes, D::OMPD_assumes); |
| 48 | +MAKE_CONSTR_ID(OpenMPDeclareMapperConstruct, D::OMPD_declare_mapper); |
| 49 | +MAKE_CONSTR_ID(OpenMPDeclareReductionConstruct, D::OMPD_declare_reduction); |
| 50 | +MAKE_CONSTR_ID(OpenMPDeclareSimdConstruct, D::OMPD_declare_simd); |
| 51 | +MAKE_CONSTR_ID(OpenMPDeclareTargetConstruct, D::OMPD_declare_target); |
| 52 | +MAKE_CONSTR_ID(OpenMPExecutableAllocate, D::OMPD_allocate); |
| 53 | +MAKE_CONSTR_ID(OpenMPRequiresConstruct, D::OMPD_requires); |
| 54 | +MAKE_CONSTR_ID(OpenMPThreadprivate, D::OMPD_threadprivate); |
| 55 | + |
| 56 | +#undef MAKE_CONSTR_ID |
| 57 | + |
| 58 | +struct DirectiveNameScope { |
| 59 | + static OmpDirectiveName MakeName(CharBlock source = {}, |
| 60 | + llvm::omp::Directive id = llvm::omp::Directive::OMPD_unknown) { |
| 61 | + OmpDirectiveName name; |
| 62 | + name.source = source; |
| 63 | + name.v = id; |
| 64 | + return name; |
| 65 | + } |
| 66 | + |
| 67 | + static OmpDirectiveName GetOmpDirectiveName(const OmpNothingDirective &x) { |
| 68 | + return MakeName(x.source, llvm::omp::Directive::OMPD_nothing); |
| 69 | + } |
| 70 | + |
| 71 | + static OmpDirectiveName GetOmpDirectiveName(const OmpBeginBlockDirective &x) { |
| 72 | + auto &dir{std::get<OmpBlockDirective>(x.t)}; |
| 73 | + return MakeName(dir.source, dir.v); |
| 74 | + } |
| 75 | + |
| 76 | + static OmpDirectiveName GetOmpDirectiveName(const OmpBeginLoopDirective &x) { |
| 77 | + auto &dir{std::get<OmpLoopDirective>(x.t)}; |
| 78 | + return MakeName(dir.source, dir.v); |
| 79 | + } |
| 80 | + |
| 81 | + static OmpDirectiveName GetOmpDirectiveName( |
| 82 | + const OmpBeginSectionsDirective &x) { |
| 83 | + auto &dir{std::get<OmpSectionsDirective>(x.t)}; |
| 84 | + return MakeName(dir.source, dir.v); |
| 85 | + } |
| 86 | + |
| 87 | + template <typename T> |
| 88 | + static OmpDirectiveName GetOmpDirectiveName(const T &x) { |
| 89 | + if constexpr (WrapperTrait<T>) { |
| 90 | + if constexpr (std::is_same_v<T, OpenMPCancelConstruct> || |
| 91 | + std::is_same_v<T, OpenMPCancellationPointConstruct> || |
| 92 | + std::is_same_v<T, OpenMPDepobjConstruct> || |
| 93 | + std::is_same_v<T, OpenMPFlushConstruct> || |
| 94 | + std::is_same_v<T, OpenMPInteropConstruct> || |
| 95 | + std::is_same_v<T, OpenMPSimpleStandaloneConstruct>) { |
| 96 | + return x.v.DirName(); |
| 97 | + } else { |
| 98 | + return GetOmpDirectiveName(x.v); |
| 99 | + } |
| 100 | + } else if constexpr (TupleTrait<T>) { |
| 101 | + if constexpr (std::is_same_v<T, OpenMPAllocatorsConstruct> || |
| 102 | + std::is_same_v<T, OpenMPAtomicConstruct> || |
| 103 | + std::is_same_v<T, OpenMPDispatchConstruct>) { |
| 104 | + return std::get<OmpDirectiveSpecification>(x.t).DirName(); |
| 105 | + } else if constexpr (std::is_same_v<T, OmpAssumeDirective> || |
| 106 | + std::is_same_v<T, OmpCriticalDirective> || |
| 107 | + std::is_same_v<T, OmpDeclareVariantDirective> || |
| 108 | + std::is_same_v<T, OmpErrorDirective> || |
| 109 | + std::is_same_v<T, OmpMetadirectiveDirective> || |
| 110 | + std::is_same_v<T, OpenMPDeclarativeAllocate> || |
| 111 | + std::is_same_v<T, OpenMPDeclarativeAssumes> || |
| 112 | + std::is_same_v<T, OpenMPDeclareMapperConstruct> || |
| 113 | + std::is_same_v<T, OpenMPDeclareReductionConstruct> || |
| 114 | + std::is_same_v<T, OpenMPDeclareSimdConstruct> || |
| 115 | + std::is_same_v<T, OpenMPDeclareTargetConstruct> || |
| 116 | + std::is_same_v<T, OpenMPExecutableAllocate> || |
| 117 | + std::is_same_v<T, OpenMPRequiresConstruct> || |
| 118 | + std::is_same_v<T, OpenMPThreadprivate>) { |
| 119 | + return MakeName(std::get<Verbatim>(x.t).source, ConstructId<T>::id); |
| 120 | + } else { |
| 121 | + return GetFromTuple( |
| 122 | + x.t, std::make_index_sequence<std::tuple_size_v<decltype(x.t)>>{}); |
| 123 | + } |
| 124 | + } else if constexpr (UnionTrait<T>) { |
| 125 | + return common::visit( |
| 126 | + [](auto &&s) { return GetOmpDirectiveName(s); }, x.u); |
| 127 | + } else { |
| 128 | + return MakeName(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + template <typename... Ts, size_t... Is> |
| 133 | + static OmpDirectiveName GetFromTuple( |
| 134 | + const std::tuple<Ts...> &t, std::index_sequence<Is...>) { |
| 135 | + OmpDirectiveName name = MakeName(); |
| 136 | + auto accumulate = [&](const OmpDirectiveName &n) { |
| 137 | + if (name.v == llvm::omp::Directive::OMPD_unknown) { |
| 138 | + name = n; |
| 139 | + } else { |
| 140 | + assert( |
| 141 | + n.v == llvm::omp::Directive::OMPD_unknown && "Conflicting names"); |
| 142 | + } |
| 143 | + }; |
| 144 | + (accumulate(GetOmpDirectiveName(std::get<Is>(t))), ...); |
| 145 | + return name; |
| 146 | + } |
| 147 | + |
| 148 | + template <typename T> |
| 149 | + static OmpDirectiveName GetOmpDirectiveName(const common::Indirection<T> &x) { |
| 150 | + return GetOmpDirectiveName(x.value()); |
| 151 | + } |
| 152 | +}; |
| 153 | +} // namespace detail |
| 154 | + |
| 155 | +template <typename T> OmpDirectiveName GetOmpDirectiveName(const T &x) { |
| 156 | + return detail::DirectiveNameScope::GetOmpDirectiveName(x); |
| 157 | +} |
| 158 | + |
| 159 | +} // namespace Fortran::parser::omp |
| 160 | + |
| 161 | +#endif // FORTRAN_PARSER_OPENMP_UTILS_H |
0 commit comments