This fixes two issues with the recently added absent-optional patch for use_device_…

(a) For nonallocatable, nonpointer arrays the data component of the array descriptor is replaced by a local variable – if the argument is absent, this variable is not initialized and, unless it is NULL, a pointer to undefined memory attempted to be mapped.

(b) For per-value arguments, the dummy argument itself always exists and as it is not a pointer, there is nothing to dereference. Hence, 'use_device_addr(val_arg)' can be run unconditionally. However, doing so will fail if 'val_arg' has never been mapped to the device. – I think the most sensible is to update the test case. (One could add a condition, to use a NULL pointer if absent, but as I cannot come up with a valid program, leaving the condition out in the generated code makes more sense.)

OK?

Tobias

PS: I wonder why I didn't see it when initially submitting the patch. I think it must be after I did a small change and did a quick regtesting. I assume for some reasons the device became unavailable – turning all checks into unsupported and I only looked for FAIL and didn't check whether some new unsupported popped up. – Namely, use_device_addr-{3,4}.f90 failed without (a) – but only with -O1 (and with 1 of 11 (hardware, cuda version) combos with -Os). use_device_ptr-optional-2.f90 failed with '-O' (which is the only option which was actually run) due to (a) and (b).

	gcc/
	* omp-low.c (lower_omp_target): Also add is-present condition for
	nonallocatable, nonpointer, optional arguments.

	libgomp/
	* testsuite/libgomp.fortran/use_device_ptr-optional-2.f90: Map
	per-value optional arg before using it in use_device_addr.


diff --git a/gcc/omp-low.c b/gcc/omp-low.c
index 19132f76da2..94f830c644b 100644
--- a/gcc/omp-low.c
+++ b/gcc/omp-low.c
@@ -12029,6 +12029,8 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
 			var = build_simple_mem_ref (var);
 			do_optional_check = true;
 		      }
+		    else if (TREE_CODE (type) == ARRAY_TYPE)
+		      do_optional_check = true;
 		    var = fold_convert (TREE_TYPE (x), var);
 		  }
 	      }
diff --git a/libgomp/testsuite/libgomp.fortran/use_device_ptr-optional-2.f90 b/libgomp/testsuite/libgomp.fortran/use_device_ptr-optional-2.f90
index 41abf17eede..bad0e00a23a 100644
--- a/libgomp/testsuite/libgomp.fortran/use_device_ptr-optional-2.f90
+++ b/libgomp/testsuite/libgomp.fortran/use_device_ptr-optional-2.f90
@@ -1,3 +1,5 @@
+! {dg-do run }
+!
 ! Check whether absent optional arguments are properly
 ! handled with use_device_{addr,ptr}.
 program main
@@ -12,6 +14,10 @@ contains
     integer, target, optional, allocatable :: z(:)
     integer :: d
 
+    !$omp target data map(to:v)
+    ! If 'v' should be usable as device pointer, it has to be mapped at some
+    ! point. As it is declared with the VALUE attribute, it needs to be done
+    ! within 'foo'
     !$omp target data map(d) use_device_addr(v, w, x, y, z)
       if(present(v)) stop 1
       if(present(w)) stop 2
@@ -19,6 +25,7 @@ contains
       if(present(y)) stop 4
       if(present(z)) stop 5
     !$omp end target data
+    !$omp end target data
 
 ! Using 'v' in use_device_ptr gives an ICE
 ! TODO: Find out what the OpenMP spec permits for use_device_ptr

Reply via email to