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

liu zhenyu <lzy20000419 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lzy20000419 at outlook dot com

--- Comment #2 from liu zhenyu <lzy20000419 at outlook dot com> ---
Here is a smaller testcase and the place where it goes wrong. It is the
lost-copy (swap) problem of PHI elimination in the _BitInt lowering pass, as
described in comment 1.

    typedef unsigned _BitInt(256) B;

    [[gnu::noipa]] B
    f (B x, B y, unsigned n)
    {
      B a = x, b = y;
      for (unsigned i = 0; i < n; i++)
        {
          B t = a;
          a = b;
          b = t;
        }
      return a * 3 + b;
    }

    int
    main ()
    {
      if (f (1, 2, 2) != 5)
        __builtin_abort ();
      if (f (1, 2, 1) != 7)
        __builtin_abort ();
      return 0;
    }

    $ gcc -O1 t.c && ./a.out
    Aborted

-O0 passes. -O1 and above abort with 14.1.0, 16.2.0 and 17.0.0 (20261113).

Godbolt (trunk): https://godbolt.org/z/q9MPGjTzo

Before bitintlower the IL is

      # a_15 = PHI <b_17(3), x_6(D)(2)>
      # b_17 = PHI <a_15(3), y_7(D)(2)>

and the copies that gimple_lower_bitint inserts on the latch edge are

      VIEW_CONVERT_EXPR<unsigned long[4]>(x) = VIEW_CONVERT_EXPR<unsigned
long[4]>(y);
      VIEW_CONVERT_EXPR<unsigned long[4]>(y) = VIEW_CONVERT_EXPR<unsigned
long[4]>(x);

Coalescing rightly puts a_15 and b_17 in different partitions (x and y here),
because they conflict. But the PHI lowering loop at the end of
gimple_lower_bitint (the for (gphi_iterator gsi = gsi_start_phis (bb); ...)
loop after the statement lowering) emits one gsi_insert_on_edge copy per PHI
argument, in PHI order. PHIs are a parallel copy, so when the source partition
of one argument is the destination partition of an earlier PHI on the same
edge, the second copy reads the value the first one just wrote.
tree-outof-ssa.cc handles this with the elim_graph in eliminate_phi, but the
bitint pass does not sequence its copies. A 3-variable rotation and the prev =
cur; cur = next; loop in comment 0 fail the same way. After loop header copying
the loop-bypass edge has the same pair of copies, so f (1, 2, 0) returns 4 too.

A minimal fix that works for me on 14.2: in that loop, before emitting the
copies for a block, go over each incoming edge, and for every SSA argument
whose source m_vars[] is also the destination of some large/huge PHI in that
block, first copy the source into a fresh limb-array temporary (inserted on the
edge before all the other copies), then use the temporary as the source of the
normal copy. With that change the testcase above, a 3-cycle rotation and a
Fibonacci loop pass at -O1 to -O3. The PR127904 and PR127905 tests still fail
on 14.2 without their own fixes, so this is a separate problem. I have not
tested the change on trunk.

Suggested fields: Known to fail 14.1.0, 16.2.0, 17.0.

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.

Reply via email to