-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathReadyToRunHashCode.cs
More file actions
259 lines (231 loc) · 10.7 KB
/
ReadyToRunHashCode.cs
File metadata and controls
259 lines (231 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.Numerics;
using System.Text;
using Internal.TypeSystem;
namespace ILCompiler
{
/// <summary>
/// Helper class used to calculate hash codes compatible with the CoreCLR
/// GetVersionResilientMethod/TypeHashCode.
/// </summary>
public static class ReadyToRunHashCode
{
/// <summary>
/// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L15">ComputeNameHashCode</a>
/// </summary>
/// <param name="name">Name string to hash</param>
public static int NameHashCode(string name)
{
if (string.IsNullOrEmpty(name))
{
return 0;
}
int hash1 = 0x6DA3B944;
int hash2 = 0;
// DIFFERENT FROM CORERT: We hash UTF-8 bytes here, while CoreRT hashes UTF-16 characters.
byte[] src = Encoding.UTF8.GetBytes(name);
for (int i = 0; i < src.Length; i += 2)
{
hash1 = unchecked(hash1 + RotateLeft(hash1, 5)) ^ (int)unchecked((sbyte)src[i]);
if (i + 1 < src.Length)
{
hash2 = unchecked(hash2 + RotateLeft(hash2, 5)) ^ (int)unchecked((sbyte)src[i + 1]);
}
else
{
break;
}
}
hash1 = unchecked(hash1 + RotateLeft(hash1, 8));
hash2 = unchecked(hash2 + RotateLeft(hash2, 8));
return unchecked((int)(hash1 ^ hash2));
}
/// <summary>
/// Calculate hash code for a namespace - name combination.
/// CoreCLR 2-parameter <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L42">ComputeNameHashCode</a>
/// DIFFERENT FROM CORERT: CoreRT hashes the full name as one string ("namespace.name"),
/// as the full name is already available. In CoreCLR we normally only have separate
/// strings for namespace and name, thus we hash them separately.
/// </summary>
/// <param name="namespacePart">Namespace name</param>
/// <param name="namePart">Type name within the namespace</param>
public static int NameHashCode(string namespacePart, string namePart)
{
return NameHashCode(namespacePart) ^ NameHashCode(namePart);
}
/// <summary>
/// CoreCLR 3-parameter <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/versionresilienthashcode.cpp#L9">GetVersionResilientTypeHashCode</a>
/// </summary>
/// <param name="type">Type to hash</param>
public static int TypeTableHashCode(DefType type)
{
int hashcode = 0;
do
{
hashcode ^= NameHashCode(type.Namespace, type.Name);
type = type.ContainingType;
}
while (type != null);
return hashcode;
}
/// <summary>
/// CoreCLR 1-parameter <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/versionresilienthashcode.cpp#L76">GetVersionResilientTypeHashCode</a>
/// </summary>
/// <param name="type">Type to hash</param>
public static int TypeHashCode(TypeDesc type)
{
if (type.GetTypeDefinition() is DefType defType)
{
int hashcode = NameHashCode(defType.Namespace, defType.Name);
DefType containingType = defType.ContainingType;
if (containingType != null)
{
hashcode = NestedTypeHashCode(TypeHashCode(containingType), hashcode);
}
if (type.HasInstantiation && !type.IsGenericDefinition)
{
return GenericInstanceHashCode(hashcode, type.Instantiation);
}
else
{
return hashcode;
}
}
if (type is ArrayType arrayType)
{
return ArrayTypeHashCode(TypeHashCode(arrayType.ElementType), arrayType.Rank);
}
if (type is PointerType pointerType)
{
return PointerTypeHashCode(TypeHashCode(pointerType.ParameterType));
}
if (type is ByRefType byRefType)
{
return ByrefTypeHashCode(TypeHashCode(byRefType.ParameterType));
}
throw new NotImplementedException();
}
/// <summary>
/// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L80">ComputeNestedTypeHashCode</a>
/// </summary>
/// <param name="enclosingTypeHashcode">Hash code of the enclosing type</param>
/// <param name="nestedTypeNameHash">Hash code of the nested type name</param>
private static int NestedTypeHashCode(int enclosingTypeHashcode, int nestedTypeNameHash)
{
return unchecked(enclosingTypeHashcode + RotateLeft(enclosingTypeHashcode, 11)) ^ nestedTypeNameHash;
}
/// <summary>
/// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L52">ComputeArrayTypeHashCode</a>
/// </summary>
/// <param name="elementTypeHashcode">Hash code representing the array element type</param>
/// <param name="rank">Array rank</param>
private static int ArrayTypeHashCode(int elementTypeHashcode, int rank)
{
// DIFFERENT FROM CORERT: This is much simplified compared to CoreRT, to avoid converting rank to string.
// For single-dimensinal array, the result is identical to CoreRT.
int hashCode = unchecked((int)0xd5313556 + rank);
if (rank == 1)
{
Debug.Assert(hashCode == NameHashCode("System.Array`1"));
}
hashCode = unchecked(hashCode + RotateLeft(hashCode, 13)) ^ elementTypeHashcode;
return unchecked(hashCode + RotateLeft(hashCode, 15));
}
/// <summary>
/// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L66">ComputePointerTypeHashCode</a>
/// </summary>
/// <param name="pointeeTypeHashcode">Hash code of the pointee type</param>
private static int PointerTypeHashCode(int pointeeTypeHashcode)
{
return unchecked(pointeeTypeHashcode + RotateLeft(pointeeTypeHashcode, 5)) ^ 0x12D0;
}
/// <summary>
/// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L73">ComputeByrefTypeHashCode</a>
/// </summary>
/// <param name="parameterTypeHashCode">Hash code representing the parameter type</param>
private static int ByrefTypeHashCode(int parameterTypeHashcode)
{
return unchecked(parameterTypeHashcode + RotateLeft(parameterTypeHashcode, 7)) ^ 0x4C85;
}
/// <summary>
/// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/typehashingalgorithms.h#L88">ComputeGenericInstanceHashCode</a>
/// </summary>
/// <param name="hashcode">Base hash code</param>
/// <param name="instantiation">Instantiation to include in the hash</param>
private static int GenericInstanceHashCode(int hashcode, Instantiation instantiation)
{
for (int i = 0; i < instantiation.Length; i++)
{
int argumentHashCode = TypeHashCode(instantiation[i]);
hashcode = unchecked(hashcode + RotateLeft(hashcode, 13)) ^ argumentHashCode;
}
return unchecked(hashcode + RotateLeft(hashcode, 15));
}
/// <summary>
/// CoreCLR <a href="https://github.com/dotnet/coreclr/blob/030e0af89bb897554acef575075c69aaf5176268/src/vm/versionresilienthashcode.cpp#L129">GetVersionResilientMethodHashCode</a>
/// </summary>
/// <param name="method">Method to hash</param>
public static int MethodHashCode(MethodDesc method)
{
int hashCode = TypeHashCode(method.OwningType);
int methodNameHashCode = NameHashCode(method.Name);
// Todo: Add signature to hash.
if (method.HasInstantiation)
{
hashCode ^= GenericInstanceHashCode(methodNameHashCode, method.Instantiation);
}
else
{
hashCode ^= methodNameHashCode;
}
return hashCode;
}
public static int ModuleNameHashCode(ModuleDesc module)
{
IAssemblyDesc assembly = module.Assembly;
Debug.Assert(assembly == module);
return NameHashCode(assembly.GetName().Name);
}
/// <summary>
/// Bitwise left 32-bit rotation with wraparound.
/// </summary>
/// <param name="value">Value to rotate</param>
/// <param name="bitCount">Number of bits</param>
private static int RotateLeft(int value, int bitCount)
{
return (int)BitOperations.RotateLeft((uint)value, bitCount);
}
private static uint XXHash32_MixEmptyState()
{
// Unlike System.HashCode, these hash values are required to be stable, so don't
// mixin a random process specific value
return 374761393U; // Prime5
}
private static uint XXHash32_QueueRound(uint hash, uint queuedValue)
{
return (BitOperations.RotateLeft((hash + queuedValue * 3266489917U/*Prime3*/), 17)) * 668265263U/*Prime4*/;
}
private static uint XXHash32_MixFinal(uint hash)
{
hash ^= hash >> 15;
hash *= 2246822519U/*Prime2*/;
hash ^= hash >> 13;
hash *= 3266489917U/*Prime3*/;
hash ^= hash >> 16;
return hash;
}
public static uint CombineTwoValuesIntoHash(uint value1, uint value2)
{
// This matches the behavior of System.HashCode.Combine(value1, value2) as of the time of authoring
uint hash = XXHash32_MixEmptyState();
hash += 8;
hash = XXHash32_QueueRound(hash, value1);
hash = XXHash32_QueueRound(hash, value2);
hash = XXHash32_MixFinal(hash);
return hash;
}
}
}