This is an archive of the discontinued LLVM Phabricator instance.

[LV] Do not create SCEVs on broken IR in emitTransformedIndex. PR39160
ClosedPublic

Authored by mkazantsev on Oct 4 2018, 4:28 AM.

Details

Summary

At the point when we perform emitTransformedIndex, we have a broken IR (in
particular, we have Phis for which not every incoming value is properly set). On
such IR, it is illegal to create SCEV expressions, because their internal
simplification process may try to prove some predicates and break when it
stumbles across some broken IR.

The only purpose of using SCEV in this particular place is attempt to simplify
the generated code slightly. It seems that the result isn't worth it, because
some trivial cases (like addition of zero and multiplication by 1) can be
handled separately if needed, but more generally InstCombine is able to achieve
the goals we want to achieve by using SCEV.

This patch fixes a functional crash described in PR39160, and as side-effect it
also generates a bit smarter code in some simple cases. It also may cause some
optimality loss (i.e. we will now generate mul by power of 2 instead of
shift etc), but there is nothing what InstCombine could not handle later. In
case of dire need, we can support more trivial cases just in place.

Note that this patch only fixes one particular case of the general problem that
LV misuses SCEV, attempting to create SCEVs or prove predicates on invalid IR.
The general solution, however, seems complex enough.

Diff Detail

Repository
rL LLVM

Event Timeline

mkazantsev created this revision.Oct 4 2018, 4:28 AM
fhahn added a comment.Oct 4 2018, 7:39 AM

I think it should be fine to not rely on SCEV to do the simplifications here and let instcombine/simplify do it.

lib/Transforms/Vectorize/LoopVectorize.cpp
2517 ↗(On Diff #168267)

Unless I msised something, it looks like capturing just B would work?

2528 ↗(On Diff #168267)

Unless I msised something, it looks like capturing just B would work?

test/Transforms/LoopVectorize/X86/pr39160.ll
1 ↗(On Diff #168267)

can this test be simplified a bit further with bugpoint?

116 ↗(On Diff #168267)

is this needed?

mkazantsev added inline comments.Oct 4 2018, 10:01 PM
test/Transforms/LoopVectorize/X86/pr39160.ll
1 ↗(On Diff #168267)

Yup, thanks for advice!

116 ↗(On Diff #168267)

I thought it is, but not really. :)

mkazantsev marked 2 inline comments as done.

Limited capture scope to builder only, reduced the test using bugpoint.

It also fixes crash in PR37221.

Added a new test from duplicate bug PR37221.

fhahn accepted this revision.Oct 5 2018, 8:45 AM

Thanks LGTM. Please wait with committing a bit, in case Hideki or anyone else has any additional comments.

This revision is now accepted and ready to land.Oct 5 2018, 8:45 AM

Thanks LGTM. Please wait with committing a bit, in case Hideki or anyone else has any additional comments.

+1. Thanks, @mkazantsev. I think this is a good small incremental fix. If really needed (i.e., others like InstCombine refuses to improve whatever shortcomings exposed), we can add more peephole optimization ---- but I sincerely hope we don't have to do it. Induction simplification is generally useful.

This revision was automatically updated to reflect the committed changes.

Hi Max,

ARM bots are broken since this patch was checked in, logs are available here:
http://lab.llvm.org:8011/builders/clang-cmake-armv7-quick/builds/4267/steps/ninja%20check%201/logs/stdio

Thanks
Yvan

Hi Yvan,

Thanks for letting me know! Justin has fixed it by moving the test, see https://reviews.llvm.org/rL344087 .

Regards,
Max