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
Problem when comparing boolean values after IL conv.i1 #12872
I am working on some tests for an DynamicMehtod IL generator called Sigil and I hit some interesting behavior on comparing booleans:
The dynamic method (Func<int, bool>) looks like:
ldarg.0
conv.i1
ret
When I invoke this function using 1 everything goes as expected for true. Although, when I invoke this function using 2 and start testing the results it happens that Visual Studio debugger and variable dumps (using Console.Write) states that it is true and ==comparisons succeed, but true.Equals() returns false. I have tried casting, making a boolean AND & operation with true, all with the same result.
Is this the expected behavior for this IL operation? It seems that Boolean.Equals(bool) behaves differently from Boolean.Equals(object?).
Also it seems that the second one is using the base Object== operator which is not defined as an operator on this object, so I infer it calls Object.Equals(objec) internally:
Although I found no code counterpart of this RuntimeHelpers.Equals(this, object), I guess it comes from the runtime and my guess would be that it calls the ObjectNative::Equals(Object, Object)
Do you have a particular need to support Boolean values other than 0 or 1? Neither the C# compiler nor the JIT compiler acknowledge the existence of such values and optimize as if only 0 and 1 are possible.
The question actually was: Is this the expected behavior for this IL operation?
Yes. Neither conv.i1 nor the IL evaluation stack do not know anything about bool values. conv.i1 simply pops an i4 value from the stack, sign extends the lower byte of it to i4 and pushes the resulting value back to the IL stack. There is no such thing as a bool value on the evaluation stack. Basically it's almost the same as an enum:
enumBoolean:byte{False=0,True=1}
I would say that it should be at least consistent:
Being consistent comes at a cost. Basically the C# compiler would need to normalize every bool value to ensure that it's either 0 or 1. It would also prevent some optimizations, such as the one that's responsible for true == value = True.
For code like if (value == true) the C# compiler naturally generates a brtrue/brfalse instruction - these instructions check for a 0/non-0 value (again, IL operates with integers, not bools). On the other hand, Equals generates a ceq instruction to compare the bool values - of course that true.Equals(value) is false because 1 != 2.
Very enlightening. I was aware about the non-existence of a bool type underneath C#, on the IL level. But I had no idea how C# does the conversion of the comparison expressions to IL. That should be on the Roslyn code, right? Maybe I should take a look there first next time.
So, from your answer that is very clear that this should be the expected behavior. Thank you for taking your time to explain me this. I guess the tests should reflect that on the kevin-montrose/Sigil project I am trying to update and add some fixes.
But I had no idea how C# does the conversion of the comparison expressions to IL. That should be on the Roslyn code, right? Maybe I should take a look there first next time.
Yes, note that most (likely all) operators on primitive types are translated directly into IL instructions, there are no actual operator overloads on the types themselves. In the case of == and != you'd typically get ceq, beq, bne (which for bools only work "correctly" if the values are 0/1) but also brtrue or brfalse (which work with 0/non-zero bools). As the document linked to by @tannergooding above describes, this is choice specifically made by the C# compiler.
I guess the tests should reflect that on the kevin-montrose/Sigil project I am trying to update and add some fixes.
I'm inclined to remove any test that relies on non 0/1 bools. The behavior of these is basically undefined. It's not only equality that is affected, both thee C# compiler and the JIT compiler also make other optimizations that assume 0/1 bools - for example transforming a && b into a & b. The later works correctly only with 0/1 bools.
Activity
arlm commentedon Jun 12, 2019
I have some test cases here:
https://dotnetfiddle.net/ulqU8N
and here
https://gist.github.com/arlm/da7cb15b22dcbd231852c55b38038ca5
with the output:
mikedn commentedon Jun 12, 2019
Do you have a particular need to support Boolean values other than 0 or 1? Neither the C# compiler nor the JIT compiler acknowledge the existence of such values and optimize as if only 0 and 1 are possible.
arlm commentedon Jun 12, 2019
No, no actual need to support that. The thing is that during tests I have hit this odd behavior. I would say that it should be at least consistent:
true == value = Trueand
true.Equals(value) = FalseI would expect both to return
falseor both to returntrue.The question actually was:
The rest was only my findings which were not conclusive on the expected behavior from the source-code analysis.
I would say that the question still holds: Is that an expected behavior? (
==andtrue. Equalsto behave differently)mikedn commentedon Jun 12, 2019
Yes. Neither
conv.i1nor the IL evaluation stack do not know anything aboutboolvalues.conv.i1simply pops ani4value from the stack, sign extends the lower byte of it toi4and pushes the resulting value back to the IL stack. There is no such thing as aboolvalue on the evaluation stack. Basically it's almost the same as an enum:Being consistent comes at a cost. Basically the C# compiler would need to normalize every
boolvalue to ensure that it's either 0 or 1. It would also prevent some optimizations, such as the one that's responsible fortrue == value = True.For code like
if (value == true)the C# compiler naturally generates abrtrue/brfalseinstruction - these instructions check for a 0/non-0 value (again, IL operates with integers, not bools). On the other hand,Equalsgenerates aceqinstruction to compare theboolvalues - of course thattrue.Equals(value)is false because1 != 2.arlm commentedon Jun 12, 2019
Very enlightening. I was aware about the non-existence of a
booltype underneath C#, on the IL level. But I had no idea how C# does the conversion of the comparison expressions to IL. That should be on the Roslyn code, right? Maybe I should take a look there first next time.So, from your answer that is very clear that this should be the expected behavior. Thank you for taking your time to explain me this. I guess the tests should reflect that on the kevin-montrose/Sigil project I am trying to update and add some fixes.
https://github.com/arlm/Sigil-vNext/blob/5f0ac692f05ad9c23a351cf484a7611cff61a050/SigilTests/Converts.cs#L156-L165
tannergooding commentedon Jun 12, 2019
@arlm, you might be interested in https://github.com/gafter/roslyn/blob/master/docs/compilers/Boolean%20Representation.md and dotnet/roslyn#24652.
Changing tests to reflect the expected behavior.
Joe4evr commentedon Jun 13, 2019
Here's some more related information about bools.
mikedn commentedon Jun 13, 2019
Yes, note that most (likely all) operators on primitive types are translated directly into IL instructions, there are no actual operator overloads on the types themselves. In the case of
==and!=you'd typically getceq,beq,bne(which for bools only work "correctly" if the values are 0/1) but alsobrtrueorbrfalse(which work with 0/non-zero bools). As the document linked to by @tannergooding above describes, this is choice specifically made by the C# compiler.I'm inclined to remove any test that relies on non 0/1 bools. The behavior of these is basically undefined. It's not only equality that is affected, both thee C# compiler and the JIT compiler also make other optimizations that assume 0/1 bools - for example transforming
a && bintoa & b. The later works correctly only with 0/1 bools.Changing tests to reflect the expected behavior.
Changing tests to reflect the expected behavior.
Changing tests to reflect the expected behavior.