-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathEmitHacksTest.cs
More file actions
360 lines (286 loc) · 16.1 KB
/
EmitHacksTest.cs
File metadata and controls
360 lines (286 loc) · 16.1 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#if NET8_0_OR_GREATER && !LIGHT_EXPRESSION
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
namespace FastExpressionCompiler.IssueTests
{
public class EmitHacksTest : ITest
{
public int Run()
{
DynamicMethod_Emit_Hack();
#if NET10_0_OR_GREATER
DynamicMethod_Emit_Hack_Net10();
return 4;
#else
return 3;
#endif
}
public void DynamicMethod_Emit_Hack()
{
var f = Get_DynamicMethod_Emit_Hack();
var a = f(41);
Asserts.AreEqual(42, a);
}
static readonly Type ilType = typeof(ILGenerator).Assembly.GetType("System.Reflection.Emit.DynamicILGenerator");
// m_scope field is on DynamicILGenerator (internal class) - accessed via reflection since
// the field type DynamicScope is also internal (UnsafeAccessorType can't return non-public types).
static readonly FieldInfo mScopeField = ilType?.GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic);
static readonly Type scopeType = ilType?.Assembly.GetType("System.Reflection.Emit.DynamicScope");
static readonly FieldInfo mTokensField = scopeType?.GetField("m_tokens", BindingFlags.Instance | BindingFlags.NonPublic);
// m_length, m_ILStream, and UpdateStackSize are on RuntimeILGenerator (the internal base class of DynamicILGenerator),
// NOT on the public ILGenerator class. Look up the fields on the correct type.
static readonly Type runtimeILGenType = ilType?.BaseType; // System.Reflection.Emit.RuntimeILGenerator
static readonly FieldInfo mLengthField = runtimeILGenType?.GetField("m_length", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
static readonly FieldInfo mILStreamField = runtimeILGenType?.GetField("m_ILStream", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
static readonly MethodInfo updateStackSizeMethod = runtimeILGenType?.GetMethod("UpdateStackSize", BindingFlags.Instance | BindingFlags.NonPublic);
private static Func<ILGenerator, IList<object>> GetScopeTokens()
{
if (mScopeField == null || mTokensField == null)
return null;
var dynMethod = new DynamicMethod(string.Empty,
typeof(IList<object>), new[] { typeof(ExpressionCompiler.ArrayClosure), typeof(ILGenerator) },
typeof(ExpressionCompiler), skipVisibility: true);
var il = dynMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldfld, mScopeField);
il.Emit(OpCodes.Ldfld, mTokensField);
il.Emit(OpCodes.Ret);
return (Func<ILGenerator, IList<object>>)dynMethod.CreateDelegate(typeof(Func<ILGenerator, IList<object>>), ExpressionCompiler.EmptyArrayClosure);
}
static readonly Func<ILGenerator, IList<object>> getScopeTokens = GetScopeTokens();
private delegate ref TField GetFieldRefDelegate<TFieldHolder, TField>(TFieldHolder holder);
private static GetFieldRefDelegate<TFieldHolder, TField> CreateFieldAccessor<TFieldHolder, TField>(FieldInfo field)
{
if (field == null) return null;
var dynMethod = new DynamicMethod(string.Empty,
typeof(TField).MakeByRefType(), new[] { typeof(ExpressionCompiler.ArrayClosure), typeof(TFieldHolder) },
typeof(TFieldHolder), skipVisibility: true);
var il = dynMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldflda, field);
il.Emit(OpCodes.Ret);
return (GetFieldRefDelegate<TFieldHolder, TField>)dynMethod.CreateDelegate(typeof(GetFieldRefDelegate<TFieldHolder, TField>));
}
static readonly GetFieldRefDelegate<ILGenerator, int> mLengthFieldAccessor = CreateFieldAccessor<ILGenerator, int>(mLengthField);
static readonly GetFieldRefDelegate<ILGenerator, byte[]> mILStreamAccessor = CreateFieldAccessor<ILGenerator, byte[]>(mILStreamField);
static readonly Action<ILGenerator, OpCode, int> updateStackSizeDelegate = GetUpdateStackSizeDelegate();
private static Action<ILGenerator, OpCode, int> GetUpdateStackSizeDelegate()
{
if (updateStackSizeMethod == null) return null;
// Cannot use Delegate.CreateDelegate with a method from a non-public declaring type (RuntimeILGenerator).
// Instead, wrap the call in a DynamicMethod with skipVisibility: true.
var dynMethod = new DynamicMethod(string.Empty,
typeof(void), new[] { typeof(ExpressionCompiler.ArrayClosure), typeof(ILGenerator), typeof(OpCode), typeof(int) },
typeof(ExpressionCompiler), skipVisibility: true);
var il = dynMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_1); // ILGenerator (runtime type: DynamicILGenerator : RuntimeILGenerator)
il.Emit(OpCodes.Ldarg_2); // OpCode
il.Emit(OpCodes.Ldarg_3); // int stackchange
il.Emit(OpCodes.Call, updateStackSizeMethod);
il.Emit(OpCodes.Ret);
return (Action<ILGenerator, OpCode, int>)dynMethod.CreateDelegate(
typeof(Action<ILGenerator, OpCode, int>), ExpressionCompiler.EmptyArrayClosure);
}
public static Func<int, int> Get_DynamicMethod_Emit_Hack()
{
if (mLengthFieldAccessor == null || mILStreamAccessor == null || updateStackSizeDelegate == null || getScopeTokens == null)
return null;
var meth = MethodStatic1Arg;
var paramCount = 1;
var dynMethod = new DynamicMethod(string.Empty,
typeof(int), new[] { typeof(ExpressionCompiler.ArrayClosure), typeof(int) },
typeof(ExpressionCompiler),
skipVisibility: true);
// Ensuring the size of stream upfront, otherwise we would need this code
// if (mILStream.Length < mLength + 13)
// Array.Resize(ref mILStream, Math.Max(mILStream.Length * 2, mLength + 13));
// Ldarg_1(3) + Call(7) + Ret(3) = 13
var il = dynMethod.GetILGenerator(16); // todo: @perf #351 how to reuse the mILStream - we may either set it initially or reuse when expanding it
// current IL stream extension
// internal void EnsureCapacity(int size)
// {
// if (m_length + size >= m_ILStream.Length)
// IncreaseCapacity(size);
// }
// private void IncreaseCapacity(int size)
// {
// byte[] temp = new byte[Math.Max(m_ILStream.Length * 2, m_length + size)]; // todo: @perf #351 how to use existing ILStream here
// Array.Copy(m_ILStream, temp, m_ILStream.Length);
// m_ILStream = temp;
// }
ref var mLength = ref mLengthFieldAccessor(il);
ref var mILStream = ref mILStreamAccessor(il);
// il.Emit(OpCodes.Ldarg_1);
mILStream[mLength++] = (byte)OpCodes.Ldarg_1.Value;
updateStackSizeDelegate(il, OpCodes.Ldarg_1, 1);
// il.Emit(OpCodes.Call, meth);
mILStream[mLength++] = (byte)OpCodes.Call.Value;
updateStackSizeDelegate(il, OpCodes.Call, CalcStackChange(meth, paramCount));
var mTokens = getScopeTokens(il);
mTokens.Add(meth.MethodHandle);
var token = mTokens.Count - 1 | (int)0x06000000; // MetadataTokenType.MethodDef
BinaryPrimitives.WriteInt32LittleEndian(mILStream.AsSpan(mLength), token);
mLength += 4;
// il.Emit(OpCodes.Ret);
mILStream[mLength++] = (byte)OpCodes.Ret.Value;
updateStackSizeDelegate(il, OpCodes.Ret, 0);
return (Func<int, int>)dynMethod.CreateDelegate(typeof(Func<int, int>), ExpressionCompiler.EmptyArrayClosure);
}
#if NET10_0_OR_GREATER
// In .NET 10+, use UnsafeAccessorType to access the private fields of non-public types directly,
// without the DynamicMethod-based delegation used in earlier .NET versions.
// RuntimeILGenerator is the internal base class of DynamicILGenerator that holds the IL stream state.
/// <summary>Directly accesses m_length on RuntimeILGenerator via UnsafeAccessorType (NET10+).</summary>
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "m_length")]
private static extern ref int GetMLength_Net10(
[UnsafeAccessorType("System.Reflection.Emit.RuntimeILGenerator")] object il);
/// <summary>Directly accesses m_ILStream on RuntimeILGenerator via UnsafeAccessorType (NET10+).</summary>
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "m_ILStream")]
private static extern ref byte[] GetMILStream_Net10(
[UnsafeAccessorType("System.Reflection.Emit.RuntimeILGenerator")] object il);
/// <summary>Directly calls UpdateStackSize on RuntimeILGenerator via UnsafeAccessorType (NET10+).</summary>
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "UpdateStackSize")]
private static extern void UpdateStackSize_Net10(
[UnsafeAccessorType("System.Reflection.Emit.RuntimeILGenerator")] object il,
OpCode opcode, int stackchange);
/// <summary>Directly accesses m_tokens on DynamicScope via UnsafeAccessorType (NET10+).</summary>
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "m_tokens")]
private static extern ref List<object> GetMTokens_Net10(
[UnsafeAccessorType("System.Reflection.Emit.DynamicScope")] object scope);
public void DynamicMethod_Emit_Hack_Net10()
{
var f = Get_DynamicMethod_Emit_Hack_Net10();
var a = f(41);
Asserts.AreEqual(42, a);
}
/// <summary>
/// Demonstrates using UnsafeAccessorType (NET10+) to directly access private fields
/// of non-public types (RuntimeILGenerator, DynamicScope) for fast IL emission.
/// Replaces the DynamicMethod-based delegation approach used in earlier .NET versions.
/// </summary>
public static Func<int, int> Get_DynamicMethod_Emit_Hack_Net10()
{
var meth = MethodStatic1Arg;
var paramCount = 1;
var dynMethod = new DynamicMethod(string.Empty,
typeof(int), new[] { typeof(ExpressionCompiler.ArrayClosure), typeof(int) },
typeof(ExpressionCompiler),
skipVisibility: true);
var il = dynMethod.GetILGenerator(16);
// Use UnsafeAccessorType to get refs to the internal IL stream fields directly
ref var mLength = ref GetMLength_Net10(il);
ref var mILStream = ref GetMILStream_Net10(il);
// il.Emit(OpCodes.Ldarg_1);
mILStream[mLength++] = (byte)OpCodes.Ldarg_1.Value;
UpdateStackSize_Net10(il, OpCodes.Ldarg_1, 1);
// il.Emit(OpCodes.Call, meth);
mILStream[mLength++] = (byte)OpCodes.Call.Value;
UpdateStackSize_Net10(il, OpCodes.Call, CalcStackChange(meth, paramCount));
// Access m_scope via reflection (DynamicILGenerator.m_scope returns DynamicScope which is a non-public type,
// so UnsafeAccessorType cannot currently be used for the return value).
// Then use UnsafeAccessorType to access m_tokens on the DynamicScope instance directly.
if (mScopeField == null) return null;
var scope = mScopeField.GetValue(il);
ref var mTokens = ref GetMTokens_Net10(scope);
mTokens.Add(meth.MethodHandle);
var token = mTokens.Count - 1 | (int)0x06000000; // MetadataTokenType.MethodDef
BinaryPrimitives.WriteInt32LittleEndian(mILStream.AsSpan(mLength), token);
mLength += 4;
// il.Emit(OpCodes.Ret);
mILStream[mLength++] = (byte)OpCodes.Ret.Value;
UpdateStackSize_Net10(il, OpCodes.Ret, 0);
return (Func<int, int>)dynMethod.CreateDelegate(typeof(Func<int, int>), ExpressionCompiler.EmptyArrayClosure);
}
#endif
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int CalcStackChange(MethodInfo meth, int paramCount)
{
var stackChange = 0;
if (meth.ReturnType != typeof(void))
stackChange++;
stackChange -= paramCount;
if (!meth.IsStatic)
stackChange--;
return stackChange;
}
public void DynamicMethod_Emit_OpCodes_Call()
{
var f = Get_DynamicMethod_Emit_OpCodes_Call();
var a = f(41);
Asserts.AreEqual(42, a);
}
public static Func<int, int> Get_DynamicMethod_Emit_OpCodes_Call()
{
var dynMethod = new DynamicMethod(string.Empty,
typeof(int), new[] { typeof(ExpressionCompiler.ArrayClosure), typeof(int) },
typeof(ExpressionCompiler), skipVisibility: true);
var il = dynMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Call, MethodStatic1Arg);
// il.Emit(OpCodes.Call, MethodStaticNoArgs);
il.Emit(OpCodes.Ret);
return (Func<int, int>)dynMethod.CreateDelegate(typeof(Func<int, int>), ExpressionCompiler.EmptyArrayClosure);
}
public void DynamicMethod_Emit_Newobj()
{
var f = Get_DynamicMethod_Emit_Newobj();
var a = f();
Asserts.IsInstanceOf<A>(a);
}
public void DynamicMethod_Hack_Emit_Newobj()
{
var f = Get_DynamicMethod_Hack_Emit_Newobj();
var a = f();
Asserts.IsInstanceOf<A>(a);
}
public static Func<A> Get_DynamicMethod_Emit_Newobj()
{
var dynMethod = new DynamicMethod(string.Empty,
typeof(A), new[] { typeof(ExpressionCompiler.ArrayClosure) },
typeof(ExpressionCompiler), skipVisibility: true);
var il = dynMethod.GetILGenerator();
il.Emit(OpCodes.Newobj, _ctor);
il.Emit(OpCodes.Ret);
return (Func<A>)dynMethod.CreateDelegate(typeof(Func<A>), ExpressionCompiler.EmptyArrayClosure);
}
public static Func<A> Get_DynamicMethod_Hack_Emit_Newobj()
{
var dynMethod = new DynamicMethod(string.Empty,
typeof(A), new[] { typeof(ExpressionCompiler.ArrayClosure) },
typeof(ExpressionCompiler), skipVisibility: true);
var il = dynMethod.GetILGenerator();
var ilType = il.GetType();
il.Emit(OpCodes.Newobj, _ctor);
Debug.Assert(_ctor.DeclaringType != null && !_ctor.DeclaringType.IsGenericType);
// var rtConstructor = con as RuntimeConstructorInfo;
var methodHandle = _ctor.MethodHandle;
// m_tokens.Add(rtConstructor.MethodHandle);
// var tk = m_tokens.Count - 1 | (int)MetadataTokenType.MethodDef;
var scopeField = ilType.GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic);
if (scopeField == null)
return null;
var mScope = scopeField.GetValue(il);
var tokensField = mScope.GetType().GetField("m_tokens", BindingFlags.Instance | BindingFlags.NonPublic);
if (tokensField == null)
return null;
var mTokens = tokensField.GetValue(mScope);
il.Emit(OpCodes.Ret);
return (Func<A>)dynMethod.CreateDelegate(typeof(Func<A>), ExpressionCompiler.EmptyArrayClosure);
}
public class A
{
public A() { }
public static int M() => 42;
public static int M1(int q) => q + 1;
}
private static readonly ConstructorInfo _ctor = typeof(A).GetConstructor(Type.EmptyTypes);
public static readonly MethodInfo MethodStaticNoArgs = typeof(A).GetMethod(nameof(A.M));
public static readonly MethodInfo MethodStatic1Arg = typeof(A).GetMethod(nameof(A.M1));
}
}
#endif