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

            Bug ID: 129083
           Summary: VEC_COND_EXPR != {0,...} with constant arms folded to
                    true; libstdc++ vector<bool> assign_range/append_range
                    fail on riscv64
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: frederic.mahe at cirad dot fr
  Target Milestone: ---
              Host: riscv64-unknown-linux-gnu
            Target: x86_64-pc-linux-gnu

Created attachment 66302
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66302&action=edit
GIMPLE testcase (-O2 -fgimple)

fold_binary_op_with_conditional_arg sinks a vector equality compare
(scalar boolean result) into the arms of a VEC_COND_EXPR. Each arm
folds on its own, both are "!= 0", so the whole compare becomes true:

    t_3 = m_2 ? { -1, -1, 0, -1 } : { 0, 0, -1, 0 };
    if (t_3 != { 0, 0, 0, 0 })

    forwprop1: Replaced 't_3 != { 0, 0, 0, 0 }' with '1'

But t_3 is all zeros when m_2 is { 0, 0, -1, 0 }. The non-COND path of
that function already bails out when the operand is a vector and the
result is scalar (PR68542); the COND/VEC_COND path does not.

Testcase, -O2 -fgimple:

    typedef int v4si __attribute__((vector_size (4 * sizeof (int))));
    typedef v4si v4sib __attribute__((vector_mask));

    /* Returns 1 iff (x != 0) == {0,0,1,0} in every lane.  */
    int __GIMPLE (ssa) __attribute__((noipa))
    f (v4si x)
    {
      v4sib m;
      v4sib t;

    __BB(2):
      m_2 = x_1(D) != _Literal (v4si) { 0, 0, 0, 0 };
      t_3 = m_2 ? _Literal (v4sib) { _Literal (int) -1, _Literal (int) -1, 0,
_Literal (int) -1 } : _Literal (v4sib) { 0, 0, _Literal (int) -1, 0 };
      if (t_3 != _Literal (v4sib) { 0, 0, 0, 0 })
        goto __BB3;
      else
        goto __BB4;

    __BB(3):
      return 0;

    __BB(4):
      return 1;
    }

    int
    main (void)
    {
      v4si x = { 0, 0, 5, 0 };
      if (f (x) != 1)
        __builtin_abort ();
      return 0;
    }

Aborts with GCC 15.2 on x86_64 (default, -msse4.1, -mavx2) and with
trunk on riscv64 (-march=rv64gc, rv64gcv, rv64gcv_zvl256b).

    $ gcc -O2 -fgimple testcase.c && ./a.out
    Aborted

The early-break vectorizer produces this pattern. On riscv64 with
--with-arch=rv64gcv_..._zvl256b (native, SpacemiT K1) these fail:

    23_containers/vector/bool/modifiers/assign/assign_range.cc
    23_containers/vector/bool/modifiers/insert/append_range.cc

The tests' eq() lambda (`l[i] != r[i]`, early exit, known trip count)
is vectorized, cunroll unrolls it, and forwprop4 folds the exit test
to the failure branch. They pass with -fno-tree-vectorize, and with
-march=rv64gcv (VLEN 128).

A patch (below) is bootstrapped and regtested on riscv64 with no new
failures; it fixes the testcase and both libstdc++ tests. I will send it
to gcc-patches with this PR number. With the patch, the testcase needs
-msse4.1 on x86_64: without SSE4.1 there is no cbranch for the vector
compare, and expand ICEs on the hand-written GIMPLE (the vectorizer only
emits such compares when the target supports them).

    --- a/gcc/fold-const.cc
    +++ b/gcc/fold-const.cc
    @@ -7043,6 +7043,11 @@ fold_binary_op_with_conditional_arg (location_t loc,
       if (TREE_CODE (cond) == COND_EXPR
           || TREE_CODE (cond) == VEC_COND_EXPR)
         {
    +      /* Do not sink an operation with a scalar result, such as an
    +    equality comparison of two vectors, into the arms of a
    +    VEC_COND_EXPR.  */
    +      if (TREE_CODE (cond) == VEC_COND_EXPR && !VECTOR_TYPE_P (type))
    +   return NULL_TREE;
           test = TREE_OPERAND (cond, 0);
           true_value = TREE_OPERAND (cond, 1);
           false_value = TREE_OPERAND (cond, 2);

Compiler (native riscv64, gcc -v):

    Target: riscv64-unknown-linux-gnu
    Configured with: /srv/gcc/src/configure --enable-languages=c,c++
      --disable-multilib
--with-arch=rv64gcv_zba_zbb_zbc_zbs_zicond_zfh_zvfh_zvl256b
      --with-abi=lp64d
    gcc version 17.0.0 20261120 (experimental) (GCC)   [728d0bebb85]
    x86_64: gcc version 15.2.0 (Ubuntu 15.2.0-16ubuntu1)

Found and reduced with help from Claude (Anthropic); checked by hand on
hardware.

Reply via email to