You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[RyuJIT] The "reuse flags" optimization handles compares incorrectly #9059
Unfortunately it looks like signed LT/LE/GT/GE relops are broken as well due to overflow:
staticboolTest(intx)=>(x+42)>0;
returns true when x == int.MaxValue. But it should return false because due to integer overflow x + 42 is actually negative.
This is a valid optimization in the C/C++ world of "undefined behavior" but it is likely not valid in the .NET world. I've yet to find anything in the ECMA spec that clearly states this is not valid but I'm going to assume that it is not valid anyway.
The optimization is valid for AND/OR/XOR (they never overflow) but it's pointless. It's not very likely that someone will write code like (x & y) < 0.
The optimization can be made to work for ADD/SUB + LT/GE but requires generating JS/JNS/SETS/SETNS instead of JL/JGE/SETL/SETLE like we do now. Unfortunately we don't currently have means to generate JS & co. (from lowering, the emitter does support these instructions).
There's no way to get this work for ADD/SUB + LE/GT. That would require instructions like JSE/JNSE which do not exist.
Unfortunately it looks like we'll have to go back to supporting only EQ/NE.
changed the title [-][RyuJIT] The "reuse flags" optimization handles unsigned compares incorrectly[/-][+][RyuJIT] The "reuse flags" optimization handles compares incorrectly[/+]on Oct 4, 2017
Are we losing information by not knowing how the flags were produced?
Hmm, more like the opposite. We're using information (flags) that that was previously (correctly) ignored and that results in a change in behavior. Consider the x + 42 > 0 example, without this optimization it generates something like:
add w0, w1 w2tst w0, w0cset w0, gt
The gt condition code means Z == 0 && N == V so it involves both N and V flags. After tstV is always 0 and N contains the sign bit of w0. So the result solely dependeds on the sign of the result.
If we eliminate tst we'll be using the flags set by add. N will again contain the sign bit of w0 but V will contain 1 if signed addition overflowed. This time the result depends on the result sign and on whether the addition operation overflow or not. If the operation doesn't overflow we get the same result as before. If it does overflow we get a different result.
Would it be easier if the compare contained the subtract et. all.?
I'm not sure what you mean. I don't think there's any way to fix this particular example.
x + 42 >= 0 can be fixed by generating cset w0, pl instead of cset w0, ge but we don't have a way to encode conditions such as pl and mi. Well, I actually do have a change that allows such conditions on JCC/SETCC but I'm not sure it's worth the trouble.
C++ compilers have more freedom and they can "optimize" all signed relops. For example GCC generates
cmn w0, dotnet/coreclr#41cset w0, ge
for
intfoo(int x) {
return (x + 42) > 0;
}
It's different from what the JIT tried to do but it has the exact same behavior when it comes to overflow, foo(int.MaxValue) returns true instead of false.
And then this kind of pattern matching done by the JIT is easily defeated by the presence of lclvars that prevent the JIT from "seeing" the arithmetic operation that sets the flags. That means that cases where this optimization is likely to be most useful - loops - won't be recognized anyway. See for loop example in #6794.
So the number of cases where a corrected optimization would kick in is likely to pretty low. Oh well, since I already have most of the work done I could give it a try and see how it looks.
Activity
mikedn commentedon Oct 4, 2017
cc @CarolEidt @sdmaclea I'll have a fix for this later today.
mikedn commentedon Oct 4, 2017
Unfortunately it looks like signed LT/LE/GT/GE relops are broken as well due to overflow:
returns
truewhenx == int.MaxValue. But it should returnfalsebecause due to integer overflowx + 42is actually negative.This is a valid optimization in the C/C++ world of "undefined behavior" but it is likely not valid in the .NET world. I've yet to find anything in the ECMA spec that clearly states this is not valid but I'm going to assume that it is not valid anyway.
The optimization is valid for AND/OR/XOR (they never overflow) but it's pointless. It's not very likely that someone will write code like
(x & y) < 0.The optimization can be made to work for ADD/SUB + LT/GE but requires generating JS/JNS/SETS/SETNS instead of JL/JGE/SETL/SETLE like we do now. Unfortunately we don't currently have means to generate JS & co. (from lowering, the emitter does support these instructions).
There's no way to get this work for ADD/SUB + LE/GT. That would require instructions like JSE/JNSE which do not exist.
Unfortunately it looks like we'll have to go back to supporting only EQ/NE.
[-][RyuJIT] The "reuse flags" optimization handles unsigned compares incorrectly[/-][+][RyuJIT] The "reuse flags" optimization handles compares incorrectly[/+]sdmaclea commentedon Oct 4, 2017
I will need to think about this some more, but it is not immediately obvious why this would be difficult/impossible.
Are we losing information by not knowing how the flags were produced?
Would it be easier if the compare contained the subtract et. all.?
mikedn commentedon Oct 4, 2017
Hmm, more like the opposite. We're using information (flags) that that was previously (correctly) ignored and that results in a change in behavior. Consider the
x + 42 > 0example, without this optimization it generates something like:The
gtcondition code meansZ == 0 && N == Vso it involves bothNandVflags. AftertstVis always 0 andNcontains the sign bit ofw0. So the result solely dependeds on the sign of the result.If we eliminate
tstwe'll be using the flags set byadd.Nwill again contain the sign bit ofw0butVwill contain 1 if signed addition overflowed. This time the result depends on the result sign and on whether the addition operation overflow or not. If the operation doesn't overflow we get the same result as before. If it does overflow we get a different result.I'm not sure what you mean. I don't think there's any way to fix this particular example.
x + 42 >= 0can be fixed by generatingcset w0, plinstead ofcset w0, gebut we don't have a way to encode conditions such asplandmi. Well, I actually do have a change that allows such conditions on JCC/SETCC but I'm not sure it's worth the trouble.sdmaclea commentedon Oct 4, 2017
Why wouldn't it be worth the trouble? Isn't that what a C++ compiler would do?
mikedn commentedon Oct 5, 2017
C++ compilers have more freedom and they can "optimize" all signed relops. For example GCC generates
for
It's different from what the JIT tried to do but it has the exact same behavior when it comes to overflow,
foo(int.MaxValue)returnstrueinstead offalse.And then this kind of pattern matching done by the JIT is easily defeated by the presence of lclvars that prevent the JIT from "seeing" the arithmetic operation that sets the flags. That means that cases where this optimization is likely to be most useful - loops - won't be recognized anyway. See
forloop example in #6794.So the number of cases where a corrected optimization would kick in is likely to pretty low. Oh well, since I already have most of the work done I could give it a try and see how it looks.