Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ internal static partial class VersionResilientHashCode
public static int TypeHashCode(RuntimeType type)
=> TypeHashCode(new QCallTypeHandle(ref type));

private static int NameHashCode(string s1, string s2)
=> NameHashCode(System.Text.Encoding.UTF8.GetBytes(s1), System.Text.Encoding.UTF8.GetBytes(s2));

/// <summary>
/// CoreCLR 1-parameter <a href="https://github.com/dotnet/runtime/blob/17154bd7b8f21d6d8d6fca71b89d7dcb705ec32b/src/coreclr/vm/versionresilienthashcode.cpp#L109">GetVersionResilientTypeHashCode</a>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public string GetName()
return Reader.GetString(method.Name);
}

public ReadOnlySpan<byte> Name
{
get
{
Method method = Reader.GetMethod(Handle);
return Reader.ReadStringAsBytes(method.Name);
}
}

public override bool Equals(object? compare)
{
if (compare == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override int GetHashCode()
{
if (!_hashCode.HasValue)
{
_hashCode = _declaringTypeHandle.GetHashCode() ^ TypeHashingAlgorithms.ComputeGenericInstanceHashCode(TypeHashingAlgorithms.ComputeNameHashCode(_methodNameAndSignature.GetName()), _genericMethodArgumentHandles);
_hashCode = _declaringTypeHandle.GetHashCode() ^ VersionResilientHashCode.GenericInstanceHashCode(VersionResilientHashCode.NameHashCode(_methodNameAndSignature.Name), _genericMethodArgumentHandles);
}
return _hashCode.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override int GetHashCode()
{
if (!_hashCode.HasValue)
{
_hashCode = TypeHashingAlgorithms.ComputeGenericInstanceHashCode(_genericTypeDefinitionHandle.GetHashCode(), _genericTypeArgumentHandles);
_hashCode = VersionResilientHashCode.GenericInstanceHashCode(_genericTypeDefinitionHandle.GetHashCode(), _genericTypeArgumentHandles);
}
return _hashCode.Value;
}
Expand Down Expand Up @@ -114,7 +114,7 @@ internal GenericTypeLookupData(RuntimeTypeHandle genericTypeDefinitionHandle, Ru

internal int LookupHashCode()
{
return _typeToLookup != null ? _typeToLookup.GetHashCode() : TypeHashingAlgorithms.ComputeGenericInstanceHashCode(_genericTypeDefinitionHandle.GetHashCode(), _genericTypeArgumentHandles);
return _typeToLookup != null ? _typeToLookup.GetHashCode() : VersionResilientHashCode.GenericInstanceHashCode(_genericTypeDefinitionHandle.GetHashCode(), _genericTypeArgumentHandles);
}

internal bool MatchParsedEntry(RuntimeTypeHandle tentativeType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ internal static InstantiatedMethod GVMLookupForSlotWorker(DefType targetType, In
sb.AppendLine();
sb.AppendLine("Declaring type: " + GetTypeNameDebug(slotMethod.OwningType));
sb.AppendLine("Target type: " + GetTypeNameDebug(targetType));
sb.AppendLine("Method name: " + slotMethod.Name);
sb.AppendLine("Method name: " + slotMethod.GetName());
sb.AppendLine("Instantiation:");
for (int i = 0; i < slotMethod.Instantiation.Length; i++)
{
Expand Down Expand Up @@ -134,7 +134,7 @@ internal unsafe IntPtr ResolveGenericVirtualMethodTarget(RuntimeTypeHandle type,
sb.AppendLine("Failed to create generic virtual method implementation");
sb.AppendLine();
sb.AppendLine("Declaring type: " + GetTypeNameDebug(result.OwningType));
sb.AppendLine("Method name: " + result.Name);
sb.AppendLine("Method name: " + result.GetName());
sb.AppendLine("Instantiation:");
for (int i = 0; i < result.Instantiation.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public bool Equals(RuntimeMethodHandleKey other)
public override int GetHashCode()
=> _handle.GetHashCode() ^ (_genericArgs == null
? _declaringType.GetHashCode()
: TypeHashingAlgorithms.ComputeGenericInstanceHashCode(_declaringType.GetHashCode(), _genericArgs));
: VersionResilientHashCode.GenericInstanceHashCode(_declaringType.GetHashCode(), _genericArgs));
}

private LowLevelDictionary<RuntimeFieldHandleKey, RuntimeFieldHandle> _runtimeFieldHandles = new LowLevelDictionary<RuntimeFieldHandleKey, RuntimeFieldHandle>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static unsafe bool TryGetArrayTypeForNonDynamicElementType(RuntimeTypeHan
arrayTypeHandle = default(RuntimeTypeHandle);

Debug.Assert(isMdArray || rank == -1);
int arrayHashcode = TypeHashingAlgorithms.ComputeArrayTypeHashCode(elementTypeHandle.GetHashCode(), rank);
int arrayHashcode = VersionResilientHashCode.ArrayTypeHashCode(elementTypeHandle.GetHashCode(), rank == -1 ? 1 : rank);

// Note: ReflectionMapBlob.ArrayMap may not exist in the module that contains the element type.
// So we must enumerate all loaded modules in order to find ArrayMap and the array type for
Expand Down Expand Up @@ -239,13 +239,13 @@ public static unsafe bool TryGetArrayTypeForNonDynamicElementType(RuntimeTypeHan

public static unsafe bool TryGetByRefTypeForNonDynamicElementType(RuntimeTypeHandle elementTypeHandle, out RuntimeTypeHandle pointerTypeHandle)
{
int byRefHashcode = TypeHashingAlgorithms.ComputeByrefTypeHashCode(elementTypeHandle.GetHashCode());
int byRefHashcode = VersionResilientHashCode.ByrefTypeHashCode(elementTypeHandle.GetHashCode());
return TryGetParameterizedTypeForNonDynamicElementType(elementTypeHandle, byRefHashcode, ReflectionMapBlob.ByRefTypeMap, out pointerTypeHandle);
}

public static unsafe bool TryGetPointerTypeForNonDynamicElementType(RuntimeTypeHandle elementTypeHandle, out RuntimeTypeHandle pointerTypeHandle)
{
int pointerHashcode = TypeHashingAlgorithms.ComputePointerTypeHashCode(elementTypeHandle.GetHashCode());
int pointerHashcode = VersionResilientHashCode.PointerTypeHashCode(elementTypeHandle.GetHashCode());
return TryGetParameterizedTypeForNonDynamicElementType(elementTypeHandle, pointerHashcode, ReflectionMapBlob.PointerTypeMap, out pointerTypeHandle);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ private static string GetTypeName(DefType type)
if (type is NoMetadata.NoMetadataType)
return ((NoMetadata.NoMetadataType)type).NameForDiagnostics;

return type.Name;
return type.GetName();
}

private static string GetTypeNamespace(DefType type)
{
if (type is NoMetadata.NoMetadataType)
return ((NoMetadata.NoMetadataType)type).NamespaceForDiagnostics;

return type.Namespace;
return type.GetNamespace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override MethodNameAndSignature NameAndSignature
#if DEBUG
public override string ToString()
{
return OwningType.ToString() + "." + Name;
return OwningType.ToString() + "." + GetName();
}
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public override MethodNameAndSignature NameAndSignature
}
}

public override string Name
public override ReadOnlySpan<byte> Name
{
get
{
return NameAndSignature.GetName();
return NameAndSignature.Name;
}
}

Expand Down Expand Up @@ -163,7 +163,7 @@ public override bool HasCustomAttribute(string attributeNamespace, string attrib

public override string ToString()
{
string result = OwningType.ToString() + ".Method(" + Name + ")";
string result = OwningType.ToString() + ".Method(" + GetName() + ")";
return result;
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public GenericTypeInstanceKey(DefType typeDefinition, Instantiation instantiatio
_typeDefinition = typeDefinition;
_instantiation = instantiation;

_hashCode = instantiation.ComputeGenericInstanceHashCode(typeDefinition.GetHashCode());
_hashCode = VersionResilientHashCode.GenericInstanceHashCode(typeDefinition.GetHashCode(), _instantiation);
}

public bool Equals(GenericTypeInstanceKey other)
Expand Down Expand Up @@ -331,7 +331,7 @@ public RuntimeMethodKey(bool unboxingStub, DefType owningType, MethodNameAndSign
_owningType = owningType;
_methodNameAndSignature = nameAndSignature;

_hashCode = TypeHashingAlgorithms.ComputeMethodHashCode(owningType.GetHashCode(), TypeHashingAlgorithms.ComputeNameHashCode(nameAndSignature.GetName()));
_hashCode = owningType.GetHashCode() ^ VersionResilientHashCode.NameHashCode(nameAndSignature.Name);
}

public class RuntimeMethodKeyHashtable : LockFreeReaderHashtable<RuntimeMethodKey, MethodDesc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<Compile Include="$(AotCommonPath)\Internal\Runtime\MethodTable.cs">
<Link>MethodTable.cs</Link>
</Compile>
<Compile Include="$(LibrariesProjectRoot)\Common\src\Internal\VersionResilientHashCode.cs">
<Link>TypeSystem\Common\VersionResilientHashCode.cs</Link>
</Compile>
<Compile Include="$(CompilerCommonPath)\Internal\Runtime\MethodTable.Constants.cs">
<Link>MethodTable.Constants.cs</Link>
</Compile>
Expand Down Expand Up @@ -201,6 +204,9 @@
<Compile Include="$(CompilerCommonPath)\TypeSystem\Common\Utilities\TypeNameFormatter.cs">
<Link>TypeNameFormatter.cs</Link>
</Compile>
<Compile Include="$(CompilerCommonPath)\TypeSystem\Common\VersionResilientHashCode.TypeSystem.cs">
<Link>TypeSystem\Common\VersionResilientHashCode.TypeSystem.cs</Link>
</Compile>
<Compile Include="$(CompilerCommonPath)\TypeSystem\Common\VirtualMethodAlgorithm.cs">
<Link>Internal\TypeSystem\VirtualMethodAlgorithm.cs</Link>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace ILLink.Shared.TypeSystemProxy

public readonly MethodDesc Method;

public string Name { get => Method.Name; }
public string Name { get => Method.GetName(); }

public string GetDisplayName() => Method.GetDisplayName();

Expand Down
33 changes: 27 additions & 6 deletions src/coreclr/tools/Common/Compiler/Dataflow/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ internal static class TypeExtensions
{
public static bool IsTypeOf(this TypeDesc type, string ns, string name)
{
return type is MetadataType mdType && mdType.Name == name && mdType.Namespace == ns;
return type is MetadataType mdType && mdType.Name.StringEquals(name) && mdType.Namespace.StringEquals(ns);
}

public static bool IsTypeOf(this TypeDesc type, string fullTypeName)
{
if (type is not MetadataType metadataType)
return false;

string metadataTypeName = metadataType.GetName();

var name = fullTypeName.AsSpan();
if (metadataType.Name.Length + 1 > name.Length)
if (metadataTypeName.Length + 1 > name.Length)
return false;

if (!name.Slice(name.Length - metadataType.Name.Length).Equals(metadataType.Name.AsSpan(), StringComparison.Ordinal))
if (!name.Slice(name.Length - metadataTypeName.Length).Equals(metadataTypeName.AsSpan(), StringComparison.Ordinal))
return false;

if (name[name.Length - metadataType.Name.Length - 1] != '.')
if (name[name.Length - metadataTypeName.Length - 1] != '.')
return false;

return name.Slice(0, name.Length - metadataType.Name.Length - 1).Equals(metadataType.Namespace, StringComparison.Ordinal);
return name.Slice(0, name.Length - metadataTypeName.Length - 1).Equals(metadataType.GetNamespace(), StringComparison.Ordinal);
}

public static bool IsTypeOf(this TypeDesc type, ILLinkSharedWellKnownType wellKnownType) =>
Expand All @@ -43,12 +45,31 @@ public static bool IsTypeOf(this TypeDesc type, ILLinkSharedWellKnownType wellKn
ILLinkSharedWellKnownType.System_String => type.IsWellKnownType(TypeSystemWellKnownType.String),
ILLinkSharedWellKnownType.System_Object => type.IsWellKnownType(TypeSystemWellKnownType.Object),
ILLinkSharedWellKnownType.System_Void => type.IsWellKnownType(TypeSystemWellKnownType.Void),
_ => wellKnownType == WellKnownTypeExtensions.GetWellKnownType((type as MetadataType)?.Namespace ?? string.Empty, ((type as MetadataType)?.Name) ?? string.Empty)
_ => wellKnownType == WellKnownTypeExtensions.GetWellKnownType((type as MetadataType)?.GetNamespace() ?? string.Empty, ((type as MetadataType)?.GetName()) ?? string.Empty)
};

public static bool IsDeclaredOnType(this MethodDesc method, string fullTypeName)
{
return method.OwningType.IsTypeOf(fullTypeName);
}

public static bool StringEquals(this ReadOnlySpan<byte> utf8bytes, string value)
{
if (utf8bytes.Length < value.Length)
return false;

for (int i = 0; i < value.Length; i++)
{
int ch = utf8bytes[i];
if (ch > 0x7F)
return System.Text.Encoding.UTF8.GetString(utf8bytes) == value;

// We are assuming here that valid UTF8 encoded byte > 0x7F cannot map to a character with code point <= 0x7F
if (ch != value[i])
return false;
}

return utf8bytes.Length == value.Length; // All char ANSI, all matching
Comment thread
MichalStrehovsky marked this conversation as resolved.
}
}
}
4 changes: 2 additions & 2 deletions src/coreclr/tools/Common/Compiler/Dataflow/TypeProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ internal partial ImmutableArray<GenericParameterProxy> GetGenericParameters()

public TypeDesc Type { get; }

public string Name { get => Type is MetadataType metadataType ? metadataType.Name : string.Empty; }
public string Name { get => Type is MetadataType metadataType ? metadataType.GetName() : string.Empty; }

public string? Namespace { get => Type is MetadataType metadataType ? metadataType.Namespace : null; }
public string? Namespace { get => Type is MetadataType metadataType ? metadataType.GetNamespace() : null; }

public bool IsTypeOf(string @namespace, string name) => Type.IsTypeOf(@namespace, name);

Expand Down
12 changes: 6 additions & 6 deletions src/coreclr/tools/Common/Compiler/DisplayNameHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static string GetDisplayName(this MethodDesc method)

if (method.IsConstructor && method.OwningType is DefType defType)
{
sb.Append(defType.Name);
sb.Append(defType.GetName());
}
#if !READYTORUN
else if (method.GetPropertyForAccessor() is PropertyPseudoDesc property)
Expand Down Expand Up @@ -70,7 +70,7 @@ public static string GetDisplayName(this MethodDesc method)
#endif
else
{
sb.Append(method.Name);
sb.Append(method.GetName());
}

if (method.HasInstantiation)
Expand Down Expand Up @@ -119,7 +119,7 @@ public static string GetDisplayName(this FieldDesc field)
{
return new StringBuilder(field.OwningType.GetDisplayName())
.Append('.')
.Append(field.Name).ToString();
.Append(field.GetName()).ToString();
}

#if !READYTORUN
Expand Down Expand Up @@ -234,15 +234,15 @@ protected override Unit AppendNameForInstantiatedType(StringBuilder sb, DefType
protected override Unit AppendNameForNamespaceType(StringBuilder sb, DefType type, FormatOptions options)
{
NamespaceQualify(sb, type, options);
sb.Append(type.Name);
sb.Append(type.GetName());
return default;
}

protected override Unit AppendNameForNestedType(StringBuilder sb, DefType nestedType, DefType containingType, FormatOptions options)
{
AppendName(sb, containingType, options);
sb.Append('.');
sb.Append(nestedType.Name);
sb.Append(nestedType.GetName());

return default;
}
Expand All @@ -251,7 +251,7 @@ private static void NamespaceQualify(StringBuilder sb, DefType type, FormatOptio
{
if ((options & FormatOptions.NamespaceQualify) != 0)
{
string ns = type.Namespace;
string ns = type.GetNamespace();
if (!string.IsNullOrEmpty(ns))
{
sb.Append(ns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static bool IsHardwareIntrinsic(MethodDesc method)
{
var owningMdType = (MetadataType)owningType;
DefType containingType = owningMdType.ContainingType;
string ns = containingType?.ContainingType?.Namespace ??
containingType?.Namespace ??
owningMdType.Namespace;
string ns = containingType?.ContainingType?.GetNamespace() ??
containingType?.GetNamespace() ??
owningMdType.GetNamespace();
return method.Context.Target.Architecture switch
{
TargetArchitecture.ARM64 => ns == "System.Runtime.Intrinsics.Arm",
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/tools/Common/Compiler/InstructionSetSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static string GetHardwareIntrinsicId(TargetArchitecture architecture, Typ
return "";

// 64-bit ISA variants are not included in the mapping dictionary, so we use the containing type instead
if (potentialType.Name is "X64" or "Arm64")
if (potentialType.Name.SequenceEqual("X64"u8) || potentialType.Name.SequenceEqual("Arm64"u8))
{
if (architecture is TargetArchitecture.X64 or TargetArchitecture.ARM64)
potentialType = (MetadataType)potentialType.ContainingType;
Expand All @@ -73,18 +73,18 @@ public static string GetHardwareIntrinsicId(TargetArchitecture architecture, Typ
string suffix = "";
while (potentialType.ContainingType is MetadataType containingType)
{
suffix = $"_{potentialType.Name}{suffix}";
suffix = $"_{potentialType.GetName()}{suffix}";
potentialType = containingType;
}

if (architecture is TargetArchitecture.X64 or TargetArchitecture.X86)
{
if (potentialType.Namespace != "System.Runtime.Intrinsics.X86")
if (!potentialType.Namespace.SequenceEqual("System.Runtime.Intrinsics.X86"u8))
return "";
}
else if (architecture is TargetArchitecture.ARM64 or TargetArchitecture.ARM)
{
if (potentialType.Namespace != "System.Runtime.Intrinsics.Arm")
if (!potentialType.Namespace.SequenceEqual("System.Runtime.Intrinsics.Arm"u8))
return "";
}
else if (architecture is TargetArchitecture.LoongArch64)
Expand All @@ -100,7 +100,7 @@ public static string GetHardwareIntrinsicId(TargetArchitecture architecture, Typ
throw new InternalCompilerErrorException($"Unknown architecture '{architecture}'");
}

return potentialType.Name + suffix;
return potentialType.GetName() + suffix;
}

public SimdVectorLength GetVectorTSimdVector()
Expand Down
Loading
Loading