Skip to content

Commit

Permalink
[OpenMP] Avoid zero size copies to the device
Browse files Browse the repository at this point in the history
This unblocks one of the XFAIL tests for AMD, though we need to work
around the missing printf still.

Differential Revision: https://reviews.llvm.org/D146592
  • Loading branch information
jdoerfert committed Mar 22, 2023
1 parent ebcc6db commit de9edf4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion openmp/libomptarget/src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ TargetPointerResultTy DeviceTy::getTargetPointer(

// If the target pointer is valid, and we need to transfer data, issue the
// data transfer.
if (TargetPointer && !IsHostPtr && HasFlagTo && (IsNew || HasFlagAlways)) {
if (TargetPointer && !IsHostPtr && HasFlagTo && (IsNew || HasFlagAlways) &&
Size != 0) {
// Lock the entry before releasing the mapping table lock such that another
// thread that could issue data movement will get the right result.
std::lock_guard<decltype(*Entry)> LG(*Entry);
Expand Down
2 changes: 1 addition & 1 deletion openmp/libomptarget/src/omptarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ int targetDataEnd(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
// Move data back to the host
const bool HasAlways = ArgTypes[I] & OMP_TGT_MAPTYPE_ALWAYS;
const bool HasFrom = ArgTypes[I] & OMP_TGT_MAPTYPE_FROM;
if (HasFrom && (HasAlways || IsLast) && !IsHostPtr) {
if (HasFrom && (HasAlways || IsLast) && !IsHostPtr && DataSize != 0) {
DP("Moving %" PRId64 " bytes (tgt:" DPxMOD ") -> (hst:" DPxMOD ")\n",
DataSize, DPxPTR(TgtPtrBegin), DPxPTR(HstPtrBegin));

Expand Down
34 changes: 22 additions & 12 deletions openmp/libomptarget/test/mapping/data_member_ref.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// RUN: %libomptarget-compilexx-run-and-check-generic

// Wrong results on amdgpu
// XFAIL: amdgcn-amd-amdhsa

#include <stdio.h>

struct View {
Expand All @@ -26,42 +23,55 @@ int main() {
int Data = 123456;
V1.Data = &Data;
Foo<ViewPtr> Baz(V1);
int D1, D2;

// CHECK: Host 123456.
printf("Host %d.\n", Bar.VRef.Data);
#pragma omp target map(Bar.VRef)
#pragma omp target map(Bar.VRef) map(from : D1, D2)
{
// CHECK: Device 123456.
printf("Device %d.\n", Bar.VRef.Data);
D1 = Bar.VRef.Data;
printf("Device %d.\n", D1);
V.Data = 654321;
// CHECK: Device 654321.
printf("Device %d.\n", Bar.VRef.Data);
D2 = Bar.VRef.Data;
printf("Device %d.\n", D2);
}
printf("Device %d.\n", D1);
printf("Device %d.\n", D2);
// CHECK: Host 654321 654321.
printf("Host %d %d.\n", Bar.VRef.Data, V.Data);
V.Data = 123456;
// CHECK: Host 123456.
printf("Host %d.\n", Bar.VRef.Data);
#pragma omp target map(Bar) map(Bar.VRef)
#pragma omp target map(Bar) map(Bar.VRef) map(from : D1, D2)
{
// CHECK: Device 123456.
printf("Device %d.\n", Bar.VRef.Data);
D1 = Bar.VRef.Data;
printf("Device %d.\n", D1);
V.Data = 654321;
// CHECK: Device 654321.
printf("Device %d.\n", Bar.VRef.Data);
D2 = Bar.VRef.Data;
printf("Device %d.\n", D2);
}
printf("Device %d.\n", D1);
printf("Device %d.\n", D2);
// CHECK: Host 654321 654321.
printf("Host %d %d.\n", Bar.VRef.Data, V.Data);
// CHECK: Host 123456.
printf("Host %d.\n", *Baz.VRef.Data);
#pragma omp target map(*Baz.VRef.Data)
#pragma omp target map(*Baz.VRef.Data) map(from : D1, D2)
{
// CHECK: Device 123456.
printf("Device %d.\n", *Baz.VRef.Data);
D1 = *Baz.VRef.Data;
printf("Device %d.\n", D1);
*V1.Data = 654321;
// CHECK: Device 654321.
printf("Device %d.\n", *Baz.VRef.Data);
D2 = *Baz.VRef.Data;
printf("Device %d.\n", D2);
}
printf("Device %d.\n", D1);
printf("Device %d.\n", D2);
// CHECK: Host 654321 654321 654321.
printf("Host %d %d %d.\n", *Baz.VRef.Data, *V1.Data, Data);
return 0;
Expand Down

0 comments on commit de9edf4

Please sign in to comment.