ispc: Test select-short-vec is failing accross platforms on neon-i8x16 with -O1

https://github.com/ispc/ispc/actions/runs/16286474415/job/45986562102#step:6:3066

Test ./tests/func-tests/select-short-vec.ispc failed (return code 1)            
./tests/func-tests/select-short-vec.ispc.run: value 0 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 2 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 3 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 4 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 6 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 8 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 10 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 12 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 13 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]
./tests/func-tests/select-short-vec.ispc.run: value 14 disagrees: returned 2.000000 [0x1p+1], expected 0.000000 [0x0p+0]

About this issue

Most upvoted comments

sext LLVM IR instructions does that for example so we can just replace the zext to a sext instruction and it works better 🙂 !

I had the same conclusion over the weekend. The fix is on the way.

So poison in shufflevector by itself is not an issue.

Why so ? AFAIK, LLVM can replace poison by anything and some operation are UB on them. For shuffles, it looks like LLVM performs a basic copy so poison values can be replaced by 0, 1, 2, etc. here.

but in shuffle + pblendvb case x86 iSel incorrectly concludes that the blend operation can be eliminated

I found out that there is an issue in the first code even without the shuffle (with poison values). Here is the code without the shuffle. In this code, the blendvb is already eliminated. Here is my understanding:

The thing is blendvb only reads the highest bit of the mask, not the lowest one. Moreover, zext just extends the 0 or 1 bit to an 8-bit value so the highest 7 bits are set to zeros. This means the blend will always select items from the first argument and never from the second one. We need a shift of to extend the bits correctly like 0xFF for 1 and 0x00 for 0. This can be done with a sub (on an unsigned 8-bit integer) or with a shift. In fact, in the second code of your link, LLVM uses the instruction psllw xmm0, 7 so to shift the mask bit at the right location. AFAIK the sext LLVM IR instructions does that for example so we can just replace the zext to a sext instruction and it works better 🙂 !

Here is the result with the fix.

So poison in shufflevector by itself is not an issue. Compare codegen for shuffle + pblendvb vs select + shuffle on Godbolt

The LLVM IR seems functionally equivalent for me but in shuffle + pblendvb case x86 iSel incorrectly concludes that the blend operation can be eliminated, even though the first 3 elements should still be conditionally selected based on the condition vector.

To avoid confusion this is the transformation we’re talking about. So it is store to a local alloca → masked.load → func call (masked store)→ load → return.

I’m not saying the transformation is correct - I tried to understand the logic behind it today but couldn’t figure it out. I’ll go ahead and file an issue in LLVM

I succeed to reproduce it on my x86-64 CPU! It fails with the targets generic-i8x16, sse4.1-i8x16 and sse4.2-i8x16. Optimizations apparently impact the results because it fails with -O1 and -O2 for the target generic-i8x16, but not with -O0. For the two other targets, it fails independently of the optimization level. I did not succeed to to reproduce the issue with many other combination involving AVX1/AVX2 and few other in SSE. Please note that the result of these targets is not always exactly identical to the neon-i8x16 target (but pretty similar).

PS1: I confirm my educated guess aforementioned: test_select<int32, 16>(); is the one causing the issue. PS2: test_select<int8, 3>(); and test_select<uint8, 3>(); also fail in sse4.1-i8x16 and sse4.2-i8x16 (but not generic-i8x16) independently of the optimization level.

Ok, I’ll access ARM machine tomorrow and check what’s going on.

This is surprising it only happens on Neon/ARM, especially since the code is architecture-independent and it does not looks like it is dependent of the size of the short vectors (at least not on my PC on an Intel Skylake CPU). More surprisingly: there is only 2 errors amongst all the tests and apparently only when cond[i] is true (so also varying condition). It might be only happening in a single test but there is two errors because of rc1 and rc2. I think it reveal an existing issue on ARM.

It is hard to know more without debugging ISPC. Unfortunately, I do not have access to an ARM CPU yet. Maybe we can split the test (e.g. 1 per type) so to reduce the size of the overall code causing this issue, and then analyze the generated assembly code.

PS: based on the output, a more educated guess is that it only happens on test_select<int32, 15>() or test_select<int32, 16>(), not others (which is pretty crazy).