This is an archive of the discontinued LLVM Phabricator instance.

[SLP]Fix emission of int division with some undef vector subelements.
AbandonedPublic

Authored by ABataev on Nov 22 2021, 9:06 AM.

Details

Summary

If it is legal and profitable to vectorize integer division operations
and some of the operands are undefs, need to replace these undefs in the
vector operands elements with some actual values. Otherwise, such
division operations will be replaced by the vector poison values in the
instcombiner. It leads to incorrect llvm ir per https://llvm.org/docs/LangRef.html#poisonvalues. According to this document "Vector elements may be independently poisoned. Therefore, transforms on instructions such as shufflevector must be careful to propagate poison across values or elements only as allowed by the original code."

Diff Detail

Event Timeline

ABataev created this revision.Nov 22 2021, 9:06 AM
ABataev requested review of this revision.Nov 22 2021, 9:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 22 2021, 9:06 AM

I am not sure I understand why you are trying to get rid of the undef operands. Could you provide a code example that shows the issue?

I am not sure I understand why you are trying to get rid of the undef operands. Could you provide a code example that shows the issue?

+1

llvm/test/Transforms/SLPVectorizer/X86/arith-div-undef.ll
6

This is *NOT* a miscompile: https://alive2.llvm.org/ce/z/iMu-eF

I am not sure I understand why you are trying to get rid of the undef operands. Could you provide a code example that shows the issue?

The test test/Transforms/SLPVectorizer/X86/arith-div-undef.ll shows the problem. We have a gather of sdivs results, 2 of them are poisonous. And generally speaking, this gather shall end up with a vector <poison, sdiv1, sdiv2, sdiv3, poison, sdiv4, sdiv5, sdiv6>. But instead, the compiler generates just <8 x i32> poison. I think this result is more poisonous than the original one. Extract from lane 1, for example, should be different for these results.

Here is an example with just instcombiner https://godbolt.org/z/9rxesoM47

llvm/test/Transforms/SLPVectorizer/X86/arith-div-undef.ll
6

Yes, also checked it via alive but not sure if alive is correct here.

lebedev.ri requested changes to this revision.Nov 22 2021, 1:25 PM
lebedev.ri added inline comments.
llvm/test/Transforms/SLPVectorizer/X86/arith-div-undef.ll
6

https://llvm.org/docs/LangRef.html#sdiv-instruction
"Division by zero is undefined behavior. For vectors,
if any element of the divisor is zero,
the operation has undefined behavior. "

https://alive2.llvm.org/ce/z/mZvBXL

This revision now requires changes to proceed.Nov 22 2021, 1:25 PM
ABataev added inline comments.Nov 22 2021, 1:36 PM
llvm/test/Transforms/SLPVectorizer/X86/arith-div-undef.ll
6

Yep, thought about this. But thought that a buildvector has different behavior. Probably here the whole program becomes UB after division by undef.

ABataev abandoned this revision.Nov 22 2021, 1:41 PM