public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore
@ 2024-02-02 15:24 bouanto at zoho dot com
  2024-02-02 16:12 ` [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: bouanto at zoho dot com @ 2024-02-02 15:24 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113722
           Summary: Folding of __builtin_bswap128 doesn't work anymore
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: regression
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bouanto at zoho dot com
  Target Milestone: ---

With the following code:

#include <stdio.h>

int main(int argc, char* argv[]) {
    __uint128_t res = __builtin_bswap128 (2);
    printf("Value: %ld\n", res >> 64);
    res = __builtin_bswap128 (argc + 1);
    printf("Value: %ld\n", res >> 64);
}

it gives the following output in gcc 13.2.1:

Value: 144115188075855872
Value: 144115188075855872

while it gives the following output in the master branch, commit
2c27aa8d75113f404bf9cd39364611af386d9719:

Value: 0
Value: 144115188075855872

So it seems to be a regression in the folding of wide_int::bswap.

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

* [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken
  2024-02-02 15:24 [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore bouanto at zoho dot com
@ 2024-02-02 16:12 ` pinskia at gcc dot gnu.org
  2024-02-02 16:50 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-02 16:12 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|regression                  |middle-end
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2024-02-02
            Summary|Folding of                  |[14 Regression] Constant
                   |__builtin_bswap128 doesn't  |folding of
                   |work anymore                |__builtin_bswap128 is
                   |                            |broken
           Keywords|                            |wrong-code
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |14.0

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Simplified testcase:
```
#define constant 2
int main() {
    __uint128_t res = __builtin_bswap128 ((__uint128_t)constant);
    __uint128_t t = constant;
    __uint128_t res1 = __builtin_bswap128 (t);
    if (res != res1)
      __builtin_abort();
}
```

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

* [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken
  2024-02-02 15:24 [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore bouanto at zoho dot com
  2024-02-02 16:12 ` [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken pinskia at gcc dot gnu.org
@ 2024-02-02 16:50 ` jakub at gcc dot gnu.org
  2024-02-02 16:57 ` [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken since r14-1455 jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-02 16:50 UTC (permalink / raw)
  To: gcc-bugs

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

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> ---
The #c0 testcase is broken, res >> 64 has still __uint128_t type, so you
shouldn't pass it to %ld format specifier.

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

* [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken since r14-1455
  2024-02-02 15:24 [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore bouanto at zoho dot com
  2024-02-02 16:12 ` [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken pinskia at gcc dot gnu.org
  2024-02-02 16:50 ` jakub at gcc dot gnu.org
@ 2024-02-02 16:57 ` jakub at gcc dot gnu.org
  2024-02-02 17:30 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-02 16:57 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[14 Regression] Constant    |[14 Regression] Constant
                   |folding of                  |folding of
                   |__builtin_bswap128 is       |__builtin_bswap128 is
                   |broken                      |broken since r14-1455
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
           Priority|P3                          |P1

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Anyway, at -O0
int
main ()
{
  unsigned __int128 a = __builtin_bswap128 ((unsigned __int128) 2);
  unsigned __int128 b = 2;
  unsigned __int128 c = __builtin_bswap128 (b);
  if (a != c)
    __builtin_abort ();
}
regressed with r14-1455-g0ede6b5ad77c4791a513ab312b9e278dafc2bff9
I'll have a look.

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

* [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken since r14-1455
  2024-02-02 15:24 [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore bouanto at zoho dot com
                   ` (2 preceding siblings ...)
  2024-02-02 16:57 ` [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken since r14-1455 jakub at gcc dot gnu.org
@ 2024-02-02 17:30 ` jakub at gcc dot gnu.org
  2024-02-03 13:39 ` cvs-commit at gcc dot gnu.org
  2024-02-03 19:45 ` jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-02 17:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 57296
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57296&action=edit
gcc14-pr113722.patch

Untested fix.

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

* [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken since r14-1455
  2024-02-02 15:24 [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore bouanto at zoho dot com
                   ` (3 preceding siblings ...)
  2024-02-02 17:30 ` jakub at gcc dot gnu.org
@ 2024-02-03 13:39 ` cvs-commit at gcc dot gnu.org
  2024-02-03 19:45 ` jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-02-03 13:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:09df058a09f888daad26fa80634068b38b4ad04d

commit r14-8776-g09df058a09f888daad26fa80634068b38b4ad04d
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sat Feb 3 14:38:27 2024 +0100

    wide-int: Fix up wi::bswap_large [PR113722]

    Since bswap has been converted from a method to a function we miscompile
    the following testcase.  The problem is the assumption that the passed in
    len argument (number of limbs in the xval array) is the upper bound for the
    bswap result, which is true only if precision is <= 64.  If precision is
    larger than that, e.g. 128 as in the testcase, if the argument has only
    one limb (i.e. 0 to ~(unsigned HOST_WIDE_INT) 0), the result can still
    need 2 limbs for that precision, or generally BLOCKS_NEEDED (precision)
    limbs, it all depends on how many least significant limbs of the operand
    are zero.  bswap_large as implemented only cleared len limbs of result,
    then swapped the bytes (invoking UB when oring something in all the limbs
    above it) and finally passed len to canonize, saying that more limbs
    aren't needed.

    The following patch fixes it by renaming len to xlen (so that it is clear
    it is X's length), using it solely for safe_uhwi argument when we attempt
    to read from X, and using new len = BLOCKS_NEEDED (precision) instead in
    the other two spots (i.e. when clearing the val array, turned it also
    into memset, and in canonize argument).  wi::bswap asserts it isn't invoked
    on widest_int, so we are always invoked on wide_int or similar and those
    have preallocated result sized for the corresponding precision (i.e.
    BLOCKS_NEEDED (precision)).

    2024-02-03  Jakub Jelinek  <jakub@redhat.com>

            PR middle-end/113722
            * wide-int.cc (wi::bswap_large): Rename third argument from
            len to xlen and adjust use in safe_uhwi.  Add len variable, set
            it to BLOCKS_NEEDED (precision) and use it for clearing of val
            and as canonize argument.  Clear val using memset instead of
            a loop.

            * gcc.dg/pr113722.c: New test.

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

* [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken since r14-1455
  2024-02-02 15:24 [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore bouanto at zoho dot com
                   ` (4 preceding siblings ...)
  2024-02-03 13:39 ` cvs-commit at gcc dot gnu.org
@ 2024-02-03 19:45 ` jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-02-03 19:45 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2024-02-03 19:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-02 15:24 [Bug regression/113722] New: Folding of __builtin_bswap128 doesn't work anymore bouanto at zoho dot com
2024-02-02 16:12 ` [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken pinskia at gcc dot gnu.org
2024-02-02 16:50 ` jakub at gcc dot gnu.org
2024-02-02 16:57 ` [Bug middle-end/113722] [14 Regression] Constant folding of __builtin_bswap128 is broken since r14-1455 jakub at gcc dot gnu.org
2024-02-02 17:30 ` jakub at gcc dot gnu.org
2024-02-03 13:39 ` cvs-commit at gcc dot gnu.org
2024-02-03 19:45 ` jakub 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).