Issue 243194
Summary [VectorCombine] shrinkLoadForShuffles narrows a load to a non-byte-sized vector type (`<32 x i1>` → `<1 x i1>`)
Labels new issue
Assignees
Reporter arie2026
    `shrinkLoadForShuffles` shrinks a load that only feeds shuffles to the lanes the shuffles use. For vectors of
sub-byte elements the new type can have a size that is not a whole number of bytes. Here a 4-byte `<32 x i1>` load is
replaced by a `<1 x i1>` load:

```llvm
define void @f(ptr %p) {
  %vec = load <32 x i1>, ptr %p, align 4
  %s = shufflevector <32 x i1> %vec, <32 x i1> poison, <32 x i32> zeroinitializer
  store <32 x i1> %s, ptr %p, align 4
  ret void
}
```

`opt -passes=vector-combine` (no target flags needed; same with x86-64 and AArch64 triples):

```llvm
define void @f(ptr %p) {
  %1 = load <1 x i1>, ptr %p, align 4
  %s = shufflevector <1 x i1> %1, <1 x i1> poison, <32 x i32> zeroinitializer
  store <32 x i1> %s, ptr %p, align 4
  ret void
}
```

LangRef (`load`): "When loading a value of a type like i20 with a size that is not an integral number of bytes, the
result is undefined if the value was not originally written using a store of the same type." The source reads a
4-byte vector; the target reads a 1-bit value from memory that was not written as `<1 x i1>`, so the result is
undefined. alive2 agrees:

```console
$ alive-tv test.ll tgt.ll
ERROR: Mismatch in memory
...
Source:
<32 x i1> %vec = < #x1 (1), poison, ... >
<32 x i1> %s = < #x1 (1), #x1 (1), ... >
Target:
<1 x i1> %#1 = < poison >
<32 x i1> %s = < poison, poison, ... >
```

It is still wrong when the memory is fully defined, e.g. after `store i32 %x, ptr %p` with `noundef %x` in front of
the load. The `<8 x i4>` → `<1 x i4>` variant is also rejected.

Every other load/store narrowing in VectorCombine.cpp checks this case with `DL->typeSizeEqualsStoreSize(...)`
(`foldSingleElementStore`, the `scalarizeLoad*` folds); `shrinkLoadForShuffles` has no such check. A minimal fix
would be to bail out when the element type is not byte-sized (or when the new vector's bit size is not a multiple of
8).

Severity seems low: on x86-64 the generated code reads bit 0 correctly, and running the original and the optimized
function under `lli` gave identical results for several byte patterns. So this is an IR-semantics issue that later
passes are entitled to exploit, not an observed wrong-code bug. #185099 reported a different problem (wrong offset) in
the low-end trim of `<N x i1>` loads that was later reverted; this one is in the remaining shrink with offset 0.

Found with a mutation-based IR fuzzer. Triage, test-case minimization and parts of this write-up were assisted by
an LLM; verified the reproducer on b23814056.

Assisted-by: Claude (Anthropic)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to