clang 23.0.0git
CIRGenOpenMPClause.cpp
Go to the documentation of this file.
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"
17
18using namespace clang;
19using namespace clang::CIRGen;
20
21static mlir::omp::ClauseMapFlags
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
40static 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>(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(builder.getContext(), elementType);
53 varPtr = cir::CastOp::create(builder, loc, genericPtrType,
54 cir::CastKind::address_space, varPtr);
55 varPtrType = genericPtrType;
56 }
57
58 return mlir::omp::MapInfoOp::create(
59 builder, loc,
60 /*omp_ptr=*/varPtrType,
61 /*var_ptr=*/varPtr,
62 /*var_ptr_type=*/mlir::TypeAttr::get(elementType),
63 /*map_type=*/builder.getAttr<mlir::omp::ClauseMapFlagsAttr>(mapFlags),
64 /*map_capture_type=*/
65 builder.getAttr<mlir::omp::VariableCaptureKindAttr>(
66 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(vd->getName()),
74 /*partial_map=*/builder.getBoolAttr(false));
75}
76
78 mlir::omp::ProcBindClauseOps &result) const {
79 for (const OMPClause *clause : clauses) {
80 const auto *pbc = dyn_cast<OMPProcBindClause>(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 builder.getContext(), mlir::omp::convertProcBindKind(kind));
91 return true;
92 }
93 return false;
94}
95
97 mlir::omp::MapClauseOps &result,
99 bool found = false;
100 for (const OMPClause *clause : clauses) {
101 const auto *mc = dyn_cast<OMPMapClause>(clause);
102 if (!mc)
103 continue;
104
105 found = true;
106
107 for (OpenMPMapModifierKind mod : mc->getMapTypeModifiers()) {
108 if (mod != OMPC_MAP_MODIFIER_unknown)
109 cgm.errorNYI(mc->getBeginLoc(),
110 std::string("OpenMP map modifier '") +
112 llvm::omp::Clause::OMPC_map, mod) +
113 "'");
114 }
115
116 if (mc->isImplicit()) {
117 cgm.errorNYI(mc->getBeginLoc(), "OpenMP implicit map clause");
118 continue;
119 }
120
121 mlir::omp::ClauseMapFlags mapFlags = mapClauseKindToFlags(mc->getMapType());
122
123 for (const Expr *varExpr : mc->varlist()) {
124 const auto *refExpr = dyn_cast<DeclRefExpr>(varExpr->IgnoreImplicit());
125 if (!refExpr) {
126 cgm.errorNYI(varExpr->getExprLoc(),
127 "OpenMP map clause with non-DeclRefExpr variable");
128 continue;
129 }
130
131 const auto *vd = dyn_cast<VarDecl>(refExpr->getDecl());
132 if (!vd) {
133 cgm.errorNYI(varExpr->getExprLoc(),
134 "OpenMP map clause with non-VarDecl variable");
135 continue;
136 }
137
138 result.mapVars.push_back(
139 emitMapInfoForVar(cgf, builder, loc, vd, mapFlags));
140 if (mapSyms)
141 mapSyms->push_back(vd);
142 }
143 }
144 return found;
145}
static mlir::Value emitMapInfoForVar(CIRGenFunction &cgf, mlir::OpBuilder &builder, mlir::Location loc, const VarDecl *vd, mlir::omp::ClauseMapFlags mapFlags)
static mlir::omp::ClauseMapFlags mapClauseKindToFlags(OpenMPMapClauseKind kind)
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
Defines some OpenMP-specific enums and functions.
mlir::Value getPointer() const
Definition Address.h:98
Address getAddrOfLocalVar(const clang::VarDecl *vd)
Return the address of a local variable.
bool emitMap(mlir::omp::MapClauseOps &result, llvm::SmallVectorImpl< const VarDecl * > *mapSyms=nullptr) const
Emit map clauses.
bool emitProcBind(mlir::omp::ProcBindClauseOps &result) const
This represents one expression.
Definition Expr.h:112
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
This is a basic class for representing single OpenMP clause.
Represents a variable declaration or definition.
Definition Decl.h:932
The JSON file list parser is used to communicate input to InstallAPI.
const char * getOpenMPSimpleClauseTypeName(OpenMPClauseKind Kind, unsigned Type)
OpenMPMapModifierKind
OpenMP modifier kind for 'map' clause.
Definition OpenMPKinds.h:79
@ OMPC_MAP_MODIFIER_unknown
Definition OpenMPKinds.h:80
OpenMPMapClauseKind
OpenMP mapping kind for 'map' clause.
Definition OpenMPKinds.h:71