[Bug tree-optimization/116098] [14 Regression] _Bool value from tagged union is incorrect when built with optimization since r14-1597-g64d90d06d2db43

2025-04-14 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116098

Andrew Pinski  changed:

   What|Removed |Added

 Resolution|--- |FIXED
 Status|ASSIGNED|RESOLVED

--- Comment #27 from Andrew Pinski  ---
Fixed now for GCC 14.3.0.  The optimization (r15-3334) was not backported
though.

[Bug tree-optimization/116098] [14 Regression] _Bool value from tagged union is incorrect when built with optimization since r14-1597-g64d90d06d2db43

2025-04-14 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116098

--- Comment #26 from GCC Commits  ---
The releases/gcc-14 branch has been updated by Andrew Pinski
:

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

commit r14-11605-gc2c1046b83a92283cd0863942efe9df453d78a78
Author: Andrew Pinski 
Date:   Tue Oct 1 18:34:00 2024 +

phiopt: Fix VCE moving by rewriting it into cast [PR116098]

Phiopt match_and_simplify might move a well defined VCE assign statement
from being conditional to being uncondtitional; that VCE might no longer
being defined. It will need a rewrite into a cast instead.

This adds the rewriting code to move_stmt for the VCE case.
This is enough to fix the issue at hand. It should also be using
rewrite_to_defined_overflow
but first I need to move the check to see a rewrite is needed into its own
function
and that is causing issues (see
https://gcc.gnu.org/pipermail/gcc-patches/2024-September/663938.html).
Plus this version is easiest to backport.

Bootstrapped and tested on x86_64-linux-gnu.

PR tree-optimization/116098

gcc/ChangeLog:

* tree-ssa-phiopt.cc (move_stmt): Rewrite VCEs from integer to
integer
types to case.

gcc/testsuite/ChangeLog:

* c-c++-common/torture/pr116098-2.c: New test.
* g++.dg/torture/pr116098-1.C: New test.

Signed-off-by: Andrew Pinski 
(cherry picked from commit 1f619fe25925a5f79b9c33962e7a72e1f9fa)

[Bug tree-optimization/116098] [14 Regression] _Bool value from tagged union is incorrect when built with optimization since r14-1597-g64d90d06d2db43

2024-10-30 Thread laria at laria dot me via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116098

--- Comment #25 from Laria Chabowski  ---
Sorry for the late reply, I have now checked the current trunk with the program
where I originally saw this bug. It's fixed now. Many thanks!

[Bug tree-optimization/116098] [14 Regression] _Bool value from tagged union is incorrect when built with optimization since r14-1597-g64d90d06d2db43

2024-10-02 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116098

Andrew Pinski  changed:

   What|Removed |Added

Summary|[14/15 Regression] _Bool|[14 Regression] _Bool value
   |value from tagged union is  |from tagged union is
   |incorrect when built with   |incorrect when built with
   |optimization since  |optimization since
   |r14-1597-g64d90d06d2db43|r14-1597-g64d90d06d2db43

--- Comment #24 from Andrew Pinski  ---
Fixed fully on the trunk. Will wait a week or 2 before backporting it.

[Bug tree-optimization/116098] [14 Regression] _Bool value from tagged union is incorrect when built with optimization since r14-1597-g64d90d06d2db43

2024-09-06 Thread laria at laria dot me via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116098

--- Comment #15 from Laria Chabowski  ---
(In reply to Andrew Pinski from comment #14)
> Fixed so far on the trunk.

Thank you! That fixed the test cases I provided.

However, when I tested it against the code where I stumbled upon this, I still
got an unexpected result.

Essentially the same idea: A function truthy() that switches over the type
enum,
returns the bool value if it's bool or always false if the type is nil or true
in all other cases. Then, inverting it and saving and reading it as a value
struct again, some garbage comes out and when using the resulting bool in a
ternary, the opposite of the expected happens.

I've reduced that code to this:

=== BEGIN test.c ===
int puts(const char *);

struct Value {
enum value_type {
VALUE_NIL,
VALUE_BOOLEAN,
VALUE_MAGIC,
} type;
union {
_Bool boolean;
void *p[2];
};
};

static struct Value s_item_mem;

static void
set_bool(_Bool b)
{
s_item_mem = (struct Value) {
.type = VALUE_BOOLEAN,
.boolean = b,
};
}

static void
set_magic(void)
{
s_item_mem = (struct Value) {
.type = VALUE_MAGIC,
.p[0] = (void *)0x7fff0123456789a0, // just something that vaguely
looks
// like a pointer. In the original,
// this was a valid pointer
returned
// from malloc. But since this
// reduced code never reads from
// here it shouldn't matter that
// it's not an actual address.
};
}

static struct Value
val_get(void)
{
// returning s_item_mem immediately makes the bug go away
struct Value value = s_item_mem;
return value;
}

static _Bool
truthy(void)
{
// Inlinig val_get makes the bug go away
struct Value value = val_get();
switch (value.type) {
case VALUE_NIL: // Removing this case makes the bug go away
return 0;
case VALUE_BOOLEAN:
return value.boolean;
default:
return 1;
}
}

int
main(void)
{
set_magic(); // sets type to VALUE_MAGIC
set_bool(!truthy()); // truthy should take default case, so
 // set_bool(!1) -> set_bool(0)

_Bool b = truthy(); // Should be 0 now
puts(b ? "true" : "false"); // Should print false, prints true
return b ? 0 : 1; // Exit code should be 1, is 161
}
=== END test.c ===

I built it with:

gcc -Wall -Werror -Wextra -pedantic -O2 -fno-strict-aliasing -fwrapv \
-fno-aggressive-loop-optimizations test.c

Happens both with the GCC installed on my Fedora 40 machine:

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/14/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --with-linker-hash-style=gnu
--enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-14.2.1-20240801/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none,amdgcn-amdhsa --enable-offload-defaulted
--without-cuda-driver --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.1 20240801 (Red Hat 14.2.1-1) (GCC)

And with the one I built a couple of days ago from the git trunk, that includes
the fix (HEAD was at ce5f2dc45038c9806088132cc923b13719f48732 when I built it.
git log from here includes the commit ceda727dafba6 with the fix):

$ ~/local/gcc/usr/local/bin/x86_64-linux-gcc -v
Using built-in specs.
COLLECT_GCC=/home/laria/local/gcc/usr/local/bin/x86_64-linux-gcc
   
COLLECT_LTO_WRAPPER=/home/laria/local/gcc/usr/local/bin/../libexec/gcc/x86_64-linux/15.0.0/lto-wrapper
Target: x86_64-linux
Configured with: ../configure --target=x86_64-linux --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 15.0.0 20240903 (experimental) (GCC)

Also reproducible on godbolt.org, which apparently uses this version for trunk:

g++
(Compiler-Explorer-Build-gcc-17

[Bug tree-optimization/116098] [14 Regression] _Bool value from tagged union is incorrect when built with optimization since r14-1597-g64d90d06d2db43

2024-08-31 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116098

Andrew Pinski  changed:

   What|Removed |Added

  Known to fail||14.1.0, 14.2.0
Summary|[14/15 Regression] _Bool|[14 Regression] _Bool value
   |value from tagged union is  |from tagged union is
   |incorrect when built with   |incorrect when built with
   |optimization since  |optimization since
   |r14-1597-g64d90d06d2db43|r14-1597-g64d90d06d2db43
  Known to work||13.1.0, 13.3.0, 15.0

--- Comment #14 from Andrew Pinski  ---
Fixed so far on the trunk.