clang 24.0.0git
CIRGenOpenMPRuntime.cpp
Go to the documentation of this file.
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"
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);
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>(s))
76 if (isOpenMPTargetExecutionDirective(e->getDirectiveKind()))
77 return true;
78 for (const Stmt *child : s->children())
79 if (containsTargetRegion(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>(gd.getDecl()))
90 return true;
91 return false;
92 }
93
94 const auto *vd = cast<ValueDecl>(gd.getDecl());
95
96 if (const auto *fd = dyn_cast<FunctionDecl>(vd))
98 return true;
99
100 // Do not emit function if it is not marked as declare target.
101 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(vd) ||
102 alreadyEmittedTargetDecls.count(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>(vd))
109 if (fd->doesThisDeclarationHaveABody() &&
110 containsTargetRegion(fd->getBody()))
111 return false;
112
113 return true;
114}
115
116bool CIRGenOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl gd) {
118 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>(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);
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.
149 if (isa<FunctionDecl>(gd.getDecl()) ||
151 return emitTargetFunctions(gd);
152
153 return emitTargetGlobalVariable(gd);
154}
155
157 if (!cgm.getLangOpts().OpenMPIsTargetDevice)
158 return true;
159
160 const auto *d = cast<FunctionDecl>(gd.getDecl());
161
162 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(d)) {
163 if (d->hasBody() && alreadyEmittedTargetDecls.count(d) == 0) {
164 auto f = dyn_cast_if_present<cir::FuncOp>(
165 cgm.getGlobalValue(cgm.getMangledName(gd)));
166 if (f)
167 return !f.isDeclaration();
168 return false;
169 }
170 return true;
171 }
172
173 return !alreadyEmittedTargetDecls.insert(d).second;
174}
175
176// Mirrors CGOpenMPRuntime::registerTargetGlobalVariable.
178 if (cgm.getLangOpts().OMPTargetTriples.empty() &&
179 !cgm.getLangOpts().OpenMPIsTargetDevice)
180 return;
181
182 std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> res =
183 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(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.
195 return;
196 }
197
198 cgm.errorNYI(vd->getSourceRange(),
199 "registerTargetGlobalVariable: declare target variable");
200}
201
203 // OGCG also sets this from '#pragma omp requires unified_shared_memory'.
205 return cgm.getLangOpts().OpenMPForceUSM;
206}
207
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(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>(funcOp.getOperation());
224 declTargetIface.setDeclareTarget(convertDeviceType(attr->getDevType()),
225 convertCaptureClause(attr->getMapType()),
226 /*automap=*/false, /*implicit=*/false);
227}
static mlir::omp::DeclareTargetCaptureClause convertCaptureClause(OMPDeclareTargetDeclAttr::MapTypeTy mapTy)
static bool containsTargetRegion(const Stmt *s)
Recursively check whether the statement tree contains any OpenMP target execution directive (e....
static bool isAssumedToBeNotEmitted(const ValueDecl *vd, bool isDevice)
Returns true if the declaration should be skipped based on its device_type attribute and the current ...
static mlir::omp::DeclareTargetDeviceType convertDeviceType(OMPDeclareTargetDeclAttr::DevTypeTy devTy)
This file defines OpenMP AST classes for clauses.
void emitDeclareTargetFunction(const FunctionDecl *fd, cir::FuncOp funcOp)
If the function has an OMPDeclareTargetDeclAttr, set the corresponding omp.declare_target attribute o...
void registerTargetGlobalVariable(const VarDecl *vd)
Register a global variable that may need an offload entry.
bool hasRequiresUnifiedSharedMemory() const
Return whether unified shared memory is required.
bool emitTargetGlobal(GlobalDecl gd)
Check whether the given GlobalDecl needs special handling for device compilation.
bool markAsGlobalTarget(GlobalDecl gd)
Mark a function reference as one that should be emitted on the device.
T * getAttr() const
Definition DeclBase.h:582
Represents a function declaration or definition.
Definition Decl.h:2082
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:4646
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
const Decl * getDecl() const
Definition GlobalDecl.h:115
Stmt - This represents one statement.
Definition Stmt.h:85
child_range children()
Definition Stmt.cpp:304
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:714
Represents a variable declaration or definition.
Definition Decl.h:934
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2171
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition Decl.h:1240
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool isOpenMPTargetExecutionDirective(OpenMPDirectiveKind DKind)
Checks if the specified directive is a target code offload directive.
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool openMPRequiresUnifiedSharedMemory()