Skip to content

Commit 769b7fe

Browse files
committed
[OpenMP][offload] Fix map-type order
Reorder mappers such that - on target entry: "from" mappers are handled at the end, where they only act as decayed alloc/storage mappers. - on target exit: "to" mappers are handled first so that they can act as decayed release/storage mappers and decrement the reference count. This avoids that map to+from or from+to result in different outcomes than mapping with a tofrom mapper. The previous behavior: - `target map(to: ptr[0:size]) map(from: ptr[0:size])`: the from-mapper was hindered from copying the data back to the host because it was handled first on target exit which means that the to-mapper hadn't been able to decrement the reference count first. - `target map(from: ptr[0:size]) map(to: ptr[0:size])`: the to-mapper was hindered from copying the data to the device because it was handled second on target entry which means that the reference count had already been incremented by the decayed alloc/storage operation of the from-mapper.
1 parent 80d7e59 commit 769b7fe

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

offload/libomptarget/omptarget.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,26 @@ static int performPointerAttachment(DeviceTy &Device, AsyncInfoTy &AsyncInfo,
483483
return HandleSubmitResult(SubmitResult);
484484
}
485485

486+
/// Reorder the mapper indices such that the mappers that are of the specified
487+
/// map type occur last.
488+
/// Important: aside from this reordering, the order of the mappers must be
489+
/// maintained to not violate other ordering requirements.
490+
static SmallVector<int32_t> reorderMapType(int32_t ArgNum, int64_t *ArgTypes,
491+
tgt_map_type MapType) {
492+
SmallVector<int32_t> Mappers(ArgNum);
493+
auto *Front = Mappers.begin();
494+
auto *Back = Mappers.end() - 1;
495+
496+
for (int32_t I = 0; I < ArgNum; ++I) {
497+
if (ArgTypes[I] & MapType)
498+
*Back-- = I;
499+
else
500+
*Front++ = I;
501+
}
502+
503+
return Mappers;
504+
}
505+
486506
/// Internal function to do the mapping and transfer the data to the device
487507
int targetDataBegin(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
488508
void **ArgsBase, void **Args, int64_t *ArgSizes,
@@ -492,7 +512,9 @@ int targetDataBegin(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
492512
assert(AttachInfo && "AttachInfo must be available for targetDataBegin for "
493513
"handling ATTACH map-types.");
494514
// process each input.
495-
for (int32_t I = 0; I < ArgNum; ++I) {
515+
SmallVector<int32_t> Mappers =
516+
reorderMapType(ArgNum, ArgTypes, OMP_TGT_MAPTYPE_FROM);
517+
for (int32_t I : Mappers) {
496518
// Ignore private variables and arrays - there is no mapping for them.
497519
if ((ArgTypes[I] & OMP_TGT_MAPTYPE_LITERAL) ||
498520
(ArgTypes[I] & OMP_TGT_MAPTYPE_PRIVATE))
@@ -1008,7 +1030,9 @@ int targetDataEnd(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
10081030
int Ret = OFFLOAD_SUCCESS;
10091031
auto *PostProcessingPtrs = new SmallVector<PostProcessingInfo>();
10101032
// process each input.
1011-
for (int32_t I = ArgNum - 1; I >= 0; --I) {
1033+
SmallVector<int32_t> Mappers =
1034+
reorderMapType(ArgNum, ArgTypes, OMP_TGT_MAPTYPE_TO);
1035+
for (int32_t I : llvm::reverse(Mappers)) {
10121036
// Ignore private variables and arrays - there is no mapping for them.
10131037
// Also, ignore the use_device_ptr directive, it has no effect here.
10141038
if ((ArgTypes[I] & OMP_TGT_MAPTYPE_LITERAL) ||

0 commit comments

Comments
 (0)