Skip to content

Commit e145a54

Browse files
committed
[OpenACC] Prototype OpenMP TR8 present map type modifier
The OpenMP TR8 `present` map type modifier is needed to support the OpenACC `present` clause, which will be implemented in a following commit. Currently, all testing of OpenMP TR8 `present` is via testing for OpenACC `present`. That is, OpenMP TR8 `present` is not well tested outside of translations from OpenACC to OpenMP, so it is not yet recommended for general use in OpenMP code.
1 parent d94087c commit e145a54

File tree

9 files changed

+93
-13
lines changed

9 files changed

+93
-13
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5278,7 +5278,7 @@ class OMPMapClause final : public OMPMappableExprListClause<OMPMapClause>,
52785278
/// Map-type-modifiers for the 'map' clause.
52795279
OpenMPMapModifierKind MapTypeModifiers[NumberOfOMPMapClauseModifiers] = {
52805280
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
5281-
OMPC_MAP_MODIFIER_unknown};
5281+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
52825282

52835283
/// Location of map-type-modifiers for the 'map' clause.
52845284
SourceLocation MapTypeModifiersLoc[NumberOfOMPMapClauseModifiers];

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ OPENMP_MAP_KIND(release)
124124
OPENMP_MAP_MODIFIER_KIND(always)
125125
OPENMP_MAP_MODIFIER_KIND(close)
126126
OPENMP_MAP_MODIFIER_KIND(mapper)
127+
OPENMP_MAP_MODIFIER_KIND(present)
127128

128129
// Modifiers for 'to' clause.
129130
OPENMP_TO_MODIFIER_KIND(mapper)

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7838,6 +7838,8 @@ class MappableExprsHandler {
78387838
/// Close is a hint to the runtime to allocate memory close to
78397839
/// the target device.
78407840
OMP_MAP_CLOSE = 0x400,
7841+
/// Produce a runtime error if the data is not already allocated.
7842+
OMP_MAP_PRESENT = 0x800,
78417843
/// The 16 MSBs of the flags indicate whether the entry is member of some
78427844
/// struct/class.
78437845
OMP_MAP_MEMBER_OF = 0xffff000000000000,
@@ -8054,6 +8056,9 @@ class MappableExprsHandler {
80548056
if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_close)
80558057
!= MapModifiers.end())
80568058
Bits |= OMP_MAP_CLOSE;
8059+
if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_present)
8060+
!= MapModifiers.end())
8061+
Bits |= OMP_MAP_PRESENT;
80578062
return Bits;
80588063
}
80598064

@@ -8713,7 +8718,8 @@ class MappableExprsHandler {
87138718
/*isSigned=*/false);
87148719
Sizes.push_back(Size);
87158720
// Map type is always TARGET_PARAM
8716-
Types.push_back(OMP_MAP_TARGET_PARAM);
8721+
Types.push_back(OMP_MAP_TARGET_PARAM |
8722+
((*CurTypes.begin()) & OMP_MAP_PRESENT));
87178723
// Remove TARGET_PARAM flag from the first element
87188724
(*CurTypes.begin()) &= ~OMP_MAP_TARGET_PARAM;
87198725

@@ -9282,6 +9288,62 @@ class MappableExprsHandler {
92829288
}
92839289
}
92849290

9291+
/// Generate the base pointers, section pointers, sizes and map types
9292+
/// associated with variables with "present" map type modifiers.
9293+
void generateInfoForMapTypeModPresent(
9294+
MapBaseValuesArrayTy &BasePointers, MapValuesArrayTy &Pointers,
9295+
MapValuesArrayTy &Sizes, MapFlagsArrayTy &Types,
9296+
llvm::DenseSet<const ValueDecl *> CapturedVarSet) {
9297+
assert(CurDir.is<const OMPExecutableDirective *>() &&
9298+
"Expect an executable directive");
9299+
const auto *CurExecDir = CurDir.get<const OMPExecutableDirective *>();
9300+
// Map other list items in the map clause which are not captured variables
9301+
// but "declare target link" global variables.
9302+
for (const auto *C : CurExecDir->getClausesOfKind<OMPMapClause>()) {
9303+
ArrayRef<OpenMPMapModifierKind> Mods = C->getMapTypeModifiers();
9304+
if (llvm::find(Mods, OMPC_MAP_MODIFIER_present) == Mods.end())
9305+
continue;
9306+
for (const auto L : C->component_lists()) {
9307+
if (!L.first)
9308+
continue;
9309+
const auto *VD = dyn_cast<VarDecl>(L.first);
9310+
if (!VD)
9311+
continue;
9312+
9313+
// If it was captured because it was referenced in the construct, don't
9314+
// generate another map entry. Thus, IsFirstComponentList below is
9315+
// false, suppressing the flag OMP_TGT_MAPTYPE_TARGET_PARAM.
9316+
if (CapturedVarSet.count(VD))
9317+
continue;
9318+
9319+
// Temporary versions of arrays
9320+
MapBaseValuesArrayTy CurBasePointers;
9321+
MapValuesArrayTy CurPointers;
9322+
MapValuesArrayTy CurSizes;
9323+
MapFlagsArrayTy CurTypes;
9324+
StructRangeInfoTy PartialStruct;
9325+
9326+
generateInfoForComponentList(
9327+
C->getMapType(), C->getMapTypeModifiers(), L.second,
9328+
CurBasePointers, CurPointers, CurSizes, CurTypes, PartialStruct,
9329+
/*IsFirstComponentList=*/false, C->isImplicit());
9330+
9331+
// If there is an entry in PartialStruct it means we have a struct with
9332+
// individual members mapped. Emit an extra combined entry.
9333+
if (PartialStruct.Base.isValid())
9334+
emitCombinedEntry(BasePointers, Pointers, Sizes, Types, CurTypes,
9335+
PartialStruct);
9336+
9337+
// We need to append the results of this capture to what we already
9338+
// have.
9339+
BasePointers.append(CurBasePointers.begin(), CurBasePointers.end());
9340+
Pointers.append(CurPointers.begin(), CurPointers.end());
9341+
Sizes.append(CurSizes.begin(), CurSizes.end());
9342+
Types.append(CurTypes.begin(), CurTypes.end());
9343+
}
9344+
}
9345+
}
9346+
92859347
/// Generate the default map information for a given capture \a CI,
92869348
/// record field declaration \a RI and captured value \a CV.
92879349
void generateDefaultMapInfo(const CapturedStmt::Capture &CI,
@@ -10171,6 +10233,7 @@ void CGOpenMPRuntime::emitTargetCall(
1017110233
// Get mappable expression information.
1017210234
MappableExprsHandler MEHandler(D, CGF);
1017310235
llvm::DenseMap<llvm::Value *, llvm::Value *> LambdaPointers;
10236+
llvm::DenseSet<const ValueDecl *> CapturedVarSet;
1017410237

1017510238
auto RI = CS.getCapturedRecordDecl()->field_begin();
1017610239
auto CV = CapturedVars.begin();
@@ -10199,6 +10262,8 @@ void CGOpenMPRuntime::emitTargetCall(
1019910262
// just do a default mapping.
1020010263
MEHandler.generateInfoForCapture(CI, *CV, CurBasePointers, CurPointers,
1020110264
CurSizes, CurMapTypes, PartialStruct);
10265+
if (!CI->capturesThis())
10266+
CapturedVarSet.insert(CI->getCapturedVar()->getCanonicalDecl());
1020210267
if (CurBasePointers.empty())
1020310268
MEHandler.generateDefaultMapInfo(*CI, **RI, *CV, CurBasePointers,
1020410269
CurPointers, CurSizes, CurMapTypes);
@@ -10236,6 +10301,10 @@ void CGOpenMPRuntime::emitTargetCall(
1023610301
// but "declare target link" global variables.
1023710302
MEHandler.generateInfoForDeclareTargetLink(BasePointers, Pointers, Sizes,
1023810303
MapTypes);
10304+
// Map other list items in the map clause which are not captured variables
10305+
// but have "present" map type modifiers.
10306+
MEHandler.generateInfoForMapTypeModPresent(BasePointers, Pointers, Sizes,
10307+
MapTypes, CapturedVarSet);
1023910308

1024010309
TargetDataInfo Info;
1024110310
// Fill up the arrays and create the arguments.

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3094,7 +3094,8 @@ bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
30943094
while (getCurToken().isNot(tok::colon)) {
30953095
OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
30963096
if (TypeModifier == OMPC_MAP_MODIFIER_always ||
3097-
TypeModifier == OMPC_MAP_MODIFIER_close) {
3097+
TypeModifier == OMPC_MAP_MODIFIER_close ||
3098+
TypeModifier == OMPC_MAP_MODIFIER_present) {
30983099
Data.MapTypeModifiers.push_back(TypeModifier);
30993100
Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
31003101
ConsumeToken();

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17328,9 +17328,9 @@ OMPClause *Sema::ActOnOpenMPMapClause(
1732817328
OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
1732917329
SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
1733017330
const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
17331-
OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
17332-
OMPC_MAP_MODIFIER_unknown,
17333-
OMPC_MAP_MODIFIER_unknown};
17331+
OpenMPMapModifierKind Modifiers[] = {
17332+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
17333+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
1733417334
SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
1733517335

1733617336
// Process map-type-modifiers, flag errors for duplicate modifiers.

openmp/libomptarget/include/omptarget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ enum tgt_map_type {
4949
OMP_TGT_MAPTYPE_IMPLICIT = 0x200,
5050
// copy data to device
5151
OMP_TGT_MAPTYPE_CLOSE = 0x400,
52+
// runtime error if not already allocated
53+
OMP_TGT_MAPTYPE_PRESENT = 0x800,
5254
// member of struct, member given by [16 MSBs] - 1
5355
OMP_TGT_MAPTYPE_MEMBER_OF = 0xffff000000000000
5456
};

openmp/libomptarget/src/device.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ LookupResult DeviceTy::lookupMapping(void *HstPtrBegin, int64_t Size) {
154154
// to do an illegal mapping.
155155
void *DeviceTy::getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase,
156156
int64_t Size, bool &IsNew, bool &IsHostPtr, bool IsImplicit,
157-
bool UpdateRefCount, bool HasCloseModifier) {
157+
bool UpdateRefCount, bool HasCloseModifier, bool HasPresentModifier) {
158158
void *rc = NULL;
159159
IsHostPtr = false;
160160
IsNew = false;
@@ -196,6 +196,10 @@ void *DeviceTy::getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase,
196196
DPxPTR((uintptr_t)HstPtrBegin), Size, (UpdateRefCount ? " updated" : ""));
197197
IsHostPtr = true;
198198
rc = HstPtrBegin;
199+
} else if (HasPresentModifier) {
200+
DP("Mapping required but does not exist%s for HstPtrBegin=" DPxMOD
201+
", Size=%ld\n",
202+
(IsImplicit ? " (implicit)" : ""), DPxPTR(HstPtrBegin), Size);
199203
} else {
200204
// If it is not contained and Size > 0 we should create a new entry for it.
201205
IsNew = true;

openmp/libomptarget/src/device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ struct DeviceTy {
175175
LookupResult lookupMapping(void *HstPtrBegin, int64_t Size);
176176
void *getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase, int64_t Size,
177177
bool &IsNew, bool &IsHostPtr, bool IsImplicit, bool UpdateRefCount = true,
178-
bool HasCloseModifier = false);
178+
bool HasCloseModifier = false, bool HasPresentModifier = false);
179179
void *getTgtPtrBegin(void *HstPtrBegin, int64_t Size);
180180
void *getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast,
181181
bool UpdateRefCount, bool &IsHostPtr);

openmp/libomptarget/src/omptarget.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ int target_data_begin(DeviceTy &Device, int32_t arg_num, void **args_base,
346346
// Force the creation of a device side copy of the data when:
347347
// a close map modifier was associated with a map that contained a to.
348348
bool HasCloseModifier = arg_types[i] & OMP_TGT_MAPTYPE_CLOSE;
349+
bool HasPresentModifier = arg_types[i] & OMP_TGT_MAPTYPE_PRESENT;
349350
// UpdateRef is based on MEMBER_OF instead of TARGET_PARAM because if we
350351
// have reached this point via __tgt_target_data_begin and not __tgt_target
351352
// then no argument is marked as TARGET_PARAM ("omp target data map" is not
@@ -374,12 +375,14 @@ int target_data_begin(DeviceTy &Device, int32_t arg_num, void **args_base,
374375
}
375376

376377
void *TgtPtrBegin = Device.getOrAllocTgtPtr(HstPtrBegin, HstPtrBase,
377-
data_size, IsNew, IsHostPtr, IsImplicit, UpdateRef, HasCloseModifier);
378+
data_size, IsNew, IsHostPtr, IsImplicit, UpdateRef, HasCloseModifier,
379+
HasPresentModifier);
380+
// If data_size==0, then the argument could be a zero-length pointer to
381+
// NULL, so getOrAlloc() returning NULL is not an error.
378382
if (!TgtPtrBegin && data_size) {
379-
// If data_size==0, then the argument could be a zero-length pointer to
380-
// NULL, so getOrAlloc() returning NULL is not an error.
381-
DP("Call to getOrAllocTgtPtr returned null pointer (device failure or "
382-
"illegal mapping).\n");
383+
DP("Call to getOrAllocTgtPtr returned null pointer (%s).\n",
384+
HasPresentModifier ? "'present' map type modifier"
385+
: "device failure or illegal mapping");
383386
OMPT_DISPATCH_CALLBACK_TARGET_MAP();
384387
return OFFLOAD_FAIL;
385388
}

0 commit comments

Comments
 (0)