Skip to content
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

Compiler should optimize common boolean return patterns #34726

Open
john-h-k opened this issue Apr 3, 2019 · 9 comments
Open

Compiler should optimize common boolean return patterns #34726

john-h-k opened this issue Apr 3, 2019 · 9 comments
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@john-h-k
Copy link
Contributor

john-h-k commented Apr 3, 2019

Version Used:
Rosyln master branch from 2nd April 2019

Steps to Reproduce:
[Sharplab Demo]

  1. Have this code:
    public bool Proper(object o) { return o is object; }
  2. Also, have this unoptimized version
    public bool ShouldBeOptimized(object o) { if (o is object) { return true; } return false; }

Expected Behavior:
Identical IL output, as they are semantically identical. This wouldn't be an issue but it does affect JIT output seemingly (JIT64 desktop, tested on Sharplab above) - it switches a setcc ; movzx pattern to a full branch, which is not ideal

Actual Behavior:
IL generation has an additional branch and changes the generated JIT

It is a very minor issue but was recommended to create an issue for it

@CyrusNajmabadi
Copy link
Member

This feels like something the JIT should recognize and optimize. otherwise, every front-end language would need this optimization.

@john-h-k
Copy link
Contributor Author

john-h-k commented Apr 3, 2019

Turning a if (bool) return true else return false pattern into a return bool pattern feels like an optimization the frontend would be responsible because it such a simple one imo. However, it would be good for the JIT to recognise it yes

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Apr 3, 2019

Turning a if (bool) return true else return false pattern into a return bool pattern feels like an optimization the frontend would be responsible because it such a simple one imo.

I disagree. Front ends should do the analysis/optimziation for patterns that are too complex for the backend to do, or which require knowledge about teh constraints on the front end that hte back end doesn't know are there. For example, for patterns, we've said order of matching is undefined. That gives the front end a lot of options for optimizing things. Back-end wouldn't be able to do that because it doesn't know if that would break semantics the front-end expects.

ON the other hand, simple stuff is great for the backend. It doesn't cost much, woudl apply to all languages, and can be done safely.

However, it would be good for the JIT to recognise it yes

Agreed :D

@tannergooding
Copy link
Member

(I know @CyrusNajmabadi and I have had this discussion in the past and didn't agree then, but I'll say it again here 😄)

While I agree it would be good for the backend to recognize; we will never be in a world where the JIT can do all of these optimizations. The JIT is a live compiler and has its own constraints; while the C# compiler is generally not live and is free to spend an appropriate amount of time on analysis/optimizations.

The JIT, due to it being live, has to make some trade-offs on what optimizations it recognizes and performs. Tiered compilation is going to improve it somewhat, but it still has to make some heuristic guesses about what will or won't be profitable. Often, these optimizations are simply based on things like "how many bytes of IL exist in the method I am about to compile" which can greatly impact things like inlining or how well it is able to analyze the overall method.

The primary downside to the C# compiler having these optimizations is that it also means that F#, VB, etc all need to implement similar optimizations. A good trade-off might be to have an intermediate ilopt.exe program that does these general-purpose optimizations. However, that itself comes with its own complexities such as also impacting PDBs (and debugging in general), sequence points, instrumentation, other post-IL rewriting programs, sourcelink, etc.

@CyrusNajmabadi
Copy link
Member

A good trade-off might be to have an intermediate ilopt.exe program that does these general-purpose optimizations. However, that itself comes with its own complexities such as also impacting PDBs (and debugging in general), sequence points, instrumentation, other post-IL rewriting programs, sourcelink, etc.

I would be ok with that :)

The JIT, due to it being live, has to make some trade-offs on what optimizations it recognizes and performs.

True... though i would certainly hope test -> return true/false could fit into what it recognized :)

@CyrusNajmabadi
Copy link
Member

(I know @CyrusNajmabadi and I have had this discussion in the past and didn't agree then, but I'll say it again here 😄)

<3

@gafter gafter added this to the Backlog milestone Apr 8, 2019
@gafter gafter added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Apr 8, 2019
@gafter
Copy link
Member

gafter commented Apr 8, 2019

I'm putting this up for grabs. I think we'd take a solution as a community contribution as long as it isn't very complicated.

@john-h-k
Copy link
Contributor Author

john-h-k commented Jul 21, 2019

I can try and give this a shot. Should the transformation go in the LocalRewriter or in the emit optimizer? ( apparently I should tag you to ask 😄 @gafter )

@gafter
Copy link
Member

gafter commented Jul 22, 2019

@johnkellyoxford I have no idea. I suspect a change would have to cross some boundaries, but without experimenting I just don't know.

@gafter gafter added the Code Gen Quality Room for improvement in the quality of the compiler's generated code label Sep 22, 2019
MykolaBalakin added a commit to MykolaBalakin/roslyn that referenced this issue Nov 21, 2019
MykolaBalakin added a commit to MykolaBalakin/roslyn that referenced this issue Nov 21, 2019
MykolaBalakin added a commit to MykolaBalakin/roslyn that referenced this issue Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Code Gen Quality Room for improvement in the quality of the compiler's generated code help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
None yet
Development

No branches or pull requests

5 participants