New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Emit cgt, likewise clt, for comparisons #61483
Comments
(x > y) ? 1 : 0
as cgt
, likewise clt
Removing the branches here will simplify the JIT work and some local tests shows it is generating better IL, IR, and native assembly. The JIT could likewise optimize the It would be beneficial to generate SuperPMI diffs against the prototype compiler if this was felt worthwhile. |
@Tanner Could you clarify the suggestion? Specific C# construct, current IL, proposed IL, etc. |
@AlekseyTs, the suggestion is for the compiler to recognize the Namely, certain comparison IL instructions already guarantee a value of
For
This involves a comparison, a branch, and requires the JIT to do quite a bit more work to recognize the pattern and "do the right thing". Alternatively, the compiler could emit simply:
This is a much simpler sequence that does the same thing. It then also allows branch-free algorithms to more easily be built which in turn would allow a more optimized implementation of |
Can this not be done with some sort of IL prepass or just in the JIT? If the C# compiler has to do this, then so do the VB/F# compilers (and any other IL emitted out there). If we had a prepass tool that could perform these optimizations, they would all benefit, instead of each compiler having to reinvent these optimizations. e.g. something more akin to an LLVM where you can do the work once and everyone can benefit since htey go to the same IR. |
Doing some IL pre-optimization isn't tenable most of the time (it requires integration another tool into the pipeline, and it gets complicated due to needing to also touch PDBs and all the other relevant metadata). The JIT can do it, but it requires more processing of IL and handling of complex branches in a time/resource constrained environment. F# is already doing a small optimization like this, as per the attached PR. |
@CyrusNajmabadi I think the problem is more that there's no direct way of generating an expression-based "cgt" etc. in C#, even though these are present in the underlying IL. It seems there should always be a way of generating each primitive instruction on primitive types. |
Just leaving a note requested by @jaredpar that
|
The JIT is adding support for |
I'm not sure of the right repo for this as it is a suggestion for C# optimization, not language.
I was talking with @tannergooding about the implementation of
CompareTo
in the BCL, which today is this: (likewise for Int64 etc.)He suggested that the following would be faster as long as the
(x > y) ? 1 : 0
and(x < y) ? 1 : 0
are emitted as the .NET instructionscgt
andclt
.We think it is reasonable for the C# compiler to emit these sequences like this when optimization is on.
Context: dotnet/fsharp#13187
The text was updated successfully, but these errors were encountered: