diff mbox

[Cilkplus] Patch to add implicit sync before try block

Message ID BF230D13CA30DD48930C31D40993300016C4F27F@FMSMSX102.amr.corp.intel.com
State New
Headers show

Commit Message

Iyer, Balaji V Aug. 10, 2012, 8:23 p.m. UTC
Hello Everyone,
	This patch is for the Cilk Plus branch affecting mainly the C++ compiler. This patch will add an implicit sync before a try block in a function that spawns.

Thanks,

Balaji V. Iyer.
diff mbox

Patch

Index: gcc/testsuite/g++.dg/cilk-plus/test-try.cc
===================================================================
--- gcc/testsuite/g++.dg/cilk-plus/test-try.cc	(revision 0)
+++ gcc/testsuite/g++.dg/cilk-plus/test-try.cc	(revision 0)
@@ -0,0 +1,32 @@ 
+/* Please run this in the following way:
+
+   time ./a.out
+
+   The output should be something like this:
+
+   real    0m10.002s     <== This is the main thing that should match. 
+   user    0m10.793s
+   sys     0m53.855s
+
+*/
+
+
+#include <unistd.h>
+void mysleep(int a) 
+{
+  sleep(5);
+}
+
+int main(int argc, char ** argv) 
+{
+  _Cilk_spawn mysleep(5); 
+  try 
+    { 
+      _Cilk_spawn mysleep(5); 
+      mysleep(5);
+    }
+		            
+    catch(int err) 
+      {
+      } 
+}
Index: gcc/testsuite/g++.dg/cilk-plus/test-try-cond.cc
===================================================================
--- gcc/testsuite/g++.dg/cilk-plus/test-try-cond.cc	(revision 0)
+++ gcc/testsuite/g++.dg/cilk-plus/test-try-cond.cc	(revision 0)
@@ -0,0 +1,41 @@ 
+/* Please run this in the following way:
+
+   time ./a.out
+
+   The output should be something like this:
+
+   real    0m5.002s     <=== This is the main thing that should match.
+   user    0m6.200s
+   sys     0m28.670s
+
+   When you run it with any number of arguments > 1 then you should get
+   something like this: 
+   
+   time ./a.out  1 2 3 
+   real    0m10.003s   <=== This is the main thing that should match.
+   user    0m11.401s
+   sys     0m53.387s
+
+*/
+
+
+#include <unistd.h>
+void mysleep(int a) {
+  sleep(5);
+}
+
+int main(int argc, char ** argv) {
+  _Cilk_spawn mysleep(5);
+  if (argc > 1) {
+    try {
+      _Cilk_spawn mysleep(5);
+      mysleep(5);
+    }
+		            
+    catch(int err) {
+    }
+  }
+  else {
+    mysleep(5);
+  }
+}
Index: gcc/testsuite/ChangeLog.cilkplus
===================================================================
--- gcc/testsuite/ChangeLog.cilkplus	(revision 190299)
+++ gcc/testsuite/ChangeLog.cilkplus	(working copy)
@@ -1,3 +1,8 @@ 
+2012-08-10  Balaji V. Iyer  <balaji.v.iyer@intel.com>
+
+	* g++.dg/cilk-plus/test-try.cc: New test file.
+	* g++.dg/cilk-plus/test-try-cond.cc: Likewise.
+
 2012-07-16  Balaji V. Iyer  <balaji.v.iyer@intel.com>
 
 	* gcc.dg/cilk-plus/cilk_plus.exp: Added flag_enable_cilk flag.
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c	(revision 190195)
+++ gcc/cp/decl.c	(working copy)
@@ -13371,7 +13371,7 @@ 
   /* Here we insert Cilk specific functions when _Cilk_spawn is used.  */
   if (flag_enable_cilk && cfun->calls_spawn)
     {
-      cfun->cilk_frame_decl = cp_make_cilk_frame ();
+      cfun->cilk_frame_decl = cp_make_cilk_frame (compstmt);
       DECL_HAS_SPAWN_P (cfun->decl) = 1;
     }
 
Index: gcc/cp/cilk.c
===================================================================
--- gcc/cp/cilk.c	(revision 190179)
+++ gcc/cp/cilk.c	(working copy)
@@ -805,11 +805,34 @@ 
   return 0;
 }
 
+/* This function will insert a _Cilk_sync right before a try block.  */
 
+static tree
+insert_sync_stmt (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
+{
+  tree new_sync_stmt = NULL_TREE, synced_stmt_list = NULL_TREE;
+  if (TREE_CODE (*tp) == TRY_BLOCK)
+    {
+      new_sync_stmt = build_stmt (UNKNOWN_LOCATION, CILK_SYNC_STMT);
+      gcc_assert (new_sync_stmt && (new_sync_stmt != error_mark_node));
+      append_to_statement_list_force (new_sync_stmt, &synced_stmt_list);
+      append_to_statement_list_force (*tp, &synced_stmt_list);
+      *tp = synced_stmt_list;
+      
+      /* We don't need to go any deeper.  */
+      *walk_subtrees = 0;
+      
+      /* We are finished here.  We only need to find the first try block.  */
+      return *tp;
+    }
+  
+  return NULL;
+}
+
 /* This function will make the frame for C++ function that uses Cilk_spawn.  */
 
 tree
-cp_make_cilk_frame (void)
+cp_make_cilk_frame (tree compstmt)
 {
   tree decl = cfun->cilk_frame_decl;
 
@@ -861,6 +884,10 @@ 
 				      &body);
 
       *saved_tree = body;
+
+      /* Here we talk through all the subtrees of compstmt and as soon as
+	 we find a try block, we insert a _Cilk_sync right before it.  */
+      cp_walk_tree (&compstmt, insert_sync_stmt, NULL, NULL);
     }
 
   return decl;
Index: gcc/cp/ChangeLog.cilkplus
===================================================================
--- gcc/cp/ChangeLog.cilkplus	(revision 190299)
+++ gcc/cp/ChangeLog.cilkplus	(working copy)
@@ -1,3 +1,10 @@ 
+2012-08-10  Balaji V. Iyer  <balaji.v.iyer@intel.com>
+
+	* decl.c (finish_function_body): Added compstmt as a new parameter.
+	(cp_make_cilk_frame): Added compstmt as a parameter and called
+	insert_sync_stmt inside cp_walk_tree.
+	(insert_sync_stmt): New function.
+
 2012-08-07  Balaji V. Iyer  <balaji.v.iyer@intel.com>
 
 	* parser.c (cp_parser_cilk_for): Stored the initial value in cilk_for tree.
Index: gcc/cp/cp-tree.h
===================================================================
--- gcc/cp/cp-tree.h	(revision 190195)
+++ gcc/cp/cp-tree.h	(working copy)
@@ -6051,7 +6051,7 @@ 
 struct cp_binding_level *in_cilk_block          (void);
 bool cp_spawnable_constructor                   (tree);
 bool cp_recognize_spawn                         (tree);
-tree cp_make_cilk_frame                         (void);
+tree cp_make_cilk_frame                         (tree);
 
 extern tree cilk_for_var_decl                   (tree, bool);
 extern void finish_cilk_for_stmt                (tree);