| Issue |
243709
|
| Summary |
[PowerPC] Crash in LowerROTL for a variable-amount rotate of `<1 x i128>`
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
20000419
|
A rotate of a one-element `__int128` vector by a non-constant amount crashes clang on 64-bit PowerPC at every optimization level, including `-O0`. The C is valid: both shift amounts are masked with `& 127`.
```c
typedef unsigned __int128 u128;
typedef u128 u128v __attribute__((vector_size(16)));
u128v rotl(u128v x, u128v k) {
return (x << (k & 127)) | (x >> ((-k) & 127));
}
```
`clang --target=powerpc64le-linux-gnu -mcpu=power9 -O0 -c t.c` segfaults during instruction selection. The same happens with `powerpc64-linux-gnu` and with `-mcpu=power8`. x86-64 compiles it and gives the correct rotate. An assertions build stops in `LowerROTL`:
```
clang: llvm/include/llvm/Support/Casting.h:560: decltype(auto) llvm::cast(const From&) [with To = llvm::ConstantSDNode; From = llvm::SDValue]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
...
#11 llvm::PPCTargetLowering::LowerROTL(llvm::SDValue, llvm::SelectionDAG&) const
#12 llvm::PPCTargetLowering::LowerOperation(llvm::SDValue, llvm::SelectionDAG&) const
#13 (anonymous namespace)::VectorLegalizer::LegalizeOp(llvm::SDValue)
#14 llvm::SelectionDAG::LegalizeVectors()
```
Godbolt: https://godbolt.org/z/n4MK8c9ed
IR, single pass:
```llvm
define <1 x i128> @f(<1 x i128> %x, <1 x i128> %k) {
%r = call <1 x i128> @llvm.fshl.v1i128(<1 x i128> %x, <1 x i128> %x, <1 x i128> %k)
ret <1 x i128> %r
}
```
`llc -mtriple=powerpc64le -mcpu=pwr9` crashes the same way, and so does `llc -mtriple=powerpc -mcpu=pwr8` (32-bit clang rejects `__int128`, so that target is reachable only from IR).
`ISD::ROTL` for `v1i128` is marked `Custom` (`PPCISelLowering.cpp` line 992 on main `65406a394f38`), so every v1i128 rotate reaches `PPCTargetLowering::LowerROTL` (line 10398). It does `peekThroughBitcasts(Op.getOperand(1))` and then `N1.getConstantOperandVal(0)` without checking that the amount is a constant, so a variable amount hits `cast<ConstantSDNode>` on a non-constant node (the assertion above in assertion builds, a segfault in release builds).
#236746 is about the constant-amount case in the same function (wrong rotate direction / dropped rotate). The patch posted there still calls `getConstantOperandVal` unconditionally, so it does not cover this crash. The problem seems to be that `LowerROTL` assumes every `v1i128` rotate amount is a constant.
Versions: crashes with clang 15.0.6, 16.0.4, 17.0.6, 18.1.8, 19, 20, 21, 22, 23 and 24 (`cf44dd9e317e` and `58561153d5fc`) at `-O0`, `-O1`, `-O2` and `-Os`; Compiler Explorer clang trunk (`abcbd442e332`) and clang assertions trunk (`7e639b74b589`) crash too.
See also #236746.
cc @nemanjai @amy-kwan
Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.
Assisted-by: Grok
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs