dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.91k stars 785 forks source link

Optimization issues with inlined il + generated IL #9753

Open KevinRansom opened 4 years ago

KevinRansom commented 4 years ago

While working on this PR: https://github.com/dotnet/fsharp/pull/9715

@abelbraaksma identified optimization issues when combining inline il with other generated code

https://github.com/dotnet/fsharp/pull/9715

@KevinRansom, @cartermp, When working with this fix I noticed some other cases where incorrect boolean optimizations are done. I'm still analyzing that, but this fix produces better IL for the majority of cases.

The optimizer is split in optimizing inline IL and normal code. When it is mixed, esp wrt booleans, it goes wrong and dead IL code is created. What's worse, when combined with null checks, the code didn't eliminate the not, but created code that was many times slower.

This simple fix takes care of all that. Yes, there are now other cases, but the way this fix was done ensures that the new cases are very limited, and that existing optimizations with comparison operators continue to work.

I intend to investigate further for optimizations of boolean expressions, esp boolean negation. But that should go in its own issue. I'll report that separately, as it's a much wider issue than this one (and may end up being wip for some time).

Originally posted by @abelbraaksma in https://github.com/dotnet/fsharp/pull/9715#issuecomment-662087154

abelbraaksma commented 4 years ago

Glad I just saw this, as I was typing a new post as well 😆.

The issue appears to be slightly less worrying as I thought, but it'll take some time to weed out the details and to see what's really going on here.

This is a follow-up to issue https://github.com/dotnet/fsharp/issues/9433.