Issue 237720
Summary [CodeGenPrepare] splitMergedValStore addresses the upper half at the half type's alloc size: an i48 store split into two i24 stores writes one byte too far and clobbers the next field
Labels backend:X86, llvm:codegen, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```cpp
/* [CodeGenPrepare] splitMergedValStore splits `store i48 (or (zext L), (shl (zext H), 24))` into two i24 stores and
 * addresses the upper half with `getelementptr i24, ptr %p, 1`, i.e. at the i24 ALLOC size (4 bytes) instead of
 * its store size (3 bytes): the upper 3 bytes are written to p+4..p+6 instead of p+3..p+5.
 * Triggered on x86-64 (and RISC-V) when one half is a bitcast FP value (isMultiStoresCheaperThanBitsMerge).
 *
 * Here: a 48-bit bitfield is assigned (bits of a _Float16) | (s << 24).  Byte 3 keeps its old contents and the
 * neighbouring field `c` (byte 6) is overwritten with 0.
 *
 * Godbolt: compiler "x86-64 clang (trunk)", language C, flags: -O2 (also -O1; -O0 is correct)
 *          "Execute the code" works (plain x86-64).
 * Asm of f (buggy):  movw %ax,(%rdi) ; movb $0,2(%rdi) ; movw %si,4(%rdi) ; movb $0,6(%rdi)
 *          (correct would store s at 3(%rdi) and 0 at 5(%rdi), and never touch 6(%rdi)).
 * EXPECTED (correct compiler, clang -O0):
 *   v = 0x1234003c00 (expected 0x1234003c00), c = 0x11 (expected 0x11), d = 0x22 (expected 0x22)
 *   OK (exit 0)
 * BUGGY (vanilla clang trunk c4a7814f8bc3, -O1/-O2):
 *   v = 0x1234ff003c00 (expected 0x1234003c00), c = 0x00 (expected 0x11), d = 0x22 (expected 0x22)
 *   MISMATCH (exit 1)
 */
#include <stdio.h>
typedef unsigned _BitInt(48) u48;
struct S { unsigned long long v : 48; unsigned char c, d; };

__attribute__((noinline)) void f(struct S *p, float x, unsigned short s) {
  p->v = (u48)__builtin_bit_cast(unsigned short, (_Float16)x) | ((u48)s << 24);
}

volatile float in_h = 1.0f;            /* bits 0x3c00 */
volatile unsigned short in_s = 0x1234;

int main(void) {
  struct S x = { 0xffffffffffffull, 0x11, 0x22 };
  f(&x, in_h, in_s);
  unsigned long long want = 0x3c00ull | (0x1234ull << 24);
  printf("v = 0x%010llx (expected 0x%010llx), c = 0x%02x (expected 0x11), d = 0x%02x (expected 0x22)\n",
         (unsigned long long)x.v, want, x.c, x.d);
  if (x.v != want || x.c != 0x11 || x.d != 0x22) { puts("MISMATCH"); return 1; }
 puts("OK");
  return 0;
}
```

Resulted in this output:
```
$ clang -O2 repro.c && ./a.out
v = 0x1234ff003c00 (expected 0x1234003c00), c = 0x00 (expected 0x11), d = 0x22 (expected 0x22)
MISMATCH
(exit 1)
$ clang -O0 repro.c && ./a.out
v = 0x1234003c00 (expected 0x1234003c00), c = 0x11 (expected 0x11), d = 0x22 (expected 0x22)
OK
```

| field | expected | actual (-O1/-O2) |
|-------|----------|------------------|
| v (48-bit bitfield) | 0x1234003c00 | 0x1234ff003c00 (byte 3 not written) |
| c (byte 6) | 0x11 | 0x00 (clobbered) |

`f` at -O2: `movw %ax,(%r14); movb $0,2(%r14); movw %bx,4(%r14); movb $0,6(%r14)`. The upper half belongs at 3(%r14)..5(%r14).

Trunk: https://godbolt.org/z/9n9sdosTq (x86-64 clang trunk, -O2, compiles and executes; prints MISMATCH as of 2026-10-23)

Root cause: `CodeGenPrepare::splitMergedValStore` (llvm/lib/CodeGen/CodeGenPrepare.cpp) splits `store (or (zext L), (shl (zext H), N/2)), p` into two half-width stores. It does this on x86-64 and RISC-V when one half is a bitcast FP value (`isMultiStoresCheaperThanBitsMerge`). It addresses the upper half with `CreateGEP(SplitStoreType, Addr, 1)`, which strides by the alloc size of the half type. The guards only check `typeSizeEqualsStoreSize`, which holds for i24, but i24 has store size 3 and alloc size 4. So for an i48 store the upper i24 lands at p+4..p+6 instead of p+3..p+5. The same happens for i80 (two i40) and i96 (two i48).

Suggested fix: Address the upper half with a byte offset of `HalfValBitSize / 8` (`Builder.CreatePtrAdd`) instead of a GEP over `SplitStoreType`, or bail out unless `DL.getTypeAllocSize(SplitStoreType) * 8 == HalfValBitSize`. The first option is validated locally (a local tree carries this fix, and the reproducer then prints OK).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to