Regression for three way min/max
| Bugzilla Link | 35717 |
| Resolution | FIXED |
| Resolved on | Jan 17, 2018 14:54 |
| Version | trunk |
| OS | Linux |
| CC | @DimitryAndric,@RKSimon,@rotateright |
Extended Description
This is a regression that came in with rL320749. We have some code with three way min/max that is now doing a lot worse than it did previously. The most basic for I could come up with is this:
int test(int xc, int xm, int xy) { int xk; if (xc < xm) xk = xc < xy ? xc : xy; else xk = xm < xy ? xm : xy; return xk; }
This used to come out as (roughly "min(min(a,b),c)"): define i32 @test(i32 %xc, i32 %xm, i32 %xy) { entry: %cmp = icmp slt i32 %xc, %xm %xm.sink15 = select i1 %cmp, i32 %xc, i32 %xm %cmp2 = icmp slt i32 %xm.sink15, %xy %cond6 = select i1 %cmp2, i32 %xm.sink15, i32 %xy ret i32 %cond6 }
But now ends up with extra selects (roughly "min(min(a,b), min(b,c))"): define i32 @test(i32 %xc, i32 %xm, i32 %xy) { entry: %cmp = icmp slt i32 %xc, %xm %cmp1 = icmp slt i32 %xc, %xy %cond = select i1 %cmp1, i32 %xc, i32 %xy %cmp2 = icmp slt i32 %xm, %xy %cond6 = select i1 %cmp2, i32 %xm, i32 %xy %xk.0 = select i1 %cmp, i32 %cond, i32 %cond6 ret i32 %xk.0 }
The original case came from a cmy conversion, where the values were i8 (not i32) so there is a number of zext and trunc's making things more messy. They are also inverted (subtracted from 255). It runs especially worse on our small thumb1 cores.
mentioned in issue llvm/llvm-bugzilla-archive#35875
Are there any other revisions > 321711 relating to this bug, apart from 322016, that you know of?
That's the only one directly in that code.
The change in value tracking will make it more likely to hit bug 35807, so you might want to also take: https://reviews.llvm.org/rL322006
So r322016 should probably also be merged into the 6.0 branch?
Yes - that should be safe.
Sorry, I don't follow the release schedules closely. I didn't even realize that r321672 was before the branch.
The release_60 branch was created off trunk r321711, at least for llvm, so anything after that which relates to this bug (and which is safe :) should be merged.
Are there any other revisions > 321711 relating to this bug, apart from 322016, that you know of?
So r322016 should probably also be merged into the 6.0 branch?
Yes - that should be safe.
Sorry, I don't follow the release schedules closely. I didn't even realize that r321672 was before the branch.
Please let me know if you do find any other bugs - thanks!
Ehm, are we going to merge any of the fixes from this bug into the 6.0 release branch? As we're running into min/max crashes with the 6.0 branch snapshot imported into FreeBSD...
I was told that trying to prevent crashes such as bug 35840 was not a good solution: llvm/llvm-bugzilla-archive#35807 #c10
So the only bug that I know about in the code that was added with this enhancement was solved at r322016. Do you have links to other bugs?
Actually, the bugs seem to be the same with regards to that overzealous assertion: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=225268 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=225269
since both have:
Bad flavor while matching min/max UNREACHABLE executed at /usr/local/poudriere/jails/head-amd64/usr/src/contrib/llvm/lib/Analysis/ValueTracking.cpp:4217!
So r322016 should probably also be merged into the 6.0 branch?
Ehm, are we going to merge any of the fixes from this bug into the 6.0 release branch? As we're running into min/max crashes with the 6.0 branch snapshot imported into FreeBSD...
I was told that trying to prevent crashes such as bug 35840 was not a good solution: llvm/llvm-bugzilla-archive#35807 #c10
So the only bug that I know about in the code that was added with this enhancement was solved at r322016. Do you have links to other bugs?
Here's the ValueTracking piece of the puzzle: https://reviews.llvm.org/rL321672
I'm assuming there's nothing controversial there. This is similar to what we already do to recognize clamping min/max patterns.
Actually, r321672 seems to cause a regression, which may have been fixed later; testcase:
// clang -cc1 -triple x86_64 -S -O1 -w testcase.c a, b, c, d; double e; f() { for (;;) if (b >= a) d = e > 0 ? e : 0; else d = floor((e - c) * a / b) > 0 ? floor((e - c) * a / b) : 0; }
r321671 compiles this just fine, r321672 gives:
Bad flavor while matching min/max UNREACHABLE executed at /home/dim/src/clang-trunk-r321672/lib/Analysis/ValueTracking.cpp:4217!
I'm looking for the point where this got fixed...
https://reviews.llvm.org/rL322016
Here's the ValueTracking piece of the puzzle: https://reviews.llvm.org/rL321672
I'm assuming there's nothing controversial there. This is similar to what we already do to recognize clamping min/max patterns.
Actually, r321672 seems to cause a regression, which may have been fixed later; testcase:
// clang -cc1 -triple x86_64 -S -O1 -w testcase.c a, b, c, d; double e; f() { for (;;) if (b >= a) d = e > 0 ? e : 0; else d = floor((e - c) * a / b) > 0 ? floor((e - c) * a / b) : 0; }
r321671 compiles this just fine, r321672 gives:
Bad flavor while matching min/max UNREACHABLE executed at /home/dim/src/clang-trunk-r321672/lib/Analysis/ValueTracking.cpp:4217!
I'm looking for the point where this got fixed...
Ehm, are we going to merge any of the fixes from this bug into the 6.0 release branch? As we're running into min/max crashes with the 6.0 branch snapshot imported into FreeBSD...
New bug with longer reproducer up at: llvm/llvm-bugzilla-archive#35875
Something like this seems to allow things to fall into place:
--- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp /// Return true if it is desirable to convert an integer computation from a /// given bit width to a new bit width. /// We don't want to convert from a legal to an illegal type or from a smaller /// to a larger illegal type. A width of '1' is always treated as a legal type /// because i1 is a fundamental type in IR, and there are many specialized /// optimizations for i1 types. bool InstCombiner::shouldChangeType(unsigned FromWidth, unsigned ToWidth) const { bool FromLegal = FromWidth == 1 || DL.isLegalInteger(FromWidth); bool ToLegal = ToWidth == 1 || DL.isLegalInteger(ToWidth);
if (FromWidth == 32 && (ToWidth == 8 || ToWidth == 16))
return true;
// If this is a legal integer from type, and the result would be an illegal // type, don't do the transformation. if (FromLegal && !ToLegal) return false;
// Otherwise, if both are illegal, do not increase the size of the result. We // do allow things like i160 -> i64, but not i64 -> i160. if (!FromLegal && !ToLegal && ToWidth > FromWidth) return false;
return true; }
I've no idea what the knock-on result of something like that would be though, especially for places where things are not just selects. It is almost certainly unwise.
That patch is something I've also wished for, but I'm also not sure what fallout would result. Can you file a new bug for that problem using a minimal example with an Aarch/ARM target (you probably only need to specify the datalayout with the IR)?
Ah. I see. https://reviews.llvm.org/rL321882 doesn't kick in any more, because of the number of uses is now 3 I think.
My cut down example was too cut down, sorry about that. I was expecting things to fall into place more when the root issue was fixed. Looks like it hasn't been that lucky. This, for the record, is the full loop from the benchmark:
for (int i = 0; i < I; i++) {
xc = *A++;
xm = *A++;
xy = *A++;
xc = 255-xc;
xm = 255-xm;
xy = 255-xy;
if (xc < xm)
xk = xc < xy ? xc : xy;
else
xk = xm < xy ? xm : xy;
xc = xc - xk;
xm = xm - xk;
xy = xy - xk;
*B++ = xk;
*B++ = xc;
*B++ = xm;
*B++ = xy;
}
Something like this seems to allow things to fall into place:
--- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp /// Return true if it is desirable to convert an integer computation from a /// given bit width to a new bit width. /// We don't want to convert from a legal to an illegal type or from a smaller /// to a larger illegal type. A width of '1' is always treated as a legal type /// because i1 is a fundamental type in IR, and there are many specialized /// optimizations for i1 types. bool InstCombiner::shouldChangeType(unsigned FromWidth, unsigned ToWidth) const { bool FromLegal = FromWidth == 1 || DL.isLegalInteger(FromWidth); bool ToLegal = ToWidth == 1 || DL.isLegalInteger(ToWidth);
-
if (FromWidth == 32 && (ToWidth == 8 || ToWidth == 16))
-
return true;
-
// If this is a legal integer from type, and the result would be an illegal // type, don't do the transformation. if (FromLegal && !ToLegal) return false;
// Otherwise, if both are illegal, do not increase the size of the result. We // do allow things like i160 -> i64, but not i64 -> i160. if (!FromLegal && !ToLegal && ToWidth > FromWidth) return false;
return true; }
I've no idea what the knock-on result of something like that would be though, especially for places where things are not just selects. It is almost certainly unwise.
Any idea what the best way to solve this might be?
(Also, I may have spoken too early about being this close. The xc/xm/xy in the original example are stored back into B, which may be preventing another of the folds required. I'll try and figure out why.)
Hello
Thanks for the work here. We are this close (holds up two fingers very close together).
The problem just seems to be that on the Arm target I am compiling with, the step where we remove the zext/truncs isn't being done. I think this is either because char are signed or (more likely) because i8 isn't a legal type on Arm. Doing that step manually, everything else falls into place.
This case and the more complicated bug 35834 should be optimal in IR after: https://reviews.llvm.org/rL321998
Dave, you mentioned at least one other problem arising from: https://reviews.llvm.org/rL320749
Please file more bugs for any remaining regressions.
Linking to bug 35807 here and the follow-up because we might need to plug that hole before exposing it more widely.
With D41603 applied, we do kill the zext/trunc:
define i8 @test(i8 %xc, i8 %xm, i8 %xy) { %c0 = xor i8 %xc, -1 %m1 = xor i8 %xm, -1 %y2 = xor i8 %xy, -1 %cmp = icmp ult i8 %xm, %xc %cmp13 = icmp ult i8 %c0, %y2 %cond = select i1 %cmp13, i8 %c0, i8 %y2 %cmp20 = icmp ult i8 %m1, %y2 %cond27 = select i1 %cmp20, i8 %m1, i8 %y2 %xk.0.in = select i1 %cmp, i8 %cond, i8 %cond27 ret i8 %xk.0.in }
Oh wait, that happens even without D41603. Let's open a new ticket for this example, so we can try to solve that independently - bug 35834.
With D41603 applied, we do kill the zext/trunc:
define i8 @test(i8 %xc, i8 %xm, i8 %xy) { %c0 = xor i8 %xc, -1 %m1 = xor i8 %xm, -1 %y2 = xor i8 %xy, -1 %cmp = icmp ult i8 %xm, %xc %cmp13 = icmp ult i8 %c0, %y2 %cond = select i1 %cmp13, i8 %c0, i8 %y2 %cmp20 = icmp ult i8 %m1, %y2 %cond27 = select i1 %cmp20, i8 %m1, i8 %y2 %xk.0.in = select i1 %cmp, i8 %cond, i8 %cond27 ret i8 %xk.0.in }
define zeroext i8 @test(i8 zeroext %xc, i8 zeroext %xm, i8 zeroext %xy) local_unnamed_addr #0 { entry: %0 = xor i8 %xc, -1 %1 = xor i8 %xm, -1 %2 = xor i8 %xy, -1 %conv8 = zext i8 %0 to i32 %conv9 = zext i8 %1 to i32 %cmp = icmp ult i8 %xm, %xc %conv12 = zext i8 %2 to i32 %cmp13 = icmp ult i32 %conv8, %conv12 %cond = select i1 %cmp13, i32 %conv8, i32 %conv12 %cmp20 = icmp ult i32 %conv9, %conv12 %cond27 = select i1 %cmp20, i32 %conv9, i32 %conv12 %xk.0.in = select i1 %cmp, i32 %cond, i32 %cond27 %xk.0 = trunc i32 %xk.0.in to i8 ret i8 %xk.0 }
Yes, that's more min/max fun. :)
If D41603 is accepted, I think we'll have all of the machinery we need in InstCombine, so we just have to figure out how this falls through the cracks.
There might be a need for: https://reviews.llvm.org/D41480 ...or something like it.
Hello
Thanks for the patches, they fix the function in this ticket nicely. I was looking at some of the other regressions we saw from the same patch, but haven't come up with anything sensible. The case I looked at had the same code going into the backend, just in a different order. The register allocator then had trouble (~15% worse trouble). Not sure how/if/where the fix to that would be.
The original code for this ticket was a little more complex that the simplified version presented here. It was working on chars, so had more zext/truncs in it, the variables were negated, and it acted in loop with loads/stores. Something like this I think:
for (int i = 0; i < I; i++) {
xc = *A++;
xm = *A++;
xy = *A++;
xc = 255-xc;
xm = 255-xm;
xy = 255-xy;
if (xc < xm)
xk = xc < xy ? xc : xy;
else
xk = xm < xy ? xm : xy;
*B++ = xk;
}
Any idea how to make this a more generic to catch such cases? The zext/trunc I can see being OK, but the neg's I'm not so sure about. It looks something like this:
define zeroext i8 @test(i8 zeroext %xc, i8 zeroext %xm, i8 zeroext %xy) local_unnamed_addr #0 { entry: %0 = xor i8 %xc, -1 %1 = xor i8 %xm, -1 %2 = xor i8 %xy, -1 %conv8 = zext i8 %0 to i32 %conv9 = zext i8 %1 to i32 %cmp = icmp ult i8 %xm, %xc %conv12 = zext i8 %2 to i32 %cmp13 = icmp ult i32 %conv8, %conv12 %cond = select i1 %cmp13, i32 %conv8, i32 %conv12 %cmp20 = icmp ult i32 %conv9, %conv12 %cond27 = select i1 %cmp20, i32 %conv9, i32 %conv12 %xk.0.in = select i1 %cmp, i32 %cond, i32 %cond27 %xk.0 = trunc i32 %xk.0.in to i8 ret i8 %xk.0 }