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

--- Comment #1 from liu zhenyu <lzy20000419 at outlook dot com> ---
The GIMPLE_CALL case of gimple_could_trap_p_1 (gcc/gimple.cc:2418-2443 on
current master):

```c
        if (gimple_call_internal_p (s))
          return false;
        ...
        /* Any floating point builtin operation could trap.  */
        if (gimple_call_builtin_p (s))
          {
            ... copysign: return false ...
            tree type = TREE_TYPE (gimple_call_fntype (s));
            bool fp_operation = FLOAT_TYPE_P (type);
            return fp_operation && flag_trapping_math;
          }
        return false;
```

Only the return type is checked. lrint (double) returns long, so the call is
treated as not trapping,
and if-conversion makes it unconditional (ifcvt dump, f):

```
  _6 = _1 != 0;
  _2 = in[i_13];
  _3 = __builtin_lrint (_2);
  .MASK_STORE (_23, 64B, _6, _3);
```

The vectorizer then emits `.LRINT (vect__2)` unmasked. This is not the case
where a conditional operation is unsupported: AVX-512 has a masked vcvtpd2qq
({%k}) and SVE has predicated frintx/fcvtzs,
but since the call is classified as non-trapping, no mask is asked for. Note
there are no COND_ IFNs for LRINT/IRINT/LLRINT/LFLOOR/... in internal-fn.def
today, so after a fix if-conversion would simply keep the branch (or would need
a new COND_LRINT etc. to vectorize it masked).

On aarch64 with SVE (-O3 -march=armv9-a -fno-math-errno, 14.2/15.2/trunk) the
same loop is not vectorized, so I could not show it there.

Possible fix: in the builtin case also return flag_trapping_math when any
argument is FLOAT_TYPE_P (keeping the existing copysign-style exclusions for
builtins that cannot raise). As Richard noted in PR122103 c27, a call to a
regular function also returns false here, which is not right either, but it
only matters when the function is const or pure. Internal calls also return
false unconditionally,
so .LRINT etc. created by the vectorizer would be seen as non-trapping too.

Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.

Reply via email to