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

            Bug ID: 128481
           Summary: [14/15/16/17 Regression] ICE: SSA corruption (Unable
                    to coalesce ... MUST COALESCE) with store-merging and
                    setjmp
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: ice-on-valid-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

```c
#include <setjmp.h>
jmp_buf jb; void usei (unsigned);
struct S { unsigned a : 4, b : 4, c : 8; };
void g1 (struct S *p, unsigned x)
{
  setjmp (jb);
  usei (x);
  p->a = x;
  x = x * 3 + 1;
  p->b = x;
  p->c = 7;
  usei (x);
}
```

```
$ gcc -O2 -c t.c
Unable to coalesce ssa_names 5 and 14 which are marked as MUST COALESCE.
x_5(ab) and  x_14(ab)
during RTL pass: expand
t.c: In function 'g1':
t.c:4:6: internal compiler error: SSA corruption
```

A second variant through the bswap/nop path of the same pass:

```c
#include <setjmp.h>
jmp_buf jb; void usei (unsigned);
void g2 (unsigned char *p, unsigned x)
{
  setjmp (jb);
  usei (x);
  p[0] = x;
  x = x >> 8;
  p[1] = x;
  x = x >> 8;
  p[2] = x;
  x = x >> 8;
  p[3] = x;
  usei (x);
}
```

A third variant goes through the bswap pass in the same file (fails since at
least 7.5.0; -fno-expensive-optimizations avoids it, -fno-store-merging does
not):

```c
#include <setjmp.h>
jmp_buf jb; void usei (unsigned);
unsigned g5 (unsigned x)
{
  setjmp (jb);
  usei (x);
  unsigned a = x & 0xff; x = x >> 8;
  unsigned b = x & 0xff; x = x >> 8;
  unsigned c = x & 0xff; x = x >> 8;
  unsigned r = a << 24 | b << 16 | c << 8 | x;
  usei (x);
  return r;
}
```

-fno-store-merging avoids the first two. The store-merging pass emits the
merged store at the last original store and reuses the SSA value stored by the
first store (an abnormal name) after a later abnormal version has been defined,
so the two abnormal SSA names get overlapping live ranges and out-of-SSA cannot
coalesce them. process_store (gimple-ssa-store-merging.cc) accepts a stored SSA
rhs through find_bswap_or_nop_1 or the BIT_INSERT_EXPR fallback with no
SSA_NAME_OCCURS_IN_ABNORMAL_PHI check on that value / bswap source (the file
has none); find_bswap_or_nop / bswap_replace likewise do not check the
symbolic-number source. Same shape as PR125502 / PR128236 (forwprop moving a
def of an abnormal name), but in store-merging / bswap rather than forwprop.

Rechecked on trunk 17.0.0 20261105 (local b76d14f773; godbolt cgsnapshot
bd921def14a4). ICE at -O2/-O3/-Os on 14.2.0, 15.3.0, 16.2.0/16.2.1 and trunk
for all three; -O1 is fine (store-merging not enabled). Compiler Explorer
matrix for sm1 at -O2: OK on 6.3.0/7.5.0/8.5.0 and ICE from 9.1.0 through
trunk; sm2 ICE on 8.5.0 and trunk; sm5 ICE on 7.5.0 and trunk. Godbolt (sm1 at
-O2 on trunk, 16.2, 14.2 and 8.5; 8.5 works): https://godbolt.org/z/6sPM85KKn

A possible one-line fix (not tested) is to reject the store in process_store
when the stored SSA rhs / bswap source / BIT_INSERT value has
SSA_NAME_OCCURS_IN_ABNORMAL_PHI set (and the same for the bswap pass source).

Related: PR125502 and PR128236 are the same kind of abnormal-PHI live-range
problem after a transform reuses an earlier abnormal SSA name past a later
definition, but in forwprop; a fix for either does not cover store-merging /
bswap. PR128463 is store-merging wrong-code with malloc effective-type, not
this ICE.

Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.
  • [Bug tree-optimization/128481]... lzy20000419 at outlook dot com via Gcc-bugs

Reply via email to