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
Inefficient codegen with ternary and additive inverse #135647
I don't see how that is valid codegen for this IL btw - bool can be any value 0-255, and that codegen is only correct for 0-1 - 2-255 bool is UB in C# but not in ECMA-335 - you would have to write C# like num - 1 + (r ? 1 : 0) and change roslyn to get it to emit IL more like:
ldarg.0ldc.i4.1subldarg.1addret
(that is, extend the bool ? 1 : 0 -> (int)bool optimization to any bool, not just for direct IL comparison sequences like x >= 0 ? 1 : 0).
Activity
dotnet-policy-service commentedon Oct 12, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
hamarb123 commentedon Oct 13, 2026
I don't see how that is valid codegen for this IL btw -
boolcan be any value0-255, and that codegen is only correct for0-1-2-255bool is UB in C# but not in ECMA-335 - you would have to write C# likenum - 1 + (r ? 1 : 0)and change roslyn to get it to emit IL more like:(that is, extend the
bool ? 1 : 0->(int)booloptimization to any bool, not just for direct IL comparison sequences likex >= 0 ? 1 : 0).As you can see, it works as expected if I insert the cast using
Unsafe.BitCastinstead: https://godbolt.org/z/78PMe5cPdhamarb123 commentedon Oct 13, 2026
https://github.com/dotnet/roslyn/blob/main/docs/compilers/Boolean%20Representation.md