Closed
Description
Description
JIT cannot devirtualize interface call in very trivial cases.
Source code:
using System;
using System.Runtime.CompilerServices;
public static class C {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static T As<T>(T obj) where T : class => obj;
public static int Cmp(String str) => As<IComparable<string>>(str).CompareTo(string.Empty);
public static int Cmp2(String str) => str.CompareTo(string.Empty);
public static int Cmp3(String str) => ((IComparable<string>)str).CompareTo(string.Empty);
public static int Cmp4(IComparable<string> str) => str.CompareTo(string.Empty);
}
Output assembly:
C.Cmp(System.String)
L0000: push ebp
L0001: mov ebp, esp
L0003: mov edx, [0x8d72018]
L0009: call dword ptr [0x11317000]
L000f: pop ebp
L0010: ret
C.Cmp2(System.String)
L0000: mov edx, [0x8d72018]
L0006: cmp [ecx], ecx
L0008: push 0
L000a: call System.String.Compare(System.String, System.String, System.StringComparison)
L000f: ret
C.Cmp3(System.String)
L0000: push ebp
L0001: mov ebp, esp
L0003: mov edx, [0x8d72018]
L0009: call dword ptr [0x11317004]
L000f: pop ebp
L0010: ret
C.Cmp4(System.IComparable`1<System.String>)
L0000: push ebp
L0001: mov ebp, esp
L0003: mov edx, [0x8d72018]
L0009: call dword ptr [0x11317008]
L000f: pop ebp
L0010: ret
Cmp2
is correctly devirtualized and inlined. I'm expecting that the assembly code should be the same for all cases except Cmp4
.
Configuration
.NET 6.0.101
Ubuntu 20.04.3 5.4.0-91-generic x86_64
Regression?
Probably yes, but I didn't check.