This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Apply foldICmpUsingKnownBits to early bailout case
AbandonedPublic

Authored by mkazantsev on Jul 4 2018, 4:18 AM.

Details

Summary

In the patch rL336172, we've sunk foldICmpUsingKnownBits down the pipeline
because it is expensive and sometimes produces instruction patterns that cannot be
analyzed by other parts of optimizer. However the method from which it is invoked
also contains a early bailout (it looks like it is needed to work around some endless
loop corner cases), in which case we don't do any transforms, including foldICmpUsingKnownBits.

There is a case where it is profitable to do foldICmpUsingKnownBits before the
early bailout. In this patch, we add its invocation to this execution path to help
the attached test.

Diff Detail

Event Timeline

mkazantsev created this revision.Jul 4 2018, 4:18 AM
mkazantsev edited the summary of this revision. (Show Details)
spatel added a comment.Jul 4 2018, 8:23 AM

Bailing out on optimizations is a hack, so I'd prefer not to perpetuate that.

I have no context for this patch other than the one test case shown here. I see 2 potential missing folds that would solve that case (and I suspect that we actually have these folds but they are again guarded by min/max bailout hacks):
https://rise4fun.com/Alive/Pe9U

Name: narrow min/max in instcombine
  %z = zext i16 %x to i32
  %c = icmp ult i32 %z, 255
  %s = select i1 %c, i32 %z, i32 255
  %t = trunc i32 %s to i16
  =>
  %c2 = icmp ult i16 %x, 255
  %t = select i1 %c2, i16 %x, i16 255
  
Name: fold min/max in instsimplify
  %a = and i16 %x, 255
  %z = zext i16 %a to i32
  %c = icmp ult i32 %z, 255
  %s = select i1 %c, i32 %z, i32 255
  =>
  %s = %z

Can we add/fix one of those folds instead of trying to solve this in visitICmpInst()?

The context is following: the patch rL336172 has broken this test, and I was asked to fix it. Basically I just restored the behavior that we used to have before this sinking. I would prefer to not introduce any new transforms to InstCombine without dire need because it is fragile, and I am not familiar with it well enough (in particular, I don't understand ordering of particular transforms in many places). So I've chosen a conservative approach (to restore old behavior).

However if you think that this approach is unacceptable, I can try to introduce a new transform and hope that it will not change behavior of other tests.

The context is following: the patch rL336172 has broken this test, and I was asked to fix it.

I assumed that much. :)
But are there any other examples that were given to you? The solution may depend on how varied the breakage is. If it's just this case, then we can probably get by with a solution that doesn't need value tracking.

Here's a partial fix that I think should be universally good (ie, can't cause regressions for any other analysis/pass and has no obvious compile-time impact):
rL336293

The context is following: the patch rL336172 has broken this test, and I was asked to fix it.

I assumed that much. :)
But are there any other examples that were given to you? The solution may depend on how varied the breakage is. If it's just this case, then we can probably get by with a solution that doesn't need value tracking.

It's the only example I have. @aditya_nandakumar, do you have any more tests where it matters?

The context is following: the patch rL336172 has broken this test, and I was asked to fix it.

I assumed that much. :)
But are there any other examples that were given to you? The solution may depend on how varied the breakage is. If it's just this case, then we can probably get by with a solution that doesn't need value tracking.

It's the only example I have. @aditya_nandakumar, do you have any more tests where it matters?

That was the simplest test case where I observed regression. There were quite a few more - I didn't get a chance to look at them all.

spatel added a comment.Jul 5 2018, 9:10 AM

The context is following: the patch rL336172 has broken this test, and I was asked to fix it.

I assumed that much. :)
But are there any other examples that were given to you? The solution may depend on how varied the breakage is. If it's just this case, then we can probably get by with a solution that doesn't need value tracking.

It's the only example I have. @aditya_nandakumar, do you have any more tests where it matters?

That was the simplest test case where I observed regression. There were quite a few more - I didn't get a chance to look at them all.

I'd like to see more examples before trying to fix it. If it's just min/max cases that were broken, that can be a specific patch as I showed in the earlier comment, but if we regressed regular icmp patterns, then we might want to redo rL336172 by taking into account metadata rather than rearranging the folds.

I've reverted the original problematic change by rL336410. I think we need to collect tests that degrade from it to make this change properly.

lebedev.ri requested changes to this revision.Aug 30 2018, 10:55 AM

Hmm, this appears to be stuck.
I feel like marking it as such just to remove it from my review queue.

This revision now requires changes to proceed.Aug 30 2018, 10:55 AM
mkazantsev abandoned this revision.Nov 6 2018, 8:36 PM