Skip to content

Commit 48ffd40

Browse files
committed
[Clang][OpenMP] Codegen generation for has_device_addr claues.
This patch add codegen support for the has_device_addr clause. It use the same logic of is_device_ptr. But passing &var instead pointer to var to kernal. Differential Revision: https://reviews.llvm.org/D134268
1 parent e0a6df5 commit 48ffd40

File tree

6 files changed

+2195
-10
lines changed

6 files changed

+2195
-10
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7389,6 +7389,13 @@ class MappableExprsHandler {
73897389
SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>>
73907390
DevPointersMap;
73917391

7392+
/// Map between device addr declarations and their expression components.
7393+
/// The key value for declarations in 'this' is null.
7394+
llvm::DenseMap<
7395+
const ValueDecl *,
7396+
SmallVector<OMPClauseMappableExprCommon::MappableExprComponentListRef, 4>>
7397+
HasDevAddrsMap;
7398+
73927399
/// Map between lambda declarations and their map type.
73937400
llvm::DenseMap<const ValueDecl *, const OMPMapClause *> LambdasMap;
73947401

@@ -8819,6 +8826,10 @@ class MappableExprsHandler {
88198826
for (const auto *C : Dir.getClausesOfKind<OMPIsDevicePtrClause>())
88208827
for (auto L : C->component_lists())
88218828
DevPointersMap[std::get<0>(L)].push_back(std::get<1>(L));
8829+
// Extract device addr clause information.
8830+
for (const auto *C : Dir.getClausesOfKind<OMPHasDeviceAddrClause>())
8831+
for (auto L : C->component_lists())
8832+
HasDevAddrsMap[std::get<0>(L)].push_back(std::get<1>(L));
88228833
// Extract map information.
88238834
for (const auto *C : Dir.getClausesOfKind<OMPMapClause>()) {
88248835
if (C->getMapType() != OMPC_MAP_to)
@@ -9065,6 +9076,30 @@ class MappableExprsHandler {
90659076
CombinedInfo.Mappers.push_back(nullptr);
90669077
return;
90679078
}
9079+
if (VD && HasDevAddrsMap.count(VD)) {
9080+
auto I = HasDevAddrsMap.find(VD);
9081+
CombinedInfo.Exprs.push_back(VD);
9082+
Expr *E = nullptr;
9083+
for (auto &MCL : I->second) {
9084+
E = MCL.begin()->getAssociatedExpression();
9085+
break;
9086+
}
9087+
llvm::Value *Ptr = nullptr;
9088+
if (E->isGLValue())
9089+
Ptr = CGF.EmitLValue(E).getPointer(CGF);
9090+
else
9091+
Ptr = CGF.EmitScalarExpr(E);
9092+
CombinedInfo.BasePointers.emplace_back(Ptr, VD);
9093+
CombinedInfo.Pointers.push_back(Ptr);
9094+
CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
9095+
CGF.getTypeSize(CGF.getContext().VoidPtrTy), CGF.Int64Ty,
9096+
/*isSigned=*/true));
9097+
CombinedInfo.Types.push_back(
9098+
(Cap->capturesVariable() ? OMP_MAP_TO : OMP_MAP_LITERAL) |
9099+
OMP_MAP_TARGET_PARAM);
9100+
CombinedInfo.Mappers.push_back(nullptr);
9101+
return;
9102+
}
90689103

90699104
using MapData =
90709105
std::tuple<OMPClauseMappableExprCommon::MappableExprComponentListRef,
@@ -9073,14 +9108,19 @@ class MappableExprsHandler {
90739108
SmallVector<MapData, 4> DeclComponentLists;
90749109
// For member fields list in is_device_ptr, store it in
90759110
// DeclComponentLists for generating components info.
9111+
static const OpenMPMapModifierKind Unknown = OMPC_MAP_MODIFIER_unknown;
90769112
auto It = DevPointersMap.find(VD);
90779113
if (It != DevPointersMap.end())
9078-
for (const auto &MCL : It->second) {
9079-
static const OpenMPMapModifierKind Unknown = OMPC_MAP_MODIFIER_unknown;
9114+
for (const auto &MCL : It->second)
90809115
DeclComponentLists.emplace_back(MCL, OMPC_MAP_to, Unknown,
90819116
/*IsImpicit = */ true, nullptr,
90829117
nullptr);
9083-
}
9118+
auto I = HasDevAddrsMap.find(VD);
9119+
if (I != HasDevAddrsMap.end())
9120+
for (const auto &MCL : I->second)
9121+
DeclComponentLists.emplace_back(MCL, OMPC_MAP_tofrom, Unknown,
9122+
/*IsImpicit = */ true, nullptr,
9123+
nullptr);
90849124
assert(CurDir.is<const OMPExecutableDirective *>() &&
90859125
"Expect a executable directive");
90869126
const auto *CurExecDir = CurDir.get<const OMPExecutableDirective *>();

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
20932093
//
20942094
// =========================================================================
20952095
// | type | defaultmap | pvt | first | is_device_ptr | map | res. |
2096-
// | |(tofrom:scalar)| | pvt | | | |
2096+
// | |(tofrom:scalar)| | pvt | |has_dv_adr| |
20972097
// =========================================================================
20982098
// | scl | | | | - | | bycopy|
20992099
// | scl | | - | x | - | - | bycopy|
@@ -2154,10 +2154,11 @@ bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level,
21542154
D](OMPClauseMappableExprCommon::MappableExprComponentListRef
21552155
MapExprComponents,
21562156
OpenMPClauseKind WhereFoundClauseKind) {
2157-
// Only the map clause information influences how a variable is
2158-
// captured. E.g. is_device_ptr does not require changing the default
2159-
// behavior.
2160-
if (WhereFoundClauseKind != OMPC_map)
2157+
// Both map and has_device_addr clauses information influences how a
2158+
// variable is captured. E.g. is_device_ptr does not require changing
2159+
// the default behavior.
2160+
if (WhereFoundClauseKind != OMPC_map &&
2161+
WhereFoundClauseKind != OMPC_has_device_addr)
21612162
return false;
21622163

21632164
auto EI = MapExprComponents.rbegin();
@@ -23070,13 +23071,17 @@ OMPClause *Sema::ActOnOpenMPHasDeviceAddrClause(ArrayRef<Expr *> VarList,
2307023071

2307123072
// Store the components in the stack so that they can be used to check
2307223073
// against other clauses later on.
23074+
Expr *Component = SimpleRefExpr;
23075+
auto *VD = dyn_cast<VarDecl>(D);
23076+
if (VD && (isa<OMPArraySectionExpr>(RefExpr->IgnoreParenImpCasts()) ||
23077+
isa<ArraySubscriptExpr>(RefExpr->IgnoreParenImpCasts())))
23078+
Component = DefaultFunctionArrayLvalueConversion(SimpleRefExpr).get();
2307323079
OMPClauseMappableExprCommon::MappableComponent MC(
23074-
SimpleRefExpr, D, /*IsNonContiguous=*/false);
23080+
Component, D, /*IsNonContiguous=*/false);
2307523081
DSAStack->addMappableExpressionComponents(
2307623082
D, MC, /*WhereFoundClauseKind=*/OMPC_has_device_addr);
2307723083

2307823084
// Record the expression we've just processed.
23079-
auto *VD = dyn_cast<VarDecl>(D);
2308023085
if (!VD && !CurContext->isDependentContext()) {
2308123086
DeclRefExpr *Ref =
2308223087
buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);

0 commit comments

Comments
 (0)