Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove warning generation for reflection access to compiler-generated…
… code.

#85042
  • Loading branch information
vitek-karas committed Apr 24, 2023
commit 33768cb4ebd3e5ddca0daec455bb25af57da8319
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Reflection.Metadata;
using ILCompiler.Logging;
using ILLink.Shared;
using ILLink.Shared.TrimAnalysis;
using Internal.IL;
using Internal.TypeSystem;
using Internal.TypeSystem.Ecma;
Expand All @@ -28,12 +27,10 @@ private readonly record struct TypeArgumentInfo(
IReadOnlyList<GenericParameterDesc?>? OriginalAttributes);

private readonly TypeCacheHashtable _typeCacheHashtable;
private DataflowRequirementHashtable? _dataflowRequirementHashtable;

public CompilerGeneratedState(ILProvider ilProvider, Logger logger)
{
_typeCacheHashtable = new TypeCacheHashtable(ilProvider, logger);
_dataflowRequirementHashtable = null;
}

private sealed class TypeCacheHashtable : LockFreeReaderHashtable<MetadataType, TypeCache>
Expand Down Expand Up @@ -665,105 +662,5 @@ public bool TryGetUserMethodForCompilerGeneratedMember(TypeSystemEntity sourceMe

return false;
}

private sealed class DataflowRequirementValue
{
public MethodDesc Method;
public bool RequiresDataflow;

public DataflowRequirementValue(MethodDesc method, FlowAnnotations flowAnnotations)
{
Debug.Assert(method == method.GetTypicalMethodDefinition());
Debug.Assert(IsNestedFunctionOrStateMachineMember(method));

Method = method;

bool requiresDataflowProcessing = ReflectionMethodBodyScanner.RequiresReflectionMethodBodyScannerForMethodBody(flowAnnotations, method);

MethodIL body = flowAnnotations.ILProvider.GetMethodIL(method);
if (body != null)
{
ILReader reader = new ILReader(body.GetILBytes());
while (reader.HasNext)
{
ILOpcode opcode = reader.ReadILOpcode();
switch (opcode)
{
case ILOpcode.newobj:
case ILOpcode.ldftn:
case ILOpcode.ldvirtftn:
case ILOpcode.call:
case ILOpcode.callvirt:
case ILOpcode.jmp:
{
if (body.GetObject(reader.ReadILToken()) is MethodDesc referencedMethod)
requiresDataflowProcessing |= ReflectionMethodBodyScanner.RequiresReflectionMethodBodyScannerForCallSite(flowAnnotations, referencedMethod);
}
break;

case ILOpcode.ldtoken:
{
object referencedObject = body.GetObject(reader.ReadILToken());
if (referencedObject is FieldDesc referencedField)
requiresDataflowProcessing |= ReflectionMethodBodyScanner.RequiresReflectionMethodBodyScannerForAccess(flowAnnotations, referencedField);
else if (referencedObject is MethodDesc referencedMethod)
requiresDataflowProcessing |= ReflectionMethodBodyScanner.RequiresReflectionMethodBodyScannerForCallSite(flowAnnotations, referencedMethod);
}
break;

case ILOpcode.ldfld:
case ILOpcode.ldsfld:
case ILOpcode.ldflda:
case ILOpcode.ldsflda:
case ILOpcode.stfld:
case ILOpcode.stsfld:
{
if (body.GetObject(reader.ReadILToken()) is FieldDesc referencedField)
requiresDataflowProcessing |= ReflectionMethodBodyScanner.RequiresReflectionMethodBodyScannerForAccess(flowAnnotations, referencedField);
}
break;

default:
reader.Skip(opcode);
break;
}
}
}

RequiresDataflow = requiresDataflowProcessing;
}
}

private sealed class DataflowRequirementHashtable : LockFreeReaderHashtable<MethodDesc, DataflowRequirementValue>
{
private FlowAnnotations _flowAnnotations;

public DataflowRequirementHashtable(FlowAnnotations flowAnnotations) => _flowAnnotations = flowAnnotations;

protected override bool CompareKeyToValue(MethodDesc key, DataflowRequirementValue value) => key == value.Method;
protected override bool CompareValueToValue(DataflowRequirementValue value1, DataflowRequirementValue value2) => value1.Method == value2.Method;
protected override int GetKeyHashCode(MethodDesc key) => key.GetHashCode();
protected override int GetValueHashCode(DataflowRequirementValue value) => value.Method.GetHashCode();

protected override DataflowRequirementValue CreateValueFromKey(MethodDesc key)
=> new DataflowRequirementValue(key, _flowAnnotations);
}

public void InitializeFlowAnnotations(FlowAnnotations flowAnnotations)
{
Debug.Assert(_dataflowRequirementHashtable == null);
_dataflowRequirementHashtable = new DataflowRequirementHashtable(flowAnnotations);
}

public bool CompilerGeneratedMethodRequiresDataflow(MethodDesc method)
{
Debug.Assert(IsNestedFunctionOrStateMachineMember(method));

if (_dataflowRequirementHashtable == null)
throw new InvalidOperationException();

method = method.GetTypicalMethodDefinition();
return _dataflowRequirementHashtable.GetOrCreateValue(method).RequiresDataflow;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public FlowAnnotations(Logger logger, ILProvider ilProvider, CompilerGeneratedSt
_logger = logger;
ILProvider = ilProvider;
CompilerGeneratedState = compilerGeneratedState;
compilerGeneratedState.InitializeFlowAnnotations(this);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ReflectionAccessFromStateMachine
[ExpectedWarning ("IL3002", "--MethodWithLocalFunctionWithRUC.LocalFunction--", CompilerGeneratedCode = true, ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL3050", "--MethodWithLocalFunctionWithRUC.LocalFunction--", CompilerGeneratedCode = true, ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction", CompilerGeneratedCode = true,
ProducedBy = Tool.Trimmer | Tool.NativeAot)]
ProducedBy = Tool.Trimmer)]
[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations), CompilerGeneratedCode = true)]
static IEnumerable<int> TestIterator ()
{
Expand All @@ -54,7 +54,7 @@ static IEnumerable<int> TestIteratorWithRUC ()
[ExpectedWarning ("IL3002", "--MethodWithLocalFunctionWithRUC.LocalFunction--", CompilerGeneratedCode = true, ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL3050", "--MethodWithLocalFunctionWithRUC.LocalFunction--", CompilerGeneratedCode = true, ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction", CompilerGeneratedCode = true,
ProducedBy = Tool.Trimmer | Tool.NativeAot)]
ProducedBy = Tool.Trimmer)]
[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations), CompilerGeneratedCode = true)]
static async void TestAsync ()
{
Expand Down Expand Up @@ -97,7 +97,7 @@ static void TestLocalFunction ()
[ExpectedWarning ("IL3002", "--MethodWithLocalFunctionWithRUC.LocalFunction--", ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL3050", "--MethodWithLocalFunctionWithRUC.LocalFunction--", ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction",
ProducedBy = Tool.Trimmer | Tool.NativeAot)]
ProducedBy = Tool.Trimmer)]
[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations))]
void LocalFunction ()
{
Expand Down Expand Up @@ -156,7 +156,7 @@ static void TestLambda ()
[ExpectedWarning ("IL3002", "--MethodWithLocalFunctionWithRUC.LocalFunction--", ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL3050", "--MethodWithLocalFunctionWithRUC.LocalFunction--", ProducedBy = Tool.NativeAot)]
[ExpectedWarning ("IL2118", nameof (TypeWithMethodWithRequires.MethodWithLocalFunctionCallsRUC), "LocalFunction",
ProducedBy = Tool.Trimmer | Tool.NativeAot)]
ProducedBy = Tool.Trimmer)]
[ExpectedWarning ("IL2111", nameof (TypeWithMethodWithRequires.MethodWithAnnotations))]
() => {
typeof (TypeWithMethodWithRequires).RequiresAll ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static void TestCallRUCMethodInLtftnLocalFunction ()

class DynamicallyAccessedLocalFunction
{
[ExpectedWarning ("IL2118", "LocalFunction", ProducedBy = Tool.Trimmer | Tool.NativeAot)]
[ExpectedWarning ("IL2118", "LocalFunction", ProducedBy = Tool.Trimmer)]
[UnconditionalSuppressMessage ("Test", "IL2026")]
public static void TestCallRUCMethodInDynamicallyAccessedLocalFunction ()
{
Expand Down Expand Up @@ -426,7 +426,7 @@ static void TestGenericTypeParameterRequirement<TUnknown> ()

class DynamicallyAccessedLambda
{
[ExpectedWarning ("IL2118", nameof (TestCallRUCMethodInDynamicallyAccessedLambda), ProducedBy = Tool.Trimmer | Tool.NativeAot)]
[ExpectedWarning ("IL2118", nameof (TestCallRUCMethodInDynamicallyAccessedLambda), ProducedBy = Tool.Trimmer)]
[UnconditionalSuppressMessage ("Test", "IL2026")]
public static void TestCallRUCMethodInDynamicallyAccessedLambda ()
{
Expand All @@ -440,7 +440,7 @@ public static void TestCallRUCMethodInDynamicallyAccessedLambda ()

class DynamicallyAccessedLambdaUnused
{
[ExpectedWarning ("IL2118", nameof (TestCallRUCMethodInDynamicallyAccessedLambda), ProducedBy = Tool.Trimmer | Tool.NativeAot)]
[ExpectedWarning ("IL2118", nameof (TestCallRUCMethodInDynamicallyAccessedLambda), ProducedBy = Tool.Trimmer)]
[UnconditionalSuppressMessage ("Test", "IL2026")]
public static void TestCallRUCMethodInDynamicallyAccessedLambda ()
{
Expand Down