Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/coreclr/tools/Common/Compiler/DevirtualizationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,42 @@ public MethodDesc ResolveVirtualMethod(MethodDesc declMethod, TypeDesc implType,
return ResolveVirtualMethod(declMethod, implType.GetClosestDefType(), out devirtualizationDetail);
}

private bool IsImplicitInterfaceOfSZArray(TypeDesc interfaceType)
{
Debug.Assert(interfaceType.IsInterface);

if (!interfaceType.HasInstantiation)
{
return false;
}

// Is target interface IList<T> or one of its ancestors, or IReadOnlyList<T>?
Comment thread
hez2010 marked this conversation as resolved.
Outdated
if (interfaceType.HasSameTypeDefinition(interfaceType.Context.GetWellKnownType(WellKnownType.IEnumerableGeneric)) ||
interfaceType.HasSameTypeDefinition(interfaceType.Context.GetWellKnownType(WellKnownType.IListGeneric)) ||
interfaceType.HasSameTypeDefinition(interfaceType.Context.GetWellKnownType(WellKnownType.IReadOnlyListGeneric)) ||
interfaceType.HasSameTypeDefinition(interfaceType.Context.GetWellKnownType(WellKnownType.ICollectionGeneric)) ||
interfaceType.HasSameTypeDefinition(interfaceType.Context.GetWellKnownType(WellKnownType.IReadOnlyCollectionGeneric)))
{
return true;
}

return false;
}
Comment thread
hez2010 marked this conversation as resolved.
Outdated

private MethodDesc GetActualImplementationForArrayGenericIListOrIReadOnlyListMethod(MethodDesc declMethod, TypeDesc typeParam)
{
MethodDesc genericImplementor = declMethod.Context.GetWellKnownType(WellKnownType.SZArrayHelper).GetMethod(declMethod.Name, null);
Debug.Assert(genericImplementor != null);
// OPTIMIZATION: For any method other than GetEnumerator(), we can safely substitute
// "Object" for reference-type theT's. This causes fewer methods to be instantiated.
if (genericImplementor.Name != "GetEnumerator"u8 && !typeParam.IsValueType)
{
typeParam = declMethod.Context.GetWellKnownType(WellKnownType.Object);
}
MethodDesc actualImplementor = genericImplementor.MakeInstantiatedMethod(new Instantiation(typeParam));
return actualImplementor;
}

protected virtual MethodDesc ResolveVirtualMethod(MethodDesc declMethod, DefType implType, out CORINFO_DEVIRTUALIZATION_DETAIL devirtualizationDetail)
{
devirtualizationDetail = CORINFO_DEVIRTUALIZATION_DETAIL.CORINFO_DEVIRTUALIZATION_UNKNOWN;
Expand All @@ -71,6 +107,26 @@ protected virtual MethodDesc ResolveVirtualMethod(MethodDesc declMethod, DefType

if (declMethod.OwningType.IsInterface)
{
if (implType.IsWellKnownType(WellKnownType.Array))
{
if (declMethod.OwningType.IsCanonicalSubtype(CanonicalFormKind.Any))
{
devirtualizationDetail = CORINFO_DEVIRTUALIZATION_DETAIL.CORINFO_DEVIRTUALIZATION_FAILED_CANON;
return null;
}
Comment thread
hez2010 marked this conversation as resolved.
Outdated

bool isArrayImplicitInterface = declMethod.OwningType.HasInstantiation && IsImplicitInterfaceOfSZArray(declMethod.OwningType);
if (isArrayImplicitInterface)
{
// The instantiation we want is based on the interface element type, not the
// array element type.
TypeDesc resultElemType = declMethod.OwningType.Instantiation[0];
// We should have ruled this out above.
Debug.Assert(!resultElemType.IsCanonicalSubtype(CanonicalFormKind.Any));
return GetActualImplementationForArrayGenericIListOrIReadOnlyListMethod(declMethod, resultElemType);
Comment thread
hez2010 marked this conversation as resolved.
Outdated
}
}
Comment thread
hez2010 marked this conversation as resolved.
Outdated

if (declMethod.OwningType.IsCanonicalSubtype(CanonicalFormKind.Any) || implType.IsCanonicalSubtype(CanonicalFormKind.Any))
{
DefType[] implTypeRuntimeInterfaces = implType.RuntimeInterfaces;
Expand Down
9 changes: 0 additions & 9 deletions src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,15 +1545,6 @@ private bool resolveVirtualMethod(CORINFO_DEVIRTUALIZATION_INFO* info)
}
}

#if READYTORUN
if (isArray)
{
// Array interface devirt is not yet supported by R2R.
info->detail = CORINFO_DEVIRTUALIZATION_DETAIL.CORINFO_DEVIRTUALIZATION_FAILED_CANON;
return false;
}
#endif

if (requiresInstMethodDescArg)
{
#if READYTORUN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,50 @@ namespace Internal.TypeSystem
{
public abstract partial class MetadataTypeSystemContext : TypeSystemContext
{
private static readonly string[] s_wellKnownTypeNames = new string[] {
"Void",

"Boolean",
"Char",
"SByte",
"Byte",
"Int16",
"UInt16",
"Int32",
"UInt32",
"Int64",
"UInt64",
"IntPtr",
"UIntPtr",
"Single",
"Double",

"ValueType",
"Enum",
"Nullable`1",

"Object",
"String",
"Array",
"MulticastDelegate",

"RuntimeTypeHandle",
"RuntimeMethodHandle",
"RuntimeFieldHandle",

"Exception",

"TypedReference",
};

public static IEnumerable<string> WellKnownTypeNames => s_wellKnownTypeNames;
private static readonly (string Namespace, string TypeName)[] s_wellKnownTypeNames =[
("System", "Void"),

("System", "Boolean"),
("System", "Char"),
("System", "SByte"),
("System", "Byte"),
("System", "Int16"),
("System", "UInt16"),
("System", "Int32"),
("System", "UInt32"),
("System", "Int64"),
("System", "UInt64"),
("System", "IntPtr"),
("System", "UIntPtr"),
("System", "Single"),
("System", "Double"),

("System", "ValueType"),
("System", "Enum"),
("System", "Nullable`1"),

("System", "Object"),
("System", "String"),
("System", "Array"),
("System", "MulticastDelegate"),

("System", "RuntimeTypeHandle"),
("System", "RuntimeMethodHandle"),
("System", "RuntimeFieldHandle"),

("System", "Exception"),

("System", "TypedReference"),

("System", "SZArrayHelper"),
("System.Collections.Generic", "IEnumerable`1"),
("System.Collections.Generic", "IList`1"),
("System.Collections.Generic", "ICollection`1"),
("System.Collections.Generic", "IReadOnlyList`1"),
("System.Collections.Generic", "IReadOnlyCollection`1"),
];

public static IEnumerable<(string Namespace, string TypeName)> WellKnownTypeNames => s_wellKnownTypeNames;
Comment thread
hez2010 marked this conversation as resolved.
Outdated

private MetadataType[] _wellKnownTypes;

Expand All @@ -63,7 +70,7 @@ public virtual void SetSystemModule(ModuleDesc systemModule)
InitializeSystemModule(systemModule);

// Sanity check the name table
Debug.Assert(s_wellKnownTypeNames[(int)WellKnownType.MulticastDelegate - 1] == "MulticastDelegate");
Debug.Assert(s_wellKnownTypeNames[(int)WellKnownType.MulticastDelegate - 1] == ("System", "MulticastDelegate"));

_wellKnownTypes = new MetadataType[s_wellKnownTypeNames.Length];

Expand All @@ -72,7 +79,10 @@ public virtual void SetSystemModule(ModuleDesc systemModule)
{
// Require System.Object to be present as a minimal sanity check.
// The set of required well-known types is not strictly defined since different .NET profiles implement different subsets.
MetadataType type = systemModule.GetType("System"u8, System.Text.Encoding.UTF8.GetBytes(s_wellKnownTypeNames[typeIndex]), throwIfNotFound: typeIndex == (int)WellKnownType.Object);
MetadataType type = systemModule.GetType(
System.Text.Encoding.UTF8.GetBytes(s_wellKnownTypeNames[typeIndex].Namespace),
System.Text.Encoding.UTF8.GetBytes(s_wellKnownTypeNames[typeIndex].TypeName),
throwIfNotFound: typeIndex == (int)WellKnownType.Object);
Comment thread
hez2010 marked this conversation as resolved.
Outdated
if (type != null)
{
type.SetWellKnownType((WellKnownType)(typeIndex + 1));
Expand All @@ -88,7 +98,7 @@ public override DefType GetWellKnownType(WellKnownType wellKnownType, bool throw
int typeIndex = (int)wellKnownType - 1;
DefType type = _wellKnownTypes[typeIndex];
if (type == null && throwIfNotFound)
ThrowHelper.ThrowTypeLoadException("System", s_wellKnownTypeNames[typeIndex], SystemModule);
ThrowHelper.ThrowTypeLoadException(s_wellKnownTypeNames[typeIndex].Namespace, s_wellKnownTypeNames[typeIndex].TypeName, SystemModule);

return type;
}
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Common/TypeDesc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ internal void SetWellKnownType(WellKnownType wellKnownType)
case WellKnownType.Array:
case WellKnownType.MulticastDelegate:
case WellKnownType.Exception:
case WellKnownType.SZArrayHelper:
flags = TypeFlags.Class;
break;

Expand All @@ -122,6 +123,14 @@ internal void SetWellKnownType(WellKnownType wellKnownType)
flags = TypeFlags.ValueType;
break;

case WellKnownType.IEnumerableGeneric:
case WellKnownType.IListGeneric:
case WellKnownType.ICollectionGeneric:
case WellKnownType.IReadOnlyListGeneric:
case WellKnownType.IReadOnlyCollectionGeneric:
flags = TypeFlags.Interface;
break;

default:
throw new ArgumentException();
}
Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/tools/Common/TypeSystem/Common/WellKnownType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,12 @@ public enum WellKnownType
Exception,

TypedReference,

SZArrayHelper,
IEnumerableGeneric,
IListGeneric,
Comment thread
hez2010 marked this conversation as resolved.
Outdated
ICollectionGeneric,
IReadOnlyListGeneric,
IReadOnlyCollectionGeneric,
Comment thread
hez2010 marked this conversation as resolved.
Outdated
}
}
Loading