Skip to content

Commit 765798c

Browse files
[NativeAOT] Miscellaneous cleanup (#93446)
* [NativeAOT] Miscellaneous cleanup - Delete unnecessary abstractions - Simplify ifdefs - Use more derived types * Apply suggestions from code review Co-authored-by: Michal Strehovský <[email protected]>
1 parent d7c8198 commit 765798c

File tree

19 files changed

+80
-133
lines changed

19 files changed

+80
-133
lines changed

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/MethodTable.Runtime.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,29 @@ namespace Internal.Runtime
1111
// Extensions to MethodTable that are specific to the use in Runtime.Base.
1212
internal unsafe partial struct MethodTable
1313
{
14-
#pragma warning disable CA1822
14+
#if !INPLACE_RUNTIME
1515
internal MethodTable* GetArrayEEType()
1616
{
17-
#if INPLACE_RUNTIME
18-
return MethodTable.Of<Array>();
19-
#else
2017
MethodTable* pThis = (MethodTable*)Unsafe.Pointer(ref this);
2118
void* pGetArrayEEType = InternalCalls.RhpGetClasslibFunctionFromEEType(pThis, ClassLibFunctionId.GetSystemArrayEEType);
2219
return ((delegate* <MethodTable*>)pGetArrayEEType)();
23-
#endif
2420
}
2521

2622
internal Exception GetClasslibException(ExceptionIDs id)
2723
{
28-
#if INPLACE_RUNTIME
29-
return RuntimeExceptionHelpers.GetRuntimeException(id);
30-
#else
3124
if (IsParameterizedType)
3225
{
3326
return RelatedParameterType->GetClasslibException(id);
3427
}
3528

3629
return EH.GetClasslibExceptionFromEEType(id, (MethodTable*)Unsafe.AsPointer(ref this));
37-
#endif
3830
}
39-
#pragma warning restore CA1822
31+
#endif
4032

4133
internal IntPtr GetClasslibFunction(ClassLibFunctionId id)
4234
{
4335
return (IntPtr)InternalCalls.RhpGetClasslibFunctionFromEEType((MethodTable*)Unsafe.AsPointer(ref this), id);
4436
}
45-
46-
internal static bool AreSameType(MethodTable* mt1, MethodTable* mt2)
47-
{
48-
return mt1 == mt2;
49-
}
5037
}
5138

5239
internal static class WellKnownEETypes

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionDomain.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,27 @@ public Type GetNamedTypeForHandle(RuntimeTypeHandle typeHandle)
117117

118118
public Type GetArrayTypeForHandle(RuntimeTypeHandle typeHandle)
119119
{
120-
RuntimeTypeHandle elementTypeHandle = ExecutionEnvironment.GetArrayTypeElementType(typeHandle);
120+
RuntimeTypeHandle elementTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle);
121121
return elementTypeHandle.GetTypeForRuntimeTypeHandle().GetArrayType(typeHandle);
122122
}
123123

124124
public Type GetMdArrayTypeForHandle(RuntimeTypeHandle typeHandle, int rank)
125125
{
126-
RuntimeTypeHandle elementTypeHandle = ExecutionEnvironment.GetArrayTypeElementType(typeHandle);
126+
RuntimeTypeHandle elementTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle);
127127
return elementTypeHandle.GetTypeForRuntimeTypeHandle().GetMultiDimArrayType(rank, typeHandle);
128128
}
129129

130130
public Type GetPointerTypeForHandle(RuntimeTypeHandle typeHandle)
131131
{
132-
RuntimeTypeHandle targetTypeHandle = ExecutionEnvironment.GetPointerTypeTargetType(typeHandle);
132+
RuntimeTypeHandle targetTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle);
133133
return targetTypeHandle.GetTypeForRuntimeTypeHandle().GetPointerType(typeHandle);
134134
}
135135

136136
public Type GetFunctionPointerTypeForHandle(RuntimeTypeHandle typeHandle)
137137
{
138-
ExecutionEnvironment.GetFunctionPointerTypeComponents(typeHandle, out RuntimeTypeHandle returnTypeHandle,
139-
out RuntimeTypeHandle[] parameterHandles,
140-
out bool isUnmanaged);
138+
RuntimeTypeHandle returnTypeHandle = RuntimeAugments.GetFunctionPointerReturnType(typeHandle);
139+
RuntimeTypeHandle[] parameterHandles = RuntimeAugments.GetFunctionPointerParameterTypes(typeHandle);
140+
bool isUnmanaged = RuntimeAugments.IsUnmanagedFunctionPointerType(typeHandle);
141141

142142
RuntimeTypeInfo returnType = returnTypeHandle.GetTypeForRuntimeTypeHandle();
143143
int count = parameterHandles.Length;
@@ -152,7 +152,7 @@ public Type GetFunctionPointerTypeForHandle(RuntimeTypeHandle typeHandle)
152152

153153
public Type GetByRefTypeForHandle(RuntimeTypeHandle typeHandle)
154154
{
155-
RuntimeTypeHandle targetTypeHandle = ExecutionEnvironment.GetByRefTypeTargetType(typeHandle);
155+
RuntimeTypeHandle targetTypeHandle = RuntimeAugments.GetRelatedParameterTypeHandle(typeHandle);
156156
return targetTypeHandle.GetTypeForRuntimeTypeHandle().GetByRefType(typeHandle);
157157
}
158158

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/ExecutionEnvironment.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,14 @@ public abstract class ExecutionEnvironment
5353
public abstract bool TryGetNamedTypeForMetadata(QTypeDefinition qTypeDefinition, out RuntimeTypeHandle runtimeTypeHandle);
5454

5555
public abstract bool TryGetArrayTypeForElementType(RuntimeTypeHandle elementTypeHandle, out RuntimeTypeHandle arrayTypeHandle);
56-
public abstract RuntimeTypeHandle GetArrayTypeElementType(RuntimeTypeHandle arrayTypeHandle);
5756

5857
public abstract bool TryGetMultiDimArrayTypeForElementType(RuntimeTypeHandle elementTypeHandle, int rank, out RuntimeTypeHandle arrayTypeHandle);
5958

6059
public abstract bool TryGetFunctionPointerTypeForComponents(RuntimeTypeHandle returnTypeHandle, RuntimeTypeHandle[] parameterHandles, bool isUnmanaged, out RuntimeTypeHandle functionPointerTypeHandle);
61-
public abstract void GetFunctionPointerTypeComponents(RuntimeTypeHandle functionPointerHandle, out RuntimeTypeHandle returnTypeHandle, out RuntimeTypeHandle[] parameterHandles, out bool isUnmanaged);
6260

6361
public abstract bool TryGetPointerTypeForTargetType(RuntimeTypeHandle targetTypeHandle, out RuntimeTypeHandle pointerTypeHandle);
64-
public abstract RuntimeTypeHandle GetPointerTypeTargetType(RuntimeTypeHandle pointerTypeHandle);
6562

6663
public abstract bool TryGetByRefTypeForTargetType(RuntimeTypeHandle targetTypeHandle, out RuntimeTypeHandle byRefTypeHandle);
67-
public abstract RuntimeTypeHandle GetByRefTypeTargetType(RuntimeTypeHandle byRefTypeHandle);
6864

6965
public abstract bool TryGetConstructedGenericTypeForComponents(RuntimeTypeHandle genericTypeDefinitionHandle, RuntimeTypeHandle[] genericTypeArgumentHandles, out RuntimeTypeHandle runtimeTypeHandle);
7066
public abstract bool TryGetConstructedGenericTypeForComponentsNoConstraintCheck(RuntimeTypeHandle genericTypeDefinitionHandle, RuntimeTypeHandle[] genericTypeArgumentHandles, out RuntimeTypeHandle runtimeTypeHandle);

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/NonPortable/RuntimeTypeUnifier.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections.Concurrent;
88
using System.Runtime.CompilerServices;
99

10+
using Internal.Runtime;
1011
using Internal.Runtime.Augments;
1112

1213
namespace Internal.Reflection.Core.NonPortable
@@ -42,12 +43,12 @@ internal static partial class RuntimeTypeUnifier
4243
//
4344
// Retrieves the unified Type object for given RuntimeTypeHandle (this is basically the Type.GetTypeFromHandle() api without the input validation.)
4445
//
45-
internal static Type GetRuntimeTypeForEEType(EETypePtr eeType)
46+
internal static unsafe RuntimeType GetRuntimeTypeForMethodTable(MethodTable* eeType)
4647
{
4748
// If writable data is supported, we shouldn't be using the hashtable - the runtime type
48-
// is accessible through a couple indirections from the EETypePtr which is much faster.
49+
// is accessible through a couple indirections from the MethodTable which is much faster.
4950
Debug.Assert(!Internal.Runtime.MethodTable.SupportsWritableData);
50-
return RuntimeTypeHandleToTypeCache.Table.GetOrAdd(eeType.RawValue);
51+
return RuntimeTypeHandleToTypeCache.Table.GetOrAdd((IntPtr)eeType);
5152
}
5253

5354
//
@@ -59,14 +60,14 @@ internal static Type GetRuntimeTypeForEEType(EETypePtr eeType)
5960
// does a second lookup in the true unifying tables rather than creating the Type itself.
6061
// Thus, the one-to-one relationship between Type reference identity and Type semantic identity is preserved.
6162
//
62-
private sealed class RuntimeTypeHandleToTypeCache : ConcurrentUnifierW<IntPtr, Type>
63+
private sealed class RuntimeTypeHandleToTypeCache : ConcurrentUnifierW<IntPtr, RuntimeType>
6364
{
6465
private RuntimeTypeHandleToTypeCache() { }
6566

66-
protected sealed override Type Factory(IntPtr rawRuntimeTypeHandleKey)
67+
protected sealed override RuntimeType Factory(IntPtr rawRuntimeTypeHandleKey)
6768
{
6869
EETypePtr eeType = new EETypePtr(rawRuntimeTypeHandleKey);
69-
return GetRuntimeTypeBypassCache(eeType);
70+
return (RuntimeType)GetRuntimeTypeBypassCache(eeType);
7071
}
7172

7273
public static readonly RuntimeTypeHandleToTypeCache Table = new RuntimeTypeHandleToTypeCache();

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ public static bool IsFunctionPointerType(RuntimeTypeHandle typeHandle)
602602

603603
public static unsafe RuntimeTypeHandle GetFunctionPointerReturnType(RuntimeTypeHandle typeHandle)
604604
{
605-
return new RuntimeTypeHandle(new EETypePtr(typeHandle.ToMethodTable()->FunctionPointerReturnType));
605+
return new RuntimeTypeHandle(typeHandle.ToMethodTable()->FunctionPointerReturnType);
606606
}
607607

608608
public static unsafe int GetFunctionPointerParameterCount(RuntimeTypeHandle typeHandle)
@@ -613,7 +613,7 @@ public static unsafe int GetFunctionPointerParameterCount(RuntimeTypeHandle type
613613
public static unsafe RuntimeTypeHandle GetFunctionPointerParameterType(RuntimeTypeHandle typeHandle, int argumentIndex)
614614
{
615615
Debug.Assert(argumentIndex < GetFunctionPointerParameterCount(typeHandle));
616-
return new RuntimeTypeHandle(new EETypePtr(typeHandle.ToMethodTable()->FunctionPointerParameters[argumentIndex]));
616+
return new RuntimeTypeHandle(typeHandle.ToMethodTable()->FunctionPointerParameters[argumentIndex]);
617617
}
618618

619619
public static unsafe RuntimeTypeHandle[] GetFunctionPointerParameterTypes(RuntimeTypeHandle typeHandle)
@@ -626,7 +626,7 @@ public static unsafe RuntimeTypeHandle[] GetFunctionPointerParameterTypes(Runtim
626626
MethodTableList parameters = typeHandle.ToMethodTable()->FunctionPointerParameters;
627627
for (int i = 0; i < result.Length; i++)
628628
{
629-
result[i] = new RuntimeTypeHandle(new EETypePtr(parameters[i]));
629+
result[i] = new RuntimeTypeHandle(parameters[i]);
630630
}
631631

632632
return result;

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/LdTokenHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private static unsafe RuntimeFieldHandle GetRuntimeFieldHandle(IntPtr pHandleSig
3131
return returnValue;
3232
}
3333

34-
private static unsafe Type GetRuntimeType(MethodTable* pMT)
34+
private static unsafe RuntimeType GetRuntimeType(MethodTable* pMT)
3535
{
3636
return Type.GetTypeFromMethodTable(pMT);
3737
}

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/SynchronizedMethodHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static unsafe void MonitorExitStatic(MethodTable* pMT, ref bool lockTake
6767
lockTaken = false;
6868
}
6969

70-
private static unsafe Type GetStaticLockObject(MethodTable* pMT)
70+
private static unsafe RuntimeType GetStaticLockObject(MethodTable* pMT)
7171
{
7272
return Type.GetTypeFromMethodTable(pMT);
7373
}

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/IDynamicInterfaceCastableSupport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ static unsafe class IDynamicCastableSupport
1212
[RuntimeExport("IDynamicCastableIsInterfaceImplemented")]
1313
internal static bool IDynamicCastableIsInterfaceImplemented(IDynamicInterfaceCastable instance, MethodTable* interfaceType, bool throwIfNotImplemented)
1414
{
15-
return instance.IsInterfaceImplemented(new RuntimeTypeHandle(new EETypePtr(interfaceType)), throwIfNotImplemented);
15+
return instance.IsInterfaceImplemented(new RuntimeTypeHandle(interfaceType), throwIfNotImplemented);
1616
}
1717

1818
[RuntimeExport("IDynamicCastableGetInterfaceImplementation")]
1919
internal static IntPtr IDynamicCastableGetInterfaceImplementation(IDynamicInterfaceCastable instance, MethodTable* interfaceType, ushort slot)
2020
{
21-
RuntimeTypeHandle handle = instance.GetInterfaceImplementation(new RuntimeTypeHandle(new EETypePtr(interfaceType)));
21+
RuntimeTypeHandle handle = instance.GetInterfaceImplementation(new RuntimeTypeHandle(interfaceType));
2222
MethodTable* implType = handle.ToMethodTable();
2323
if (implType == null)
2424
{

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/MethodTable.Runtime.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,43 @@ namespace Internal.Runtime
1111
// Extensions to MethodTable that are specific to the use in the CoreLib.
1212
internal unsafe partial struct MethodTable
1313
{
14-
#if !INPLACE_RUNTIME
15-
internal static MethodTable* GetArrayEEType()
14+
#pragma warning disable CA1822
15+
internal MethodTable* GetArrayEEType()
1616
{
17-
1817
return MethodTable.Of<Array>();
1918
}
2019

20+
internal Exception GetClasslibException(ExceptionIDs id)
21+
{
22+
return RuntimeExceptionHelpers.GetRuntimeException(id);
23+
}
24+
#pragma warning restore CA1822
25+
2126
internal static bool AreSameType(MethodTable* mt1, MethodTable* mt2)
2227
{
2328
return mt1 == mt2;
2429
}
25-
#endif
30+
31+
internal bool IsEnum
32+
{
33+
get
34+
{
35+
// Q: When is an enum type a constructed generic type?
36+
// A: When it's nested inside a generic type.
37+
38+
// Generic type definitions that return true for IsPrimitive are type definitions of generic enums.
39+
// Otherwise check the base type.
40+
return IsPrimitive && (IsGenericTypeDefinition || NonArrayBaseType == MethodTable.Of<Enum>());
41+
}
42+
}
43+
44+
// Returns true for actual primitives only, returns false for enums
45+
internal bool IsActualPrimitive
46+
{
47+
get
48+
{
49+
return IsPrimitive && NonArrayBaseType == MethodTable.Of<ValueType>();
50+
}
51+
}
2652
}
2753
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System/EETypePtr.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,11 @@ internal bool IsPrimitive
156156
}
157157
}
158158

159-
// WARNING: Never call unless the MethodTable came from an instanced object. Nested enums can be open generics (typeof(Outer<>).NestedEnum)
160-
// and this helper has undefined behavior when passed such as a enum.
161159
internal bool IsEnum
162160
{
163161
get
164162
{
165-
// Q: When is an enum type a constructed generic type?
166-
// A: When it's nested inside a generic type.
167-
if (!IsDefType)
168-
return false;
169-
170-
// Generic type definitions that return true for IsPrimitive are type definitions of generic enums.
171-
// Otherwise check the base type.
172-
return (IsGenericTypeDefinition && IsPrimitive) || this.BaseType == EETypePtr.EETypePtrOf<Enum>();
163+
return _value->IsEnum;
173164
}
174165
}
175166

0 commit comments

Comments
 (0)