Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions flang/test/Integration/OpenMP/map-types-and-sizes.f90
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ subroutine mapType_array
!$omp end target
end subroutine mapType_array

!CHECK: @.offload_sizes{{.*}} = private unnamed_addr constant [1 x i64] [i64 8]
!CHECK: @.offload_maptypes{{.*}} = private unnamed_addr constant [1 x i64] [i64 288]
subroutine mapType_is_device_ptr
use iso_c_binding, only : c_ptr
type(c_ptr) :: p
!$omp target is_device_ptr(p)
!$omp end target
end subroutine mapType_is_device_ptr

!CHECK: @.offload_sizes{{.*}} = private unnamed_addr constant [4 x i64] [i64 0, i64 24, i64 8, i64 0]
!CHECK: @.offload_maptypes{{.*}} = private unnamed_addr constant [4 x i64] [i64 32, i64 281474976711169, i64 281474976711171, i64 281474976711187]
subroutine mapType_ptr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,6 @@ static LogicalResult checkImplementationStatus(Operation &op) {
op.getInReductionSyms())
result = todo("in_reduction");
};
auto checkIsDevicePtr = [&todo](auto op, LogicalResult &result) {
if (!op.getIsDevicePtrVars().empty())
result = todo("is_device_ptr");
};
auto checkLinear = [&todo](auto op, LogicalResult &result) {
if (!op.getLinearVars().empty() || !op.getLinearStepVars().empty())
result = todo("linear");
Expand Down Expand Up @@ -444,7 +440,6 @@ static LogicalResult checkImplementationStatus(Operation &op) {
checkBare(op, result);
checkDevice(op, result);
checkInReduction(op, result);
checkIsDevicePtr(op, result);
})
.Default([](Operation &) {
// Assume all clauses for an operation can be translated unless they are
Expand Down Expand Up @@ -3875,6 +3870,11 @@ convertClauseMapFlags(omp::ClauseMapFlags mlirFlags) {
if (mapTypeToBool(omp::ClauseMapFlags::attach))
mapType |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_ATTACH;

if (mapTypeToBool(omp::ClauseMapFlags::is_device_ptr)) {
mapType |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TARGET_PARAM;
mapType |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_LITERAL;
}

return mapType;
}

Expand Down Expand Up @@ -3996,6 +3996,9 @@ static void collectMapDataFromMapOperands(
llvm::Value *origValue = moduleTranslation.lookupValue(offloadPtr);
auto mapType = convertClauseMapFlags(mapOp.getMapType());
auto mapTypeAlways = llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_ALWAYS;
bool isDevicePtr =
(mapOp.getMapType() & omp::ClauseMapFlags::is_device_ptr) !=
omp::ClauseMapFlags::none;

mapData.OriginalValue.push_back(origValue);
mapData.BasePointers.push_back(origValue);
Expand Down Expand Up @@ -4029,7 +4032,8 @@ static void collectMapDataFromMapOperands(
mapData.Names.push_back(LLVM::createMappingInformation(
mapOp.getLoc(), *moduleTranslation.getOpenMPBuilder()));
mapData.DevicePointers.push_back(
llvm::OpenMPIRBuilder::DeviceInfoTy::Address);
isDevicePtr ? llvm::OpenMPIRBuilder::DeviceInfoTy::Pointer
: llvm::OpenMPIRBuilder::DeviceInfoTy::Address);
mapData.IsAMapping.push_back(false);
mapData.IsAMember.push_back(checkIsAMember(hasDevAddrOperands, mapOp));
}
Expand Down
17 changes: 17 additions & 0 deletions mlir/test/Target/LLVMIR/omptarget-llvm.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,20 @@ module attributes {omp.target_triples = ["amdgcn-amd-amdhsa"]} {
// CHECK: br label %[[VAL_40]]
// CHECK: omp.done: ; preds = %[[VAL_68]], %[[VAL_63]], %[[VAL_32]]
// CHECK: ret void

// -----

module attributes {omp.target_triples = ["amdgcn-amd-amdhsa"]} {
llvm.func @_QPomp_target_is_device_ptr(%arg0 : !llvm.ptr) {
%map = omp.map.info var_ptr(%arg0 : !llvm.ptr, !llvm.ptr)
map_clauses(is_device_ptr) capture(ByRef) -> !llvm.ptr {name = ""}
omp.target map_entries(%map -> %ptr_arg : !llvm.ptr) {
omp.terminator
}
llvm.return
}
}

// CHECK: @.offload_sizes = private unnamed_addr constant [1 x i64] [i64 8]
// CHECK: @.offload_maptypes = private unnamed_addr constant [1 x i64] [i64 288]
// CHECK-LABEL: define void @_QPomp_target_is_device_ptr
11 changes: 0 additions & 11 deletions mlir/test/Target/LLVMIR/openmp-todo.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,6 @@ llvm.func @target_in_reduction(%x : !llvm.ptr) {

// -----

llvm.func @target_is_device_ptr(%x : !llvm.ptr) {
// expected-error@below {{not yet implemented: Unhandled clause is_device_ptr in omp.target operation}}
// expected-error@below {{LLVM Translation failed for operation: omp.target}}
omp.target is_device_ptr(%x : !llvm.ptr) {
omp.terminator
}
llvm.return
}

// -----

llvm.func @target_enter_data_depend(%x: !llvm.ptr) {
// expected-error@below {{not yet implemented: Unhandled clause depend in omp.target_enter_data operation}}
// expected-error@below {{LLVM Translation failed for operation: omp.target_enter_data}}
Expand Down
46 changes: 46 additions & 0 deletions offload/test/offloading/fortran/target-is-device-ptr.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
! Validate that a device pointer obtained via omp_get_mapped_ptr can be used
! inside a TARGET region with the is_device_ptr clause.
! REQUIRES: flang, amdgcn-amd-amdhsa

! RUN: %libomptarget-compile-fortran-run-and-check-generic

program is_device_ptr_target
use iso_c_binding, only : c_ptr, c_loc
implicit none

interface
function omp_get_mapped_ptr(host_ptr, device_num) &
bind(C, name="omp_get_mapped_ptr")
use iso_c_binding, only : c_ptr, c_int
type(c_ptr) :: omp_get_mapped_ptr
type(c_ptr), value :: host_ptr
integer(c_int), value :: device_num
end function omp_get_mapped_ptr
end interface

integer, parameter :: n = 4
integer, parameter :: dev = 0
integer, target :: a(n)
type(c_ptr) :: dptr
integer :: flag

a = [2, 4, 6, 8]
flag = 0

!$omp target data map(tofrom: a, flag)
dptr = omp_get_mapped_ptr(c_loc(a), dev)

!$omp target is_device_ptr(dptr) map(tofrom: flag)
flag = flag + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dptr is still not used inside the target construct. Is your goal to validate that dptr is correctly passed into the target, or just that this doesn't cause a segfault/offload-failure?

If it's the former, then one way to do it is by calling c_f_pointer inside the target region, and reading/writing the pointee via the fortran pointer. Another way is to transfer dptr into an INTEGER(C_INTPTR_T) both inside and outside the target region, print it, and CHECK that the two addresses printed are identical.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried adding the call to c_f_pointer inside the target region in the previous test. But it causes a runtime failure, and I'm not sure if the failure is directly related to the is_device_ptr implementation or some other mapping bug.

If you're happy with the current simpler test for this PR, then I can work on debugging a fix for that failure when I return from vacation in the new year. Otherwise, I'll put this PR on hold until I can come up with a working version of that test with a call to c_f_pointer inside the target region.

Let me know which you prefer, thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to tighten the test before merging. I might be missing something but I tried the following experiment:

 ! RUN: %libomptarget-compile-fortran-run-and-check-generic
 
 program is_device_ptr_target
-  use iso_c_binding, only : c_ptr, c_loc
+  use iso_c_binding, only : c_ptr, c_loc, c_intptr_t, C_NULL_PTR
   implicit none
 
   interface
@@ -22,19 +22,25 @@ program is_device_ptr_target
   integer, parameter :: dev = 0
   integer, target :: a(n)
   type(c_ptr) :: dptr
+  type(c_ptr) :: dptr_cpy
   integer :: flag
 
   a = [2, 4, 6, 8]
   flag = 0
+  dptr_cpy = C_NULL_PTR
 
   !$omp target data map(tofrom: a, flag)
     dptr = omp_get_mapped_ptr(c_loc(a), dev)
+    write(*, '(Z16)') dptr
+    write(*, '(Z16)') dptr_cpy
 
-    !$omp target is_device_ptr(dptr) map(tofrom: flag)
+    !$omp target is_device_ptr(dptr) map(tofrom: flag, dptr_cpy)
       flag = flag + 1
+      dptr_cpy = dptr
     !$omp end target
   !$omp end target data
 
+  write(*, '(Z16)') dptr_cpy
   if (flag .eq. 1 .and. all(a == [2, 4, 6, 8])) then
     print *, "PASS"
   else

And this prints:

    7C35B9220000
               0
               0
 PASS

which might mean that the device pointer kernel argument is not properly passed to the kernel. Let me know if I misinterpreted this.

!$omp end target
!$omp end target data

if (flag .eq. 1 .and. all(a == [2, 4, 6, 8])) then
print *, "PASS"
else
print *, "FAIL", a
end if

end program is_device_ptr_target

!CHECK: PASS