public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/113614] New: wrong code with _BitInt() division at -O1
@ 2024-01-26  6:55 zsojka at seznam dot cz
  2024-01-26 14:23 ` [Bug tree-optimization/113614] " jakub at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: zsojka at seznam dot cz @ 2024-01-26  6:55 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113614
           Summary: wrong code with _BitInt() division at -O1
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zsojka at seznam dot cz
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu

Created attachment 57220
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57220&action=edit
reduced testcase

Output:
$ x86_64-pc-linux-gnu-gcc -O1 testcase.c
$ ./a.out
Aborted

The wrong result is -39, as if the division were with signed types.

$ x86_64-pc-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/repo/gcc-trunk/binary-latest-amd64/bin/x86_64-pc-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/repo/gcc-trunk/binary-trunk-r14-8419-20240125172014-gc6c2a1d79eb-checking-yes-rtl-df-extra-nobootstrap-amd64/bin/../libexec/gcc/x86_64-pc-linux-gnu/14.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /repo/gcc-trunk//configure --enable-languages=c,c++
--enable-valgrind-annotations --disable-nls --enable-checking=yes,rtl,df,extra
--disable-bootstrap --with-cloog --with-ppl --with-isl
--build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu
--target=x86_64-pc-linux-gnu --with-ld=/usr/bin/x86_64-pc-linux-gnu-ld
--with-as=/usr/bin/x86_64-pc-linux-gnu-as --disable-libstdcxx-pch
--prefix=/repo/gcc-trunk//binary-trunk-r14-8419-20240125172014-gc6c2a1d79eb-checking-yes-rtl-df-extra-nobootstrap-amd64
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.0.1 20240125 (experimental) (GCC)

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

* [Bug tree-optimization/113614] wrong code with _BitInt() division at -O1
  2024-01-26  6:55 [Bug tree-optimization/113614] New: wrong code with _BitInt() division at -O1 zsojka at seznam dot cz
@ 2024-01-26 14:23 ` jakub at gcc dot gnu.org
  2024-01-26 18:19 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-01-26 14:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I guess the thing is that while using for a signed operand a positive precision
(if smaller than the precision of the signed operand) is always fine,
indicating zero extension from something narrower, for unsigned operand a
negative precision (if smaller in absolute value than the precision of the
unsigned operand) might be fine for __mulbitint3 (but even then perhaps only if
the result precision isn't higher than the precision of the operands), for
other libgcc _BitInt operations, including __divmodbitint4 but also the
conversions from _BitInt to binary/decimal floating point really consider an
operand with negative precision as signed with precision equal to the absolute
value of the argument, so there is no way to represent something that has been
sign extended from narrower precision and then cast to unsigned (we'd need to
tell libgcc two numbers somehow, from which smaller precision it is sign
extended and to which wider precision where it becomes unsigned).
But, guess this means we need to make sure such widening casts are not merged
with the libgcc operations.

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

* [Bug tree-optimization/113614] wrong code with _BitInt() division at -O1
  2024-01-26  6:55 [Bug tree-optimization/113614] New: wrong code with _BitInt() division at -O1 zsojka at seznam dot cz
  2024-01-26 14:23 ` [Bug tree-optimization/113614] " jakub at gcc dot gnu.org
@ 2024-01-26 18:19 ` jakub at gcc dot gnu.org
  2024-01-27 12:07 ` cvs-commit at gcc dot gnu.org
  2024-01-27 12:11 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-01-26 18:19 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
   Last reconfirmed|                            |2024-01-26

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

Untested fix.

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

* [Bug tree-optimization/113614] wrong code with _BitInt() division at -O1
  2024-01-26  6:55 [Bug tree-optimization/113614] New: wrong code with _BitInt() division at -O1 zsojka at seznam dot cz
  2024-01-26 14:23 ` [Bug tree-optimization/113614] " jakub at gcc dot gnu.org
  2024-01-26 18:19 ` jakub at gcc dot gnu.org
@ 2024-01-27 12:07 ` cvs-commit at gcc dot gnu.org
  2024-01-27 12:11 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-27 12:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 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:a12b0e9360e88fceb0414bfb34c8c1ad87c5ac90

commit r14-8468-ga12b0e9360e88fceb0414bfb34c8c1ad87c5ac90
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Sat Jan 27 13:06:55 2024 +0100

    lower-bitint: Avoid sign-extending cast to unsigned types feeding
div/mod/float [PR113614]

    The following testcase is miscompiled, because some narrower value
    is sign-extended to wider unsigned _BitInt used as division operand.
    handle_operand_addr for that case returns the narrower value and
    precision -prec_of_narrower_value.  That works fine for multiplication
    (at least, normal multiplication, but we don't merge casts with
    .MUL_OVERFLOW or the ubsan multiplication right now), because the
    result is the same whether we treat the arguments as signed or unsigned.
    But is completely wrong for division/modulo or conversions to
    floating-point, if we pass negative prec for an input operand of a libgcc
    handler, those treat it like a negative number, not an unsigned one
    sign-extended from something smaller (and it doesn't know to what precision
    it has been extended).

    So, the following patch fixes it by making sure we don't merge such
    sign-extensions to unsigned _BitInt type with division, modulo or
    conversions to floating point.

    2024-01-27  Jakub Jelinek  <jakub@redhat.com>

            PR tree-optimization/113614
            * gimple-lower-bitint.cc (gimple_lower_bitint): Don't merge
            widening casts from signed to unsigned types with TRUNC_DIV_EXPR,
            TRUNC_MOD_EXPR or FLOAT_EXPR uses.

            * gcc.dg/torture/bitint-54.c: New test.

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

* [Bug tree-optimization/113614] wrong code with _BitInt() division at -O1
  2024-01-26  6:55 [Bug tree-optimization/113614] New: wrong code with _BitInt() division at -O1 zsojka at seznam dot cz
                   ` (2 preceding siblings ...)
  2024-01-27 12:07 ` cvs-commit at gcc dot gnu.org
@ 2024-01-27 12:11 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2024-01-27 12:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

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

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

end of thread, other threads:[~2024-01-27 12:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-26  6:55 [Bug tree-optimization/113614] New: wrong code with _BitInt() division at -O1 zsojka at seznam dot cz
2024-01-26 14:23 ` [Bug tree-optimization/113614] " jakub at gcc dot gnu.org
2024-01-26 18:19 ` jakub at gcc dot gnu.org
2024-01-27 12:07 ` cvs-commit at gcc dot gnu.org
2024-01-27 12:11 ` 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).