Skip to content

Commit 6cf5291

Browse files
authored
Integrate changes in shared files from feature/NativeAOT (#60811)
1 parent 6df2b8d commit 6cf5291

File tree

6 files changed

+122
-66
lines changed

6 files changed

+122
-66
lines changed

src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ private void PublishCode()
445445
_methodCodeNode.Fixups.Add(node);
446446
}
447447
#else
448-
MethodIL methodIL = HandleToObject(_methodScope);
448+
var methodIL = (MethodIL)HandleToObject((IntPtr)_methodScope);
449449
CodeBasedDependencyAlgorithm.AddDependenciesDueToMethodCodePresence(ref _additionalDependencies, _compilation.NodeFactory, MethodBeingCompiled, methodIL);
450450
_methodCodeNode.InitializeNonRelocationDependencies(_additionalDependencies);
451451
_methodCodeNode.InitializeDebugInfo(_debugInfo);
@@ -1654,7 +1654,7 @@ private void resolveToken(ref CORINFO_RESOLVED_TOKEN pResolvedToken)
16541654
resolver.AddModuleTokenForMethod(method, methodModuleToken);
16551655
}
16561656
#else
1657-
_compilation.NodeFactory.MetadataManager.GetDependenciesDueToAccess(ref _additionalDependencies, _compilation.NodeFactory, methodIL, method);
1657+
_compilation.NodeFactory.MetadataManager.GetDependenciesDueToAccess(ref _additionalDependencies, _compilation.NodeFactory, (MethodIL)methodIL, method);
16581658
#endif
16591659
}
16601660
else
@@ -1682,7 +1682,7 @@ private void resolveToken(ref CORINFO_RESOLVED_TOKEN pResolvedToken)
16821682
_compilation.NodeFactory.Resolver.AddModuleTokenForField(field, HandleToModuleToken(ref pResolvedToken));
16831683
}
16841684
#else
1685-
_compilation.NodeFactory.MetadataManager.GetDependenciesDueToAccess(ref _additionalDependencies, _compilation.NodeFactory, methodIL, field);
1685+
_compilation.NodeFactory.MetadataManager.GetDependenciesDueToAccess(ref _additionalDependencies, _compilation.NodeFactory, (MethodIL)methodIL, field);
16861686
#endif
16871687
}
16881688
else

src/coreclr/tools/Common/TypeSystem/Ecma/MetadataExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ public static class MetadataExtensions
2424
return metadataReader.GetCustomAttribute(attributeHandle).DecodeValue(new CustomAttributeTypeProvider(This.EcmaModule));
2525
}
2626

27+
public static IEnumerable<CustomAttributeValue<TypeDesc>> GetDecodedCustomAttributes(this EcmaType This,
28+
string attributeNamespace, string attributeName)
29+
{
30+
var metadataReader = This.MetadataReader;
31+
var attributeHandles = metadataReader.GetTypeDefinition(This.Handle).GetCustomAttributes();
32+
foreach (var attributeHandle in attributeHandles)
33+
{
34+
if (IsEqualCustomAttributeName(attributeHandle, metadataReader, attributeNamespace, attributeName))
35+
{
36+
yield return metadataReader.GetCustomAttribute(attributeHandle).DecodeValue(new CustomAttributeTypeProvider(This.EcmaModule));
37+
}
38+
}
39+
}
40+
2741
public static CustomAttributeValue<TypeDesc>? GetDecodedCustomAttribute(this EcmaMethod This,
2842
string attributeNamespace, string attributeName)
2943
{

src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/PropertyValue.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,15 @@ protected static Delegate GetGetMethod(PropertyInfo property, Type propertyType)
211211
}
212212
}
213213

214-
private sealed class ReferenceTypeHelper<TContainer> : TypeHelper where TContainer : class?
214+
private sealed class ReferenceTypeHelper<TContainer> : TypeHelper where TContainer : class
215215
{
216+
private static Func<TContainer, TProperty> GetGetMethod<TProperty>(PropertyInfo property) where TProperty : struct =>
217+
#if ES_BUILD_STANDALONE
218+
(Func<TContainer, TProperty>)property.GetMethod!.CreateDelegate(typeof(Func<TContainer, TProperty>));
219+
#else
220+
property.GetMethod!.CreateDelegate<Func<TContainer, TProperty>>();
221+
#endif
222+
216223
public override Func<PropertyValue, PropertyValue> GetPropertyGetter(PropertyInfo property)
217224
{
218225
Type type = property.PropertyType;
@@ -227,25 +234,25 @@ public override Func<PropertyValue, PropertyValue> GetPropertyGetter(PropertyInf
227234
if (type.IsEnum)
228235
type = Enum.GetUnderlyingType(type);
229236

230-
if (type == typeof(bool)) { var f = (Func<TContainer, bool>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
231-
if (type == typeof(byte)) { var f = (Func<TContainer, byte>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
232-
if (type == typeof(sbyte)) { var f = (Func<TContainer, sbyte>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
233-
if (type == typeof(char)) { var f = (Func<TContainer, char>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
234-
if (type == typeof(short)) { var f = (Func<TContainer, short>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
235-
if (type == typeof(ushort)) { var f = (Func<TContainer, ushort>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
236-
if (type == typeof(int)) { var f = (Func<TContainer, int>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
237-
if (type == typeof(uint)) { var f = (Func<TContainer, uint>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
238-
if (type == typeof(long)) { var f = (Func<TContainer, long>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
239-
if (type == typeof(ulong)) { var f = (Func<TContainer, ulong>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
240-
if (type == typeof(IntPtr)) { var f = (Func<TContainer, IntPtr>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
241-
if (type == typeof(UIntPtr)) { var f = (Func<TContainer, UIntPtr>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
242-
if (type == typeof(float)) { var f = (Func<TContainer, float>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
243-
if (type == typeof(double)) { var f = (Func<TContainer, double>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
244-
if (type == typeof(Guid)) { var f = (Func<TContainer, Guid>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
245-
if (type == typeof(DateTime)) { var f = (Func<TContainer, DateTime>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
246-
if (type == typeof(DateTimeOffset)) { var f = (Func<TContainer, DateTimeOffset>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
247-
if (type == typeof(TimeSpan)) { var f = (Func<TContainer, TimeSpan>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
248-
if (type == typeof(decimal)) { var f = (Func<TContainer, decimal>)GetGetMethod(property, type); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
237+
if (type == typeof(bool)) { var f = GetGetMethod<bool>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
238+
if (type == typeof(byte)) { var f = GetGetMethod<byte>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
239+
if (type == typeof(sbyte)) { var f = GetGetMethod<sbyte>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
240+
if (type == typeof(char)) { var f = GetGetMethod<char>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
241+
if (type == typeof(short)) { var f = GetGetMethod<short>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
242+
if (type == typeof(ushort)) { var f = GetGetMethod<ushort>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
243+
if (type == typeof(int)) { var f = GetGetMethod<int>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
244+
if (type == typeof(uint)) { var f = GetGetMethod<uint>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
245+
if (type == typeof(long)) { var f = GetGetMethod<long>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
246+
if (type == typeof(ulong)) { var f = GetGetMethod<ulong>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
247+
if (type == typeof(IntPtr)) { var f = GetGetMethod<IntPtr>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
248+
if (type == typeof(UIntPtr)) { var f = GetGetMethod<UIntPtr>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
249+
if (type == typeof(float)) { var f = GetGetMethod<float>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
250+
if (type == typeof(double)) { var f = GetGetMethod<double>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
251+
if (type == typeof(Guid)) { var f = GetGetMethod<Guid>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
252+
if (type == typeof(DateTime)) { var f = GetGetMethod<DateTime>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
253+
if (type == typeof(DateTimeOffset)) { var f = GetGetMethod<DateTimeOffset>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
254+
if (type == typeof(TimeSpan)) { var f = GetGetMethod<TimeSpan>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
255+
if (type == typeof(decimal)) { var f = GetGetMethod<decimal>(property); return container => new PropertyValue(f((TContainer)container.ReferenceValue!)); }
249256

250257
return container => new PropertyValue(property.GetValue(container.ReferenceValue));
251258
}

src/libraries/System.Private.CoreLib/src/System/Enum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ private static RuntimeType ValidateRuntimeType(Type enumType)
15041504
if (enumType is not RuntimeType rtType)
15051505
throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType));
15061506
#if CORERT
1507-
// Check for the unfortunate "typeof(Outer<>).InnerEnum" corner case.
1507+
// Check for the unfortunate "typeof(Outer<>.InnerEnum)" corner case.
15081508
// https://github.com/dotnet/runtime/issues/7976
15091509
if (enumType.ContainsGenericParameters)
15101510
throw new InvalidOperationException(SR.Format(SR.Arg_OpenType, enumType.ToString()));

src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,17 @@ static async ValueTask<int> FinishReadAsync(Task<int> readTask, byte[] localBuff
332332
}
333333
}
334334

335+
#if CORERT // TODO: https://github.com/dotnet/corert/issues/3251
336+
private bool HasOverriddenBeginEndRead() => true;
337+
338+
private bool HasOverriddenBeginEndWrite() => true;
339+
#else
335340
[MethodImpl(MethodImplOptions.InternalCall)]
336341
private extern bool HasOverriddenBeginEndRead();
337342

338343
[MethodImpl(MethodImplOptions.InternalCall)]
339344
private extern bool HasOverriddenBeginEndWrite();
345+
#endif
340346

341347
private Task<int> BeginEndReadAsync(byte[] buffer, int offset, int count)
342348
{

src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/AccessorBuilder.cs

Lines changed: 71 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
88
using System.Reflection;
9+
using System.Runtime.CompilerServices;
910
using System.Runtime.Serialization;
1011
using System.Text;
1112
using System.Threading.Tasks;
@@ -40,9 +41,41 @@ public static Getter CreateGetter(MemberInfo memberInfo)
4041
{
4142
if (memberInfo is PropertyInfo propInfo)
4243
{
43-
var createGetterGeneric = s_createGetterInternal.MakeGenericMethod(propInfo.DeclaringType!, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Getter>>();
44-
Getter accessor = createGetterGeneric(propInfo);
45-
return accessor;
44+
Type declaringType = propInfo.DeclaringType!;
45+
Type propertyType = propInfo.PropertyType!;
46+
47+
if (declaringType.IsGenericType && declaringType.GetGenericTypeDefinition() == typeof(KeyValue<,>))
48+
{
49+
if (propInfo.Name == "Key")
50+
{
51+
return (obj) =>
52+
{
53+
return ((IKeyValue)obj).Key;
54+
};
55+
}
56+
else
57+
{
58+
return (obj) =>
59+
{
60+
return ((IKeyValue)obj).Value;
61+
};
62+
}
63+
}
64+
65+
// If either of the arguments to MakeGenericMethod is a valuetype, this is going to cause JITting.
66+
// Only JIT if dynamic code is supported.
67+
if (RuntimeFeature.IsDynamicCodeSupported || (!declaringType.IsValueType && !propertyType.IsValueType))
68+
{
69+
var createGetterGeneric = s_createGetterInternal.MakeGenericMethod(declaringType, propertyType).CreateDelegate<Func<PropertyInfo, Getter>>();
70+
return createGetterGeneric(propInfo);
71+
}
72+
else
73+
{
74+
return (obj) =>
75+
{
76+
return propInfo.GetValue(obj);
77+
};
78+
}
4679
}
4780
else if (memberInfo is FieldInfo fieldInfo)
4881
{
@@ -67,9 +100,41 @@ public static Setter CreateSetter(MemberInfo memberInfo)
67100
PropertyInfo propInfo = (PropertyInfo)memberInfo;
68101
if (propInfo.CanWrite)
69102
{
70-
var buildSetAccessorGeneric = s_createSetterInternal.MakeGenericMethod(propInfo.DeclaringType!, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Setter>>();
71-
Setter accessor = buildSetAccessorGeneric(propInfo);
72-
return accessor;
103+
Type declaringType = propInfo.DeclaringType!;
104+
Type propertyType = propInfo.PropertyType!;
105+
106+
if (declaringType.IsGenericType && declaringType.GetGenericTypeDefinition() == typeof(KeyValue<,>))
107+
{
108+
if (propInfo.Name == "Key")
109+
{
110+
return (ref object obj, object? val) =>
111+
{
112+
((IKeyValue)obj).Key = val;
113+
};
114+
}
115+
else
116+
{
117+
return (ref object obj, object? val) =>
118+
{
119+
((IKeyValue)obj).Value = val;
120+
};
121+
}
122+
}
123+
124+
// If either of the arguments to MakeGenericMethod is a valuetype, this is going to cause JITting.
125+
// Only JIT if dynamic code is supported.
126+
if (RuntimeFeature.IsDynamicCodeSupported || (!declaringType.IsValueType && !propertyType.IsValueType))
127+
{
128+
var createSetterGeneric = s_createSetterInternal.MakeGenericMethod(propInfo.DeclaringType!, propInfo.PropertyType).CreateDelegate<Func<PropertyInfo, Setter>>();
129+
return createSetterGeneric(propInfo);
130+
}
131+
else
132+
{
133+
return (ref object obj, object? val) =>
134+
{
135+
propInfo.SetValue(obj, val);
136+
};
137+
}
73138
}
74139
else
75140
{
@@ -98,24 +163,6 @@ public static Setter CreateSetter(MemberInfo memberInfo)
98163

99164
private static Getter CreateGetterInternal<DeclaringType, PropertyType>(PropertyInfo propInfo)
100165
{
101-
if (typeof(DeclaringType).IsGenericType && typeof(DeclaringType).GetGenericTypeDefinition() == typeof(KeyValue<,>))
102-
{
103-
if (propInfo.Name == "Key")
104-
{
105-
return (obj) =>
106-
{
107-
return ((IKeyValue)obj).Key;
108-
};
109-
}
110-
else
111-
{
112-
return (obj) =>
113-
{
114-
return ((IKeyValue)obj).Value;
115-
};
116-
}
117-
}
118-
119166
if (typeof(DeclaringType).IsValueType)
120167
{
121168
var getMethod = propInfo.GetMethod!.CreateDelegate<StructGetDelegate<DeclaringType, PropertyType>>();
@@ -139,24 +186,6 @@ private static Getter CreateGetterInternal<DeclaringType, PropertyType>(Property
139186

140187
private static Setter CreateSetterInternal<DeclaringType, PropertyType>(PropertyInfo propInfo)
141188
{
142-
if (typeof(DeclaringType).IsGenericType && typeof(DeclaringType).GetGenericTypeDefinition() == typeof(KeyValue<,>))
143-
{
144-
if (propInfo.Name == "Key")
145-
{
146-
return (ref object obj, object? val) =>
147-
{
148-
((IKeyValue)obj).Key = val;
149-
};
150-
}
151-
else
152-
{
153-
return (ref object obj, object? val) =>
154-
{
155-
((IKeyValue)obj).Value = val;
156-
};
157-
}
158-
}
159-
160189
if (typeof(DeclaringType).IsValueType)
161190
{
162191
var setMethod = propInfo.SetMethod!.CreateDelegate<StructSetDelegate<DeclaringType, PropertyType>>();

0 commit comments

Comments
 (0)