This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] remove a buggy set of zext-icmp transforms
ClosedPublic

Authored by spatel on Sep 8 2021, 8:13 AM.

Details

Summary

The motivating case is an infinite loop shown with a reduced test from:
https://llvm.org/PR51762

To solve this, I'm proposing we delete the most obviously broken part of this code.

The bug example shows a fundamental problem: we ask computeKnownBits if a transform will be profitable, alter the code by creating new instructions, then rely on computeKnownBits to return the same answer to actually eliminate instructions.

But there's no guarantee that the results will be the same between the 1st and 2nd calls. In the infinite loop example, we get different answers, so we add instructions that conflict with some other transform, and we're stuck.

There's at least one other problem visible in the test diff for @zext_or_masked_bit_test_uses: the code doesn't check uses properly, so we can end up with extra instructions created.

Last, it's not clear if this set of transforms actually improves analysis or codegen. I spot-checked a few targets and don't see a clear win:
https://godbolt.org/z/x87EWovso

If we do see a regression from this change, codegen seems like the right place to add a cmp -> bit-hack fold.

If this is too big of a step, we could limit the computeKnownBits calls by not passing a context instruction and/or limiting the recursion. I checked that those would stop the infinite loop for PR51762, but that won't guarantee that some other example does not fall into the same loop.

Diff Detail

Event Timeline

spatel created this revision.Sep 8 2021, 8:13 AM
spatel requested review of this revision.Sep 8 2021, 8:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 8 2021, 8:13 AM
spatel updated this revision to Diff 371386.Sep 8 2021, 10:21 AM

Patch updated:
Removed what would become stale test comments; no code changes.

lebedev.ri accepted this revision.Sep 8 2021, 11:58 AM

LGTM on the basis of directly preventing an endless combine loop and de-uglifying transformZExtICmp() at the same time - that DoTransform is super bugprone.

This revision is now accepted and ready to land.Sep 8 2021, 11:58 AM
spatel added a comment.Sep 9 2021, 4:59 AM

LGTM on the basis of directly preventing an endless combine loop and de-uglifying transformZExtICmp() at the same time - that DoTransform is super bugprone.

Thanks! Yes, the DoTransform was blamed in D104567 .
Also for reference, I added a fold to IR to mitigate a potential loss from this patch at:
a3c1669b1717

This revision was landed with ongoing or failed builds.Sep 9 2021, 6:00 AM
This revision was automatically updated to reflect the committed changes.