This is an archive of the discontinued LLVM Phabricator instance.

[Libomptarget] Implement OpenMP 5.2 semantics for device pointers
ClosedPublic

Authored by jhuber6 on Sep 7 2022, 12:39 PM.

Details

Summary

In OpenMP 5.2, §5.8.6, page 160 line 32-33, when a device pointer
allocated by omp_target_alloc has implicitly been included on a target
construct as a zero-length array, the pointer initialisation should not
find a matching mapped list item, and so should retain its value as a
firstprivate variable. Previously, we would return a null pointer if the
list item was not found. This patch updates the map handling to the
OpenMP 5.2 semantics.

Diff Detail

Event Timeline

jhuber6 created this revision.Sep 7 2022, 12:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 7 2022, 12:39 PM
jhuber6 requested review of this revision.Sep 7 2022, 12:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 7 2022, 12:39 PM
This revision is now accepted and ready to land.Sep 7 2022, 1:17 PM
ye-luo requested changes to this revision.Sep 7 2022, 1:30 PM
ye-luo added inline comments.
openmp/libomptarget/include/device.h
285

IsPresent is not orthogonal to IsHostPointer. This unamed struct design is flawed.

Also directly access TargetPointerResultTy member is also bad. There is lack of read only protection. accessing members should via a function.

something like.
struct TargetPointerResultTy {

std::unique_ptr<EntryFlags> flags# null is the entry doesn't exist.

inline bool isPresent() const { return bool(flags); }

}

openmp/libomptarget/test/mapping/implicit_device_ptr.c
13

Doesn't compile. Need (int *) omp_target_alloc

This revision now requires changes to proceed.Sep 7 2022, 1:30 PM
jhuber6 added inline comments.Sep 7 2022, 1:37 PM
openmp/libomptarget/include/device.h
285

IsPresent is not orthogonal to IsHostPointer. This unamed struct design is flawed.

Could you elaborate on this? They should mean different things, with the current semantics we pass the pointer first-private, but do not assume it is a valid host pointer. That's why we use both IsPresent and IsHostPointer. For USM we will have IsPresent be false and IsHostPointer be true, for this implicit mapping IsHostPointer will be false.

Also directly access TargetPointerResultTy member is also bad. There is lack of read only protection. accessing members should via a function.

Sure, I'll add it. I'm not exactly sure what the utility of this struct is anyway, considering that these are just bools.

openmp/libomptarget/test/mapping/implicit_device_ptr.c
13

This is C code, the cast shouldn't be necessary.

jhuber6 updated this revision to Diff 458551.Sep 7 2022, 1:51 PM

Updating according to @ye-luo's suggestions.

jdoerfert added inline comments.Sep 7 2022, 2:01 PM
openmp/libomptarget/include/device.h
285
  1. IsPresent and IsHostPointer are orthogonal, as elaborated this morning in the call.
  2. All the rest is unrelated to this patch. Potentially fair but unrelated.
ye-luo accepted this revision.Sep 7 2022, 2:04 PM
ye-luo added inline comments.
openmp/libomptarget/include/device.h
285

OK. I was thinking of IsHostPointer for zero-length array. Actually your are right they are for USM. There is still some interaction between isNew and isPresent. I think isNew only make sense when isPresent is true.

Maybe std::unque_ptr is not a good option because it involves memory alloc/dealloc, std::optional is better but it requires c++17. Probably still need to stay in C++14 now. So the current way is acceptable.

openmp/libomptarget/src/device.cpp
433

I'm wondering if the USM case and this case should be fused in a more compact way.
The action is the same, when a pointer is not present in the map table. Keep the original value.

Even the logic of USM is fishy https://github.com/llvm/llvm-project/blob/178554f3c8f983bd192818b6d46e4d95560c4964/openmp/libomptarget/src/api.cpp#L133
But I would consider doing any related change in a separate patch.

openmp/libomptarget/test/mapping/implicit_device_ptr.c
13

You are right, I was compiling as C++.

This revision is now accepted and ready to land.Sep 7 2022, 2:04 PM
ye-luo added a comment.Sep 7 2022, 2:09 PM
This comment was removed by ye-luo.
ye-luo added inline comments.Sep 7 2022, 2:13 PM
openmp/libomptarget/src/api.cpp
128

129-134 seems to be useless with this patch.