public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Fortran, Patch] Memory sync after coarray image control statements and assignment
@ 2015-12-06 21:02 Alessandro Fanfarillo
  2015-12-07  7:20 ` Tobias Burnus
  0 siblings, 1 reply; 15+ messages in thread
From: Alessandro Fanfarillo @ 2015-12-06 21:02 UTC (permalink / raw)
  To: gfortran, gcc-patches, opencoarrays

[-- Attachment #1: Type: text/plain, Size: 779 bytes --]

Dear all,

currently, a coarray assignment in a program composed by a single
segment (without any sync statements) produces wrong results.
Furthermore, a coarray code
compiled with an optimization flag higher that -O0 may produce wrong
results. The patch (re)introduces a __sync_synchronize() after coarray
image control statements (sync all, sync images, critical, locks and
events) and get/put.

The attached patch fixes the problems reported by Deepak in the
following discussion:
https://gcc.gnu.org/ml/fortran/2015-04/msg00062.html.

Built and regtested on x86_64-pc-linux-gnu.


PS: Adding the __sync_synchronize() after get/put and sync statements
fix all the problems reported by Deepak. I do the same also for locks,
critical and events. Suggestions are warmly welcome.

[-- Attachment #2: patch_sync.diff --]
[-- Type: text/plain, Size: 4792 bytes --]

commit 75c93d3085116748115c8f69ad5ad58f4ad9369c
Author: Alessandro Fanfarillo <fanfarillo@ing.uniroma2.it>
Date:   Sun Dec 6 18:50:51 2015 +0100

    Introducing __sync_synchronize() after image control statements, send and get

diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog
index ba176a1..b450fae 100644
--- a/gcc/fortran/ChangeLog
+++ b/gcc/fortran/ChangeLog
@@ -1,3 +1,13 @@
+2015-12-06  Alessandro Fanfarillo <fanfarillo.gcc@gmail.com>
+
+	* trans.c (gfc_allocate_using_lib,gfc_deallocate_with_status):
+	Introducing __sync_synchronize() after image control statements.
+	* trans-stmt.c (gfc_trans_sync,gfc_trans_event_post_wait,
+	gfc_trans_lock_unlock): Ditto.
+	* trans-intrinsic.c (gfc_conv_intrinsic_caf_get,
+	conv_caf_send): Introducing __sync_synchronize() after send,
+	get and sendget.
+
 2015-12-05  Paul Thomas  <pault@gcc.gnu.org>
 
 	PR fortran/68676
diff --git a/gcc/fortran/trans-intrinsic.c b/gcc/fortran/trans-intrinsic.c
index 21efe44..07795ca 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -1222,6 +1222,12 @@ gfc_conv_intrinsic_caf_get (gfc_se *se, gfc_expr *expr, tree lhs, tree lhs_kind,
   se->expr = res_var;
   if (array_expr->ts.type == BT_CHARACTER)
     se->string_length = argse.string_length;
+
+  /* It guarantees memory consistency within the same segment */
+  tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
+  tmp = build_call_expr_loc (input_location, tmp, 0);
+  gfc_add_expr_to_block (&se->pre, tmp);
+
 }
 
 
@@ -1390,6 +1396,12 @@ conv_caf_send (gfc_code *code) {
   gfc_add_expr_to_block (&block, tmp);
   gfc_add_block_to_block (&block, &lhs_se.post);
   gfc_add_block_to_block (&block, &rhs_se.post);
+
+  /* It guarantees memory consistency within the same segment */
+  tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
+  tmp = build_call_expr_loc (input_location, tmp, 0);
+  gfc_add_expr_to_block (&block, tmp);
+
   return gfc_finish_block (&block);
 }
 
diff --git a/gcc/fortran/trans-stmt.c b/gcc/fortran/trans-stmt.c
index 3df483a..b1de0f5 100644
--- a/gcc/fortran/trans-stmt.c
+++ b/gcc/fortran/trans-stmt.c
@@ -818,6 +818,11 @@ gfc_trans_lock_unlock (gfc_code *code, gfc_exec_op op)
 				   errmsg, errmsg_len);
       gfc_add_expr_to_block (&se.pre, tmp);
 
+      /* It guarantees memory consistency within the same segment */
+      tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
+      tmp = build_call_expr_loc (input_location, tmp, 0);
+      gfc_add_expr_to_block (&se.pre, tmp);
+
       if (stat2 != NULL_TREE)
 	gfc_add_modify (&se.pre, stat2,
 			fold_convert (TREE_TYPE (stat2), stat));
@@ -995,6 +1000,11 @@ gfc_trans_event_post_wait (gfc_code *code, gfc_exec_op op)
 			       errmsg, errmsg_len);
   gfc_add_expr_to_block (&se.pre, tmp);
 
+  /* It guarantees memory consistency within the same segment */
+  tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
+  tmp = build_call_expr_loc (input_location, tmp, 0);
+  gfc_add_expr_to_block (&se.pre, tmp);
+
   if (stat2 != NULL_TREE)
     gfc_add_modify (&se.pre, stat2, fold_convert (TREE_TYPE (stat2), stat));
 
@@ -1080,6 +1090,15 @@ gfc_trans_sync (gfc_code *code, gfc_exec_op type)
 			       fold_convert (integer_type_node, images));
     }
 
+  /* Per F2008, 8.5.1, a SYNC MEMORY is implied by calling the
+     image control statements SYNC IMAGES and SYNC ALL.  */
+  if (flag_coarray == GFC_FCOARRAY_LIB)
+    {
+      tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
+      tmp = build_call_expr_loc (input_location, tmp, 0);
+      gfc_add_expr_to_block (&se.pre, tmp);
+    }
+  
   if (flag_coarray != GFC_FCOARRAY_LIB)
     {
       /* Set STAT to zero.  */
diff --git a/gcc/fortran/trans.c b/gcc/fortran/trans.c
index 001db41..e7803bd 100644
--- a/gcc/fortran/trans.c
+++ b/gcc/fortran/trans.c
@@ -746,6 +746,11 @@ gfc_allocate_using_lib (stmtblock_t * block, tree pointer, tree size,
 			 TREE_TYPE (pointer), pointer,
 			 fold_convert ( TREE_TYPE (pointer), tmp));
   gfc_add_expr_to_block (block, tmp);
+
+  /* It guarantees memory consistency within the same segment */
+  tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
+  tmp = build_call_expr_loc (input_location, tmp, 0);
+  gfc_add_expr_to_block (block, tmp);
 }
 
 
@@ -1356,6 +1361,11 @@ gfc_deallocate_with_status (tree pointer, tree status, tree errmsg,
 	     token, pstat, errmsg, errlen);
       gfc_add_expr_to_block (&non_null, tmp);
 
+      /* It guarantees memory consistency within the same segment */
+      tmp = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
+      tmp = build_call_expr_loc (input_location, tmp, 0);
+      gfc_add_expr_to_block (&non_null, tmp);
+
       if (status != NULL_TREE)
 	{
 	  tree stat = build_fold_indirect_ref_loc (input_location, status);

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2015-12-14 19:14 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-06 21:02 [Fortran, Patch] Memory sync after coarray image control statements and assignment Alessandro Fanfarillo
2015-12-07  7:20 ` Tobias Burnus
2015-12-07  9:16   ` Alessandro Fanfarillo
2015-12-07 10:06   ` Tobias Burnus
2015-12-07 14:09     ` Matthew Wahab
2015-12-08  9:26       ` Tobias Burnus
2015-12-09 14:47         ` Matthew Wahab
2015-12-07 14:48     ` Alessandro Fanfarillo
2015-12-08 10:01       ` Tobias Burnus
2015-12-08 14:21         ` Alessandro Fanfarillo
2015-12-09  7:23           ` Tobias Burnus
2015-12-09 16:08             ` Alessandro Fanfarillo
2015-12-09 22:16               ` Tobias Burnus
2015-12-10  8:44                 ` Alessandro Fanfarillo
     [not found]                   ` <20151210090345.GB6991@physik.fu-berlin.de>
     [not found]                     ` <CAHqFgjVhx6pcVFEaCT8=9P+vkR=SP-mohZwr+B315N0_41OtKA@mail.gmail.com>
2015-12-14 19:14                       ` Tobias Burnus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).