| Issue |
244149
|
| Summary |
[PowerPC] Altivec (no VSX) promotes vector SELECT_CC to v4i32 and bitcasts the FP compare operands, so maximumnum/minimumnum compare floats as integers
|
| Labels |
backend:PowerPC,
miscompilation,
llvm:SelectionDAG,
generated by fuzzer
|
| Assignees |
|
| Reporter |
YuanchengJiang
|
The following code:
```c
// clang --target=powerpc-linux-gnu -mcpu=7450 -O2 repro.c (also powerpc64-linux-gnu -mcpu=970/g5, ppc64le -mcpu=pwr8 -mno-vsx)
typedef float v4f __attribute__((vector_size(16)));
typedef int v4i __attribute__((vector_size(16)));
__attribute__((noinline)) v4f vmax(v4i a, v4i b) {
v4f x = __builtin_convertvector(a, v4f) * 0.5f, y = __builtin_convertvector(b, v4f) * 0.5f;
return __builtin_elementwise_maximumnum(x, y);
}
int printf(const char *, ...);
volatile v4i A = {-2000, 5, -7000, 1}, B = {-10000, -3, -3000, 2};
int main(void) {
v4f r = vmax(A, B);
int bad = r[0] != -1000.f || r[1] != 2.5f || r[2] != -1500.f || r[3] != 1.f;
printf("%llx\n", (unsigned long long)bad);
return bad; // expect 0; Altivec without VSX prints 1: r = {-5000, 2.5, -3500, 1} (the smaller negative)
}
```
Resulted in this output:
```
$ clang --target=powerpc-linux-gnu -mcpu=7450 -O2 repro.c -o r && qemu-ppc -cpu 7450 ./r
1
```
The expected output is `0`. For the negative lanes, `max(-1000, -5000)` returns `-5000` and `max(-3500, -1500)` returns `-3500`: the elements are compared as signed integers (`cmpw` on the float bits) instead of as floats. The same happens with `powerpc64 -mcpu=970` and `ppc64le -mcpu=pwr8 -mno-vsx`, at -O0 and -O2. With VSX the result is correct. IR repro: repro-min.ll (`sitofp` + `llvm.maximumnum.v4f32`); `llvm.minimumnum` is affected the same way.
Trunk: https://godbolt.org/z/ddGabfGr6 (clang trunk `--target=powerpc-linux-gnu -mcpu=7450 -O2`: `cmpw` on the stored float lanes).
Root cause: `expandFMINIMUMNUM_FMAXIMUMNUM` produces a `SELECT_CC v4f32 a, b, a, b, setgt`. PPCISelLowering.cpp marks `SELECT_CC` as `Promote` to `v4i32` for all Altivec vector types (`AddPromotedToType(ISD::SELECT_CC, VT, MVT::v4i32)`). The generic `VectorLegalizer::Promote` (LegalizeVectorOps.cpp) bitcasts *every* vector operand to the promoted type, including the compare operands 0 and 1, so the FP `setgt` becomes an integer `setgt` on the float bit patterns. With other inputs (FP-only condition codes reaching integer operands) llc asserts in `FoldSetCC` ("Illegal setcc for integer!") instead.
Suggested fix: keep the compare operands of SELECT_CC unpromoted (fix.diff):
```diff
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorOps.cpp
@@ void VectorLegalizer::Promote(SDNode *Node, SmallVectorImpl<SDValue> &Results) {
// Do not promote the mask operand of a VP OP.
bool SkipPromote = ISD::isVPOpcode(Node->getOpcode()) &&
ISD::getVPMaskIdx(Node->getOpcode()) == j;
+ // The compare operands of SELECT_CC keep their type: bitcasting FP
+ // compare operands to integers would change the comparison.
+ if (Node->getOpcode() == ISD::SELECT_CC && j < 2)
+ SkipPromote = true;
```
With the fix the C repro prints `0` (ppc32 7450 -O0/-O2, ppc64le -mno-vsx). Among the PowerPC CodeGen tests (393 llc RUN lines checked) the only output change is `pr45709.ll` (fast `maxnum` on `-mattr=-vsx`), which now computes the FP compare instead of folding the integer one; its CHECK lines need regenerating. Alternatively, PowerPC could custom-lower/expand vector SELECT_CC with FP compare operands.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs