Issue 243458
Summary [X86][GlobalISel] Stores of non-power-of-2 integer types write past their store size
Labels backend:X86, miscompilation, llvm:globalisel
Assignees
Reporter chfast
    
On x86 with GlobalISel, a store of an integer type whose width is not a power of 2 writes the whole widened register.
- **Past the store size:** `store i17` writes 4 bytes instead of 3, `store i33` writes 8 instead of 5, and `store i65` writes 16 instead of 9. So it overwrites the bytes that follow.
- **Padding not zeroed:** `store i3` writes the register's byte as is. LangRef says a non-byte-sized store "behaves like a `zext` followed by a byte-sized store", so the padding bits should be zero.

SelectionDAG gets both right.

```llvm
; Each function first stores a marker byte right after the store size of
; the narrow type, then stores the narrow value.
define void @store_i33(ptr %p, i64 %x) {
  %q = getelementptr i8, ptr %p, i64 5
  store i8 7, ptr %q
  %v = trunc i64 %x to i33
  store i33 %v, ptr %p
  ret void
}

define void @store_i17(ptr %p, i32 %x) {
  %q = getelementptr i8, ptr %p, i64 3
  store i8 7, ptr %q
  %v = trunc i32 %x to i17
  store i17 %v, ptr %p
  ret void
}

define void @store_i3(ptr %p, i8 %x) {
  %v = trunc i8 %x to i3
  store i3 %v, ptr %p
  ret void
}

define void @store_i65(ptr %p, i128 %x) {
  %q = getelementptr i8, ptr %p, i64 9
 store i8 7, ptr %q
  %v = trunc i128 %x to i65
  store i65 %v, ptr %p
 ret void
}
```

`llc -mtriple=x86_64-linux-gnu -O2 -global-isel -global-isel-abort=1`:

```asm
store_i33:
        movb    $7, 5(%rdi)
 movq    %rsi, (%rdi)
        retq
store_i17:
        movb    $7, 3(%rdi)
        movl    %esi, (%rdi)
        retq
store_i3:
        movb %sil, (%rdi)
        retq
store_i65:
        movb    $7, 9(%rdi)
 movq    %rsi, (%rdi)
        movq    %rdx, 8(%rdi)
 retq
```

This driver fills a buffer with 0xAA, then calls each function with all-ones values:

```c
#include <stdio.h>
#include <string.h>
void store_i33(unsigned char *p, unsigned long x);
void store_i17(unsigned char *p, unsigned x);
void store_i3(unsigned char *p, unsigned char x);
void store_i65(unsigned char *p, unsigned __int128 x);
static void dump(const char *name, const unsigned char *b) {
  printf("%-9s", name);
  for (int i = 0; i < 16; ++i) printf(" %02x", b[i]);
  printf("\n");
}
int main(void) {
  unsigned char b[16];
  memset(b, 0xAA, 16); store_i33(b, ~0ul); dump("store_i33", b);
  memset(b, 0xAA, 16); store_i17(b, ~0u); dump("store_i17", b);
  memset(b, 0xAA, 16); store_i3(b, 0xFF); dump("store_i3", b);
  memset(b, 0xAA, 16); store_i65(b, ~(unsigned __int128)0); dump("store_i65", b);
  return 0;
}
```

`clang main.c store.s` with `store.s` from SelectionDAG (`llc` without `-global-isel`):

```
store_i33 ff ff ff ff 01 07 aa aa aa aa aa aa aa aa aa aa
store_i17 ff ff 01 07 aa aa aa aa aa aa aa aa aa aa aa aa
store_i3 07 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
store_i65 ff ff ff ff ff ff ff ff 01 07 aa aa aa aa aa aa
```

With GlobalISel:

```
store_i33 ff ff ff ff ff ff ff ff aa aa aa aa aa aa aa aa
store_i17 ff ff ff ff aa aa aa aa aa aa aa aa aa aa aa aa
store_i3  ff aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
store_i65 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
```

The cause is in the G_STORE rules in `X86LegalizerInfo.cpp`:
1. `widenScalarToNextPow2(0, /*Min=*/8)` widens the value, e.g. s33 to s64.
2. The memory type stays s33, so `customIf(Types[0] != MemoryTy)` matches.
3. `legalizeNarrowingStore()` then replaces the memory type with the value type, so the store becomes a full s64 store.

The generic `LegalizerHelper::lowerStore` handles a memory type that is not byte-sized or not a power of 2: it zero-extends to the store size and splits the store. The custom rule bypasses that. The rule came with #130445.

GlobalISel is not the default on x86, so this needs `-global-isel`. This is with trunk e0b437836a21.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to