Issue 240296
Summary [SPIR-V] Wrong overflow flag for llvm.smul.with.overflow
Labels new issue
Assignees
Reporter maleadt
    The SPIR-V backend lowers `llvm.smul.with.overflow` to `OpSMulExtended` and reports overflow when the high half of the product is nonzero (`selectOverflowArith` in `SPIRVInstructionSelector.cpp`). That is the unsigned test. A signed product overflows when the high half differs from the sign extension of the low half, `ashr(lo, N-1)`, so some products are flagged wrongly and some overflows are missed:

| `i64` operands | high half | overflow | reported |
|---|---:|---|---|
| `-1 * 1` | `-1` | no | yes |
| `2^62 * 2` | `0` | yes | no |

```llvm
declare { i64, i1 } @llvm.smul.with.overflow.i64(i64, i64)

define spir_kernel void @f(i64 %a, i64 %b, ptr addrspace(1) %out) {
 %r = call { i64, i1 } @llvm.smul.with.overflow.i64(i64 %a, i64 %b)
  %o = extractvalue { i64, i1 } %r, 1
  %z = zext i1 %o to i8
  store i8 %z, ptr addrspace(1) %out
  ret void
}
```

`llc -mtriple=spirv64-unknown-unknown` emits:

```
%11 = OpConstantNull %2
...
%16 = OpSMulExtended %10 %12 %13
%17 = OpCompositeExtract %2 %16 0
%18 = OpCompositeExtract %2 %16 1
%19 = OpINotEqual %7 %18 %11
```

`%18` should be compared against `OpShiftRightArithmetic %17 63` instead (per element for vectors). The unsigned case is correct. The lowering was introduced in #108893; `test/CodeGen/SPIRV/llvm-intrinsics/smul.with.overflow.ll` checks the current output.

Related: OpenCL runtimes that read SPIR-V through SPIRV-LLVM-Translator, such as PoCL, fail to build these modules at all, because the translator turns `OpSMulExtended`/`OpUMulExtended` into calls to `__spirv_SMulExtended`/`__spirv_UMulExtended`, which they don't implement. That needs a translator fix.

Found via JuliaGPU/KernelAbstractions.jl#916.

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

Reply via email to