This is an archive of the discontinued LLVM Phabricator instance.

[CVP] Guard against poison in common phi value transform (PR50399)
ClosedPublic

Authored by nikic on May 22 2021, 3:07 AM.

Details

Summary

The common phi value transform replaces constants with values that have the same value as the constant on a given edge. However, LVI generally only provides information that is correct up to poison, so this can end up replacing a well-defined value with poison. D69442 addressed an instance of this problem by clearing poison flags on the generating instruction, which was sufficient at the time. rGa917fb89dc28 made LVI's edge value analysis slightly more powerful, and clearing poison flags is no longer sufficient.

This patch changes the transform to instead explicitly guard against a poison value instead. This should be satisfied for most cases due to a branch on poison.

Fixes https://bugs.llvm.org/show_bug.cgi?id=50399.

Diff Detail

Event Timeline

nikic created this revision.May 22 2021, 3:07 AM
nikic requested review of this revision.May 22 2021, 3:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2021, 3:07 AM

Hi!

I've verified that this indeed solves the problem I saw in
https://bugs.llvm.org/show_bug.cgi?id=50399 .
Thanks!

spatel added inline comments.May 24 2021, 5:16 AM
llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll
144

Check my understanding (add this test?): if the sub does not have nsw, then we still allow this transform because the plain sub can not create poison and arg is not poison?

nikic added inline comments.May 24 2021, 8:31 AM
llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll
144

Yes, that's right. I've added the additional test in https://github.com/llvm/llvm-project/commit/e42636d3c1a41a9b7c5d8095ae5ef6682e26d4a2 and it does still fold for the reason you described.

spatel accepted this revision.May 24 2021, 8:52 AM

LGTM - need to fix the miscompile regardless of whether we could salvage the existing test.

llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll
144

Thanks. So it's one of those awkward cases where improving wrapping analysis could actually inhibit an optimization. I don't know if we have any plans for adjusting for that sort of thing, but might be worth a code or test comment?

This revision is now accepted and ready to land.May 24 2021, 8:52 AM
nikic added inline comments.May 24 2021, 9:45 AM
llvm/test/Transforms/CorrelatedValuePropagation/phi-common-val.ll
144

Right. Unfortunately I don't have a good idea for how to retain this optimization. What the similar select optimization in InstCombine does is this monstrosity: https://github.com/llvm/llvm-project/blob/694068d0db4378d9956fe0d3234ee0c4d353d94c/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp#L1175-L1213 We unset the nowrap flags, rerun the analysis and then either do the fold or restore the flags. I don't think something like that is feasible (or advisable) here, because it will interact with the LVI caching in a dangerous way.

That said, I don't think this is a particularly important corner case either.

nikic retitled this revision from [CVP] Guard against poison in common phji value transform (PR50399) to [CVP] Guard against poison in common phi value transform (PR50399).May 25 2021, 11:44 AM