1//===--- CIRGenOpenMPRuntime.cpp - OpenMP code generation helpers ------=--===//
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// Helpers for OpenMP-specific CIR code generation.
10//
11////===--------------------------------------------------------------------===//
12
13#include "CIRGenOpenMPRuntime.h"
14#include "CIRGenModule.h"
15
16#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
17#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
18#include "clang/AST/OpenMPClause.h"
19#include "clang/CIR/MissingFeatures.h"
20
21using namespace clang;
22using namespace clang::CIRGen;
23
24static mlir::omp::DeclareTargetDeviceType
25convertDeviceType(OMPDeclareTargetDeclAttr::DevTypeTy devTy) {
26 switch (devTy) {
27 case OMPDeclareTargetDeclAttr::DT_Host:
28 return mlir::omp::DeclareTargetDeviceType::host;
29 case OMPDeclareTargetDeclAttr::DT_NoHost:
30 return mlir::omp::DeclareTargetDeviceType::nohost;
31 case OMPDeclareTargetDeclAttr::DT_Any:
32 return mlir::omp::DeclareTargetDeviceType::any;
33 }
34 llvm_unreachable("unexpected device type");
35}
36
37static mlir::omp::DeclareTargetCaptureClause
38convertCaptureClause(OMPDeclareTargetDeclAttr::MapTypeTy mapTy) {
39 switch (mapTy) {
40 case OMPDeclareTargetDeclAttr::MT_To:
41 return mlir::omp::DeclareTargetCaptureClause::to;
42 case OMPDeclareTargetDeclAttr::MT_Enter:
43 return mlir::omp::DeclareTargetCaptureClause::enter;
44 case OMPDeclareTargetDeclAttr::MT_Link:
45 return mlir::omp::DeclareTargetCaptureClause::link;
46 case OMPDeclareTargetDeclAttr::MT_Local:
47 return mlir::omp::DeclareTargetCaptureClause::none;
48 }
49 llvm_unreachable("unexpected map type");
50}
51
52/// Returns true if the declaration should be skipped based on its
53/// device_type attribute and the current compilation mode.
54static bool isAssumedToBeNotEmitted(const ValueDecl *vd, bool isDevice) {
55 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> devTy =
56 OMPDeclareTargetDeclAttr::getDeviceType(VD: vd);
57 if (!devTy)
58 return false;
59 // Do not emit device_type(nohost) functions for the host.
60 if (!isDevice && *devTy == OMPDeclareTargetDeclAttr::DT_NoHost)
61 return true;
62 // Do not emit device_type(host) functions for the device.
63 if (isDevice && *devTy == OMPDeclareTargetDeclAttr::DT_Host)
64 return true;
65 return false;
66}
67
68/// Recursively check whether the statement tree contains any OpenMP target
69/// execution directive (e.g. 'omp target', 'omp target parallel', etc.).
70/// Used to identify host functions that must be emitted on the device because
71/// they contain target regions that will be outlined during MLIR lowering.
72static bool containsTargetRegion(const Stmt *s) {
73 if (!s)
74 return false;
75 if (const auto *e = dyn_cast<OMPExecutableDirective>(Val: s))
76 if (isOpenMPTargetExecutionDirective(DKind: e->getDirectiveKind()))
77 return true;
78 for (const Stmt *child : s->children())
79 if (containsTargetRegion(s: child))
80 return true;
81 return false;
82}
83
84bool CIRGenOpenMPRuntime::emitTargetFunctions(GlobalDecl gd) {
85 bool isDevice = cgm.getLangOpts().OpenMPIsTargetDevice;
86
87 if (!isDevice) {
88 if (const auto *fd = dyn_cast<FunctionDecl>(Val: gd.getDecl()))
89 if (isAssumedToBeNotEmitted(vd: cast<ValueDecl>(Val: fd), isDevice))
90 return true;
91 return false;
92 }
93
94 const auto *vd = cast<ValueDecl>(Val: gd.getDecl());
95
96 if (const auto *fd = dyn_cast<FunctionDecl>(Val: vd))
97 if (isAssumedToBeNotEmitted(vd: cast<ValueDecl>(Val: fd), isDevice))
98 return true;
99
100 // Do not emit function if it is not marked as declare target.
101 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: vd) ||
102 alreadyEmittedTargetDecls.count(V: vd) != 0)
103 return false;
104
105 // We must also host functions that contain target regions,
106 // because the omp.target ops are nested inside the host function rather than
107 // being outlined early. The containsTargetRegion check handles this.
108 if (const auto *fd = dyn_cast<FunctionDecl>(Val: vd))
109 if (fd->doesThisDeclarationHaveABody() &&
110 containsTargetRegion(s: fd->getBody()))
111 return false;
112
113 return true;
114}
115
116bool CIRGenOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl gd) {
117 if (isAssumedToBeNotEmitted(vd: cast<ValueDecl>(Val: gd.getDecl()),
118 isDevice: cgm.getLangOpts().OpenMPIsTargetDevice))
119 return true;
120
121 if (!cgm.getLangOpts().OpenMPIsTargetDevice)
122 return false;
123
124 // We do not need to scan for target regions since there's not early
125 // outlining like in OGCG, they will be emitted as omp.target ops instead.
126
127 const auto *vd = cast<VarDecl>(Val: gd.getDecl());
128
129 // Do not emit variable if it is not marked as declare target.
130 // OGCG also defers link-clause and USM variables here; we emit errorNYI
131 // for those since they are not yet supported.
132 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> res =
133 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: vd);
134 if (!res || *res == OMPDeclareTargetDeclAttr::MT_Link ||
135 ((*res == OMPDeclareTargetDeclAttr::MT_To ||
136 *res == OMPDeclareTargetDeclAttr::MT_Enter) &&
137 false /* NYI: HasRequiresUnifiedSharedMemory */)) {
138 if (res && *res == OMPDeclareTargetDeclAttr::MT_Link)
139 cgm.errorNYI(vd->getSourceRange(),
140 "declare target global variable with link clause");
141 // OGCG defers these variables for later emission. We skip them for now.
142 return true;
143 }
144 return false;
145}
146
147// Mirrors CGOpenMPRuntime::emitTargetGlobal.
148bool CIRGenOpenMPRuntime::emitTargetGlobal(GlobalDecl gd) {
149 if (isa<FunctionDecl>(Val: gd.getDecl()) ||
150 isa<OMPDeclareReductionDecl>(Val: gd.getDecl()))
151 return emitTargetFunctions(gd);
152
153 return emitTargetGlobalVariable(gd);
154}
155
156bool CIRGenOpenMPRuntime::markAsGlobalTarget(GlobalDecl gd) {
157 if (!cgm.getLangOpts().OpenMPIsTargetDevice)
158 return true;
159
160 const auto *d = cast<FunctionDecl>(Val: gd.getDecl());
161
162 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: d)) {
163 if (d->hasBody() && alreadyEmittedTargetDecls.count(V: d) == 0) {
164 auto f = dyn_cast_if_present<cir::FuncOp>(
165 Val: cgm.getGlobalValue(ref: cgm.getMangledName(gd)));
166 if (f)
167 return !f.isDeclaration();
168 return false;
169 }
170 return true;
171 }
172
173 return !alreadyEmittedTargetDecls.insert(V: d).second;
174}
175
176// Mirrors CGOpenMPRuntime::registerTargetGlobalVariable.
177void CIRGenOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *vd) {
178 if (cgm.getLangOpts().OMPTargetTriples.empty() &&
179 !cgm.getLangOpts().OpenMPIsTargetDevice)
180 return;
181
182 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> res =
183 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: vd);
184
185 if (res && *res != OMPDeclareTargetDeclAttr::MT_Link &&
186 vd->hasExternalStorage())
187 return;
188
189 if (res && *res == OMPDeclareTargetDeclAttr::MT_Local)
190 return;
191
192 if (!res) {
193 // OGCG records non-target variables emitted in device code.
194 assert(!cir::MissingFeatures::openMP());
195 return;
196 }
197
198 cgm.errorNYI(vd->getSourceRange(),
199 "registerTargetGlobalVariable: declare target variable");
200}
201
202bool CIRGenOpenMPRuntime::hasRequiresUnifiedSharedMemory() const {
203 // OGCG also sets this from '#pragma omp requires unified_shared_memory'.
204 assert(!cir::MissingFeatures::openMPRequiresUnifiedSharedMemory());
205 return cgm.getLangOpts().OpenMPForceUSM;
206}
207
208void CIRGenOpenMPRuntime::emitDeclareTargetFunction(const FunctionDecl *fd,
209 cir::FuncOp funcOp) {
210 const auto *attr = fd->getAttr<OMPDeclareTargetDeclAttr>();
211 assert(attr && "expected OMPDeclareTargetDeclAttr");
212
213 // Handles the 'indirect' clause here by creating a global variable to hold
214 // the device function address for runtime resolution of indirect calls on
215 // the device.
216 if (std::optional<OMPDeclareTargetDeclAttr *> activeAttr =
217 OMPDeclareTargetDeclAttr::getActiveAttr(VD: fd))
218 if ((*activeAttr)->getIndirect())
219 cgm.errorNYI(fd->getSourceRange(),
220 "declare target function with indirect clause");
221
222 auto declTargetIface =
223 llvm::cast<mlir::omp::DeclareTargetInterface>(Val: funcOp.getOperation());
224 declTargetIface.setDeclareTarget(deviceType: convertDeviceType(devTy: attr->getDevType()),
225 captureClause: convertCaptureClause(mapTy: attr->getMapType()),
226 /*automap=*/false, /*implicit=*/false);
227}
228