public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/97004] New: [nvptx] __int128 initializer incorrect
@ 2020-09-09 22:39 vries at gcc dot gnu.org
  2020-09-10 13:24 ` [Bug target/97004] " vries at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: vries at gcc dot gnu.org @ 2020-09-09 22:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97004

            Bug ID: 97004
           Summary: [nvptx] __int128 initializer incorrect
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vries at gcc dot gnu.org
  Target Milestone: ---

Consider this code from c-c++-common/spec-barrier-1.c:
...
#ifdef __SIZEOF_INT128__
__int128 g = 9;
#endif
...

We seem to be generating:
...
// BEGIN GLOBAL VAR DEF: g
.visible .global .align 8 .u64 g[2] =
{9,9 };
...

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

* [Bug target/97004] [nvptx] __int128 initializer incorrect
  2020-09-09 22:39 [Bug target/97004] New: [nvptx] __int128 initializer incorrect vries at gcc dot gnu.org
@ 2020-09-10 13:24 ` vries at gcc dot gnu.org
  2020-09-10 13:34 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: vries at gcc dot gnu.org @ 2020-09-10 13:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97004

--- Comment #1 from Tom de Vries <vries at gcc dot gnu.org> ---
Tentative patch:
...
diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c
index 0376ad6ce9f..26868590322 100644
--- a/gcc/config/nvptx/nvptx.c
+++ b/gcc/config/nvptx/nvptx.c
@@ -2054,7 +2054,11 @@ nvptx_assemble_value (unsigned HOST_WIDE_INT val,
unsigned size)

   for (unsigned part = 0; size; size -= part)
     {
-      val >>= part * BITS_PER_UNIT;
+      if (part == sizeof (val))
+       /* Avoid undefined behaviour.  */
+       val = 0;
+      else
+       val >>= (part * BITS_PER_UNIT);
       part = init_frag.size - init_frag.offset;
       part = MIN (part, size);

...

And we have:
...
// BEGIN GLOBAL VAR DEF: g
        .visible .global .align 8 .u64 g[2] = { 9, 0 };
...

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

* [Bug target/97004] [nvptx] __int128 initializer incorrect
  2020-09-09 22:39 [Bug target/97004] New: [nvptx] __int128 initializer incorrect vries at gcc dot gnu.org
  2020-09-10 13:24 ` [Bug target/97004] " vries at gcc dot gnu.org
@ 2020-09-10 13:34 ` jakub at gcc dot gnu.org
  2020-09-10 19:30 ` cvs-commit at gcc dot gnu.org
  2020-09-10 19:33 ` vries at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-09-10 13:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97004

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Tom de Vries from comment #1)
> Tentative patch:
> ...
> diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c
> index 0376ad6ce9f..26868590322 100644
> --- a/gcc/config/nvptx/nvptx.c
> +++ b/gcc/config/nvptx/nvptx.c
> @@ -2054,7 +2054,11 @@ nvptx_assemble_value (unsigned HOST_WIDE_INT val,
> unsigned size)
>  
>    for (unsigned part = 0; size; size -= part)
>      {
> -      val >>= part * BITS_PER_UNIT;
> +      if (part == sizeof (val))

That assumes that CHAR_BIT == BITS_PER_UNIT, which is mostly the case, but
shouldn't be relied on.
Perhaps just use
val >>= (part * BITS_PER_UNIT / 2);
val >>= (part * BITS_PER_UNIT / 2);
or if (part * BITS_PER_UNIT == sizeof (val) * CHAR_BIT)
Though, as val is unsigned HOST_WIDE_INT, we already have a macro for that,
so if (part * BITS_PER_UNIT == HOST_BITS_PER_WIDE_INT) ?

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

* [Bug target/97004] [nvptx] __int128 initializer incorrect
  2020-09-09 22:39 [Bug target/97004] New: [nvptx] __int128 initializer incorrect vries at gcc dot gnu.org
  2020-09-10 13:24 ` [Bug target/97004] " vries at gcc dot gnu.org
  2020-09-10 13:34 ` jakub at gcc dot gnu.org
@ 2020-09-10 19:30 ` cvs-commit at gcc dot gnu.org
  2020-09-10 19:33 ` vries at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-10 19:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97004

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Tom de Vries <vries@gcc.gnu.org>:

https://gcc.gnu.org/g:af47a2035a4882e6d4506e3d00b5a42414e3ee2b

commit r11-3127-gaf47a2035a4882e6d4506e3d00b5a42414e3ee2b
Author: Tom de Vries <tdevries@suse.de>
Date:   Thu Sep 10 15:54:14 2020 +0200

    [nvptx] Fix printing of 128-bit constant

    Currently, for this code from c-c++-common/spec-barrier-1.c:
    ...
    __int128 g = 9;
    ...
    we generate:
    ...
    // BEGIN GLOBAL VAR DEF: g
    .visible .global .align 8 .u64 g[2] = { 9, 9 };
    ...
    and consequently the test-case fails in execution.

    The problem is caused by a shift in nvptx_assemble_value:
    ...
          val >>= part * BITS_PER_UNIT;
    ...
    where the shift amount is equal to the number of bits in val, which is
    undefined behaviour.

    Fix this by detecting the situation and setting val to 0.

    Tested on nvptx.

    gcc/ChangeLog:

            PR target/97004
            * config/nvptx/nvptx.c (nvptx_assemble_value): Handle shift by
            number of bits in shift operand.

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

* [Bug target/97004] [nvptx] __int128 initializer incorrect
  2020-09-09 22:39 [Bug target/97004] New: [nvptx] __int128 initializer incorrect vries at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2020-09-10 19:30 ` cvs-commit at gcc dot gnu.org
@ 2020-09-10 19:33 ` vries at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: vries at gcc dot gnu.org @ 2020-09-10 19:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97004

Tom de Vries <vries at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|UNCONFIRMED                 |RESOLVED
   Target Milestone|---                         |11.0

--- Comment #4 from Tom de Vries <vries at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #2)
> (In reply to Tom de Vries from comment #1)
> > Tentative patch:
> > ...
> > diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c
> > index 0376ad6ce9f..26868590322 100644
> > --- a/gcc/config/nvptx/nvptx.c
> > +++ b/gcc/config/nvptx/nvptx.c
> > @@ -2054,7 +2054,11 @@ nvptx_assemble_value (unsigned HOST_WIDE_INT val,
> > unsigned size)
> >  
> >    for (unsigned part = 0; size; size -= part)
> >      {
> > -      val >>= part * BITS_PER_UNIT;
> > +      if (part == sizeof (val))
> 
> That assumes that CHAR_BIT == BITS_PER_UNIT, which is mostly the case, but
> shouldn't be relied on.
> Perhaps just use
> val >>= (part * BITS_PER_UNIT / 2);
> val >>= (part * BITS_PER_UNIT / 2);
> or if (part * BITS_PER_UNIT == sizeof (val) * CHAR_BIT)
> Though, as val is unsigned HOST_WIDE_INT, we already have a macro for that,
> so if (part * BITS_PER_UNIT == HOST_BITS_PER_WIDE_INT) ?

I went with the last suggestion, thanks for the review.

Patch committed to trunk, no test-case required.

Marking resolved-fixed.

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

end of thread, other threads:[~2020-09-10 19:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-09 22:39 [Bug target/97004] New: [nvptx] __int128 initializer incorrect vries at gcc dot gnu.org
2020-09-10 13:24 ` [Bug target/97004] " vries at gcc dot gnu.org
2020-09-10 13:34 ` jakub at gcc dot gnu.org
2020-09-10 19:30 ` cvs-commit at gcc dot gnu.org
2020-09-10 19:33 ` vries at gcc dot gnu.org

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).