dotnet/runtime

F# typeof comparison suboptimal codegen

Open

#82,394 opened on Feb 20, 2023

View on GitHub
 (13 comments) (1 reaction) (0 assignees)C# (5,445 forks)batch import
area-CodeGen-coreclrhelp wanted

Repository metrics

Stars
 (17,886 stars)
PR merge metrics
 (Avg merge 12d 11h) (661 merged PRs in 30d)

Description

The following F# snippet:

let test() = typeof<float> = typeof<bool>

Emits this IL:

ldtoken !!a
call class [netstandard]System.Type [netstandard]System.Type::GetTypeFromHandle(valuetype [netstandard]System.RuntimeTypeHandle)
ldtoken [System.Runtime]System.Boolean
call class [netstandard]System.Type [netstandard]System.Type::GetTypeFromHandle(valuetype [netstandard]System.RuntimeTypeHandle)
tail.
call bool [FSharp.Core]Microsoft.FSharp.Core.LanguagePrimitives/HashCompare::GenericEqualityIntrinsic<class [System.Runtime]System.Type>(!!0, !!0)
ret

and this codegen:

_.test()
    L0000: push rsi
    L0001: sub rsp, 0x20
    L0005: mov rcx, 0x7ff9f93c77a8
    L000f: call 0x00007ffa58e77670
    L0014: mov rsi, rax
    L0017: mov rcx, 0x7ff9f934b3c0
    L0021: call 0x00007ffa58e77670
    L0026: mov r8, rax
    L0029: mov rdx, rsi
    L002c: mov rcx, 0x7ff9febc1e88
    L0036: mov rax, 0x7ff9feb43be8
    L0040: add rsp, 0x20
    L0044: pop rsi
    L0045: jmp qword ptr [rax]

expected codegen:

_.test()
    L0000: xor eax, eax
    L0002: ret

Contributor guide