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

            Bug ID: 129075
           Summary: [16/17 Regression] wrong code at -O2 after vectorized
                    element move, FRE folds vector lane to 0
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aressem at gmail dot com
  Target Milestone: ---

The testcase below aborts at -O2 and -O3 on aarch64-linux-gnu with GCC
16.2.0, the releases/gcc-16 branch (888cd4d1bb) and trunk (2e6a851c691),
and at -O2 on x86_64-linux-gnu with 16.2.0 and trunk (f8fb076af5).  It
passes with 15.3.0 on both targets.  On aarch64 it passes at -O1 and with
-fno-tree-vectorize or -fdisable-tree-fre5 with all three, and with
-fno-strict-aliasing with 16.2.0 and trunk.

$ cat pr.C
// { dg-do run }
// { dg-options "-O2" }

typedef __SIZE_TYPE__ size_t;
inline void *operator new (size_t, void *p) noexcept { return p; }

struct V
{
  long *s = nullptr, *f = nullptr;
  V () = default;
  V (long *b, long *e) : s (b), f (e) {}
  V (const V &o)
  {
    size_t n = o.f - o.s;
    s = n ? (long *) __builtin_operator_new (n * sizeof (long)) : nullptr;
    if (n)
      __builtin_memcpy (s, o.s, n * sizeof (long));
    f = s + n;
  }
  V (V &&o) : s (o.s), f (o.f) {}
};

struct VV
{
  V *s = nullptr, *f = nullptr;
  void push_back (const V &x)
  {
    size_t n = f - s;
    V *ns = (V *) __builtin_operator_new ((n + 1) * sizeof (V));
    new (ns + n) V (x);
    for (V *p = s, *d = ns; p != f; ++p, ++d)
      new (d) V (static_cast<V &&> (*p));
    s = ns;
    f = ns + n + 1;
  }
};

__attribute__((noipa)) long *
f (const V &a, const V &b)
{
  VV v;
  v.push_back (a);
  v.push_back (b);
  v.push_back (a);
  return v.s[1].s;
}

int
main ()
{
  long x = 2;
  V a, b (&x, &x + 1);
  if (f (a, b) == nullptr)
    __builtin_abort ();
}

$ g++ -O2 pr.C && ./a.out
Aborted (core dumped)

f is compiled to "return 0B;".  The move loop in push_back is vectorized
(one 16-byte vector per V), and FRE after vectorization value-numbers
the lane holding the moved s to 0 (trunk, -O2 -fdump-tree-fre5-details):

  Value numbering stmt = _77 = BIT_FIELD_REF <vect__21.15_91, 64, 0>;
  Setting value number of _77 to 0B (changed)

v.s[1] was copy-constructed from b, where s is nullptr on the n == 0
path and the operator new result otherwise.  FRE appears to use the
nullptr from the n == 0 path and miss the store on the other.  As
-fno-strict-aliasing hides it, the alias query between the vector load
and that store may be at fault.

-Wall -Wextra -Wstrict-aliasing=1 gives no warnings and
-fsanitize=address,undefined reports nothing.  -fwrapv and
-fno-aggressive-loop-optimizations do not help.  -fno-strict-aliasing
does, but the testcase does no type punning: the V objects are created
with placement new in storage from __builtin_operator_new and only
accessed as V, and the longs only as long.

This was reduced from std::vector<std::vector<long>> growth, which is
miscompiled the same way by 16.2.0 at -O2.  With the GCC 16 libstdc++
that case passes on trunk at -O2 since r17-5574 (1b0ba4c6a52) but still
fails at -O3.  Possibly related: PR128714 (alias info on vector refs),
but trunk with that fix still fails.

$ g++ -v
Target: aarch64-unknown-linux-gnu
Configured with: ../src/configure --disable-bootstrap --disable-multilib
--enable-languages=c,c++
gcc version 17.0.0 20261125 (experimental) (GCC)

Reply via email to