public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: "Dominique d'Humières" <dominiq@tournesol.lps.ens.fr>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>,
	       Rainer Orth <ro@cebitec.uni-bielefeld.de>
Subject: [PATCH] Fix up affinity-fmt.c (was Re: [committed 0/4] (Partial) OpenMP 5.0 support for GCC 9)
Date: Fri, 09 Nov 2018 14:37:00 -0000	[thread overview]
Message-ID: <20181109143719.GL11582@tucnak> (raw)
In-Reply-To: <20181109110454.GA11625@tucnak>

Hi!

The earlier patch doesn't work, because there were still expressions
where handle could be still cast to integer of different size if it
happened to be a pointer, or an invalid cast of e.g. aggregate to
integer.

The following patch so far only tested on a simplified code should
handle it properly though, gomp_integral should yield an integral type
without a warning (for aggregates etc. 0, but that is what I wanted to
print, don't know what else to print if pthread_t is an aggregate I have no
idea what it contains).  Plus I've added some portability stuff for mingw
%llx vs. %I64x.

Can you please give it a whirl on Darwin and see what the
display-affinity-1.c testcase prints (in libgomp/testsuite/libgomp.log) ?

Thanks.

2018-11-09  Jakub Jelinek  <jakub@redhat.com>

	* affinity-fmt.c: Include inttypes.h if HAVE_INTTYPES_H.
	(gomp_display_affinity): Use __builtin_choose_expr to handle
	properly handle argument having integral, or pointer or some other
	type.  If inttypes.h is available and PRIx64 is defined, use PRIx64
	with uint64_t type instead of %llx and unsigned long long.

--- libgomp/affinity-fmt.c.jj	2018-11-08 18:08:01.412987460 +0100
+++ libgomp/affinity-fmt.c	2018-11-09 15:24:52.049169494 +0100
@@ -30,6 +30,9 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_INTTYPES_H
+# include <inttypes.h>  /* For PRIx64.  */
+#endif
 #ifdef HAVE_UNAME
 #include <sys/utsname.h>
 #endif
@@ -356,37 +359,42 @@ gomp_display_affinity (char *buffer, siz
 	  goto do_int;
 	case 'i':
 #if defined(LIBGOMP_USE_PTHREADS) && defined(__GNUC__)
-	  /* Handle integral pthread_t.  */
-	  if (__builtin_classify_type (handle) == 1)
-	    {
-	      char buf[3 * (sizeof (handle) + sizeof (int)) + 4];
-
-	      if (sizeof (handle) == sizeof (long))
-		sprintf (buf, "0x%lx", (long) handle);
-	      else if (sizeof (handle) == sizeof (long long))
-		sprintf (buf, "0x%llx", (long long) handle);
-	      else
-		sprintf (buf, "0x%x", (int) handle);
-	      gomp_display_num (buffer, size, &ret, zero, right, sz, buf);
-	      break;
-	    }
-	  /* And pointer pthread_t.  */
-	  else if (__builtin_classify_type (handle) == 5)
-	    {
-	      char buf[3 * (sizeof (uintptr_t) + sizeof (int)) + 4];
-
-	      if (sizeof (uintptr_t) == sizeof (long))
-		sprintf (buf, "0x%lx", (long) (uintptr_t) handle);
-	      else if (sizeof (uintptr_t) == sizeof (long long))
-		sprintf (buf, "0x%llx", (long long) (uintptr_t) handle);
-	      else
-		sprintf (buf, "0x%x", (int) (uintptr_t) handle);
-	      gomp_display_num (buffer, size, &ret, zero, right, sz, buf);
-	      break;
-	    }
+	  {
+	    char buf[3 * (sizeof (handle) + sizeof (uintptr_t) + sizeof (int))
+		     + 4];
+	    /* This macro returns expr unmodified for integral or pointer
+	       types and 0 for anything else (e.g. aggregates).  */
+#define gomp_nonaggregate(expr) \
+  __builtin_choose_expr (__builtin_classify_type (expr) == 1		    \
+			 || __builtin_classify_type (expr) == 5, expr, 0)
+	    /* This macro returns expr unmodified for integral types,
+	       (uintptr_t) (expr) for pointer types and 0 for anything else
+	       (e.g. aggregates).  */
+#define gomp_integral(expr) \
+  __builtin_choose_expr (__builtin_classify_type (expr) == 5,		    \
+			 (uintptr_t) gomp_nonaggregate (expr),		    \
+			 gomp_nonaggregate (expr))
+
+	    if (sizeof (gomp_integral (handle)) == sizeof (unsigned long))
+	      sprintf (buf, "0x%lx", (unsigned long) gomp_integral (handle));
+#if defined (HAVE_INTTYPES_H) && defined (PRIx64)
+	    else if (sizeof (gomp_integral (handle)) == sizeof (uint64_t))
+	      sprintf (buf, "0x" PRIx64, (uint64_t) gomp_integral (handle));
+#else
+	    else if (sizeof (gomp_integral (handle))
+		     == sizeof (unsigned long long))
+	      sprintf (buf, "0x%llx",
+		       (unsigned long long) gomp_integral (handle));
 #endif
+	    else
+	      sprintf (buf, "0x%x", (unsigned int) gomp_integral (handle));
+	    gomp_display_num (buffer, size, &ret, zero, right, sz, buf);
+	    break;
+	  }
+#else
 	  val = 0;
 	  goto do_int;
+#endif
 	case 'A':
 	  if (sz == (size_t) -1)
 	    gomp_display_affinity_place (buffer, size, &ret,


	Jakub

  parent reply	other threads:[~2018-11-09 14:37 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-09 10:34 [committed 0/4] (Partial) OpenMP 5.0 support for GCC 9 Dominique d'Humières
2018-11-09 11:05 ` Jakub Jelinek
2018-11-09 11:11   ` Jakub Jelinek
2018-11-09 14:49     ` David Malcolm
2018-11-09 14:37   ` Jakub Jelinek [this message]
2018-11-09 15:33     ` [PATCH] Fix up affinity-fmt.c (was Re: [committed 0/4] (Partial) OpenMP 5.0 support for GCC 9) Iain Sandoe
2018-11-09 15:40       ` Jakub Jelinek
2018-11-09 18:23         ` Iain Sandoe
2018-11-09 20:24           ` Jakub Jelinek

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=20181109143719.GL11582@tucnak \
    --to=jakub@redhat.com \
    --cc=dominiq@tournesol.lps.ens.fr \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ro@cebitec.uni-bielefeld.de \
    /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).