| 1 | //===--- CIRGenOpenMPClause.h - OpenMP clause emitter -----------*- C++ -*-===// |
| 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 | #ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPCLAUSE_H |
| 10 | #define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPCLAUSE_H |
| 11 | |
| 12 | #include "CIRGenBuilder.h" |
| 13 | #include "CIRGenModule.h" |
| 14 | #include "mlir/Dialect/OpenMP/OpenMPClauseOperands.h" |
| 15 | #include "clang/AST/OpenMPClause.h" |
| 16 | #include "clang/AST/StmtOpenMP.h" |
| 17 | #include "llvm/Frontend/OpenMP/OMPConstants.h" |
| 18 | |
| 19 | #include <type_traits> |
| 20 | |
| 21 | namespace clang::CIRGen { |
| 22 | |
| 23 | class CIRGenFunction; |
| 24 | |
| 25 | /// A type-only list of OpenMP clause AST node types. |
| 26 | template <typename... Clauses> struct OpenMPNYIClauseList {}; |
| 27 | |
| 28 | /// Emits OpenMP clauses for a directive, writing results into the |
| 29 | /// auto-generated ClauseOps from the OMP dialect. |
| 30 | class OpenMPClauseEmitter { |
| 31 | CIRGenFunction &cgf; |
| 32 | CIRGenModule &cgm; |
| 33 | CIRGenBuilderTy &builder; |
| 34 | mlir::Location loc; |
| 35 | llvm::ArrayRef<const OMPClause *> clauses; |
| 36 | |
| 37 | public: |
| 38 | OpenMPClauseEmitter(CIRGenFunction &cgf, CIRGenModule &cgm, |
| 39 | CIRGenBuilderTy &builder, mlir::Location loc, |
| 40 | llvm::ArrayRef<const OMPClause *> clauses) |
| 41 | : cgf(cgf), cgm(cgm), builder(builder), loc(loc), clauses(clauses) {} |
| 42 | |
| 43 | bool emitIf(mlir::omp::IfClauseOps &result, |
| 44 | llvm::omp::Directive directiveName) const; |
| 45 | |
| 46 | /// Emit map clauses. The optional \p mapSyms parameter collects the |
| 47 | /// VarDecls corresponding to each map operand. |
| 48 | bool emitMap(mlir::omp::MapClauseOps &result, |
| 49 | llvm::SmallVectorImpl<const VarDecl *> *mapSyms = nullptr) const; |
| 50 | |
| 51 | bool emitNumThreads(mlir::omp::NumThreadsClauseOps &result) const; |
| 52 | |
| 53 | bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const; |
| 54 | |
| 55 | /// Verify the clauses of a directive to make sure all legal cases are either |
| 56 | /// implemented or give a NYI error. The \p SupportedClauses and \p |
| 57 | /// NYIClauses type lists must be disjoint and cover all clauses eligible for |
| 58 | /// the directive being processed. Returns failure if any not-yet-implemented |
| 59 | /// clause was present (and an error was emitted), success otherwise. |
| 60 | template <typename... SupportedClauses, typename... NYIClauses> |
| 61 | mlir::LogicalResult emitNYI(OpenMPNYIClauseList<NYIClauses...> nyi, |
| 62 | llvm::omp::Directive directive) const; |
| 63 | |
| 64 | private: |
| 65 | /// True if T is the same type as any of Ts. |
| 66 | template <typename T, typename... Ts> |
| 67 | static constexpr bool isAnyOf = (std::is_same_v<T, Ts> || ...); |
| 68 | }; |
| 69 | |
| 70 | template <typename... SupportedClauses, typename... NYIClauses> |
| 71 | mlir::LogicalResult |
| 72 | OpenMPClauseEmitter::emitNYI(OpenMPNYIClauseList<NYIClauses...>, |
| 73 | llvm::omp::Directive directive) const { |
| 74 | static_assert( |
| 75 | (!isAnyOf<NYIClauses, SupportedClauses...> && ...), |
| 76 | "the supported and not-yet-implemented clause lists must be disjoint" ); |
| 77 | |
| 78 | mlir::LogicalResult result = mlir::success(); |
| 79 | for (const OMPClause *c : clauses) { |
| 80 | if (isa<NYIClauses...>(c)) { |
| 81 | std::string msg = |
| 82 | (llvm::Twine("OpenMP " ) + |
| 83 | llvm::omp::getOpenMPDirectiveName(D: directive).upper() + " '" + |
| 84 | llvm::omp::getOpenMPClauseName(C: c->getClauseKind()) + "' clause" ) |
| 85 | .str(); |
| 86 | cgm.errorNYI(c->getBeginLoc(), msg); |
| 87 | result = mlir::failure(); |
| 88 | } else if (!isa<SupportedClauses...>(c)) { |
| 89 | // Unknown/illegal clause encountered. |
| 90 | llvm_unreachable("unexpected OpenMP clause" ); |
| 91 | } |
| 92 | } |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | } // namespace clang::CIRGen |
| 97 | |
| 98 | #endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENOPENMPCLAUSE_H |
| 99 | |