Skip to content

Commit 2a55d45

Browse files
committed
[OpenACC] Prototype OpenMP extension: no_alloc map type modifier
The `no_alloc` map type modifier is an original OpenMP extension for the sake of supporting OpenACC's `no_create` clause, which will be implemented in a following commit. Currently, all testing of `no_alloc` is via testing for `no_create`. That is, `no_alloc` 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 b8ba223 commit 2a55d45

File tree

9 files changed

+50
-18
lines changed

9 files changed

+50
-18
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5278,7 +5278,8 @@ 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, OMPC_MAP_MODIFIER_unknown};
5281+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
5282+
OMPC_MAP_MODIFIER_unknown};
52825283

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

clang/include/clang/Basic/OpenMPKinds.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ OPENMP_MAP_MODIFIER_KIND(always)
125125
OPENMP_MAP_MODIFIER_KIND(close)
126126
OPENMP_MAP_MODIFIER_KIND(mapper)
127127
OPENMP_MAP_MODIFIER_KIND(present)
128+
// This is an OpenMP extension for the sake of OpenACC support.
129+
// TODO: Currently, it is not well tested outside of translations from OpenACC
130+
// to OpenMP, so it is not yet recommended for general use in OpenMP code.
131+
OPENMP_MAP_MODIFIER_KIND(no_alloc)
128132

129133
// Modifiers for 'to' clause.
130134
OPENMP_TO_MODIFIER_KIND(mapper)

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7840,6 +7840,12 @@ class MappableExprsHandler {
78407840
OMP_MAP_CLOSE = 0x400,
78417841
/// Produce a runtime error if the data is not already allocated.
78427842
OMP_MAP_PRESENT = 0x800,
7843+
// Skip allocating the data if it is not already allocated.
7844+
// This is an OpenMP extension for the sake of OpenACC support.
7845+
// TODO: Currently, it is not well tested outside of translations from
7846+
// OpenACC to OpenMP, so it is not yet recommended for general use in
7847+
// OpenMP code.
7848+
OMP_MAP_NO_ALLOC = 0x1000,
78437849
/// The 16 MSBs of the flags indicate whether the entry is member of some
78447850
/// struct/class.
78457851
OMP_MAP_MEMBER_OF = 0xffff000000000000,
@@ -8059,6 +8065,9 @@ class MappableExprsHandler {
80598065
if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_present)
80608066
!= MapModifiers.end())
80618067
Bits |= OMP_MAP_PRESENT;
8068+
if (llvm::find(MapModifiers, OMPC_MAP_MODIFIER_no_alloc)
8069+
!= MapModifiers.end())
8070+
Bits |= OMP_MAP_NO_ALLOC;
80628071
return Bits;
80638072
}
80648073

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,8 @@ bool Parser::parseMapTypeModifiers(OpenMPVarListDataTy &Data) {
30953095
OpenMPMapModifierKind TypeModifier = isMapModifier(*this);
30963096
if (TypeModifier == OMPC_MAP_MODIFIER_always ||
30973097
TypeModifier == OMPC_MAP_MODIFIER_close ||
3098-
TypeModifier == OMPC_MAP_MODIFIER_present) {
3098+
TypeModifier == OMPC_MAP_MODIFIER_present ||
3099+
TypeModifier == OMPC_MAP_MODIFIER_no_alloc) {
30993100
Data.MapTypeModifiers.push_back(TypeModifier);
31003101
Data.MapTypeModifiersLoc.push_back(Tok.getLocation());
31013102
ConsumeToken();

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17330,7 +17330,8 @@ OMPClause *Sema::ActOnOpenMPMapClause(
1733017330
const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
1733117331
OpenMPMapModifierKind Modifiers[] = {
1733217332
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
17333-
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown};
17333+
OMPC_MAP_MODIFIER_unknown, OMPC_MAP_MODIFIER_unknown,
17334+
OMPC_MAP_MODIFIER_unknown};
1733417335
SourceLocation ModifiersLoc[NumberOfOMPMapClauseModifiers];
1733517336

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

openmp/libomptarget/include/omptarget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ enum tgt_map_type {
5151
OMP_TGT_MAPTYPE_CLOSE = 0x400,
5252
// runtime error if not already allocated
5353
OMP_TGT_MAPTYPE_PRESENT = 0x800,
54+
// skip allocation if not already allocated
55+
// This is an OpenMP extension for the sake of OpenACC support.
56+
// TODO: Currently, it is not well tested outside of translations from OpenACC
57+
// to OpenMP, so it is not yet recommended for general use in OpenMP code.
58+
OMP_TGT_MAPTYPE_NO_ALLOC = 0x1000,
5459
// member of struct, member given by [16 MSBs] - 1
5560
OMP_TGT_MAPTYPE_MEMBER_OF = 0xffff000000000000
5661
};

openmp/libomptarget/src/device.cpp

Lines changed: 6 additions & 6 deletions
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, bool HasPresentModifier) {
157+
bool UpdateRefCount, bool HasCloseModifier, bool DoNotAllocate) {
158158
void *rc = NULL;
159159
IsHostPtr = false;
160160
IsNew = false;
@@ -196,9 +196,8 @@ 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",
199+
} else if (DoNotAllocate) {
200+
DP("Mapping does not exist%s for HstPtrBegin=" DPxMOD ", Size=%ld\n",
202201
(IsImplicit ? " (implicit)" : ""), DPxPTR(HstPtrBegin), Size);
203202
} else {
204203
// If it is not contained and Size > 0 we should create a new entry for it.
@@ -252,14 +251,15 @@ void *DeviceTy::getOrAllocTgtPtr(void *HstPtrBegin, void *HstPtrBase,
252251
// Return the target pointer begin (where the data will be moved).
253252
// Decrement the reference counter if called from target_data_end.
254253
void *DeviceTy::getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast,
255-
bool UpdateRefCount, bool &IsHostPtr) {
254+
bool UpdateRefCount, bool &IsHostPtr, bool MustContain) {
256255
void *rc = NULL;
257256
IsHostPtr = false;
258257
IsLast = false;
259258
DataMapMtx.lock();
260259
LookupResult lr = lookupMapping(HstPtrBegin, Size);
261260

262-
if (lr.Flags.IsContained || lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter) {
261+
if (lr.Flags.IsContained ||
262+
(!MustContain && (lr.Flags.ExtendsBefore || lr.Flags.ExtendsAfter))) {
263263
auto &HT = *lr.Entry;
264264
IsLast = HT.getRefCount() == 1;
265265

openmp/libomptarget/src/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ 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, bool HasPresentModifier = false);
178+
bool HasCloseModifier = false, bool DoNotAllocate = false);
179179
void *getTgtPtrBegin(void *HstPtrBegin, int64_t Size);
180180
void *getTgtPtrBegin(void *HstPtrBegin, int64_t Size, bool &IsLast,
181-
bool UpdateRefCount, bool &IsHostPtr);
181+
bool UpdateRefCount, bool &IsHostPtr, bool MustContain = false);
182182
int deallocTgtPtr(void *TgtPtrBegin, int64_t Size, bool ForceDelete,
183183
bool HasCloseModifier = false);
184184
int associatePtr(void *HstPtrBegin, void *TgtPtrBegin, int64_t Size);

openmp/libomptarget/src/omptarget.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ int target_data_begin(DeviceTy &Device, int32_t arg_num, void **args_base,
347347
// a close map modifier was associated with a map that contained a to.
348348
bool HasCloseModifier = arg_types[i] & OMP_TGT_MAPTYPE_CLOSE;
349349
bool HasPresentModifier = arg_types[i] & OMP_TGT_MAPTYPE_PRESENT;
350+
bool HasNoAllocModifier = arg_types[i] & OMP_TGT_MAPTYPE_NO_ALLOC;
350351
// UpdateRef is based on MEMBER_OF instead of TARGET_PARAM because if we
351352
// have reached this point via __tgt_target_data_begin and not __tgt_target
352353
// then no argument is marked as TARGET_PARAM ("omp target data map" is not
@@ -376,13 +377,17 @@ int target_data_begin(DeviceTy &Device, int32_t arg_num, void **args_base,
376377

377378
void *TgtPtrBegin = Device.getOrAllocTgtPtr(HstPtrBegin, HstPtrBase,
378379
data_size, IsNew, IsHostPtr, IsImplicit, UpdateRef, HasCloseModifier,
379-
HasPresentModifier);
380+
HasPresentModifier || HasNoAllocModifier);
380381
// If data_size==0, then the argument could be a zero-length pointer to
381382
// NULL, so getOrAlloc() returning NULL is not an error.
382383
if (!TgtPtrBegin && data_size) {
383384
DP("Call to getOrAllocTgtPtr returned null pointer (%s).\n",
384-
HasPresentModifier ? "'present' map type modifier"
385-
: "device failure or illegal mapping");
385+
HasPresentModifier
386+
? "'present' map type modifier"
387+
: HasNoAllocModifier ? "'no_alloc' map type modifier"
388+
: "device failure or illegal mapping");
389+
if (!HasPresentModifier && HasNoAllocModifier)
390+
continue;
386391
OMPT_DISPATCH_CALLBACK_TARGET_MAP();
387392
return OFFLOAD_FAIL;
388393
}
@@ -488,13 +493,19 @@ int target_data_end(DeviceTy &Device, int32_t arg_num, void **args_base,
488493
(arg_types[i] & OMP_TGT_MAPTYPE_PTR_AND_OBJ);
489494
bool ForceDelete = arg_types[i] & OMP_TGT_MAPTYPE_DELETE;
490495
bool HasCloseModifier = arg_types[i] & OMP_TGT_MAPTYPE_CLOSE;
496+
bool HasNoAllocModifier = arg_types[i] & OMP_TGT_MAPTYPE_NO_ALLOC;
491497

492498
// If PTR_AND_OBJ, HstPtrBegin is address of pointee
493499
void *TgtPtrBegin = Device.getTgtPtrBegin(HstPtrBegin, data_size, IsLast,
494-
UpdateRef, IsHostPtr);
495-
DP("There are %" PRId64 " bytes allocated at target address " DPxMOD
496-
" - is%s last\n", data_size, DPxPTR(TgtPtrBegin),
497-
(IsLast ? "" : " not"));
500+
UpdateRef, IsHostPtr, HasNoAllocModifier);
501+
if (!TgtPtrBegin) {
502+
DP("Data is not allocated on device\n");
503+
assert(!IsLast && "expected no deallocation for unallocated data");
504+
} else {
505+
DP("There are %" PRId64 " bytes allocated at target address " DPxMOD
506+
" - is%s last\n", data_size, DPxPTR(TgtPtrBegin),
507+
(IsLast ? "" : " not"));
508+
}
498509

499510
bool DelEntry = IsLast || ForceDelete;
500511

0 commit comments

Comments
 (0)