16#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
17#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
24static mlir::omp::DeclareTargetDeviceType
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;
34 llvm_unreachable(
"unexpected device type");
37static mlir::omp::DeclareTargetCaptureClause
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;
49 llvm_unreachable(
"unexpected map type");
55 std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> devTy =
56 OMPDeclareTargetDeclAttr::getDeviceType(vd);
60 if (!isDevice && *devTy == OMPDeclareTargetDeclAttr::DT_NoHost)
63 if (isDevice && *devTy == OMPDeclareTargetDeclAttr::DT_Host)
75 if (
const auto *e = dyn_cast<OMPExecutableDirective>(s))
84bool CIRGenOpenMPRuntime::emitTargetFunctions(
GlobalDecl gd) {
85 bool isDevice = cgm.getLangOpts().OpenMPIsTargetDevice;
88 if (
const auto *fd = dyn_cast<FunctionDecl>(gd.
getDecl()))
96 if (
const auto *fd = dyn_cast<FunctionDecl>(vd))
101 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(vd) ||
102 alreadyEmittedTargetDecls.count(vd) != 0)
108 if (
const auto *fd = dyn_cast<FunctionDecl>(vd))
109 if (fd->doesThisDeclarationHaveABody() &&
116bool CIRGenOpenMPRuntime::emitTargetGlobalVariable(GlobalDecl gd) {
118 cgm.getLangOpts().OpenMPIsTargetDevice))
121 if (!cgm.getLangOpts().OpenMPIsTargetDevice)
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) &&
138 if (res && *res == OMPDeclareTargetDeclAttr::MT_Link)
139 cgm.errorNYI(vd->getSourceRange(),
140 "declare target global variable with link clause");
151 return emitTargetFunctions(gd);
153 return emitTargetGlobalVariable(gd);
157 if (!cgm.getLangOpts().OpenMPIsTargetDevice)
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)));
167 return !f.isDeclaration();
173 return !alreadyEmittedTargetDecls.insert(d).second;
179 return cgm.getLangOpts().OpenMPForceUSM;
183 cir::FuncOp funcOp) {
184 const auto *
attr = fd->
getAttr<OMPDeclareTargetDeclAttr>();
185 assert(
attr &&
"expected OMPDeclareTargetDeclAttr");
190 if (std::optional<OMPDeclareTargetDeclAttr *> activeAttr =
191 OMPDeclareTargetDeclAttr::getActiveAttr(fd))
192 if ((*activeAttr)->getIndirect())
194 "declare target function with indirect clause");
196 auto declTargetIface =
197 llvm::cast<mlir::omp::DeclareTargetInterface>(funcOp.getOperation());
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...
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.
Represents a function declaration or definition.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
GlobalDecl - represents a global declaration.
const Decl * getDecl() const
Stmt - This represents one statement.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
bool isOpenMPTargetExecutionDirective(OpenMPDirectiveKind DKind)
Checks if the specified directive is a target code offload directive.
U cast(CodeGen::Address addr)
static bool openMPRequiresUnifiedSharedMemory()