<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/62260>62260</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Miscompilation of union containing __fp16 and _BitInt for big-endian targets
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            miscompilation
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ostannard
      </td>
    </tr>
</table>

<pre>
    clang generates incorrect code for function foo in this example, at all optimisation levels, and when targeting any big-endian architecture:

```c
union U1 {
  __fp16 M0;
  _BitInt(28) M1;
};

union U2 {
  __fp16 M0;
  int M1;
};

union U1 foo() {
  union U1 result = {1.0};
  return result;
}

union U2 bar() {
  union U2 result = {1.0};
 return result;
}
```

The encoding of 1.0 in FP16 is 0x3c00 (checked with https://evanw.github.io/float-toy/), so that must appear in the return value somewhere, but for foo, that value gets shifted and truncated to 0xc00:

```
$ /work/llvm/build/bin/clang --target=arm-none-eabi -march=armv7-a -c test.c -o - -S -O1 -mbig-endian
...
foo:
 .fnstart
        mov     r0, #-1073741824
        bx lr
...
bar:
        .fnstart
        mov     r0, #1006632960
 bx      lr
...
```

The generated code for AArch64 is similar:
```
$ /work/llvm/build/bin/clang --target=aarch64-none-eabi -c test.c -o - -S -O1 -mbig-endian
...
foo: // @foo
// %bb.0:                               // %entry
 mov     x0, #-4611686018427387904       // =0xc000000000000000
 ret
...
bar:                                    // @bar
// %bb.0: // %entry
        mov     x0, #4323455642275676160        // =0x3c00000000000000
 ret
...
```

Powerpc (big-endian) also has the same wrong constants in the output:
```
$ /work/llvm/build/bin/clang --target=powerpc -c test.c -o - -S -O1
...
foo: # @foo
.Lfunc_begin0:
        .cfi_startproc
# %bb.0: # %entry
        lis 3, -16384
...
 .type   bar,@function
bar:                                    # @bar
.Lfunc_begin1:
        .cfi_startproc
# %bb.0: # %entry
        lis 3, 15360
        blr
...
```
</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