]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran "declare create"/allocate support for OpenACC
authorJulian Brown <julian@codesourcery.com>
Tue, 26 Feb 2019 23:48:00 +0000 (15:48 -0800)
committerKwok Cheung Yeung <kcy@codesourcery.com>
Thu, 22 Apr 2021 17:14:20 +0000 (10:14 -0700)
2018-10-04  Cesar Philippidis  <cesar@codesourcery.com>
            Julian Brown  <julian@codesourcery.com>

gcc/
* omp-low.c (scan_sharing_clauses): Update handling of OpenACC declare
create, declare copyin and declare deviceptr to have local lifetimes.
(convert_to_firstprivate_int): Handle pointer types.
(convert_from_firstprivate_int): Likewise.  Create local storage for
the values being pointed to.  Add new orig_type argument.
(lower_omp_target): Handle GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE}.
Add orig_type argument to convert_from_firstprivate_int call.
Allow pointer types with GOMP_MAP_FIRSTPRIVATE_INT.  Don't privatize
firstprivate VLAs.
* tree-pretty-print.c (dump_omp_clause): Handle
GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE}.

gcc/fortran/
* gfortran.h (enum gfc_omp_map_op): Add OMP_MAP_DECLARE_ALLOCATE,
OMP_MAP_DECLARE_DEALLOCATE.
(gfc_omp_clauses): Add update_allocatable.
* trans-array.c (gfc_array_allocate): Call
gfc_trans_oacc_declare_allocate for decls that have oacc_declare_create
attribute set.
* trans-decl.c (find_module_oacc_declare_clauses): Relax
oacc_declare_create to OMP_MAP_ALLOC, and oacc_declare_copyin to
OMP_MAP_TO, in order to match OpenACC 2.5 semantics.
* trans-openmp.c (gfc_trans_omp_clauses): Use GOMP_MAP_ALWAYS_POINTER
(for update directive) or GOMP_MAP_FIRSTPRIVATE_POINTER (otherwise) for
allocatable scalar decls.  Handle OMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE}
clauses.
(gfc_trans_oacc_executable_directive): Use GOMP_MAP_ALWAYS_POINTER
for allocatable scalar data clauses inside acc update directives.
(gfc_trans_oacc_declare_allocate): New function.
* trans-stmt.c (gfc_trans_allocate): Call
gfc_trans_oacc_declare_allocate for decls with oacc_declare_create
attribute set.
(gfc_trans_deallocate): Likewise.
* trans.h (gfc_trans_oacc_declare_allocate): Declare.

gcc/testsuite/
* gfortran.dg/goacc/declare-allocatable-1.f90: New test.

include/
* gomp-constants.h (enum gomp_map_kind): Define
GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE} and GOMP_MAP_FLAG_SPECIAL_4.

libgomp/
* oacc-mem.c (gomp_acc_declare_allocate): New function.
* oacc-parallel.c (GOACC_enter_exit_data): Handle
GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE}.
* testsuite/libgomp.oacc-fortran/allocatable-scalar.f90: New test.
* testsuite/libgomp.oacc-fortran/declare-allocatable-1.f90: New test.
* testsuite/libgomp.oacc-fortran/declare-allocatable-2.f90: New test.
* testsuite/libgomp.oacc-fortran/declare-allocatable-3.f90: New test.
* testsuite/libgomp.oacc-fortran/declare-allocatable-4.f90: New test.

2020-02-19  Julian Brown  <julian@codesourcery.com>

gcc/fortran/
* trans-openmp.c (gfc_omp_check_optional_argument): Handle non-decl
case.

gcc/
* gimplify.c (gimplify_scan_omp_clauses): Handle
GOMP_MAP_DECLARE_ALLOCATE and GOMP_MAP_DECLARE_DEALLOCATE.

libgomp/
* libgomp.h (gomp_acc_declare_allocate): Remove prototype.
* oacc-mem.c (gomp_acc_declare_allocate): Make static.  Add POINTER
argument. Use acc_delete instead of acc_free.  Handle scalar
mappings.
(find_group_last): Handle GOMP_MAP_DECLARE_ALLOCATE and
GOMP_MAP_DECLARE_DEALLOCATE groupings.
(goacc_enter_data_internal): Fix kind check for
GOMP_MAP_DECLARE_ALLOCATE. Pass new pointer argument to
gomp_acc_declare_allocate.
(goacc_exit_data_internal): Unlock device mutex around
gomp_acc_declare_allocate call. Pass new pointer argument. Handle
group pointer mapping for deallocate.

2021-04-07  Kwok Cheung Yeung  <kcy@codesourcery.com>

libgomp/
* oacc-mem.c (goacc_enter_data_internal): Unlock mutex before calling
gomp_acc_declare_allocate and relock it afterwards.

23 files changed:
gcc/ChangeLog.omp
gcc/fortran/ChangeLog.omp
gcc/fortran/gfortran.h
gcc/fortran/trans-array.c
gcc/fortran/trans-decl.c
gcc/fortran/trans-openmp.c
gcc/fortran/trans-stmt.c
gcc/fortran/trans.h
gcc/gimplify.c
gcc/omp-low.c
gcc/testsuite/ChangeLog.omp
gcc/testsuite/gfortran.dg/goacc/declare-allocatable-1.f90 [new file with mode: 0644]
gcc/tree-pretty-print.c
include/ChangeLog.omp
include/gomp-constants.h
libgomp/ChangeLog.omp
libgomp/libgomp.h
libgomp/oacc-mem.c
libgomp/testsuite/libgomp.oacc-fortran/allocatable-scalar.f90 [new file with mode: 0644]
libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-1.f90 [new file with mode: 0644]
libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-2.f90 [new file with mode: 0644]
libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-3.f90 [new file with mode: 0644]
libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-4.f90 [new file with mode: 0644]

index c374ebe92ae691a2c798a7cabd261e5e979d9405..b9b3545d2d623f449506fe9815d61f0cab788950 100644 (file)
@@ -1,3 +1,23 @@
+2020-02-19  Julian Brown  <julian@codesourcery.com>
+
+       * gimplify.c (gimplify_scan_omp_clauses): Handle
+       GOMP_MAP_DECLARE_ALLOCATE and GOMP_MAP_DECLARE_DEALLOCATE.
+
+2018-10-04  Cesar Philippidis  <cesar@codesourcery.com>
+            Julian Brown  <julian@codesourcery.com>
+
+       * omp-low.c (scan_sharing_clauses): Update handling of OpenACC declare
+       create, declare copyin and declare deviceptr to have local lifetimes.
+       (convert_to_firstprivate_int): Handle pointer types.
+       (convert_from_firstprivate_int): Likewise.  Create local storage for
+       the values being pointed to.  Add new orig_type argument.
+       (lower_omp_target): Handle GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE}.
+       Add orig_type argument to convert_from_firstprivate_int call.
+       Allow pointer types with GOMP_MAP_FIRSTPRIVATE_INT.  Don't privatize
+       firstprivate VLAs.
+       * tree-pretty-print.c (dump_omp_clause): Handle
+       GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE}.
+
 2019-10-16  Julian Brown  <julian@codesourcery.com>
            Tobias Burnus  <tobias@codesourcery.com>
 
index d682219778d475f67463c270c4faf1cf186a25c4..4a64c0955a988c8104757688a71b25acf4e68bb3 100644 (file)
@@ -1,3 +1,33 @@
+2020-02-19  Julian Brown  <julian@codesourcery.com>
+
+       * trans-openmp.c (gfc_omp_check_optional_argument): Handle non-decl
+       case.
+
+2018-10-04  Cesar Philippidis  <cesar@codesourcery.com>
+           Julian Brown  <julian@codesourcery.com>
+
+       * gfortran.h (enum gfc_omp_map_op): Add OMP_MAP_DECLARE_ALLOCATE,
+       OMP_MAP_DECLARE_DEALLOCATE.
+       (gfc_omp_clauses): Add update_allocatable.
+       * trans-array.c (gfc_array_allocate): Call
+       gfc_trans_oacc_declare_allocate for decls that have oacc_declare_create
+       attribute set.
+       * trans-decl.c (find_module_oacc_declare_clauses): Relax oacc_declare_create to
+       OMP_MAP_ALLOC, and oacc_declare_copyin to OMP_MAP_TO, in order to
+       match OpenACC 2.5 semantics.
+       * trans-openmp.c (gfc_trans_omp_clauses): Use GOMP_MAP_ALWAYS_POINTER
+       (for update directive) or GOMP_MAP_FIRSTPRIVATE_POINTER (otherwise) for
+       allocatable scalar decls.  Handle OMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE}
+       clauses.
+       (gfc_trans_oacc_executable_directive): Use GOMP_MAP_ALWAYS_POINTER
+       for allocatable scalar data clauses inside acc update directives.
+       (gfc_trans_oacc_declare_allocate): New function.
+       * trans-stmt.c (gfc_trans_allocate): Call
+       gfc_trans_oacc_declare_allocate for decls with oacc_declare_create
+       attribute set.
+       (gfc_trans_deallocate): Likewise.
+       * trans.h (gfc_trans_oacc_declare_allocate): Declare.
+
 2019-07-10  Julian Brown  <julian@codesourcery.com>
 
        * trans-openmp.c (gfc_omp_finish_clause): Change clauses mapping
index ce1252f98b3e5983a40454b2f5904cf2916b59b3..3017e35160c66cb3dca3cb8c22f24ab1c017f127 100644 (file)
@@ -1227,7 +1227,9 @@ enum gfc_omp_map_op
   OMP_MAP_RELEASE,
   OMP_MAP_ALWAYS_TO,
   OMP_MAP_ALWAYS_FROM,
-  OMP_MAP_ALWAYS_TOFROM
+  OMP_MAP_ALWAYS_TOFROM,
+  OMP_MAP_DECLARE_ALLOCATE,
+  OMP_MAP_DECLARE_DEALLOCATE
 };
 
 enum gfc_omp_linear_op
@@ -1436,7 +1438,7 @@ typedef struct gfc_omp_clauses
   gfc_expr_list *tile_list;
   unsigned async:1, gang:1, worker:1, vector:1, seq:1, independent:1;
   unsigned par_auto:1, gang_static:1;
-  unsigned if_present:1, finalize:1, nohost:1;
+  unsigned if_present:1, finalize:1, nohost:1, update_allocatable:1;
   locus loc;
 
 }
index e99980fd2233f8ac776bac0122f116556b5c207c..d3282d96e596bbd3b422cf7dbfd7b3624592170d 100644 (file)
@@ -5866,6 +5866,7 @@ gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
   gfc_ref *ref, *prev_ref = NULL, *coref;
   bool allocatable, coarray, dimension, alloc_w_e3_arr_spec = false,
       non_ulimate_coarray_ptr_comp;
+  bool oacc_declare = false;
 
   ref = expr->ref;
 
@@ -5880,6 +5881,7 @@ gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
       allocatable = expr->symtree->n.sym->attr.allocatable;
       dimension = expr->symtree->n.sym->attr.dimension;
       non_ulimate_coarray_ptr_comp = false;
+      oacc_declare = expr->symtree->n.sym->attr.oacc_declare_create;
     }
   else
     {
@@ -6064,6 +6066,8 @@ gfc_array_allocate (gfc_se * se, gfc_expr * expr, tree status, tree errmsg,
       gfc_conv_descriptor_offset_set (&set_descriptor_block, se->expr, offset);
       tmp = fold_convert (gfc_array_index_type, element_size);
       gfc_conv_descriptor_span_set (&set_descriptor_block, se->expr, tmp);
+      if (oacc_declare)
+        gfc_trans_oacc_declare_allocate (&set_descriptor_block, expr, true);
     }
 
   set_descriptor = gfc_finish_block (&set_descriptor_block);
index 952166334775e6aea258346f31ea8d7b2584e27d..51fe2b144d8d14cbdf4657b53529d8774112e4bd 100644 (file)
@@ -6640,10 +6640,10 @@ find_module_oacc_declare_clauses (gfc_symbol *sym)
       gfc_omp_map_op map_op;
 
       if (sym->attr.oacc_declare_create)
-       map_op = OMP_MAP_FORCE_ALLOC;
+       map_op = OMP_MAP_ALLOC;
 
       if (sym->attr.oacc_declare_copyin)
-       map_op = OMP_MAP_FORCE_TO;
+       map_op = OMP_MAP_TO;
 
       if (sym->attr.oacc_declare_deviceptr)
        map_op = OMP_MAP_FORCE_DEVICEPTR;
index f879f1770893f3abda8d4f94ec32524c642cc9f3..66e47fdbc7ef0cde0202dd5245ce83ad569b7a25 100644 (file)
@@ -93,6 +93,10 @@ gfc_omp_check_optional_argument (tree decl, bool for_present_check)
   if (!for_present_check)
     return gfc_omp_is_optional_argument (decl) ? decl : NULL_TREE;
 
+  if (!DECL_P (decl))
+    return fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
+                           decl, null_pointer_node);
+
   if (!DECL_LANG_SPECIFIC (decl))
     return NULL_TREE;
 
@@ -2682,6 +2686,12 @@ gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
                case OMP_MAP_FORCE_DEVICEPTR:
                  OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_FORCE_DEVICEPTR);
                  break;
+               case OMP_MAP_DECLARE_ALLOCATE:
+                 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_DECLARE_ALLOCATE);
+                 break;
+               case OMP_MAP_DECLARE_DEALLOCATE:
+                 OMP_CLAUSE_SET_MAP_KIND (node, GOMP_MAP_DECLARE_DEALLOCATE);
+                 break;
                default:
                  gcc_unreachable ();
                }
@@ -2764,6 +2774,15 @@ gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
                               || n->sym->ts.type == BT_DERIVED))
                    {
                      tree orig_decl = decl;
+                     enum gomp_map_kind gmk = GOMP_MAP_POINTER;
+                     if (GFC_DECL_GET_SCALAR_ALLOCATABLE (decl)
+                         && n->sym->attr.oacc_declare_create)
+                       {
+                         if (clauses->update_allocatable)
+                           gmk = GOMP_MAP_ALWAYS_POINTER;
+                         else
+                           gmk = GOMP_MAP_FIRSTPRIVATE_POINTER;
+                       }
 
                      /* For nonallocatable, nonpointer arrays, a temporary
                         variable is generated, but this one is only defined if
@@ -2790,7 +2809,7 @@ gfc_trans_omp_clauses (stmtblock_t *block, gfc_omp_clauses *clauses,
                        }
                      node4 = build_omp_clause (input_location,
                                                OMP_CLAUSE_MAP);
-                     OMP_CLAUSE_SET_MAP_KIND (node4, GOMP_MAP_POINTER);
+                     OMP_CLAUSE_SET_MAP_KIND (node4, gmk);
                      OMP_CLAUSE_DECL (node4) = decl;
                      OMP_CLAUSE_SIZE (node4) = size_int (0);
                      decl = build_fold_indirect_ref (decl);
@@ -4017,12 +4036,14 @@ gfc_trans_oacc_executable_directive (gfc_code *code)
 {
   stmtblock_t block;
   tree stmt, oacc_clauses;
+  gfc_omp_clauses *clauses = code->ext.omp_clauses;
   enum tree_code construct_code;
 
   switch (code->op)
     {
       case EXEC_OACC_UPDATE:
        construct_code = OACC_UPDATE;
+       clauses->update_allocatable = 1;
        break;
       case EXEC_OACC_ENTER_DATA:
        construct_code = OACC_ENTER_DATA;
@@ -4038,8 +4059,8 @@ gfc_trans_oacc_executable_directive (gfc_code *code)
     }
 
   gfc_start_block (&block);
-  oacc_clauses = gfc_trans_omp_clauses (&block, code->ext.omp_clauses,
-                                       code->loc, false, true);
+  oacc_clauses = gfc_trans_omp_clauses (&block, clauses, code->loc,
+                                       false, true);
   stmt = build1_loc (input_location, construct_code, void_type_node, 
                     oacc_clauses);
   gfc_add_expr_to_block (&block, stmt);
@@ -6156,6 +6177,41 @@ gfc_trans_oacc_declare (gfc_code *code)
   return gfc_finish_block (&block);
 }
 
+/* Create an OpenACC enter or exit data construct for an OpenACC declared
+   variable that has been allocated or deallocated.  */
+
+tree
+gfc_trans_oacc_declare_allocate (stmtblock_t *block, gfc_expr *expr,
+                                bool allocate)
+{
+  gfc_omp_clauses *clauses = gfc_get_omp_clauses ();
+  gfc_omp_namelist *p = gfc_get_omp_namelist ();
+  tree oacc_clauses, stmt;
+  enum tree_code construct_code;
+
+  p->sym = expr->symtree->n.sym;
+  p->where = expr->where;
+
+  if (allocate)
+    {
+      p->u.map_op = OMP_MAP_DECLARE_ALLOCATE;
+      construct_code = OACC_ENTER_DATA;
+    }
+  else
+    {
+      p->u.map_op = OMP_MAP_DECLARE_DEALLOCATE;
+      construct_code = OACC_EXIT_DATA;
+    }
+  clauses->lists[OMP_LIST_MAP] = p;
+
+  oacc_clauses = gfc_trans_omp_clauses (block, clauses, expr->where);
+  stmt = build1_loc (input_location, construct_code, void_type_node,
+                    oacc_clauses);
+  gfc_add_expr_to_block (block, stmt);
+
+  return stmt;
+}
+
 tree
 gfc_trans_oacc_directive (gfc_code *code)
 {
index 7cbdef7a30453ce8f1d81ea9267ae99d74677a78..fa14e0d68fa61a631a8c23c2e9220730b530b2f3 100644 (file)
@@ -6850,6 +6850,10 @@ gfc_trans_allocate (gfc_code * code)
                                      label_finish, expr, 0);
          else
            gfc_allocate_using_malloc (&se.pre, se.expr, memsz, stat);
+
+         /* Allocate memory for OpenACC declared variables.  */
+         if (expr->symtree->n.sym->attr.oacc_declare_create)
+           gfc_trans_oacc_declare_allocate (&se.pre, expr, true);
        }
       else
        {
@@ -7323,6 +7327,10 @@ gfc_trans_deallocate (gfc_code *code)
 
          if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se.expr)))
            {
+             if (!is_coarray
+                 && expr->symtree->n.sym->attr.oacc_declare_create)
+               gfc_trans_oacc_declare_allocate (&se.pre, expr, false);
+
              gfc_coarray_deregtype caf_dtype;
 
              if (is_coarray)
@@ -7376,6 +7384,10 @@ gfc_trans_deallocate (gfc_code *code)
        }
       else
        {
+         /* Deallocate memory for OpenACC declared variables.  */
+         if (expr->symtree->n.sym->attr.oacc_declare_create)
+           gfc_trans_oacc_declare_allocate (&se.pre, expr, false);
+
          tmp = gfc_deallocate_scalar_with_status (se.expr, pstat, label_finish,
                                                   false, al->expr,
                                                   al->expr->ts, is_coarray);
index 8c6f82ff1b121d51b04b1a0e26fb2c708562838c..91ddcc604c59eaf3280337a385f18557ad72fd98 100644 (file)
@@ -824,6 +824,7 @@ bool gfc_omp_private_debug_clause (tree, bool);
 bool gfc_omp_private_outer_ref (tree);
 struct gimplify_omp_ctx;
 void gfc_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *, tree);
+tree gfc_trans_oacc_declare_allocate (stmtblock_t *, gfc_expr *, bool);
 
 /* In trans-intrinsic.c.  */
 void gfc_conv_intrinsic_mvbits (gfc_se *, gfc_actual_arglist *,
index f2c55cca5fb5ba51eec60ce39d1b9e42c6c5bd8b..0ef1583d98eb7f6171972f10230cc26304ef5cd6 100644 (file)
@@ -9043,9 +9043,15 @@ gimplify_scan_omp_clauses (tree *list_p, gimple_seq *pre_p,
            case OACC_ENTER_DATA:
            case OACC_EXIT_DATA:
            case OACC_HOST_DATA:
-             if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
-                 || (OMP_CLAUSE_MAP_KIND (c)
-                     == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
+             if ((OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER
+                  || (OMP_CLAUSE_MAP_KIND (c)
+                      == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
+                 && !(prev_list_p
+                      && OMP_CLAUSE_CODE (*prev_list_p) == OMP_CLAUSE_MAP
+                      && ((OMP_CLAUSE_MAP_KIND (*prev_list_p)
+                           == GOMP_MAP_DECLARE_ALLOCATE)
+                          || (OMP_CLAUSE_MAP_KIND (*prev_list_p)
+                              == GOMP_MAP_DECLARE_DEALLOCATE))))
                /* For target {,enter ,exit }data only the array slice is
                   mapped, but not the pointer to it.  */
                remove = true;
index 206f4328e02a496da1a04a5a2aa9302a0a560e67..1e509b36d62e0110c0fab88b4d449a9dc641573d 100644 (file)
@@ -1581,7 +1581,8 @@ scan_sharing_clauses (tree clauses, omp_context *ctx,
              && is_global_var (maybe_lookup_decl_in_outer_ctx (decl, ctx))
              && varpool_node::get_create (decl)->offloadable
              && !lookup_attribute ("omp declare target link",
-                                   DECL_ATTRIBUTES (decl)))
+                                   DECL_ATTRIBUTES (decl))
+             && !is_gimple_omp_oacc (ctx->stmt))
            break;
          if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
              && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER)
@@ -12230,7 +12231,7 @@ convert_to_firstprivate_int (tree var, gimple_seq *gs)
 {
   tree type = TREE_TYPE (var), new_type = NULL_TREE;
 
-  if (omp_is_reference (var))
+  if (omp_is_reference (var) || POINTER_TYPE_P (type))
     {
       type = TREE_TYPE (type);
       tree tmp = create_tmp_var (type);
@@ -12255,7 +12256,8 @@ convert_to_firstprivate_int (tree var, gimple_seq *gs)
 /* Like convert_to_firstprivate_int, but restore the original type.  */
 
 static tree
-convert_from_firstprivate_int (tree var, bool is_ref, gimple_seq *gs)
+convert_from_firstprivate_int (tree var, tree orig_type, bool is_ref,
+                              gimple_seq *gs)
 {
   tree type = TREE_TYPE (var);
   tree new_type = NULL_TREE;
@@ -12264,7 +12266,31 @@ convert_from_firstprivate_int (tree var, bool is_ref, gimple_seq *gs)
   gcc_assert (TREE_CODE (var) == MEM_REF);
   var = TREE_OPERAND (var, 0);
 
-  if (INTEGRAL_TYPE_P (var) || POINTER_TYPE_P (type))
+  if (is_ref || POINTER_TYPE_P (orig_type))
+    {
+      tree_code code = NOP_EXPR;
+
+      if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == COMPLEX_TYPE)
+       code = VIEW_CONVERT_EXPR;
+
+      if (code == VIEW_CONVERT_EXPR
+         && TYPE_SIZE (type) != TYPE_SIZE (orig_type))
+       {
+         tree ptype = build_pointer_type (type);
+         var = fold_build1 (code, ptype, build_fold_addr_expr (var));
+         var = build_simple_mem_ref (var);
+       }
+      else
+       var = fold_build1 (code, type, var);
+
+      tree inst = create_tmp_var (type);
+      gimplify_assign (inst, var, gs);
+      var = build_fold_addr_expr (inst);
+
+      return var;
+    }
+
+  if (INTEGRAL_TYPE_P (var))
     return fold_convert (type, var);
 
   gcc_assert (tree_to_uhwi (TYPE_SIZE (type)) <= POINTER_SIZE);
@@ -12275,16 +12301,8 @@ convert_from_firstprivate_int (tree var, bool is_ref, gimple_seq *gs)
   tmp = create_tmp_var (new_type);
   var = fold_convert (new_type, var);
   gimplify_assign (tmp, var, gs);
-  var = fold_build1 (VIEW_CONVERT_EXPR, type, tmp);
-
-  if (is_ref)
-    {
-      tmp = create_tmp_var (build_pointer_type (type));
-      gimplify_assign (tmp, build_fold_addr_expr (var), gs);
-      var = tmp;
-    }
 
-  return var;
+  return fold_build1 (VIEW_CONVERT_EXPR, type, tmp);
 }
 
 /* Lower the GIMPLE_OMP_TARGET in the current statement
@@ -12402,6 +12420,8 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
          case GOMP_MAP_NONCONTIG_ARRAY_ALLOC:
          case GOMP_MAP_NONCONTIG_ARRAY_FORCE_ALLOC:
          case GOMP_MAP_NONCONTIG_ARRAY_FORCE_PRESENT:
+         case GOMP_MAP_DECLARE_ALLOCATE:
+         case GOMP_MAP_DECLARE_DEALLOCATE:
          case GOMP_MAP_LINK:
          case GOMP_MAP_FORCE_DETACH:
            gcc_assert (is_gimple_omp_oacc (stmt));
@@ -12494,7 +12514,8 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
                && !maybe_lookup_field_in_outer_ctx (var, ctx))
              {
                gcc_assert (is_gimple_omp_oacc (ctx->stmt));
-               x = convert_from_firstprivate_int (x, omp_is_reference (var),
+               x = convert_from_firstprivate_int (x, TREE_TYPE (new_var),
+                                                  omp_is_reference (var),
                                                   &fplist);
                gimplify_assign (new_var, x, &fplist);
                map_cnt++;
@@ -12511,13 +12532,20 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
                gcc_assert (is_gimple_omp_oacc (ctx->stmt));
                if (omp_is_reference (new_var)
                    && (TREE_CODE (var_type) != POINTER_TYPE
-                       || DECL_BY_REFERENCE (var)))
+                       || DECL_BY_REFERENCE (var))
+                   /* Accelerators may not have alloca, so it's not
+                      possible to privatize local storage for those
+                      objects.  */
+                   && TREE_CONSTANT (TYPE_SIZE (TREE_TYPE (var_type))))
                  {
                    /* Create a local object to hold the instance
                       value.  */
                    const char *id = IDENTIFIER_POINTER (DECL_NAME (new_var));
                    tree inst = create_tmp_var (TREE_TYPE (var_type), id);
-                   gimplify_assign (inst, fold_indirect_ref (x), &fplist);
+                   if (TREE_CODE (var_type) == POINTER_TYPE)
+                     gimplify_assign (inst, x, &fplist);
+                   else
+                     gimplify_assign (inst, fold_indirect_ref (x), &fplist);
                    x = build_fold_addr_expr (inst);
                  }
                gimplify_assign (new_var, x, &fplist);
@@ -12807,9 +12835,10 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
                else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
                  {
                    gcc_checking_assert (is_gimple_omp_oacc (ctx->stmt));
+                   tree new_var = lookup_decl (var, ctx);
                    tree type = TREE_TYPE (var);
                    tree inner_type
-                     = omp_is_reference (var) ? TREE_TYPE (type) : type;
+                     = omp_is_reference (new_var) ? TREE_TYPE (type) : type;
                    if ((FLOAT_TYPE_P (inner_type)
                         || ANY_INTEGRAL_TYPE_P (inner_type))
                        && tree_to_uhwi (TYPE_SIZE (inner_type)) <= POINTER_SIZE
index b673fb84422e7e644c625ee454cb504f173bda3b..88f0e426c553cff7bfad987207e9c3ebf60d29b8 100644 (file)
@@ -1,3 +1,8 @@
+2018-10-04  Cesar Philippidis  <cesar@codesourcery.com>
+            Julian Brown  <julian@codesourcery.com>
+
+       * gfortran.dg/goacc/declare-allocatable-1.f90: New test.
+
 2019-09-19  Tobias Burnus  <tobias@codesourcery.com>
 
        * gfortran.dg/goacc/classify-kernels-unparallelized.f95: Add
diff --git a/gcc/testsuite/gfortran.dg/goacc/declare-allocatable-1.f90 b/gcc/testsuite/gfortran.dg/goacc/declare-allocatable-1.f90
new file mode 100644 (file)
index 0000000..5349e0d
--- /dev/null
@@ -0,0 +1,25 @@
+! Verify that OpenACC declared allocatable arrays have implicit
+! OpenACC enter and exit pragmas at the time of allocation and
+! deallocation.
+
+! { dg-additional-options "-fdump-tree-original" }
+
+program allocate
+  implicit none
+  integer, allocatable :: a(:), b
+  integer, parameter :: n = 100
+  integer i
+  !$acc declare create(a,b)
+
+  allocate (a(n), b)
+
+  !$acc parallel loop copyout(a, b)
+  do i = 1, n
+     a(i) = b
+  end do
+
+  deallocate (a, b)
+end program allocate
+
+! { dg-final { scan-tree-dump-times "pragma acc enter data map.declare_allocate" 2 "original" } }
+! { dg-final { scan-tree-dump-times "pragma acc exit data map.declare_deallocate" 2 "original" } }
index f64413ff32461c97258e98e06520c389943de5ea..997585e19ac94b145f98d1bdd169da2d2649bc7f 100644 (file)
@@ -882,6 +882,12 @@ dump_omp_clause (pretty_printer *pp, tree clause, int spc, dump_flags_t flags)
        case GOMP_MAP_LINK:
          pp_string (pp, "link");
          break;
+       case GOMP_MAP_DECLARE_ALLOCATE:
+         pp_string (pp, "declare_allocate");
+         break;
+       case GOMP_MAP_DECLARE_DEALLOCATE:
+         pp_string (pp, "declare_deallocate");
+         break;
        case GOMP_MAP_ATTACH:
          pp_string (pp, "attach");
          break;
index 9bd3fb60a78ded5bb0b73131f356944ca2d55741..a460e639d22993f14fc7831c85d70eb3051751a9 100644 (file)
@@ -1,3 +1,9 @@
+2018-10-04  Cesar Philippidis  <cesar@codesourcery.com>
+            Julian Brown  <julian@codesourcery.com>
+
+       * gomp-constants.h (enum gomp_map_kind): Define
+       GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE} and GOMP_MAP_FLAG_SPECIAL_4.
+
 2020-04-19  Chung-Lin Tang  <cltang@codesourcery.com>
 
        PR other/76739
index 3a4334ba079437199d8f193153b19912c8ee4778..6239f8ac9bdfa78ab42af5caf7450c365128593e 100644 (file)
@@ -140,6 +140,11 @@ enum gomp_map_kind
     /* Decrement usage count and deallocate if zero.  */
     GOMP_MAP_RELEASE =                 (GOMP_MAP_FLAG_SPECIAL_2
                                         | GOMP_MAP_DELETE),
+    /* Mapping kinds for allocatable arrays.  */
+    GOMP_MAP_DECLARE_ALLOCATE =                (GOMP_MAP_FLAG_SPECIAL_4
+                                        | GOMP_MAP_FORCE_TO),
+    GOMP_MAP_DECLARE_DEALLOCATE =      (GOMP_MAP_FLAG_SPECIAL_4
+                                        | GOMP_MAP_FORCE_FROM),
     /* The attach/detach mappings below use the OMP_CLAUSE_SIZE field as a
        bias.  This will typically be zero, except when mapping an array slice
        with a non-zero base.  In that case the bias will indicate the
index 6ff1f707fed3f43434c973a68136587f40bc186b..67cc9223a42067d3f6c7a7fd40aa60755bb9f856 100644 (file)
@@ -1,3 +1,27 @@
+2021-04-07  Kwok Cheung Yeung  <kcy@codesourcery.com>
+
+       * oacc-mem.c (goacc_enter_data_internal): Unlock mutex before calling
+         gomp_acc_declare_allocate and relock it afterwards.
+
+2018-10-04  Cesar Philippidis  <cesar@codesourcery.com>
+           Julian Brown  <julian@codesourcery.com>
+
+       * libgomp.h (gomp_acc_declare_allocate): Remove prototype.
+       * oacc-mem.c (gomp_acc_declare_allocate): New function.
+       * oacc-parallel.c (goacc_enter_data_internal): Handle
+       GOMP_MAP_DECLARE_ALLOCATE.  Pass new pointer argument to
+       gomp_acc_declare_allocate.
+       (goacc_exit_data_internal): Handle GOMP_MAP_DECLARE_DEALLOCATE.
+       Unlock device mutex around gomp_acc_declare_allocate call.  Pass
+       new pointer argument.  Handle group pointer mapping for deallocate.
+       (find_group_last): Handle GOMP_MAP_DECLARE_ALLOCATE and
+       GOMP_MAP_DECLARE_DEALLOCATE groupings.
+       * testsuite/libgomp.oacc-fortran/allocatable-scalar.f90: New test.
+       * testsuite/libgomp.oacc-fortran/declare-allocatable-1.f90: New test.
+       * testsuite/libgomp.oacc-fortran/declare-allocatable-2.f90: New test.
+       * testsuite/libgomp.oacc-fortran/declare-allocatable-3.f90: New test.
+       * testsuite/libgomp.oacc-fortran/declare-allocatable-4.f90: New test.
+
 2019-10-16  Julian Brown  <julian@codesourcery.com>
 
        * testsuite/libgomp.oacc-fortran/gangprivate-attrib-1.f90: Use
index 7a71df21ba0e0d9a01fb070fbfcb89cd25b4e9c5..38aa589c8c3c7ef5d8251bb0a60007a34cc04ba1 100644 (file)
@@ -1195,8 +1195,6 @@ enum gomp_map_vars_kind
   GOMP_MAP_VARS_ENTER_DATA = 8
 };
 
-extern void gomp_acc_declare_allocate (bool, size_t, void **, size_t *,
-                                      unsigned short *);
 struct gomp_coalesce_buf;
 extern void gomp_copy_host2dev (struct gomp_device_descr *,
                                struct goacc_asyncqueue *, void *, const void *,
index 2636fb8f166d0ced77abba40829e583c9284f5ad..3b98b65a8595754ec0e2890fe3371e754da1be18 100644 (file)
@@ -913,6 +913,35 @@ acc_update_self_async (void *h, size_t s, int async)
   update_dev_host (0, h, s, async);
 }
 
+/* Implement "declare allocate" and "declare deallocate" operations.  The
+   device lock must not be held before calling this function.  */
+
+static void
+gomp_acc_declare_allocate (bool allocate, bool pointer, void **hostaddrs,
+                          size_t *sizes, unsigned short *kinds)
+{
+  gomp_debug (0, "  %s: processing\n", __FUNCTION__);
+
+  if (allocate)
+    {
+      /* Allocate memory for the array data.  */
+      uintptr_t data = (uintptr_t) acc_create (hostaddrs[0], sizes[0]);
+
+      if (pointer)
+       {
+         /* Update the PSET.  */
+         acc_update_device (hostaddrs[1], sizes[1]);
+         void *pset = acc_deviceptr (hostaddrs[1]);
+         acc_memcpy_to_device (pset, &data, sizeof (uintptr_t));
+       }
+    }
+  else
+    /* Deallocate memory for the array data.  */
+    acc_delete (hostaddrs[0], sizes[0]);
+
+  gomp_debug (0, "  %s: end\n", __FUNCTION__);
+}
+
 void
 acc_attach_async (void **hostaddr, int async)
 {
@@ -1043,6 +1072,28 @@ find_group_last (int pos, size_t mapnum, size_t *sizes, unsigned short *kinds)
     case GOMP_MAP_ATTACH:
       break;
 
+    case GOMP_MAP_DECLARE_ALLOCATE:
+    case GOMP_MAP_DECLARE_DEALLOCATE:
+      {
+       /* The "declare allocate" and "declare deallocate" mappings can be
+          used to specify either a scalar allocatable (which just appears as
+          GOMP_MAP_DECLARE_{ALLOCATE,DEALLOCATE} by itself), or an array
+          allocatable (which appears as that directive followed by a
+          GOMP_MAP_TO_PSET and one (or more?) GOMP_MAP_POINTER mappings.  */
+       if (pos + 1 >= mapnum)
+         break;
+
+       unsigned char kind1 = kinds[pos + 1] & 0xff;
+       if (kind1 != GOMP_MAP_TO_PSET)
+         break;
+
+       pos++;
+
+       while (pos + 1 < mapnum && (kinds[pos + 1] & 0xff) == GOMP_MAP_POINTER)
+         pos++;
+      }
+      break;
+
     default:
       /* GOMP_MAP_ALWAYS_POINTER can only appear directly after some other
         mapping.  */
@@ -1107,7 +1158,14 @@ goacc_enter_data_internal (struct gomp_device_descr *acc_dev, size_t mapnum,
 
       n = lookup_host (acc_dev, hostaddrs[i], size);
 
-      if (n && struct_p)
+      if ((kinds[i] & 0xff) == GOMP_MAP_DECLARE_ALLOCATE)
+       {
+         gomp_mutex_unlock (&acc_dev->lock);
+         gomp_acc_declare_allocate (true, group_last > i, &hostaddrs[i],
+                                    &sizes[i], &kinds[i]);
+         gomp_mutex_lock (&acc_dev->lock);
+       }
+      else if (n && struct_p)
        {
          for (size_t j = i + 1; j <= group_last; j++)
            {
@@ -1311,6 +1369,24 @@ goacc_exit_data_internal (struct gomp_device_descr *acc_dev, size_t mapnum,
             reference counts ('n->refcount', 'n->dynamic_refcount').  */
          break;
 
+       case GOMP_MAP_DECLARE_DEALLOCATE:
+         {
+           bool deallocate_pointer
+             = i + 1 < mapnum && (kinds[i + 1] & 0xff) == GOMP_MAP_TO_PSET;
+           gomp_mutex_unlock (&acc_dev->lock);
+           gomp_acc_declare_allocate (false, deallocate_pointer,
+                                      &hostaddrs[i], &sizes[i], &kinds[i]);
+           gomp_mutex_lock (&acc_dev->lock);
+           if (deallocate_pointer)
+             {
+               i++;
+               while (i + 1 < mapnum
+                      && (kinds[i + 1] & 0xff) == GOMP_MAP_POINTER)
+                 i++;
+             }
+         }
+         break;
+
        default:
          gomp_fatal (">>>> goacc_exit_data_internal UNHANDLED kind 0x%.2x",
                          kind);
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/allocatable-scalar.f90 b/libgomp/testsuite/libgomp.oacc-fortran/allocatable-scalar.f90
new file mode 100644 (file)
index 0000000..42b3408
--- /dev/null
@@ -0,0 +1,33 @@
+! Test non-declared allocatable scalars in OpenACC data clauses.
+
+! { dg-do run }
+
+program main
+  implicit none
+  integer, parameter :: n = 100
+  integer, allocatable :: a, c
+  integer :: i, b(n)
+
+  allocate (a)
+
+  a = 50
+
+  !$acc parallel loop
+  do i = 1, n;
+     b(i) = a
+  end do
+
+  do i = 1, n
+     if (b(i) /= a) stop 1
+  end do
+
+  allocate (c)
+
+  !$acc parallel copyout(c) num_gangs(1)
+  c = a
+  !$acc end parallel
+
+  if (c /= a) stop 2
+
+  deallocate (a, c)
+end program main
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-1.f90 b/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-1.f90
new file mode 100644 (file)
index 0000000..ec7f7c1
--- /dev/null
@@ -0,0 +1,211 @@
+! Test declare create with allocatable arrays.
+
+! { dg-do run }
+
+module vars
+  implicit none
+  integer, parameter :: n = 100
+  real*8, allocatable :: b(:)
+ !$acc declare create (b)
+end module vars
+
+program test
+  use vars
+  use openacc
+  implicit none
+  real*8 :: a
+  integer :: i
+
+  interface
+     subroutine sub1
+       !$acc routine gang
+     end subroutine sub1
+
+     subroutine sub2
+     end subroutine sub2
+
+     real*8 function fun1 (ix)
+       integer ix
+       !$acc routine seq
+     end function fun1
+
+     real*8 function fun2 (ix)
+       integer ix
+       !$acc routine seq
+     end function fun2
+  end interface
+
+  if (allocated (b)) stop 1
+
+  ! Test local usage of an allocated declared array.
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 2
+  if (acc_is_present (b) .neqv. .true.) stop 3
+
+  a = 2.0
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = i * a
+  end do
+
+  if (.not.acc_is_present (b)) stop 4
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= i*a) stop 5
+  end do
+
+  deallocate (b)
+
+  ! Test the usage of an allocated declared array inside an acc
+  ! routine subroutine.
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 6
+  if (acc_is_present (b) .neqv. .true.) stop 7
+
+  !$acc parallel
+  call sub1
+  !$acc end parallel
+
+  if (.not.acc_is_present (b)) stop 8
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= i*2) stop 9
+  end do
+
+  deallocate (b)
+
+  ! Test the usage of an allocated declared array inside a host
+  ! subroutine.
+
+  call sub2
+
+  if (.not.acc_is_present (b)) stop 10
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= 1.0) stop 11
+  end do
+
+  deallocate (b)
+
+  if (allocated (b)) stop 12
+
+  ! Test the usage of an allocated declared array inside an acc
+  ! routine function.
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 13
+  if (acc_is_present (b) .neqv. .true.) stop 14
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = 1.0
+  end do
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = fun1 (i)
+  end do
+
+  if (.not.acc_is_present (b)) stop 15
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= i) stop 16
+  end do
+
+  deallocate (b)
+
+  ! Test the usage of an allocated declared array inside a host
+  ! function.
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 17
+  if (acc_is_present (b) .neqv. .true.) stop 18
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = 1.0
+  end do
+
+  !$acc update host(b)
+
+  do i = 1, n
+     b(i) = fun2 (i)
+  end do
+
+  if (.not.acc_is_present (b)) stop 19
+
+  do i = 1, n
+     if (b(i) /= i*i) stop 20
+  end do
+
+  deallocate (b)
+end program test
+
+! Set each element in array 'b' at index i to i*2.
+
+subroutine sub1 ! { dg-warning "region is worker partitioned" }
+  use vars
+  implicit none
+  integer i
+  !$acc routine gang
+
+  !$acc loop
+  do i = 1, n
+     b(i) = i*2
+  end do
+end subroutine sub1
+
+! Allocate array 'b', and set it to all 1.0.
+
+subroutine sub2
+  use vars
+  use openacc
+  implicit none
+  integer i
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 21
+  if (acc_is_present (b) .neqv. .true.) stop 22
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = 1.0
+  end do
+end subroutine sub2
+
+! Return b(i) * i;
+
+real*8 function fun1 (i)
+  use vars
+  implicit none
+  integer i
+  !$acc routine seq
+
+  fun1 = b(i) * i
+end function fun1
+
+! Return b(i) * i * i;
+
+real*8 function fun2 (i)
+  use vars
+  implicit none
+  integer i
+
+  fun2 = b(i) * i * i
+end function fun2
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-2.f90 b/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-2.f90
new file mode 100644 (file)
index 0000000..df5ab26
--- /dev/null
@@ -0,0 +1,48 @@
+! Test declare create with allocatable scalars.
+
+! { dg-do run }
+
+program main
+  use openacc
+  implicit none
+  integer, parameter :: n = 100
+  integer, allocatable :: a, c
+  integer :: i, b(n)
+  !$acc declare create (c)
+
+  allocate (a)
+
+  a = 50
+
+  !$acc parallel loop firstprivate(a)
+  do i = 1, n;
+     b(i) = a
+  end do
+
+  do i = 1, n
+     if (b(i) /= a) stop 1
+  end do
+
+  allocate (c)
+  a = 100
+
+  if (.not.acc_is_present(c)) stop 2
+
+  !$acc parallel num_gangs(1) present(c)
+  c = a
+  !$acc end parallel
+
+  !$acc update host(c)
+  if (c /= a) stop 3
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = c
+  end do
+
+  do i = 1, n
+     if (b(i) /= a) stop 4
+  end do
+
+  deallocate (a, c)
+end program main
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-3.f90 b/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-3.f90
new file mode 100644 (file)
index 0000000..9381b00
--- /dev/null
@@ -0,0 +1,218 @@
+! Test declare create with allocatable arrays.
+
+! { dg-do run }
+
+module vars
+  implicit none
+  integer, parameter :: n = 100
+  real*8, allocatable :: a, b(:)
+ !$acc declare create (a, b)
+end module vars
+
+program test
+  use vars
+  use openacc
+  implicit none
+  integer :: i
+
+  interface
+     subroutine sub1
+       !$acc routine gang
+     end subroutine sub1
+
+     subroutine sub2
+     end subroutine sub2
+
+     real*8 function fun1 (ix)
+       integer ix
+       !$acc routine seq
+     end function fun1
+
+     real*8 function fun2 (ix)
+       integer ix
+       !$acc routine seq
+     end function fun2
+  end interface
+
+  if (allocated (a)) stop 1
+  if (allocated (b)) stop 2
+
+  ! Test local usage of an allocated declared array.
+
+  allocate (a)
+
+  if (.not.allocated (a)) stop 3
+  if (acc_is_present (a) .neqv. .true.) stop 4
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 5
+  if (acc_is_present (b) .neqv. .true.) stop 6
+
+  a = 2.0
+  !$acc update device(a)
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = i * a
+  end do
+
+  if (.not.acc_is_present (b)) stop 7
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= i*a) stop 8
+  end do
+
+  deallocate (b)
+
+  ! Test the usage of an allocated declared array inside an acc
+  ! routine subroutine.
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 9
+  if (acc_is_present (b) .neqv. .true.) stop 10
+
+  !$acc parallel
+  call sub1
+  !$acc end parallel
+
+  if (.not.acc_is_present (b)) stop 11
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= a+i*2) stop 12
+  end do
+
+  deallocate (b)
+
+  ! Test the usage of an allocated declared array inside a host
+  ! subroutine.
+
+  call sub2
+
+  if (.not.acc_is_present (b)) stop 13
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= 1.0) stop 14
+  end do
+
+  deallocate (b)
+
+  if (allocated (b)) stop 15
+
+  ! Test the usage of an allocated declared array inside an acc
+  ! routine function.
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 16
+  if (acc_is_present (b) .neqv. .true.) stop 17
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = 1.0
+  end do
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = fun1 (i)
+  end do
+
+  if (.not.acc_is_present (b)) stop 18
+
+  !$acc update host(b)
+
+  do i = 1, n
+     if (b(i) /= i) stop 19
+  end do
+
+  deallocate (b)
+
+  ! Test the usage of an allocated declared array inside a host
+  ! function.
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 20
+  if (acc_is_present (b) .neqv. .true.) stop 21
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = 1.0
+  end do
+
+  !$acc update host(b)
+
+  do i = 1, n
+     b(i) = fun2 (i)
+  end do
+
+  if (.not.acc_is_present (b)) stop 22
+
+  do i = 1, n
+     if (b(i) /= i*a) stop 23
+  end do
+
+  deallocate (a)
+  deallocate (b)
+end program test
+
+! Set each element in array 'b' at index i to a+i*2.
+
+subroutine sub1 ! { dg-warning "region is worker partitioned" }
+  use vars
+  implicit none
+  integer i
+  !$acc routine gang
+
+  !$acc loop
+  do i = 1, n
+     b(i) = a+i*2
+  end do
+end subroutine sub1
+
+! Allocate array 'b', and set it to all 1.0.
+
+subroutine sub2
+  use vars
+  use openacc
+  implicit none
+  integer i
+
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 24
+  if (acc_is_present (b) .neqv. .true.) stop 25
+
+  !$acc parallel loop
+  do i = 1, n
+     b(i) = 1.0
+  end do
+end subroutine sub2
+
+! Return b(i) * i;
+
+real*8 function fun1 (i)
+  use vars
+  implicit none
+  integer i
+  !$acc routine seq
+
+  fun1 = b(i) * i
+end function fun1
+
+! Return b(i) * i * a;
+
+real*8 function fun2 (i)
+  use vars
+  implicit none
+  integer i
+
+  fun2 = b(i) * i * a
+end function fun2
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-4.f90 b/libgomp/testsuite/libgomp.oacc-fortran/declare-allocatable-4.f90
new file mode 100644 (file)
index 0000000..afbe52f
--- /dev/null
@@ -0,0 +1,66 @@
+! Test declare create with allocatable arrays and scalars.  The unused
+! declared array 'b' caused an ICE in the past.
+
+! { dg-do run }
+
+module vars
+  implicit none
+  integer, parameter :: n = 100
+  real*8, allocatable :: a, b(:)
+ !$acc declare create (a, b)
+end module vars
+
+program test
+  use vars
+  implicit none
+  integer :: i
+
+  interface
+     subroutine sub1
+     end subroutine sub1
+
+     subroutine sub2
+     end subroutine sub2
+
+     real*8 function fun1 (ix)
+       integer ix
+       !$acc routine seq
+     end function fun1
+
+     real*8 function fun2 (ix)
+       integer ix
+       !$acc routine seq
+     end function fun2
+  end interface
+
+  if (allocated (a)) stop 1
+  if (allocated (b)) stop 2
+
+  ! Test the usage of an allocated declared array inside an acc
+  ! routine subroutine.
+
+  allocate (a)
+  allocate (b(n))
+
+  if (.not.allocated (b)) stop 3
+
+  call sub1
+
+  !$acc update self(a)
+  if (a /= 50) stop 4
+
+  deallocate (a)
+  deallocate (b)
+
+end program test
+
+! Set 'a' to 50.
+
+subroutine sub1
+  use vars
+  implicit none
+  integer i
+
+  a = 50
+  !$acc update device(a)
+end subroutine sub1