Jerry D wrote:
It does touch on my patch for PR129002. I will do a fixup on my end after you commit.

Thanks!

I have yet another version v4 - hopefully the last one.

It fixes one issue of mine: for the 'defaultmap' clause a missing
diagnostic. I have to admit that I do not quite understand when a
simple 'break' works and when only a 'goto error' to show an error,
but the latter surely works.

Being there, I also added parsing support for the 'optional'
modifier of OpenMP 6.1 (fails after parsing); eventually, it
needs to be wired up for 'default' and 'defaultmap', including
middle end support - likewise for parsing the other now permitted
values like 'storage' (alias for 'alloc') and ...

Otherwise, it fixes polymorphic handling or class and pointer
- and I decided to also silence that AI complaint about not
handling ENTRY.

Let's see whether that survives AI review - if it does or it
only has some odd nit-pigging, I will commit it

Tobias
OpenMP: Fix Fortran defaultmap handling for pointer

Fortran arrays use array descriptors and the current code
mishandled derived types passed as argument (by pointer,
i.e. 'optional') as being a pointer - while locally declared
pointer arrays (using an array descriptor) were misclassified
as aggregate.

The obvious fix is to handle it as language hook, which this
code does. Allocatables and scalars were handled already as
language hook such that only pointer vs. aggregate were affected.

Additionally fix a missing diagnostic with 'defaultmap' and add
parser support (with 'sorry') for the optional modifier, added
in OpenMP 6.1.

Assisted-by: Following suggestion by gemini/gemini-4-argon (gcc.sashiko.dev) for gfc_omp_scalar_p

        PR fortran/129067

gcc/fortran/ChangeLog:

	* f95-lang.cc (LANG_HOOKS_OMP_POINTER_P): Define to
	point to gfc_omp_pointer_p.
        * openmp.cc (gfc_match_omp_clauses): For defaultmap clause,
	fix error diagnostic and parse-and-sorry 'optional' modifier.
	* trans-openmp.cc (gfc_omp_pointer_p): New.
	(gfc_omp_scalar_p): Fix bug related to optional arguments.
	* trans.h (gfc_omp_pointer_p): New prototype.

gcc/ChangeLog:

	* gimplify.cc (omp_notice_variable): Call omp_pointer_p hook.
	* langhooks-def.h (lhd_omp_pointer_p): Add, use check previously
	in omp_notice_variable.
	(LANG_HOOKS_OMP_POINTER_P): New, default to lhd_omp_pointer_p.
	(lhd_omp_pointer_p): Declare.
	* langhooks.cc (lhd_omp_pointer_p): New.
	* langhooks.h (struct lang_hooks_for_decls): Add omp_pointer_p
	function-pointer member.

libgomp/ChangeLog:

	PR fortran/100991

        * testsuite/libgomp.fortran/defaultmap-8.f90: Remove xfail
	now that scalars are properly mapped.
        * testsuite/libgomp.fortran/optional-map.f90: Add
	'defaultmap(tofrom:scalar)' as they are now properly mapped.
	* testsuite/libgomp.fortran/defaultmap-10.f90: New test.
	* testsuite/libgomp.fortran/defaultmap-11.f90: New test.
	* testsuite/libgomp.fortran/defaultmap-9.f90: New test.

gcc/testsuite/ChangeLog:

	* gfortran.dg/gomp/defaultmap-1.f90: Update dg-error.
	* gfortran.dg/gomp/defaultmap-4.f90: Update scan-tree-dump
	for now properly handling 'pointer'.
	* gfortran.dg/gomp/defaultmap-11.f90: New test.
        * gfortran.dg/gomp/defaultmap-12.f90: New test.
	* gfortran.dg/gomp/lastprivate-conditional-6.f90: New test.
	* gfortran.dg/gomp/lastprivate-conditional-7.f90: New test.

 gcc/fortran/f95-lang.cc                            |   2 +
 gcc/fortran/openmp.cc                              |  45 ++++++-
 gcc/fortran/trans-openmp.cc                        |  61 +++++++--
 gcc/fortran/trans.h                                |   1 +
 gcc/gimplify.cc                                    |   5 +-
 gcc/langhooks-def.h                                |   3 +
 gcc/langhooks.cc                                   |  14 ++
 gcc/langhooks.h                                    |   4 +
 gcc/testsuite/gfortran.dg/gomp/defaultmap-1.f90    |   2 +-
 gcc/testsuite/gfortran.dg/gomp/defaultmap-11.f90   |  61 +++++++++
 gcc/testsuite/gfortran.dg/gomp/defaultmap-12.f90   |  27 ++++
 gcc/testsuite/gfortran.dg/gomp/defaultmap-4.f90    |  36 ++---
 .../gfortran.dg/gomp/lastprivate-conditional-6.f90 | 137 +++++++++++++++++++
 .../gfortran.dg/gomp/lastprivate-conditional-7.f90 | 120 +++++++++++++++++
 .../testsuite/libgomp.fortran/defaultmap-10.f90    | 148 ++++++++++++++++++++
 .../testsuite/libgomp.fortran/defaultmap-11.f90    | 149 +++++++++++++++++++++
 libgomp/testsuite/libgomp.fortran/defaultmap-8.f90 |   7 +-
 libgomp/testsuite/libgomp.fortran/defaultmap-9.f90 | 143 ++++++++++++++++++++
 libgomp/testsuite/libgomp.fortran/optional-map.f90 |   2 +-
 19 files changed, 924 insertions(+), 43 deletions(-)

diff --git a/gcc/fortran/f95-lang.cc b/gcc/fortran/f95-lang.cc
index 10d18fe5a22..ff2fa672410 100644
--- a/gcc/fortran/f95-lang.cc
+++ b/gcc/fortran/f95-lang.cc
@@ -155,6 +155,7 @@ gfc_get_sarif_source_language (const char *)
 #undef LANG_HOOKS_OMP_DEEP_MAPPING_P
 #undef LANG_HOOKS_OMP_DEEP_MAPPING_CNT
 #undef LANG_HOOKS_OMP_ALLOCATABLE_P
+#undef LANG_HOOKS_OMP_POINTER_P
 #undef LANG_HOOKS_OMP_SCALAR_TARGET_P
 #undef LANG_HOOKS_OMP_SCALAR_P
 #undef LANG_HOOKS_OMP_DISREGARD_VALUE_EXPR
@@ -199,6 +200,7 @@ gfc_get_sarif_source_language (const char *)
 #define LANG_HOOKS_OMP_DEEP_MAPPING_P		gfc_omp_deep_mapping_p
 #define LANG_HOOKS_OMP_DEEP_MAPPING_CNT		gfc_omp_deep_mapping_cnt
 #define LANG_HOOKS_OMP_ALLOCATABLE_P		gfc_omp_allocatable_p
+#define LANG_HOOKS_OMP_POINTER_P		gfc_omp_pointer_p
 #define LANG_HOOKS_OMP_SCALAR_P			gfc_omp_scalar_p
 #define LANG_HOOKS_OMP_SCALAR_TARGET_P		gfc_omp_scalar_target_p
 #define LANG_HOOKS_OMP_DISREGARD_VALUE_EXPR	gfc_omp_disregard_value_expr
diff --git a/gcc/fortran/openmp.cc b/gcc/fortran/openmp.cc
index 670e4af1399..233679f61e4 100644
--- a/gcc/fortran/openmp.cc
+++ b/gcc/fortran/openmp.cc
@@ -3028,6 +3028,7 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const omp_mask mask,
 	      enum gfc_omp_defaultmap behavior;
 	      gfc_omp_defaultmap_category category
 		= OMP_DEFAULTMAP_CAT_UNCATEGORIZED;
+	      bool optional = false;
 	      if (gfc_match ("alloc ") == MATCH_YES)
 		behavior = OMP_DEFAULTMAP_ALLOC;
 	      else if (gfc_match ("tofrom ") == MATCH_YES)
@@ -3048,15 +3049,31 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const omp_mask mask,
 		{
 		  gfc_error ("Expected ALLOC, TO, FROM, TOFROM, FIRSTPRIVATE, "
 			     "PRESENT, NONE or DEFAULT at %C");
-		  break;
+		  goto error;
 		}
 	      if (')' == gfc_peek_ascii_char ())
 		;
 	      else if (gfc_match (": ") != MATCH_YES)
-		break;
+		{
+		  gfc_error ("Expected %<)%> or %<:%> at %C");
+		  goto error;
+		}
 	      else
 		{
-		  if (gfc_match ("scalar ") == MATCH_YES)
+		  if (gfc_match ("optional ") == MATCH_YES)
+		    {
+		      optional = true;
+		      if (')' == gfc_peek_ascii_char ())
+			category = OMP_DEFAULTMAP_CAT_ALL;
+		      else if (gfc_match (", ") != MATCH_YES)
+			{
+			  gfc_error ("Expected %<)%> or %<:%> at %C");
+			  goto error;
+			}
+		    }
+		  if (category == OMP_DEFAULTMAP_CAT_ALL)
+		    ;
+		  else if (gfc_match ("scalar ") == MATCH_YES)
 		    category = OMP_DEFAULTMAP_CAT_SCALAR;
 		  else if (gfc_match ("aggregate ") == MATCH_YES)
 		    category = OMP_DEFAULTMAP_CAT_AGGREGATE;
@@ -3069,8 +3086,18 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const omp_mask mask,
 		  else
 		    {
 		      gfc_error ("Expected SCALAR, AGGREGATE, ALLOCATABLE, "
-				 "POINTER or ALL at %C");
-		      break;
+				 "POINTER, ALL, or OPTIONAL at %C");
+		      goto error;
+		    }
+		  if (!optional && gfc_match (", ") == MATCH_YES)
+		    {
+		      if (gfc_match ("optional ") == MATCH_YES)
+			optional = true;
+		      else
+			{
+			  gfc_error ("Expected OPTIONAL at %C");
+			  goto error;
+			}
 		    }
 		}
 	      for (int i = 0; i < OMP_DEFAULTMAP_CAT_NUM; ++i)
@@ -3111,7 +3138,13 @@ gfc_match_omp_clauses (gfc_omp_clauses **cp, const omp_mask mask,
 		}
 	      c->defaultmap[category] = behavior;
 	      if (gfc_match (")") != MATCH_YES)
-		break;
+		{
+		  gfc_error ("Expected %<)%> at %C");
+		  goto error;
+		}
+	      if (optional)
+		sorry_at (gfc_get_location (&gfc_current_locus),
+			   "%<optional%> modifier is not supported yet");
 	      continue;
 	    }
 	  if ((mask & OMP_CLAUSE_DEFAULT)
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 13b17e573db..22c8ca36649 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -557,7 +557,40 @@ gfc_omp_allocatable_p (tree decl)
     type = TREE_TYPE (type);
 
   if (GFC_DESCRIPTOR_TYPE_P (type)
-      && GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE)
+      && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ALLOCATABLE
+	  || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_ALLOCATABLE))
+    return true;
+
+  if (gfc_class_container_kind (type) == 'a')
+    return true;
+
+  return false;
+}
+
+
+/* Return true if the DECL is for a pointer array or scalar.  */
+
+bool
+gfc_omp_pointer_p (tree decl)
+{
+  if (!DECL_P (decl))
+    return false;
+
+  if (GFC_DECL_GET_SCALAR_POINTER (decl))
+    return true;
+
+  tree type = TREE_TYPE (decl);
+  if (gfc_omp_privatize_by_reference (decl))
+    type = TREE_TYPE (type);
+
+  if (GFC_DESCRIPTOR_TYPE_P (type)
+      && (GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER
+	  || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_POINTER_CONT
+	  || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_POINTER
+	  || GFC_TYPE_ARRAY_AKIND (type) == GFC_ARRAY_ASSUMED_RANK_POINTER_CONT))
+    return true;
+
+  if (gfc_class_container_kind (type) == 'p')
     return true;
 
   return false;
@@ -2712,21 +2745,25 @@ bool
 gfc_omp_scalar_p (tree decl, bool ptr_alloc_ok)
 {
   tree type = TREE_TYPE (decl);
-  if (TREE_CODE (type) == REFERENCE_TYPE)
+
+  if (gfc_omp_privatize_by_reference (decl))
     type = TREE_TYPE (type);
-  if (TREE_CODE (type) == POINTER_TYPE)
+
+  if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
+      || GFC_DECL_GET_SCALAR_POINTER (decl))
     {
-      if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
-	  || GFC_DECL_GET_SCALAR_POINTER (decl))
-	{
-	  if (!ptr_alloc_ok)
-	    return false;
-	  type = TREE_TYPE (type);
-	}
-      if (GFC_ARRAY_TYPE_P (type)
-	  || GFC_CLASS_TYPE_P (type))
+      if (!ptr_alloc_ok)
 	return false;
+      if (POINTER_TYPE_P (type) && !VOID_TYPE_P (TREE_TYPE (type)))
+	type = TREE_TYPE (type);
+      /* Special case: pointer/allocatable dummy argument to an ENTRY
+	 function, but only if not optional - as the optional case is
+	 already handled by 'gfc_omp_privatize_by_reference' returning 'true'. */
+      if (POINTER_TYPE_P (type) && !VOID_TYPE_P (TREE_TYPE (type)))
+	type = TREE_TYPE (type);
     }
+  if (GFC_ARRAY_TYPE_P (type) || GFC_CLASS_TYPE_P (type))
+    return false;
   if ((TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == INTEGER_TYPE)
       && TYPE_STRING_FLAG (type))
     return false;
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index b0bc38fc3cc..a2736994d63 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -850,6 +850,7 @@ tree gfc_omp_deep_mapping_cnt (const gimple *, tree, gimple_seq *);
 void gfc_omp_deep_mapping (const gimple *, tree, unsigned HOST_WIDE_INT, tree,
 			   tree, tree, tree, tree, gimple_seq *);
 bool gfc_omp_allocatable_p (tree);
+bool gfc_omp_pointer_p (tree);
 bool gfc_omp_scalar_p (tree, bool);
 bool gfc_omp_scalar_target_p (tree);
 bool gfc_omp_disregard_value_expr (tree, bool);
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index 630ba882533..53d2e7e142c 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -9288,10 +9288,7 @@ omp_notice_variable (struct gimplify_omp_ctx *ctx, tree decl, bool in_code)
 		    gdmk = GDMK_SCALAR_TARGET;
 		  else if (lang_hooks.decls.omp_scalar_p (decl, false))
 		    gdmk = GDMK_SCALAR;
-		  else if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
-			   || (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE
-			       && (TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
-				   == POINTER_TYPE)))
+		  else if (lang_hooks.decls.omp_pointer_p (decl))
 		    gdmk = GDMK_POINTER;
 		  else
 		    gdmk = GDMK_AGGREGATE;
diff --git a/gcc/langhooks-def.h b/gcc/langhooks-def.h
index 76cc9474b7e..a9c0c7bfcb2 100644
--- a/gcc/langhooks-def.h
+++ b/gcc/langhooks-def.h
@@ -96,6 +96,7 @@ extern tree lhd_omp_map_array_section (location_t, tree);
 struct gimplify_omp_ctx;
 extern void lhd_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *,
 					       tree);
+extern bool lhd_omp_pointer_p (tree);
 extern bool lhd_omp_scalar_p (tree, bool);
 extern tree *lhd_omp_get_decl_init (tree);
 extern void lhd_omp_finish_decl_inits ();
@@ -290,6 +291,7 @@ extern tree lhd_unit_size_without_reusable_padding (tree);
   lhd_omp_extract_mapper_directive
 #define LANG_HOOKS_OMP_MAP_ARRAY_SECTION lhd_omp_map_array_section
 #define LANG_HOOKS_OMP_ALLOCATABLE_P hook_bool_tree_false
+#define LANG_HOOKS_OMP_POINTER_P lhd_omp_pointer_p
 #define LANG_HOOKS_OMP_SCALAR_P lhd_omp_scalar_p
 #define LANG_HOOKS_OMP_SCALAR_TARGET_P hook_bool_tree_false
 #define LANG_HOOKS_OMP_GET_DECL_INIT lhd_omp_get_decl_init
@@ -332,6 +334,7 @@ extern tree lhd_unit_size_without_reusable_padding (tree);
   LANG_HOOKS_OMP_EXTRACT_MAPPER_DIRECTIVE, \
   LANG_HOOKS_OMP_MAP_ARRAY_SECTION, \
   LANG_HOOKS_OMP_ALLOCATABLE_P, \
+  LANG_HOOKS_OMP_POINTER_P, \
   LANG_HOOKS_OMP_SCALAR_P, \
   LANG_HOOKS_OMP_SCALAR_TARGET_P, \
   LANG_HOOKS_OMP_GET_DECL_INIT, \
diff --git a/gcc/langhooks.cc b/gcc/langhooks.cc
index d7413a8778a..aad10d500c6 100644
--- a/gcc/langhooks.cc
+++ b/gcc/langhooks.cc
@@ -698,6 +698,20 @@ lhd_omp_map_array_section (location_t, tree)
   return error_mark_node;
 }
 
+/* Return true if DECL is a pointer variable (for the purpose of
+   implicit mapping).  */
+
+bool
+lhd_omp_pointer_p (tree decl)
+{
+  tree type = TREE_TYPE (decl);
+  if (TREE_CODE (type) == POINTER_TYPE
+      || (TREE_CODE (type) == REFERENCE_TYPE
+	  && (TREE_CODE (TREE_TYPE (type)) == POINTER_TYPE)))
+    return true;
+  return false;
+}
+
 /* Return true if DECL is a scalar variable (for the purpose of
    implicit firstprivatization & mapping). Only if alloc_ptr_ok
    are allocatables and pointers accepted. */
diff --git a/gcc/langhooks.h b/gcc/langhooks.h
index c1b08862b37..685839b7d18 100644
--- a/gcc/langhooks.h
+++ b/gcc/langhooks.h
@@ -367,6 +367,10 @@ struct lang_hooks_for_decls
      implicit mapping).  */
   bool (*omp_allocatable_p) (tree decl);
 
+  /* Return true if DECL is a pointer variable (for the purpose of
+     implicit mapping).  */
+  bool (*omp_pointer_p) (tree decl);
+
   /* Return true if DECL is a scalar variable (for the purpose of
      implicit firstprivatization). If 'ptr_or', pointers and
      allocatables are also permitted.  */
diff --git a/gcc/testsuite/gfortran.dg/gomp/defaultmap-1.f90 b/gcc/testsuite/gfortran.dg/gomp/defaultmap-1.f90
index 5123e078e98..5dab2c8e1ee 100644
--- a/gcc/testsuite/gfortran.dg/gomp/defaultmap-1.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/defaultmap-1.f90
@@ -4,7 +4,7 @@ implicit none
 
 !$omp target defaultmap(bar)  ! { dg-error "25: Expected ALLOC, TO, FROM, TOFROM, FIRSTPRIVATE, PRESENT, NONE or DEFAULT" }
 
-!$omp target defaultmap ( alloc: foo)  ! { dg-error "34: Expected SCALAR, AGGREGATE, ALLOCATABLE, POINTER or ALL" }
+!$omp target defaultmap ( alloc: foo)  ! { dg-error "34: Expected SCALAR, AGGREGATE, ALLOCATABLE, POINTER, ALL, or OPTIONAL" }
 
 !$omp target defaultmap(alloc:scalar) defaultmap(none:Scalar)  ! { dg-error "DEFAULTMAP at .1. but prior DEFAULTMAP for category SCALAR" }
 
diff --git a/gcc/testsuite/gfortran.dg/gomp/defaultmap-11.f90 b/gcc/testsuite/gfortran.dg/gomp/defaultmap-11.f90
new file mode 100644
index 00000000000..4fe84f99a95
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/defaultmap-11.f90
@@ -0,0 +1,61 @@
+! { dg-additional-options "-fdump-tree-gimple" }
+
+! For defaultmap:
+! Check that 'type(t)' is an aggreate, unless it has a pointer or allocate attribute
+
+subroutine sub(d,vd,od,ad,pd,oad,opd, da,oda,ada,pda,oada,opda,n)
+  use iso_c_binding
+  implicit none
+  integer, value :: n
+  type(c_ptr) :: d,vd,od,ad,pd,oad,opd, s, ps, as
+  type(c_ptr) :: da(n),oda(n),ada(:),pda(:),oada(:),opda(:), sa(n), psa(:), asa(:)
+  value :: vd
+  optional :: od, oad, opd, oda, oada, opda
+  pointer :: pd, opd, pda, opda, ps, psa
+  allocatable :: ad, oad, ada, oada, as, asa
+
+  ! all variables unallocated & not initialized - but that's sufficient for a dump test!
+
+  ! non-ptr, non-alloc
+  !$omp target defaultmap(firstprivate : aggregate) defaultmap(tofrom : scalar) defaultmap(present : pointer) defaultmap(alloc: allocatable)
+  block
+    logical :: pr
+    pr =      c_associated(d) &   ! OK
+         .or. c_associated(vd) &  ! OK
+         .or. c_associated(od) &  ! OK
+         .or. c_associated(s) &   ! OK
+         .or. c_associated(da(1)) &  ! OK
+         .or. c_associated(oda(1)) & ! OK
+         .or. c_associated(sa(1)) ! OK
+  end block
+
+  ! ptr
+  !$omp target defaultmap(present : aggregate) defaultmap(tofrom : scalar) defaultmap(firstprivate : pointer) defaultmap(alloc: allocatable)
+  block
+    logical :: pr
+    pr =      c_associated(pd) &  ! OK
+         .or. c_associated(opd) & ! OK
+         .or. c_associated(ps) &  ! OK
+         .or. c_associated(pda(1)) & ! wrong: aggregate but is pointer
+         .or. c_associated(opda(1))& ! wrong: aggregate but is pointer
+         .or. c_associated(psa(1))  ! wrong: aggregate but is pointer
+  end block
+
+  ! alloc
+  !$omp target defaultmap(alloc : aggregate) defaultmap(tofrom : scalar) defaultmap(present : pointer) defaultmap(firstprivate: allocatable)
+  block
+    logical :: pr
+    pr =      c_associated(ad) &   ! OK
+         .or. c_associated(oad) &  ! OK
+         .or. c_associated(as) &   ! OK
+         .or. c_associated(ada(1)) &  ! -> wrong: aggregate is allocatable!
+         .or. c_associated(oada(1)) & ! -> wrong: aggregate is allocatable!
+         .or. c_associated(asa(1)) ! OK
+  end block
+end
+
+! Except for some additional bookkeeping, all referenced variables should be 'firstprivate'
+
+! { dg-final { scan-tree-dump "#pragma omp target num_teams\\(-2\\) thread_limit\\(0\\) defaultmap\\(tofrom:scalar\\) defaultmap\\(firstprivate:aggregate\\) defaultmap\\(alloc:allocatable\\) defaultmap\\(present:pointer\\) \[^\\n\\r\]*firstprivate\\(sa\\) firstprivate\\(s\\) firstprivate\\(oda\\) firstprivate\\(da\\) firstprivate\\(od\\) firstprivate\\(vd\\) firstprivate\\(d\\)" "gimple" } }
+! { dg-final { scan-tree-dump "#pragma omp target num_teams\\(-2\\) thread_limit\\(0\\) defaultmap\\(tofrom:scalar\\) defaultmap\\(present:aggregate\\) defaultmap\\(alloc:allocatable\\) defaultmap\\(firstprivate:pointer\\) firstprivate\\(psa\\) firstprivate\\(ps\\) firstprivate\\(opda\\) firstprivate\\(pda\\) firstprivate\\(opd\\) firstprivate\\(pd\\)" "gimple" } }
+! { dg-final { scan-tree-dump "#pragma omp target num_teams\\(-2\\) thread_limit\\(0\\) defaultmap\\(tofrom:scalar\\) defaultmap\\(alloc:aggregate\\) defaultmap\\(firstprivate:allocatable\\) defaultmap\\(present:pointer\\) firstprivate\\(asa\\) firstprivate\\(as\\) firstprivate\\(oada\\) firstprivate\\(ada\\) firstprivate\\(oad\\) firstprivate\\(ad\\)" "gimple" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/defaultmap-12.f90 b/gcc/testsuite/gfortran.dg/gomp/defaultmap-12.f90
new file mode 100644
index 00000000000..467e0162881
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/defaultmap-12.f90
@@ -0,0 +1,27 @@
+! PR fortran/129067
+
+subroutine sub
+  !$omp target defaultmap ( firstprivate : optional , allocatable )  ! { dg-message "sorry, unimplemented: 'optional' modifier is not supported yet" }
+  !$omp end target
+
+  !$omp target defaultmap(tofrom:optional,all)  ! { dg-message "sorry, unimplemented: 'optional' modifier is not supported yet" }
+  !$omp end target
+
+  !$omp target defaultmap (alloc : scalar , optional )  ! { dg-message "sorry, unimplemented: 'optional' modifier is not supported yet" }
+  !$omp end target
+
+  !$omp target defaultmap(present:aggregate,optional)  ! { dg-message "sorry, unimplemented: 'optional' modifier is not supported yet" }
+  !$omp end target
+
+  !$omp target defaultmap ( to : optional )  ! { dg-message "sorry, unimplemented: 'optional' modifier is not supported yet" }
+  !$omp end target
+
+  !$omp target defaultmap(from:optional)  ! { dg-message "sorry, unimplemented: 'optional' modifier is not supported yet" }
+  !$omp end target
+
+  !$omp target defaultmap(firstprivate : aggregate) defaultmap(tofrom : scalar) defaultmap(present : pointer) defaultmap(alloc ! { dg-error "Expected '\\)' or ':' at .1." }
+  block; end block
+
+  !$omp target defaultmap(firstprivate : aggregate) defaultmap(tofrom : scalar) defaultmap(present : pointer) defaultmap(alloc:  ! { dg-error "Expected SCALAR, AGGREGATE, ALLOCATABLE, POINTER, ALL, or OPTIONAL at .1." }
+  block; end block
+end
diff --git a/gcc/testsuite/gfortran.dg/gomp/defaultmap-4.f90 b/gcc/testsuite/gfortran.dg/gomp/defaultmap-4.f90
index 90811599291..56500daf1a9 100644
--- a/gcc/testsuite/gfortran.dg/gomp/defaultmap-4.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/defaultmap-4.f90
@@ -98,24 +98,25 @@ end
 ! { dg-final { scan-tree-dump-times "map\\(alloc:strxa \\\[pointer assign, bias: 0\\\]\\)" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(alloc:\\*strxp \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(alloc:strxp \\\[pointer assign, bias: 0\\\]\\)" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:1\\\] \\*\\) str1parr\\.data \\\[pointer assign, bias: 0\\\]\\)" 2 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:5\\\] \\*\\) str5parr\\.data \\\[pointer assign, bias: 0\\\]\\)" 2 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:\\.strxparr\\\] \\*\\) strxparr\\.data \\\[pointer assign, bias: 0\\\]\\)" 2 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(integer\\(kind=4\\)\\\[0:\\\] \\*\\) parr\\.data \\\[pointer assign, bias: 0\\\]\\)" 2 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(struct t\\\[0:\\\] \\*\\) dtparr\\.data \\\[pointer assign, bias: 0\\\]\\)" 2 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:1\\\] \\*\\) str1parr\\.data \\\[pointer assign, bias: 0\\\]\\)" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:5\\\] \\*\\) str5parr\\.data \\\[pointer assign, bias: 0\\\]\\)" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:\\.strxparr\\\] \\*\\) strxparr\\.data \\\[pointer assign, bias: 0\\\]\\)" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(integer\\(kind=4\\)\\\[0:\\\] \\*\\) parr\\.data \\\[pointer assign, bias: 0\\\]\\)" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(always_pointer:\\(struct t\\\[0:\\\] \\*\\) dtparr\\.data \\\[pointer assign, bias: 0\\\]\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:aarr \\\[pointer set, len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:arr \\\[len:" 1 "gimple" } }
 
-! { dg-final { scan-tree-dump-times "map\\(to:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:\\.strxparr\\\] \\*\\) strxparr\\.data \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:5\\\] \\*\\) str5parr\\.data \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:1\\\] \\*\\) str1parr\\.data \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:\\*\\(integer\\(kind=4\\)\\\[0:\\\] \\*\\) parr\\.data \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:\\*\\(struct t\\\[0:\\\] \\*\\) dtparr\\.data \\\[len:" 1 "gimple" } }
+! { dg-final { scan-tree-dump-not "map\\(to:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:\\.strxparr\\\] \\*\\) strxparr\\.data \\\[len:" "gimple" } }
+! { dg-final { scan-tree-dump-not "map\\(to:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:5\\\] \\*\\) str5parr\\.data \\\[len:" "gimple" } }
+! { dg-final { scan-tree-dump-not "map\\(to:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:1\\\] \\*\\) str1parr\\.data \\\[len:" "gimple" } }
+! { dg-final { scan-tree-dump-not "map\\(to:\\*\\(integer\\(kind=4\\)\\\[0:\\\] \\*\\) parr\\.data \\\[len:" "gimple" } }
+! { dg-final { scan-tree-dump-not "map\\(to:\\*\\(struct t\\\[0:\\\] \\*\\) dtparr\\.data \\\[len:" "gimple" } }
 
 ! { dg-final { scan-tree-dump-times "map\\(to:dtaarr \\\[pointer set, len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:dtarr \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:dt \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:dtparr \\\[pointer set, len:" 2 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(to:dtparr \\\[pointer set, len:" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "firstprivate\\(dtparr\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(tofrom:\\*aii \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(tofrom:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:\\.strxaarr\\\] \\* restrict\\) strxaarr\\.data \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(tofrom:\\*\\(character\\(kind=1\\)\\\[0:\\\]\\\[1:5\\\] \\* restrict\\) str5aarr\\.data \\\[len:" 1 "gimple" } }
@@ -126,20 +127,25 @@ end
 ! { dg-final { scan-tree-dump-times "map\\(tofrom:\\*str1a \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(tofrom:\\*str5a \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(tofrom:\\*strxa \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:parr \\\[pointer set, len:" 2 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(to:parr \\\[pointer set, len:" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "firstprivate\\(parr\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:str1aarr \\\[pointer set, len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:str1arr \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:str1 \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:str1parr \\\[pointer set, len:" 2 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(to:str1parr \\\[pointer set, len:" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "firstprivate\\(str1parr\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:str5aarr \\\[pointer set, len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:str5arr \\\[len:" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:str5 \\\[len:" 1 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:str5parr \\\[pointer set, len:" 2 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(to:str5parr \\\[pointer set, len:" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "firstprivate\\(str5parr\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:\\.strxaarr \\\[len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:strxaarr \\\[pointer set, len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:\\.strxa \\\[len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:\\.strxparr \\\[len:" 2 "gimple" } }
-! { dg-final { scan-tree-dump-times "map\\(to:strxparr \\\[pointer set, len:" 2 "gimple" } }
+! { dg-final { scan-tree-dump-times "firstprivate\\(strxparr\\)" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "map\\(to:strxparr \\\[pointer set, len:" 1 "gimple" } }
+! { dg-final { scan-tree-dump-times "firstprivate\\(strxparr\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "map\\(to:\\.strxp \\\[len:" 2 "gimple" } }
 ! { dg-final { scan-tree-dump-times "#pragma omp target num_teams\\(-2\\) thread_limit\\(0\\) defaultmap\\(alloc\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "#pragma omp target num_teams\\(-2\\) thread_limit\\(0\\) defaultmap\\(alloc:scalar\\) defaultmap\\(to:aggregate\\) defaultmap\\(tofrom:allocatable\\) defaultmap\\(firstprivate:pointer\\)" 1 "gimple" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/lastprivate-conditional-6.f90 b/gcc/testsuite/gfortran.dg/gomp/lastprivate-conditional-6.f90
new file mode 100644
index 00000000000..132003147f5
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/lastprivate-conditional-6.f90
@@ -0,0 +1,137 @@
+! Check that gfc_omp_scalar_p works with ptr_alloc_ok
+!
+! { dg-additional-options "-fdump-tree-original" }
+
+subroutine sub(p, n, d, od, vd, pd, ad, opd, oad)
+  implicit none
+  integer, value :: n
+  logical :: p(n)
+  integer :: i
+
+  integer :: d, od, vd, pd, ad, opd, oad, s, ps, as
+  optional :: od, opd, oad
+  value :: vd
+  pointer :: pd, opd, ps
+  allocatable :: ad, oad, as
+
+  !$omp simd lastprivate (conditional: d)
+  do i = 1, n; if (p(i)) d = i; end do
+
+  !$omp simd lastprivate (conditional: od)
+  do i = 1, n; if (p(i)) od = i; end do
+
+  !$omp simd lastprivate (conditional: vd)
+  do i = 1, n; if (p(i)) vd = i; end do
+
+  !$omp simd lastprivate (conditional: pd)
+  do i = 1, n; if (p(i)) pd = i; end do
+
+  !$omp simd lastprivate (conditional: ad)
+  do i = 1, n; if (p(i)) ad = i; end do
+
+  !$omp simd lastprivate (conditional: opd)
+  do i = 1, n; if (p(i)) opd = i; end do
+
+  !$omp simd lastprivate (conditional: oad)
+  do i = 1, n; if (p(i)) oad = i; end do
+
+  !$omp simd lastprivate (conditional: s)
+  do i = 1, n; if (p(i)) s = i; end do
+
+  !$omp simd lastprivate (conditional: ps)
+  do i = 1, n; if (p(i)) ps = i; end do
+
+  !$omp simd lastprivate (conditional: as)
+  do i = 1, n; if (p(i)) as = i; end do
+end
+
+subroutine my_entry(pp, nn, aa, bb)
+  implicit none
+  logical, save :: was_5 = .false.
+
+  integer, value :: nn
+  logical :: pp(nn)
+  integer :: aa, bb
+  allocatable :: aa
+  pointer :: bb
+
+  integer :: j
+
+  !$omp simd lastprivate (conditional: aa)
+  do j = 1, nn; if (pp(j)) aa = j; end do
+  
+  was_5 = .true.
+
+entry my_entry2(pp, nn, bb)
+  !$omp simd lastprivate (conditional: bb)
+  do j = 1, nn; if (pp(j)) bb = j; end do
+
+  if (was_5) bb = bb + 5
+end subroutine
+
+subroutine my_entry_opt(pp, nn, cc, dd)
+  implicit none
+  logical, save :: was_5 = .false.
+
+  integer, value :: nn
+  logical :: pp(nn)
+  integer :: cc, dd
+  optional :: cc, dd ! added
+  allocatable :: cc
+  pointer :: dd
+
+  integer :: j
+
+  !$omp simd lastprivate (conditional: cc)
+  do j = 1, nn; if (pp(j)) cc = j; end do
+  
+  was_5 = .true.
+
+entry my_entry2_opt(pp, nn, dd)
+  !$omp simd lastprivate (conditional: dd)
+  do j = 1, nn; if (pp(j)) dd = j; end do
+
+  if (was_5) dd = dd + 5
+end subroutine
+
+subroutine my_entry_opt2(pp, nn, ee, ff)
+  implicit none
+  logical, save :: was_5 = .false.
+
+  integer, value :: nn
+  logical :: pp(nn)
+  integer :: ee, ff
+  optional :: ee, ff ! no allocatable + pointer
+
+  integer :: j
+
+  !$omp simd lastprivate (conditional: ee)
+  do j = 1, nn; if (pp(j)) ee = j; end do
+  
+  was_5 = .true.
+
+entry my_entry2_opt2(pp, nn, ff)
+  !$omp simd lastprivate (conditional: ff)
+  do j = 1, nn; if (pp(j)) ff = j; end do
+
+  if (was_5) ff = ff + 5
+end subroutine
+
+
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:d\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:od\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:vd\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:pd\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:ad\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:opd\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:oad\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:s\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:ps\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(i:1\\) lastprivate\\(conditional:as\\)" "original" } }
+
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(j:1\\) lastprivate\\(conditional:aa\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(j:1\\) lastprivate\\(conditional:bb\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(j:1\\) lastprivate\\(conditional:cc\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(j:1\\) lastprivate\\(conditional:dd\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(j:1\\) lastprivate\\(conditional:ee\\)" "original" } }
+! { dg-final { scan-tree-dump "#pragma omp simd linear\\(j:1\\) lastprivate\\(conditional:ff\\)" "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/lastprivate-conditional-7.f90 b/gcc/testsuite/gfortran.dg/gomp/lastprivate-conditional-7.f90
new file mode 100644
index 00000000000..47274b592e6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/lastprivate-conditional-7.f90
@@ -0,0 +1,120 @@
+! Check that gfc_omp_scalar_p works with ptr_alloc_ok
+
+subroutine sub(p, n, d, od, vd, pd, ad, opd, oad)
+  use iso_c_binding
+  implicit none
+  integer, value :: n
+  logical :: p(n)
+  integer, target :: i
+
+  type(c_ptr) :: d, od, vd, pd, ad, opd, oad, s, ps, as
+  optional :: od, opd, oad
+  value :: vd
+  pointer :: pd, opd, ps
+  allocatable :: ad, oad, as
+
+  !$omp simd lastprivate (conditional: d)  ! { dg-error "non-scalar variable 'd' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) d = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: od)  ! { dg-error "non-scalar variable 'od' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) od = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: vd)  ! { dg-error "non-scalar variable 'vd' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) vd = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: pd)  ! { dg-error "non-scalar variable 'pd' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) pd = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: ad)  ! { dg-error "non-scalar variable 'ad' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) ad = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: opd)  ! { dg-error "non-scalar variable 'opd' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) opd = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: oad)  ! { dg-error "non-scalar variable 'oad' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) oad = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: s)  ! { dg-error "non-scalar variable 's' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) s = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: ps)  ! { dg-error "non-scalar variable 'ps' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) ps = c_loc(i); end do
+
+  !$omp simd lastprivate (conditional: as)  ! { dg-error "non-scalar variable 'as' in conditional 'lastprivate' clause" }
+  do i = 1, n; if (p(i)) as = c_loc(i); end do
+end
+
+subroutine my_entry(pp, nn, aa, bb)
+  use iso_c_binding
+  implicit none
+  logical, save :: was_5 = .false.
+
+  integer, value :: nn
+  logical :: pp(nn)
+  type(c_ptr) :: aa, bb
+  allocatable :: aa
+  pointer :: bb
+
+  integer, target :: j
+
+  !$omp simd lastprivate (conditional: aa)  ! { dg-error "non-scalar variable 'aa' in conditional 'lastprivate' clause" }
+  do j = 1, nn; if (pp(j)) aa = c_loc(j); end do
+  
+  was_5 = .true.
+
+entry my_entry2(pp, nn, bb)
+  !$omp simd lastprivate (conditional: bb)  ! { dg-error "non-scalar variable 'bb' in conditional 'lastprivate' clause" }
+  do j = 1, nn; if (pp(j)) bb = c_loc(j); end do
+
+  if (was_5) bb = c_null_ptr
+end subroutine
+
+subroutine my_entry_opt(pp, nn, cc, dd)
+  use iso_c_binding
+  implicit none
+  logical, save :: was_5 = .false.
+
+  integer, value :: nn
+  logical :: pp(nn)
+  type(c_ptr) :: cc, dd
+  optional :: cc, dd ! added
+  allocatable :: cc
+  pointer :: dd
+
+  integer, target :: j
+
+  !$omp simd lastprivate (conditional: cc)  ! { dg-error "non-scalar variable 'cc' in conditional 'lastprivate' clause" }
+  do j = 1, nn; if (pp(j)) cc = c_loc(j); end do
+  
+  was_5 = .true.
+
+entry my_entry2_opt(pp, nn, dd)
+  !$omp simd lastprivate (conditional: dd)  ! { dg-error "non-scalar variable 'dd' in conditional 'lastprivate' clause" }
+  do j = 1, nn; if (pp(j)) dd = c_loc(j); end do
+
+  if (was_5) dd = c_null_ptr
+end subroutine
+
+subroutine my_entry_opt2(pp, nn, ee, ff)
+  use iso_c_binding
+  implicit none
+  logical, save :: was_5 = .false.
+
+  integer, value :: nn
+  logical :: pp(nn)
+  type(c_ptr) :: ee, ff
+  optional :: ee, ff ! no allocatable + pointer
+
+  integer, target :: j
+
+  !$omp simd lastprivate (conditional: ee)  ! { dg-error "non-scalar variable 'ee' in conditional 'lastprivate' clause" }
+  do j = 1, nn; if (pp(j)) ee = c_loc(j); end do
+  
+  was_5 = .true.
+
+entry my_entry2_opt2(pp, nn, ff)
+  !$omp simd lastprivate (conditional: ff)  ! { dg-error "non-scalar variable 'ff' in conditional 'lastprivate' clause" }
+  do j = 1, nn; if (pp(j)) ff = c_loc(j); end do
+
+  if (was_5) ff = c_null_ptr
+end subroutine
diff --git a/libgomp/testsuite/libgomp.fortran/defaultmap-10.f90 b/libgomp/testsuite/libgomp.fortran/defaultmap-10.f90
new file mode 100644
index 00000000000..19e7b93649b
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/defaultmap-10.f90
@@ -0,0 +1,148 @@
+! { dg-skip-if "" { offload_device_shared_as } }
+! As runtime test, this only works when mapping actually happens (no self map, no host fallback)
+
+! Add some checks for pointer/allocatable/aggregate and 'defaultmap' (sorry, no scalar)
+
+! Before pointer arrays were misclassified as aggregate
+! and aggregates passed as dummy argument as pointer.
+
+module m
+  implicit none
+  type t
+    integer :: x
+  end type t
+contains
+
+subroutine s(da,dt,pa,p,a,aa)
+type(t) :: da(2), dt
+integer, pointer :: pa(:), p
+integer, allocatable :: aa(:), a
+
+if (da(1)%x /= 333 .or. da(2)%x /= 444 .or. dt%x /= 555   &
+    .or. pa(1) /= 11 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 111 &
+    .or. aa(1) /= 121 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 222) &
+  stop 1
+
+! ---------------
+
+!$omp target defaultmap(to : pointer)
+  da(1)%x = 1
+  dt%x = 1
+  pa(1) = 1
+  p = 1
+  aa(1) = 1
+  a = 1
+!$omp end target
+
+! All change but pointer
+if (da(1)%x /= 1 .or. da(2)%x /= 444 .or. dt%x /= 1   &
+    .or. pa(1) /= 11 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 111 &
+    .or. aa(1) /= 1 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 1) &
+  stop 2
+
+! ---------------
+
+!$omp target defaultmap(to : allocatable)
+  da(1)%x = 2
+  dt%x = 2
+  pa(1) = 2
+  p = 2
+  aa(1) = 2
+  a = 2
+!$omp end target
+
+! All change but allocatable
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 2 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 2 &
+    .or. aa(1) /= 1 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 1) &
+  stop 3
+
+! ---------------
+
+!$omp target defaultmap(to : aggregate)
+  da(1)%x = 3
+  dt%x = 3
+  pa(1) = 3
+  p = 3
+  aa(1) = 3
+  a = 3
+!$omp end target
+
+! All change but aggregate
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 4
+
+! ---------------
+
+!$omp target map(to: da, dt, pa, p, aa, a)
+  da(1)%x = 4
+  dt%x = 4
+  pa(1) = 4
+  p = 4
+  aa(1) = 4
+  a = 4
+!$omp end target
+
+! None change
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 5
+
+! ---------------
+
+!$omp target defaultmap(to : all)
+  da(1)%x = 5
+  dt%x = 5
+  pa(1) = 5
+  p = 5
+  aa(1) = 5
+  a = 5
+!$omp end target
+
+! None change
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 6
+
+! ---------------
+
+!$omp target defaultmap(tofrom : all)
+  da(1)%x = 6
+  dt%x = 6
+  pa(1) = 6
+  p = 6
+  aa(1) = 6
+  a = 6
+!$omp end target
+
+
+! All change
+if (da(1)%x /= 6 .or. da(2)%x /= 444 .or. dt%x /= 6   &
+    .or. pa(1) /= 6 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 6 &
+    .or. aa(1) /= 6 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 6) &
+  stop 7
+end
+end module m
+
+use m
+implicit none
+
+type(t) :: da(2), dt
+integer, pointer :: pa(:), p
+integer, allocatable :: aa(:), a
+
+allocate(pa(3),p,a)
+
+da = [t(333),t(444)]
+dt = t(555)
+pa = [11,22,33]
+p = 111
+aa = [121, 222, 323, 424]
+a = 222
+
+call s(da,dt,pa,p,a,aa)
+end
diff --git a/libgomp/testsuite/libgomp.fortran/defaultmap-11.f90 b/libgomp/testsuite/libgomp.fortran/defaultmap-11.f90
new file mode 100644
index 00000000000..6629e6e8b96
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/defaultmap-11.f90
@@ -0,0 +1,149 @@
+! { dg-skip-if "" { offload_device_shared_as } }
+! As runtime test, this only works when mapping actually happens (no self map, no host fallback)
+
+! Add some checks for pointer/allocatable/aggregate and 'defaultmap' (sorry, no scalar)
+
+! Before pointer arrays were misclassified as aggregate
+! and aggregates passed as dummy argument as pointer.
+
+module m
+  implicit none
+  type t
+    integer :: x
+  end type t
+contains
+
+subroutine s(da,dt,pa,p,a,aa)
+type(t) :: da(2), dt
+integer, pointer :: pa(:), p
+integer, allocatable :: aa(:), a
+optional :: da,dt,pa,p,a,aa
+
+if (da(1)%x /= 333 .or. da(2)%x /= 444 .or. dt%x /= 555   &
+    .or. pa(1) /= 11 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 111 &
+    .or. aa(1) /= 121 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 222) &
+  stop 1
+
+! ---------------
+
+!$omp target defaultmap(to : pointer)
+  da(1)%x = 1
+  dt%x = 1
+  pa(1) = 1
+  p = 1
+  aa(1) = 1
+  a = 1
+!$omp end target
+
+! All change but pointer
+if (da(1)%x /= 1 .or. da(2)%x /= 444 .or. dt%x /= 1   &
+    .or. pa(1) /= 11 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 111 &
+    .or. aa(1) /= 1 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 1) &
+  stop 2
+
+! ---------------
+
+!$omp target defaultmap(to : allocatable)
+  da(1)%x = 2
+  dt%x = 2
+  pa(1) = 2
+  p = 2
+  aa(1) = 2
+  a = 2
+!$omp end target
+
+! All change but allocatable
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 2 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 2 &
+    .or. aa(1) /= 1 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 1) &
+  stop 3
+
+! ---------------
+
+!$omp target defaultmap(to : aggregate)
+  da(1)%x = 3
+  dt%x = 3
+  pa(1) = 3
+  p = 3
+  aa(1) = 3
+  a = 3
+!$omp end target
+
+! All change but aggregate
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 4
+
+! ---------------
+
+!$omp target map(to: da, dt, pa, p, aa, a)
+  da(1)%x = 4
+  dt%x = 4
+  pa(1) = 4
+  p = 4
+  aa(1) = 4
+  a = 4
+!$omp end target
+
+! None change
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 5
+
+! ---------------
+
+!$omp target defaultmap(to : all)
+  da(1)%x = 5
+  dt%x = 5
+  pa(1) = 5
+  p = 5
+  aa(1) = 5
+  a = 5
+!$omp end target
+
+! None change
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 6
+
+! ---------------
+
+!$omp target defaultmap(tofrom : all)
+  da(1)%x = 6
+  dt%x = 6
+  pa(1) = 6
+  p = 6
+  aa(1) = 6
+  a = 6
+!$omp end target
+
+
+! All change
+if (da(1)%x /= 6 .or. da(2)%x /= 444 .or. dt%x /= 6   &
+    .or. pa(1) /= 6 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 6 &
+    .or. aa(1) /= 6 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 6) &
+  stop 7
+end
+end module m
+
+use m
+implicit none
+
+type(t) :: da(2), dt
+integer, pointer :: pa(:), p
+integer, allocatable :: aa(:), a
+
+allocate(pa(3),p,a)
+
+da = [t(333),t(444)]
+dt = t(555)
+pa = [11,22,33]
+p = 111
+aa = [121, 222, 323, 424]
+a = 222
+
+call s(da,dt,pa,p,a,aa)
+end
diff --git a/libgomp/testsuite/libgomp.fortran/defaultmap-8.f90 b/libgomp/testsuite/libgomp.fortran/defaultmap-8.f90
index 54f4b2e9ddc..5116cb24b74 100644
--- a/libgomp/testsuite/libgomp.fortran/defaultmap-8.f90
+++ b/libgomp/testsuite/libgomp.fortran/defaultmap-8.f90
@@ -276,10 +276,9 @@ end
 
 end
 
-! FIXME/xfail: Optional scalars wrongly classified, PR fortran/100991
-! { dg-final { scan-tree-dump-times "firstprivate\\(ii1\\)" 3 "gimple" { xfail *-*-* } } }
-! { dg-final { scan-tree-dump-not "firstprivate\\(et1\\)" "gimple" { xfail *-*-* } } }
-! { dg-final { scan-tree-dump-not "firstprivate\\(ei1\\)" "gimple" { xfail *-*-* } } }
+! { dg-final { scan-tree-dump-times "firstprivate\\(ii1\\)" 3 "gimple" } }
+! { dg-final { scan-tree-dump-not "firstprivate\\(et1\\)" "gimple" } }
+! { dg-final { scan-tree-dump-not "firstprivate\\(ei1\\)" "gimple" } }
 
 ! { dg-final { scan-tree-dump-times "firstprivate\\(ea1\\)" 1 "gimple" } }
 ! { dg-final { scan-tree-dump-times "firstprivate\\(ea2\\)" 1 "gimple" } }
diff --git a/libgomp/testsuite/libgomp.fortran/defaultmap-9.f90 b/libgomp/testsuite/libgomp.fortran/defaultmap-9.f90
new file mode 100644
index 00000000000..deeb92aed4f
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/defaultmap-9.f90
@@ -0,0 +1,143 @@
+! { dg-skip-if "" { offload_device_shared_as } }
+! As runtime test, this only works when mapping actually happens (no self map, no host fallback)
+
+! Add some checks for pointer/allocatable/aggregate and 'defaultmap' (sorry, no scalar)
+
+! Before pointer arrays were misclassified as aggregate
+! and aggregates passed as dummy argument as pointer.
+
+module m
+  implicit none
+  type t
+    integer :: x
+  end type t
+contains
+
+subroutine s
+type(t) :: da(2), dt
+integer, pointer :: pa(:), p
+integer, allocatable :: aa(:), a
+
+allocate(pa(3),p,a)
+
+da = [t(333),t(444)]
+dt = t(555)
+pa = [11,22,33]
+p = 111
+aa = [121, 222, 323, 424]
+a = 222
+
+if (da(1)%x /= 333 .or. da(2)%x /= 444 .or. dt%x /= 555   &
+    .or. pa(1) /= 11 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 111 &
+    .or. aa(1) /= 121 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 222) &
+  stop 1
+
+! ---------------
+
+!$omp target defaultmap(to : pointer)
+  da(1)%x = 1
+  dt%x = 1
+  pa(1) = 1
+  p = 1
+  aa(1) = 1
+  a = 1
+!$omp end target
+
+! All change but pointer
+if (da(1)%x /= 1 .or. da(2)%x /= 444 .or. dt%x /= 1   &
+    .or. pa(1) /= 11 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 111 &
+    .or. aa(1) /= 1 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 1) &
+  stop 2
+
+! ---------------
+
+!$omp target defaultmap(to : allocatable)
+  da(1)%x = 2
+  dt%x = 2
+  pa(1) = 2
+  p = 2
+  aa(1) = 2
+  a = 2
+!$omp end target
+
+! All change but allocatable
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 2 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 2 &
+    .or. aa(1) /= 1 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 1) &
+  stop 3
+
+! ---------------
+
+!$omp target defaultmap(to : aggregate)
+  da(1)%x = 3
+  dt%x = 3
+  pa(1) = 3
+  p = 3
+  aa(1) = 3
+  a = 3
+!$omp end target
+
+! All change but aggregate
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 4
+
+! ---------------
+
+!$omp target map(to: da, dt, pa, p, aa, a)
+  da(1)%x = 4
+  dt%x = 4
+  pa(1) = 4
+  p = 4
+  aa(1) = 4
+  a = 4
+!$omp end target
+
+! None change
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 5
+
+! ---------------
+
+!$omp target defaultmap(to : all)
+  da(1)%x = 5
+  dt%x = 5
+  pa(1) = 5
+  p = 5
+  aa(1) = 5
+  a = 5
+!$omp end target
+
+! None change
+if (da(1)%x /= 2 .or. da(2)%x /= 444 .or. dt%x /= 2   &
+    .or. pa(1) /= 3 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 3 &
+    .or. aa(1) /= 3 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 3) &
+  stop 6
+
+! ---------------
+
+!$omp target defaultmap(tofrom : all)
+  da(1)%x = 6
+  dt%x = 6
+  pa(1) = 6
+  p = 6
+  aa(1) = 6
+  a = 6
+!$omp end target
+
+
+! All change
+if (da(1)%x /= 6 .or. da(2)%x /= 444 .or. dt%x /= 6   &
+    .or. pa(1) /= 6 .or. pa(2) /= 22 .or. pa(3) /= 33  .or. p /= 6 &
+    .or. aa(1) /= 6 .or. aa(2) /= 222 .or. aa(3) /= 323 .or. aa(4) /= 424 .or. a /= 6) &
+  stop 7
+end
+end module m
+
+use m
+implicit none
+call s
+end
diff --git a/libgomp/testsuite/libgomp.fortran/optional-map.f90 b/libgomp/testsuite/libgomp.fortran/optional-map.f90
index b06efcc90d1..9c31bc07af7 100644
--- a/libgomp/testsuite/libgomp.fortran/optional-map.f90
+++ b/libgomp/testsuite/libgomp.fortran/optional-map.f90
@@ -79,7 +79,7 @@ subroutine sub2(ii, ival, iarr, iptr, iparr)
   value :: ival
   integer :: err(1) ! otherwise, implied defaultmap is firstprivate
   err(1) = 42
-  !$omp target  ! automatic mapping with implied defaultmap(tofrom) 
+  !$omp target defaultmap(tofrom:scalar) ! automatic mapping with implied defaultmap(tofrom) 
   if (present(ii)) then
     ii = iptr + ival
     iarr = iparr

Reply via email to