llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.94k stars 11.94k forks source link

[LoopVectorizer] -prefer-inloop-reducrions flag is not working with smax Intrinsic #100982

Open saharofe opened 3 months ago

saharofe commented 3 months ago

In the LoopVectorize pass, when the -prefer-inloop-reductions flag is enabled and the reduction instruction is the intrinsic smax, the flag does not function correctly, resulting in reductions being performed outside the loop.

Producer: Godbolt Example

More Explanation: In the function RecurrenceDescriptor::getReductionOpChain in IVDescriptors.cpp, the code only looks for select or compare instructions. If the compare instruction is replaced with the intrinsic smax before the vectorizer runs, isCorrectOpcode returns false, causing the reduction to be handled incorrectly.

However, if you block the function in InstCombinePass that canonicalizes SPG to min/max, allowing the compare/select instructions to remain, the reduction works correctly and is performed within the loop.

saharofe commented 2 months ago

?