| 1 | //===----------------------------------------------------------------------===// |
| 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 | // OpenMP clause emitter implementation. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "CIRGenOpenMPClause.h" |
| 14 | #include "CIRGenFunction.h" |
| 15 | #include "mlir/Dialect/OpenMP/OpenMPDialect.h" |
| 16 | #include "clang/Basic/OpenMPKinds.h" |
| 17 | |
| 18 | using namespace clang; |
| 19 | using namespace clang::CIRGen; |
| 20 | |
| 21 | static mlir::omp::ClauseMapFlags |
| 22 | mapClauseKindToFlags(OpenMPMapClauseKind kind) { |
| 23 | switch (kind) { |
| 24 | case OMPC_MAP_to: |
| 25 | return mlir::omp::ClauseMapFlags::to; |
| 26 | case OMPC_MAP_from: |
| 27 | return mlir::omp::ClauseMapFlags::from; |
| 28 | case OMPC_MAP_tofrom: |
| 29 | return mlir::omp::ClauseMapFlags::to | mlir::omp::ClauseMapFlags::from; |
| 30 | case OMPC_MAP_alloc: |
| 31 | case OMPC_MAP_release: |
| 32 | return mlir::omp::ClauseMapFlags::storage; |
| 33 | case OMPC_MAP_delete: |
| 34 | return mlir::omp::ClauseMapFlags::del; |
| 35 | default: |
| 36 | return mlir::omp::ClauseMapFlags::none; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static mlir::Value emitMapInfoForVar(CIRGenFunction &cgf, |
| 41 | mlir::OpBuilder &builder, |
| 42 | mlir::Location loc, const VarDecl *vd, |
| 43 | mlir::omp::ClauseMapFlags mapFlags) { |
| 44 | Address addr = cgf.getAddrOfLocalVar(vd); |
| 45 | mlir::Value varPtr = addr.getPointer(); |
| 46 | auto varPtrType = mlir::cast<cir::PointerType>(Val: varPtr.getType()); |
| 47 | mlir::Type elementType = varPtrType.getPointee(); |
| 48 | |
| 49 | // Cast to generic pointer if needed. |
| 50 | if (varPtrType.getAddrSpace()) { |
| 51 | auto genericPtrType = |
| 52 | cir::PointerType::get(context: builder.getContext(), pointee: elementType); |
| 53 | varPtr = cir::CastOp::create(builder, location: loc, result: genericPtrType, |
| 54 | kind: cir::CastKind::address_space, src: varPtr); |
| 55 | varPtrType = genericPtrType; |
| 56 | } |
| 57 | |
| 58 | return mlir::omp::MapInfoOp::create( |
| 59 | builder, location: loc, |
| 60 | /*omp_ptr=*/varPtrType, |
| 61 | /*var_ptr=*/varPtr, |
| 62 | /*var_ptr_type=*/mlir::TypeAttr::get(type: elementType), |
| 63 | /*map_type=*/builder.getAttr<mlir::omp::ClauseMapFlagsAttr>(args&: mapFlags), |
| 64 | /*map_capture_type=*/ |
| 65 | builder.getAttr<mlir::omp::VariableCaptureKindAttr>( |
| 66 | args: mlir::omp::VariableCaptureKind::ByRef), |
| 67 | /*var_ptr_ptr=*/mlir::Value{}, |
| 68 | /*var_ptr_ptr_type=*/mlir::TypeAttr{}, |
| 69 | /*members=*/mlir::ValueRange{}, |
| 70 | /*members_index=*/mlir::ArrayAttr{}, |
| 71 | /*bounds=*/mlir::ValueRange{}, |
| 72 | /*mapper_id=*/mlir::FlatSymbolRefAttr{}, |
| 73 | /*name=*/builder.getStringAttr(bytes: vd->getName()), |
| 74 | /*partial_map=*/builder.getBoolAttr(value: false)); |
| 75 | } |
| 76 | |
| 77 | bool OpenMPClauseEmitter::emitProcBind( |
| 78 | mlir::omp::ProcBindClauseOps &result) const { |
| 79 | for (const OMPClause *clause : clauses) { |
| 80 | const auto *pbc = dyn_cast<OMPProcBindClause>(Val: clause); |
| 81 | if (!pbc) |
| 82 | continue; |
| 83 | |
| 84 | llvm::omp::ProcBindKind kind = pbc->getProcBindKind(); |
| 85 | assert(kind != llvm::omp::ProcBindKind::OMP_PROC_BIND_unknown && |
| 86 | "unknown proc-bind kind" ); |
| 87 | // The 'default' kind has no dialect counterpart; leave the attribute unset. |
| 88 | if (kind != llvm::omp::ProcBindKind::OMP_PROC_BIND_default) |
| 89 | result.procBindKind = mlir::omp::ClauseProcBindKindAttr::get( |
| 90 | context: builder.getContext(), value: mlir::omp::convertProcBindKind(kind)); |
| 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | bool OpenMPClauseEmitter::emitNumThreads( |
| 97 | mlir::omp::NumThreadsClauseOps &result) const { |
| 98 | for (const OMPClause *clause : clauses) { |
| 99 | const auto *ntc = dyn_cast<OMPNumThreadsClause>(Val: clause); |
| 100 | if (!ntc) |
| 101 | continue; |
| 102 | |
| 103 | for (const Expr *expr : ntc->getNumThreads()) { |
| 104 | mlir::Value numThreadsValue = cgf.emitScalarExpr(e: expr); |
| 105 | auto intType = builder.getIntegerType(width: 32); |
| 106 | numThreadsValue = builder.createBuiltinIntCast(src: numThreadsValue, newTy: intType); |
| 107 | result.numThreadsVars.push_back(Elt: numThreadsValue); |
| 108 | } |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | bool OpenMPClauseEmitter::emitIf(mlir::omp::IfClauseOps &result, |
| 116 | llvm::omp::Directive directiveName) const { |
| 117 | for (const OMPClause *clause : clauses) { |
| 118 | const auto *ic = dyn_cast<OMPIfClause>(Val: clause); |
| 119 | if (!ic) |
| 120 | continue; |
| 121 | |
| 122 | if (!(ic->getNameModifier() == llvm::omp::Directive::OMPD_unknown) && |
| 123 | ic->getNameModifier() != directiveName) |
| 124 | continue; |
| 125 | |
| 126 | Expr *ifCondition = ic->getCondition(); |
| 127 | mlir::Value ifBoolValue = cgf.evaluateExprAsBool(e: ifCondition); // !cir.bool |
| 128 | |
| 129 | mlir::Type uIntType = builder.getUIntNTy(n: 1); |
| 130 | mlir::Value ifUIntValue = |
| 131 | builder.createBoolToInt(src: ifBoolValue, newTy: uIntType); // u1 |
| 132 | |
| 133 | mlir::Type intType = builder.getI1Type(); |
| 134 | mlir::Value ifExpr = |
| 135 | builder.createBuiltinIntCast(src: ifUIntValue, newTy: intType); // i1 |
| 136 | |
| 137 | result.ifExpr = ifExpr; |
| 138 | return true; |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | bool OpenMPClauseEmitter::emitMap( |
| 144 | mlir::omp::MapClauseOps &result, |
| 145 | llvm::SmallVectorImpl<const VarDecl *> *mapSyms) const { |
| 146 | bool found = false; |
| 147 | for (const OMPClause *clause : clauses) { |
| 148 | const auto *mc = dyn_cast<OMPMapClause>(Val: clause); |
| 149 | if (!mc) |
| 150 | continue; |
| 151 | |
| 152 | found = true; |
| 153 | |
| 154 | for (OpenMPMapModifierKind mod : mc->getMapTypeModifiers()) { |
| 155 | if (mod != OMPC_MAP_MODIFIER_unknown) |
| 156 | cgm.errorNYI(mc->getBeginLoc(), |
| 157 | std::string("OpenMP map modifier '" ) + |
| 158 | getOpenMPSimpleClauseTypeName( |
| 159 | Kind: llvm::omp::Clause::OMPC_map, Type: mod) + |
| 160 | "'" ); |
| 161 | } |
| 162 | |
| 163 | if (mc->isImplicit()) { |
| 164 | cgm.errorNYI(mc->getBeginLoc(), "OpenMP implicit map clause" ); |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | mlir::omp::ClauseMapFlags mapFlags = mapClauseKindToFlags(kind: mc->getMapType()); |
| 169 | |
| 170 | for (const Expr *varExpr : mc->varlist()) { |
| 171 | const auto *refExpr = dyn_cast<DeclRefExpr>(Val: varExpr->IgnoreImplicit()); |
| 172 | if (!refExpr) { |
| 173 | cgm.errorNYI(varExpr->getExprLoc(), |
| 174 | "OpenMP map clause with non-DeclRefExpr variable" ); |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | const auto *vd = dyn_cast<VarDecl>(Val: refExpr->getDecl()); |
| 179 | if (!vd) { |
| 180 | cgm.errorNYI(varExpr->getExprLoc(), |
| 181 | "OpenMP map clause with non-VarDecl variable" ); |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | result.mapVars.push_back( |
| 186 | Elt: emitMapInfoForVar(cgf, builder, loc, vd, mapFlags)); |
| 187 | if (mapSyms) |
| 188 | mapSyms->push_back(Elt: vd); |
| 189 | } |
| 190 | } |
| 191 | return found; |
| 192 | } |
| 193 | |