Issue 234278
Summary [clang] `combineShiftToPMULH`: `lshr(mul(sext a, sext b), 16)` on i64 lanes becomes `zext(pmulhw)`, dropping the sign bits of negative products (miscompile from C, `-O1`+)
Labels backend:X86, miscompilation, generated by fuzzer
Assignees
Reporter YuanchengJiang
    ## C reproducer — `repro.c`
```c
#include <stdio.h>
typedef short v8s __attribute__((vector_size(16)));
typedef long long v8l __attribute__((vector_size(64)));
typedef unsigned long long v8ul __attribute__((vector_size(64)));

/* (a * b) >> 16 on the sign-extended 64-bit product, as an unsigned (logical) shift */
__attribute__((noinline)) v8ul mulh_lshr(v8s a, v8s b) {
  v8l p = __builtin_convertvector(a, v8l) * __builtin_convertvector(b, v8l);
  return (v8ul)p >> 16;
}

int main(void) {
  v8s a = {-1, -108, 300, -32768, 7, -2, 12345, -30000};
  v8s b = {108, 1, -200, -32768, 9, 3, -2, 30000};
  v8ul r = mulh_lshr(a, b);
  int ok = 1;
  for (int i = 0; i < 8; i++) {
    unsigned long long want = (unsigned long long)((long long)a[i] * b[i]) >> 16;
    printf("%llx ", r[i]);
    ok &= r[i] == want;
  }
  printf("(%s)\n", ok ? "correct" : "WRONG: negative products must keep their sign bits 16..47");
  return !ok;
}
```

| compiler | output |
|---|---|
| `clang -O0` | `ffffffffffff ffffffffffff ffffffffffff 4000 0 ffffffffffff ffffffffffff ffffffffca5b` (correct) |
| `clang -O1` / `-O2` / `-O2 -mavx2` | **`ffff ffff ffff 4000 0 ffff ffff ca5b` (WRONG)** |
| `clang -O2` with the fix below | correct |

Trunk: https://godbolt.org/z/dPdn6zh1Y

The output also has a `-Wpsabi` note about returning a 512-bit vector without AVX-512; that's unrelated, and the
same happens with the result stored to memory.

## IR / test case
`llvm/test/CodeGen/X86/pmulh.ll`, `@sext_mulhsw_v8i16_lshr_i64`:
```llvm
  %a1 = sext <8 x i16> %a to <8 x i64>
  %b1 = sext <8 x i16> %b to <8 x i64>
  %c = mul <8 x i64> %a1, %b1
  %d = lshr <8 x i64> %c, splat (i64 16)
  ret <8 x i64> %d
```
Its CHECK lines expect `pmulhw` followed by a zero-extension, i.e. the miscompiled code.

`h.ll` wraps it with negative products:
- **llubi** and a Python reference: `ffffffffffff …`;
- **x86 with SSE2 / AVX2, run natively:** `ffff …`.

## Root cause
`combineShiftToPMULH` (X86ISelLowering.cpp) rewrites `srl/sra(mul(ext a, ext b), 16)` on vectors of **i32 or
wider** elements to `zext/sext(mulhs/mulhu(a, b))`. It takes the outer extension from the shift kind and the mulh
kind from the input extension.
- **i32 lanes:** both are equivalent.
- **Wider lanes:** the product's bits above 31 are its sign (sext inputs) or zero (zext inputs), so the mixed
  combinations differ:
  - `srl` of a **sign-extended** product: bits 16..47 of a negative product are ones; `zext(mulhs)` makes them zero.
    **This is the miscompile.**
  - `sra` of a **zero-extended** product: the product is non-negative; `sext(mulhu)` sign-extends values
    ≥ 0x8000. It was not reproducible from C here, since InstCombine turns `ashr` of a non-negative value into
    `lshr` first.

PR #219996 (fixing #218844, merged 2026-09-02) fixed the related `trunc(lshr(mul(sext, sext)) to i16)` path of
`combinePMULH` for types wider than 32 bits. This non-truncating path in `combineShiftToPMULH` was not covered and is
still wrong on trunk.

## Fix (validated)
For elements wider than 32 bits, only allow the matching combinations (`srl` with `zext` inputs, `sra` with `sext`
inputs):
```cpp
  if (VT.getScalarSizeInBits() > 32 &&
      ((N->getOpcode() == ISD::SRL) != (ExtOpc == ISD::ZERO_EXTEND)))
    return SDValue();
```
Results:
- `repro.c` is correct.
- `h.ll` with SSE2 and AVX2 now prints the sign-extended results.
- DAG-CE is silent on `pmulh.ll`.

The CHECK lines of `sext_mulhsw_*_lshr_i64` (and of any `zext`-input `ashr_i64` variants) must be updated.

In the AGT tree the fix is commit 0b430f8f98 (v193); `AGT_NOFIX=1` disables it. The positive suite entry is S24.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to