Issue 239148
Summary [SelectionDAG] expandVECTOR_COMPRESS element stores claim the scalar ABI alignment at StackPtr + k*EltBytes: compress of <3 x i48> drops a lane (x86-64/AArch64/s390x) or SIGBUS (MIPS)
Labels miscompilation, llvm:SelectionDAG, generated by fuzzer
Assignees
Reporter YuanchengJiang
    The following code:

```llvm
; vector.compress <3 x i48>: lane 1 lost at -O1+ (x86-64, AArch64, s390x); SIGBUS on mips64el (O0 and O2)
; llc -O2 -mtriple=x86_64-linux-gnu repro.ll ; clang -O0 repro.s -o r && ./r ; echo $?   (expected 0)
@In = global [3 x i64] [i64 u0x5e7f, i64 u0xf3da76, i64 u0x1e7566d]
@M = global [3 x i8] [i8 1, i8 1, i8 0]
@O = global [3 x i64] zeroinitializer

define void @f(ptr %pi, ptr %pm, ptr %po) noinline {
  %a = load <3 x i64>, ptr %pi, align 8
 %t = trunc <3 x i64> %a to <3 x i48>
  %mb = load <3 x i8>, ptr %pm, align 1
  %m = trunc <3 x i8> %mb to <3 x i1>
  %c = call <3 x i48> @llvm.experimental.vector.compress.v3i48(<3 x i48> %t, <3 x i1> %m, <3 x i48> zeroinitializer)
  %s = zext <3 x i48> %c to <3 x i64>
  store <3 x i64> %s, ptr %po, align 8
  ret void
}

define i32 @main() {
  call void @f(ptr @In, ptr @M, ptr @O)
  %q1 = getelementptr i64, ptr @O, i64 1
  %l1 = load i64, ptr %q1
  %ok = icmp eq i64 %l1, u0xf3da76     ; compress({a,b,c},{1,1,0},0) = {a,b,0}
  %r = select i1 %ok, i32 0, i32 1
 ret i32 %r
}
```

Resulted in this output:
```
$ llc -O2 -mtriple=x86_64-linux-gnu repro.ll -filetype=obj -o r.o && gcc -no-pie r.o -o r && ./r; echo $?
1
```
The expected exit code is `0`: `compress({a,b,c}, {1,1,0}, 0)` must be `{a, b, 0}`. At -O1 and -O2 lane 1 comes back as 0 (the passthru value), on x86-64, AArch64, s390x and Hexagon. On mips64el and mipsel the program dies with SIGBUS at both -O0 and -O2, because `sw` is used at FI+6. `<7 x i24>` (compress-v7i24*.ll) loses lane 1 on x86-64 and on all i686 configurations at -O2.

Trunk: https://godbolt.org/z/bPf5351d4 (llc trunk -O2, x86-64 and AArch64 on compress-v3i48-func.ll: lane 1 of the result is never reloaded from the stack slot).

Root cause: `TargetLowering::expandVECTOR_COMPRESS` (llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp) stores each element at `StackPtr + OutPos * EltBytes`. It also loads and stores `LastWriteVal` at such addresses. All three accesses use `MachinePointerInfo::getUnknownStack(MF)` without an alignment, so they default to the ABI alignment of the element type: align 8 for i48 (6-byte elements) and align 4 for i24 (3-byte elements). Those claims are false at odd multiples of the element size.
- With this claim, DAGCombiner's "relatively aligned" no-alias rule in `mayAlias` sees a 6-byte reload of lane 1 at offset 6 (base alignment 8) and a 6-byte element store with base alignment 8. It decides they cannot overlap, so the reload is forwarded from the earlier passthru store.
- On MIPS the same claim selects word accesses at 2-aligned addresses.
- #200622 fixed the alignment of the whole-vector passthru store and the result load, but not these element accesses.

Suggested fix: give these accesses only the alignment common to the slot and the element size (fix.diff):
```diff
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ SDValue TargetLowering::expandVECTOR_COMPRESS(SDNode *Node,
+  // Elements are accessed at StackPtr + k * EltBytes: only the alignment
+  // common to the slot and the element size is known.
+  Align EltAlign = commonAlignment(Alignment, ScalarVT.getStoreSize());
   MachinePointerInfo PtrInfo =
@@
     LastWriteVal = DAG.getLoad(
         ScalarVT, DL, Chain, LastElmtPtr,
- MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
+ MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltAlign);
@@
     Chain = DAG.getStore(
         Chain, DL, ValI, OutPtr,
- MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
+ MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltAlign);
@@
       Chain = DAG.getStore(
           Chain, DL, LastWriteVal, OutPtr,
- MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
+ MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltAlign);
```
With the fix, the repro exits 0 on x86-64 at -O1 and -O2, and compress-v3i48.ll prints `5e7f f3da76 0` on AArch64, s390x and mips64el (no SIGBUS). The 26 llc RUN lines of the in-tree vector.compress tests give identical output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to