public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Martin Liška" <mliska@suse.cz>
To: Nathan Sidwell <nathan@acm.org>, gcc-patches@gcc.gnu.org
Cc: jh@suse.cz
Subject: Re: [PATCH 1/4] Cherry-pick fprofile-generate-atomic from google/gcc-4_9 branch
Date: Fri, 05 Aug 2016 13:43:00 -0000	[thread overview]
Message-ID: <04a05835-4666-4d7d-c1a9-d4bcc4ea924a@suse.cz> (raw)
In-Reply-To: <1ff3cc75-7cee-79f3-395b-ef7a4d286a3d@acm.org>

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

On 08/05/2016 03:14 PM, Nathan Sidwell wrote:
> On 08/05/16 08:48, Martin Liška wrote:
> 
>> Ok, after all the experimenting and inventing "almost" thread-safe code, I incline to not to include __gcov_one_value_profiler_body_atomic
>> counter. The final solution is cumbersome and probably does not worth doing. Moreover, even having a thread-safe implementation, result of an indirect call counter
>> is not going to be stable among different runs (due to a single value storage capability).
>>
>> If you agree, I'll prepare a final version of patch?
> 
> Agreed.
> 
> nathan
> 

Great, attaching install candidate.

Martin

[-- Attachment #2: 0001-Cherry-pick-fprofile-generate-atomic-from-google-gcc-v3.patch --]
[-- Type: text/x-patch, Size: 8707 bytes --]

From 0b3ac8636ef34b02e301f22c86dde0602f9969ef Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Thu, 28 Jul 2016 14:32:47 +0200
Subject: [PATCH 1/4] Cherry-pick fprofile-generate-atomic from google/gcc-4_9
 branch

gcc/ChangeLog:

2016-08-05  Martin Liska  <mliska@suse.cz>

	Cherry picked (and modified) from google-4_7 branch
	2012-12-26  Rong Xu  <xur@google.com>
	* common.opt (fprofile-update): Add new flag.
	* coretypes.h: Define enum profile_update.
	* doc/invoke.texi: Document -fprofile-update.
	* gcov-io.h: Declare GCOV_TYPE_ATOMIC_FETCH_ADD and
	GCOV_TYPE_ATOMIC_FETCH_ADD_FN.
	* tree-profile.c (gimple_init_edge_profiler): Generate
	also atomic profiler update.
	(gimple_gen_edge_profiler): Likewise.

gcc/testsuite/ChangeLog:

2016-08-05  Martin Liska  <mliska@suse.cz>

	* g++.dg/gcov/gcov-threads-1.C: New test.
---
 gcc/common.opt                             | 13 ++++++++
 gcc/coretypes.h                            |  6 ++++
 gcc/doc/invoke.texi                        | 12 +++++++
 gcc/gcov-io.h                              |  8 +++++
 gcc/testsuite/g++.dg/gcov/gcov-threads-1.C | 46 ++++++++++++++++++++++++++
 gcc/tree-profile.c                         | 53 ++++++++++++++++++++----------
 6 files changed, 120 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/gcov/gcov-threads-1.C

diff --git a/gcc/common.opt b/gcc/common.opt
index 8a292ed..44adae8 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1916,6 +1916,19 @@ fprofile-correction
 Common Report Var(flag_profile_correction)
 Enable correction of flow inconsistent profile data input.
 
+fprofile-update=
+Common Joined RejectNegative Enum(profile_update) Var(flag_profile_update) Init(PROFILE_UPDATE_SINGLE)
+-fprofile-update=[single|atomic]	Set the profile update method.
+
+Enum
+Name(profile_update) Type(enum profile_update) UnknownError(unknown profile update method %qs)
+
+EnumValue
+Enum(profile_update) String(single) Value(PROFILE_UPDATE_SINGLE)
+
+EnumValue
+Enum(profile_update) String(atomic) Value(PROFILE_UPDATE_ATOMIC)
+
 fprofile-generate
 Common
 Enable common options for generating profile info for profile feedback directed optimizations.
diff --git a/gcc/coretypes.h b/gcc/coretypes.h
index b3a91a6..fe1e984 100644
--- a/gcc/coretypes.h
+++ b/gcc/coretypes.h
@@ -174,6 +174,12 @@ enum offload_abi {
   OFFLOAD_ABI_ILP32
 };
 
+/* Types of profile update methods.  */
+enum profile_update {
+  PROFILE_UPDATE_SINGLE,
+  PROFILE_UPDATE_ATOMIC
+};
+
 /* Types of unwind/exception handling info that can be generated.  */
 
 enum unwind_info_type
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 22001f9..1cfaae7 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -9933,6 +9933,18 @@ the profile feedback data files. See @option{-fprofile-dir}.
 To optimize the program based on the collected profile information, use
 @option{-fprofile-use}.  @xref{Optimize Options}, for more information.
 
+@item -fprofile-update=@var{method}
+@opindex fprofile-update
+
+Alter the update method for an application instrumented for profile
+feedback based optimization.  The @var{method} argument should be one of
+@samp{single} or @samp{atomic}.  The first one is useful for single-threaded
+applications, while the second one prevents profile corruption by emitting
+thread-safe code.
+
+@strong{Warning:} When an application does not properly join all threads
+(or creates an detached thread), a profile file can be still corrupted.
+
 @item -fsanitize=address
 @opindex fsanitize=address
 Enable AddressSanitizer, a fast memory error detector.
diff --git a/gcc/gcov-io.h b/gcc/gcov-io.h
index bbf013a..afd00ac 100644
--- a/gcc/gcov-io.h
+++ b/gcc/gcov-io.h
@@ -164,6 +164,14 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #ifndef GCC_GCOV_IO_H
 #define GCC_GCOV_IO_H
 
+#if LONG_LONG_TYPE_SIZE > 32
+#define GCOV_TYPE_ATOMIC_FETCH_ADD_FN __atomic_fetch_add_8
+#define GCOV_TYPE_ATOMIC_FETCH_ADD BUILT_IN_ATOMIC_FETCH_ADD_8
+#else
+#define GCOV_TYPE_ATOMIC_FETCH_ADD_FN __atomic_fetch_add_4
+#define GCOV_TYPE_ATOMIC_FETCH_ADD BUILT_IN_ATOMIC_FETCH_ADD_4
+#endif
+
 #ifndef IN_LIBGCOV
 /* About the host */
 
diff --git a/gcc/testsuite/g++.dg/gcov/gcov-threads-1.C b/gcc/testsuite/g++.dg/gcov/gcov-threads-1.C
new file mode 100644
index 0000000..a4a6f0a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gcov/gcov-threads-1.C
@@ -0,0 +1,46 @@
+/* { dg-options "-fprofile-arcs -ftest-coverage -pthread -fprofile-update=atomic" } */
+/* { dg-do run { target native } } */
+
+#include <stdint.h>
+#include <pthread.h>
+#include <assert.h>
+
+#define NR 5
+
+pthread_mutex_t cndMs[NR];
+static void *ContentionNoDeadlock_thread(void *start)
+{
+  for (uint32_t k = 0; k < 100000; ++k)		/* count(500005) */
+    {
+      int starti = *(int*)start;		/* count(500000) */
+      for (uint32_t i = starti; i < NR; ++i) 
+	pthread_mutex_lock (&cndMs[i]);
+      for (int32_t i = NR - 1; i >= starti; --i)
+	pthread_mutex_unlock (&cndMs[i]);
+  }
+}
+int main(int argc, char **argv) {
+  for (unsigned i = 0; i < NR; i++)
+    cndMs[i] = PTHREAD_MUTEX_INITIALIZER;
+
+  pthread_t t[NR];
+  int ids[NR];
+
+  for (int i = 0; i < NR; i++)
+  {
+    ids[i] = i;
+    int r = pthread_create (&t[i], NULL, ContentionNoDeadlock_thread, &ids[i]);
+    assert (r == 0);				/* count(5) */
+  }
+
+  int ret;
+  for (int i = 0; i < NR; i++)
+    {
+      int r = pthread_join (t[i], (void**)&ret);
+      assert (r == 0);				/* count(5) */
+    }
+
+  return 0;					/* count(1) */
+}
+
+/* { dg-final { run-gcov gcov-threads-1.C } } */
diff --git a/gcc/tree-profile.c b/gcc/tree-profile.c
index 39fe15f..740f7ab 100644
--- a/gcc/tree-profile.c
+++ b/gcc/tree-profile.c
@@ -127,6 +127,7 @@ gimple_init_edge_profiler (void)
   tree ic_profiler_fn_type;
   tree average_profiler_fn_type;
   tree time_profiler_fn_type;
+  const char *profiler_fn_name;
 
   if (!gcov_type_node)
     {
@@ -180,11 +181,12 @@ gimple_init_edge_profiler (void)
 					  gcov_type_node,
 					  ptr_void,
 					  NULL_TREE);
+      profiler_fn_name = "__gcov_indirect_call_profiler_v2";
+      if (PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE))
+	profiler_fn_name = "__gcov_indirect_call_topn_profiler";
+
       tree_indirect_call_profiler_fn
-	      = build_fn_decl ( (PARAM_VALUE (PARAM_INDIR_CALL_TOPN_PROFILE) ?
-				 "__gcov_indirect_call_topn_profiler":
-				 "__gcov_indirect_call_profiler_v2"),
-			       ic_profiler_fn_type);
+	      = build_fn_decl (profiler_fn_name, ic_profiler_fn_type);
 
       TREE_NOTHROW (tree_indirect_call_profiler_fn) = 1;
       DECL_ATTRIBUTES (tree_indirect_call_profiler_fn)
@@ -241,22 +243,37 @@ gimple_init_edge_profiler (void)
 void
 gimple_gen_edge_profiler (int edgeno, edge e)
 {
-  tree ref, one, gcov_type_tmp_var;
-  gassign *stmt1, *stmt2, *stmt3;
+  tree one;
 
-  ref = tree_coverage_counter_ref (GCOV_COUNTER_ARCS, edgeno);
   one = build_int_cst (gcov_type_node, 1);
-  gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
-					  NULL, "PROF_edge_counter");
-  stmt1 = gimple_build_assign (gcov_type_tmp_var, ref);
-  gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
-					  NULL, "PROF_edge_counter");
-  stmt2 = gimple_build_assign (gcov_type_tmp_var, PLUS_EXPR,
-			       gimple_assign_lhs (stmt1), one);
-  stmt3 = gimple_build_assign (unshare_expr (ref), gimple_assign_lhs (stmt2));
-  gsi_insert_on_edge (e, stmt1);
-  gsi_insert_on_edge (e, stmt2);
-  gsi_insert_on_edge (e, stmt3);
+
+  if (flag_profile_update == PROFILE_UPDATE_ATOMIC)
+    {
+      /* __atomic_fetch_add (&counter, 1, MEMMODEL_RELAXED); */
+      tree addr = tree_coverage_counter_addr (GCOV_COUNTER_ARCS, edgeno);
+      gcall *stmt
+	= gimple_build_call (builtin_decl_explicit (GCOV_TYPE_ATOMIC_FETCH_ADD),
+			     3, addr, one,
+			     build_int_cst (integer_type_node,
+					    MEMMODEL_RELAXED));
+      gsi_insert_on_edge (e, stmt);
+    }
+  else
+    {
+      tree ref = tree_coverage_counter_ref (GCOV_COUNTER_ARCS, edgeno);
+      tree gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
+						   NULL, "PROF_edge_counter");
+      gassign *stmt1 = gimple_build_assign (gcov_type_tmp_var, ref);
+      gcov_type_tmp_var = make_temp_ssa_name (gcov_type_node,
+					      NULL, "PROF_edge_counter");
+      gassign *stmt2 = gimple_build_assign (gcov_type_tmp_var, PLUS_EXPR,
+					    gimple_assign_lhs (stmt1), one);
+      gassign *stmt3 = gimple_build_assign (unshare_expr (ref),
+					    gimple_assign_lhs (stmt2));
+      gsi_insert_on_edge (e, stmt1);
+      gsi_insert_on_edge (e, stmt2);
+      gsi_insert_on_edge (e, stmt3);
+    }
 }
 
 /* Emits code to get VALUE to instrument at GSI, and returns the
-- 
2.9.2


  reply	other threads:[~2016-08-05 13:43 UTC|newest]

Thread overview: 95+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-01  8:50 [PATCH 0/4] Various GCOV/PGO improvements marxin
2016-08-01  8:49 ` [PATCH 3/4] Fix typo in gcov.texi marxin
2016-08-01  8:50 ` [PATCH 4/4] Add tests for __gcov_dump and __gcov_reset marxin
2016-08-01  8:50 ` [PATCH 2/4] Remove __gcov_indirect_call_profiler marxin
2016-08-01  8:50 ` [PATCH 1/4] Cherry-pick fprofile-generate-atomic from google/gcc-4_9 branch marxin
2016-08-01 12:22   ` Nathan Sidwell
2016-08-01 13:29     ` Martin Liška
2016-08-04 14:48       ` Nathan Sidwell
2016-08-04 15:34         ` Martin Liška
2016-08-04 16:43           ` Nathan Sidwell
2016-08-04 17:03             ` Nathan Sidwell
2016-08-05  8:55               ` Martin Liška
2016-08-05 12:38                 ` Nathan Sidwell
2016-08-05 12:48                   ` Martin Liška
2016-08-05 13:14                     ` Nathan Sidwell
2016-08-05 13:43                       ` Martin Liška [this message]
2016-08-08 13:59                         ` [PATCH 5/N] Add new *_atomic counter update function, (-fprofile-update=atomic) Martin Liška
2016-08-08 15:24                           ` Nathan Sidwell
2016-08-08 16:51                             ` Martin Liška
2016-08-08 17:03                               ` Martin Liška
2016-08-09 12:36                                 ` Nathan Sidwell
2016-08-08 16:56                             ` [PATCH] Fix POW2 histogram Martin Liška
2016-08-09  8:41                               ` [PATCH 2/N] Fix usage of " Martin Liška
2016-08-09 12:37                                 ` Nathan Sidwell
2016-08-09 12:34                               ` [PATCH] Fix " Nathan Sidwell
2016-08-09 11:24                         ` [PATCH] Set -fprofile-update=atomic when -pthread is present Martin Liška
2016-08-09 12:40                           ` Nathan Sidwell
2016-08-09 19:04                           ` Andi Kleen
2016-08-12 13:31                             ` Martin Liška
2016-08-18  3:16                               ` Jeff Law
2016-08-18 11:02                                 ` Nathan Sidwell
2016-08-18 15:51                                 ` Andi Kleen
2016-08-18 15:53                                   ` Jeff Law
2016-10-03 12:13                                     ` Martin Liška
2016-10-03 12:26                                       ` Nathan Sidwell
2016-10-03 16:46                                         ` Jeff Law
2016-10-03 17:52                                           ` Andi Kleen
2016-10-04 12:05                                         ` Martin Liška
2016-10-05 17:54                                           ` Jeff Law
2016-10-13 15:34                                           ` [PATCH] Introduce -fprofile-update=maybe-atomic Martin Liška
2016-10-31  9:13                                             ` Martin Liška
2016-11-10 13:19                                               ` Martin Liška
2016-11-10 15:43                                                 ` Nathan Sidwell
2016-11-10 15:55                                                   ` David Edelsohn
2016-11-10 16:18                                                     ` Nathan Sidwell
2016-11-10 15:58                                                   ` Martin Liška
2016-11-10 16:17                                                     ` David Edelsohn
2016-11-10 16:24                                                       ` Martin Liška
2016-11-10 17:31                                                         ` Nathan Sidwell
2016-11-11 10:48                                                           ` Martin Liška
2016-11-11 15:11                                                             ` Nathan Sidwell
2016-11-10 16:14                                                   ` Nathan Sidwell
2016-11-10 16:16                                                     ` David Edelsohn
2016-08-18 15:54                                   ` [PATCH] Set -fprofile-update=atomic when -pthread is present Jakub Jelinek
2016-08-18 16:06                                     ` Richard Biener
2016-09-07 11:41                                       ` Martin Liška
     [not found]                                         ` <CAFiYyc0UaSzXhZmyG9QRkHGT4JFowxBfE2yb-NvXE=hR1xafdA@mail.gmail.com>
2016-09-15 10:18                                           ` [RFC] Speed-up -fprofile-update=atomic Martin Liška
2016-10-04  9:45                                             ` Richard Biener
2016-10-12 13:53                                               ` Martin Liška
2016-10-13  9:43                                                 ` Richard Biener
2016-10-17 11:47                                                   ` Martin Liška
     [not found]                                                     ` <CAFiYyc3eDT4g926PPZuktz5fEW=k-PibAcxhigx4GBcxoXNJFQ@mail.gmail.com>
2016-10-24 12:09                                                       ` Martin Liška
     [not found]                                                         ` <CAFiYyc1tSdTdqqkHcMp+dgE43+8tHL6kY8E07TCHoZBeUT-ggQ@mail.gmail.com>
2016-10-25 14:32                                                           ` Martin Liška
2016-10-26  9:29                                                             ` Richard Biener
2016-10-26  9:32                                                               ` Richard Biener
2016-08-18 16:04                                   ` [PATCH] Set -fprofile-update=atomic when -pthread is present Richard Biener
2016-08-10 12:57                         ` [PATCH 1/4] Cherry-pick fprofile-generate-atomic from google/gcc-4_9 branch Nathan Sidwell
2016-08-13 12:14                         ` [BUILDROBOT] avr broken (was: [PATCH 1/4] Cherry-pick fprofile-generate-atomic from google/gcc-4_9 branch) Jan-Benedict Glaw
     [not found]                           ` <4455937b-eba7-fe66-fe1a-3172567dd1e4@suse.cz>
2016-08-16 13:36                             ` [BUILDROBOT] avr broken Nathan Sidwell
     [not found]                               ` <617e8799-b7db-fefd-b3a3-842e9a7decfd@suse.cz>
2016-08-16 14:31                                 ` Nathan Sidwell
2016-08-16 17:05                                   ` Jan-Benedict Glaw
2016-08-16 18:26                                     ` Nathan Sidwell
2016-08-17  7:21                                       ` Denis Chertykov
2016-08-17  7:22                                         ` Martin Liška
2016-08-17  8:11                                       ` Jan-Benedict Glaw
2016-08-16 12:56                         ` [PATCH] Detect whether target can use -fprofile-update=atomic Martin Liška
2016-08-16 14:31                           ` Nathan Sidwell
2016-09-06 10:57                             ` Martin Liška
2016-09-06 11:17                               ` David Edelsohn
2016-09-06 12:15                                 ` Nathan Sidwell
2016-09-06 12:39                                   ` Jakub Jelinek
2016-09-06 12:43                                     ` David Edelsohn
2016-09-06 12:41                                   ` David Edelsohn
2016-09-06 12:51                                     ` Martin Liška
2016-09-06 13:13                                       ` Jakub Jelinek
2016-09-06 13:15                                         ` Martin Liška
2016-09-06 13:45                                           ` Jakub Jelinek
2016-09-06 13:50                                             ` Martin Liška
2016-09-06 14:06                                               ` Jakub Jelinek
2016-09-07  7:52                                               ` Christophe Lyon
2016-09-07  9:35                                                 ` Martin Liška
2016-09-07 16:06                                                   ` Christophe Lyon
2016-09-12 20:20                                                   ` Jeff Law
2016-09-29  8:31                                               ` Rainer Orth
2016-08-01 12:11 ` [PATCH 0/4] Various GCOV/PGO improvements Nathan Sidwell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=04a05835-4666-4d7d-c1a9-d4bcc4ea924a@suse.cz \
    --to=mliska@suse.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jh@suse.cz \
    --cc=nathan@acm.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).