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

[Optimization] Compiler creates branches with allocs that can never be hit #8983

Open
NinoFloris opened this issue Apr 17, 2020 · 1 comment
Labels
Area-Compiler-Optimization The F# optimizer, release code gen etc. Feature Request
Milestone

Comments

@NinoFloris
Copy link
Contributor

Compiler creates branches with allocs that can never be hit, or it may create superfluous allocs that are never really used, the JIT will not be able to figure that out though.

https://sharplab.io/#v2:DYLgZgzgPsCmAuACAlgO2G2iC2BDADgPL7zID2qOB+sATomSeZQLyICwAUIj1fAMYALBkwqIA7snjCuvRFEQBlMtiwAPRAFoAfEpWwAFHnw16agJSzeCgHIUsOxHdSwuXOElzBsZCPACitLgQsAAmxKQUBuaILFY8HogAnojRsYgAzPEc3Lx4AsLKqgZJ0TFQusYRzKlgAK6UGo4aANSIAEwxktI5cgpF6lq6AKzZtvZDiAAs2e4IiA0u/LAQELi0SdVRMXG5CfMpaWxZe715uAV6xaXm5ZUEW5QG9Y2TrR1dUjKn/fqITbo1GMnBNHFMgA=

let inline mapOption mapper option = 
    match option with 
    | Some x -> Some(mapper x)
    | None -> None

let almostErasedOption() =
    let y () = 3
    
    match Some(y()) |> mapOption (fun x -> x + 2) with 
    | Some x -> 5
    | None -> 4
    
let unnecessaryOption() =
    let y () = 3
    
    match Some(y()) |> mapOption (fun x -> x + 2) with 
    | Some x -> x
    | None -> 4

Decompiled:

    public static int almostErasedOption()
    {
        if (FSharpOption<int>.Some(5) == null)
        {
            return 4;
        }
        return 5;
    }

    public static int unnecessaryOption()
    {
        FSharpOption<int> fSharpOption = FSharpOption<int>.Some(5);
        if (fSharpOption == null)
        {
            return 4;
        }
        return fSharpOption.Value;
    }

The compiler is doing a pretty good job at constant folding but it really falls short by such a tiny margin to remove all the useless code.

These examples are contrived but in real cases cause inline code to not have the desired performance increase. Forcing people to write unidiomatic nullable reference returns or tricks with byref out vars.

Related:
athas/raytracers#29
dotnet/coreclr#21950 (comment)
#8953

ValueOption is worse at inlining, especially without #8970 https://sharplab.io/#v2:DYLgZgzgPsCmAuACAlgO2G2iC2BDADgPL7zID2qOB+sATomSeZQLyICwAUIj1fAMYALBkwqIA7snjCuvRFEQA1XMACusAMplsWAB6IAtAD4lK9Vp0AKPPhr1dASlm8FytbAByFLMdPuvqLBcXHBIKthkEPAAorS4ELAAJsSkFJYOiCzOPKGIAJ6I6ZmIAMzZHNy8eALCbubasJZ56RlQJjYpzIVgqpT6vvoA1IgATBmS0hVyrmaaDYj9JgCs5TP+3oYmACzlIQiIvYH8sBAQuLR5nWkZWZU5+wVFbGV3U1W4NX71Vs0Ore0EK6USw9PqbBaIYZjCRSGSvNbfPTg3SrL6eDa+LZAA
@dsyme
Copy link
Contributor

dsyme commented Sep 1, 2020

I'll relabel this to feature improvement as there's specific thing that says we'll definitely remove all such bindings, especially when optimizations interact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-Optimization The F# optimizer, release code gen etc. Feature Request
Projects
Status: New
Development

No branches or pull requests

4 participants