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

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> ---
The bar() half of this is not specific to unions. It is reachable without any
type punning, at the ABI boundary:

    typedef unsigned _BitInt(9) u9;
    struct S { u9 b; };
    __attribute__((noipa)) unsigned f (struct S s) { return s.b; }
    int main (void)
    {
      struct S s;
      __builtin_memset (&s, 0xff, sizeof s);
      if (f (s) != 511)
        __builtin_abort ();
    }

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

On x86_64 this aborts at -O1 and above with 14.1.0, 16.2.0 and 17.0.0
(20261113); -O0 passes. f is just movzwl %di, %eax. Reading a field of a struct
returned by value (g ().b, with g filling the struct the same way) aborts even
at -O0 with 14.2.0 and trunk. Per the psABI the bits beyond N are unspecified
in registers, so the callee has to extend, as clang does.

Godbolt (struct argument, trunk): https://godbolt.org/z/nMj9r4hMP
Godbolt (struct return, trunk -O0): https://godbolt.org/z/xKGd8PjsE

The cause is in expand_expr_real_1's normal_inner_ref. EXTEND_BITINT is applied
only on the MEM path. When the containing object is a REG (struct argument or
return value, union rvalue), the extract_bit_field branch returns the
TYPE_SIZE-wide field without reducing it to the _BitInt precision. Adding

    +            if (mode != BLKmode && !AGGREGATE_TYPE_P (type))
    +              op0 = EXTEND_BITINT (op0);
                 return op0;

before that return fixes the struct argument/return cases and y in comment 0 at
-O0, on 14.2 and trunk sources. The -O1 failure of foo()/x is a different
expander site, the VIEW_CONVERT_EXPR case (the same one as PR115405 and
PR121693).

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

Reply via email to