Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
112 changes: 112 additions & 0 deletions src/ILCompiler.Compiler/src/Compiler/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,118 @@ public bool HasFixedSlotVTable(TypeDesc type)
return NodeFactory.VTable(type).HasFixedSlots;
}

public bool NeedsRuntimeLookup(ReadyToRunHelperId lookupKind, object targetOfLookup)
{
switch (lookupKind)
{
case ReadyToRunHelperId.TypeHandle:
case ReadyToRunHelperId.NecessaryTypeHandle:
case ReadyToRunHelperId.DefaultConstructor:
return ((TypeDesc)targetOfLookup).IsRuntimeDeterminedSubtype;

case ReadyToRunHelperId.MethodDictionary:
case ReadyToRunHelperId.MethodEntry:
case ReadyToRunHelperId.VirtualDispatchCell:
case ReadyToRunHelperId.MethodHandle:
return ((MethodDesc)targetOfLookup).IsRuntimeDeterminedExactMethod;

case ReadyToRunHelperId.FieldHandle:
return ((FieldDesc)targetOfLookup).OwningType.IsRuntimeDeterminedSubtype;

default:
throw new NotImplementedException();
}
}

public ISymbolNode ComputeConstantLookup(ReadyToRunHelperId lookupKind, object targetOfLookup)
{
switch (lookupKind)
{
case ReadyToRunHelperId.TypeHandle:
return NodeFactory.ConstructedTypeSymbol((TypeDesc)targetOfLookup);
case ReadyToRunHelperId.NecessaryTypeHandle:
return NodeFactory.NecessaryTypeSymbol((TypeDesc)targetOfLookup);
case ReadyToRunHelperId.MethodDictionary:
return NodeFactory.MethodGenericDictionary((MethodDesc)targetOfLookup);
case ReadyToRunHelperId.MethodEntry:
return NodeFactory.FatFunctionPointer((MethodDesc)targetOfLookup);
case ReadyToRunHelperId.MethodHandle:
return NodeFactory.RuntimeMethodHandle((MethodDesc)targetOfLookup);
case ReadyToRunHelperId.FieldHandle:
return NodeFactory.RuntimeFieldHandle((FieldDesc)targetOfLookup);
case ReadyToRunHelperId.DefaultConstructor:
{
var type = (TypeDesc)targetOfLookup;
MethodDesc ctor = type.GetDefaultConstructor();
if (ctor == null)
{
MetadataType activatorType = TypeSystemContext.SystemModule.GetKnownType("System", "Activator");
MetadataType classWithMissingCtor = activatorType.GetKnownNestedType("ClassWithMissingConstructor");
ctor = classWithMissingCtor.GetParameterlessConstructor();
}
return NodeFactory.CanonicalEntrypoint(ctor);
}

default:
throw new NotImplementedException();
}
}

public GenericDictionaryLookup ComputeGenericLookup(MethodDesc contextMethod, ReadyToRunHelperId lookupKind, object targetOfLookup)
{
GenericContextSource contextSource;

if (contextMethod.RequiresInstMethodDescArg())
{
contextSource = GenericContextSource.MethodParameter;
}
else if (contextMethod.RequiresInstMethodTableArg())
{
contextSource = GenericContextSource.TypeParameter;
}
else
{
Debug.Assert(contextMethod.AcquiresInstMethodTableFromThis());
contextSource = GenericContextSource.ThisObject;
}

// Can we do a fixed lookup? Start by checking if we can get to the dictionary.
// Context source having a vtable with fixed slots is a prerequisite.
if (contextSource == GenericContextSource.MethodParameter
|| HasFixedSlotVTable(contextMethod.OwningType))
{
DictionaryLayoutNode dictionaryLayout;
if (contextSource == GenericContextSource.MethodParameter)
dictionaryLayout = _nodeFactory.GenericDictionaryLayout(contextMethod);
else
dictionaryLayout = _nodeFactory.GenericDictionaryLayout(contextMethod.OwningType);

// If the dictionary layout has fixed slots, we can compute the lookup now. Otherwise defer to helper.
if (dictionaryLayout.HasFixedSlots)
{
int pointerSize = _nodeFactory.Target.PointerSize;

GenericLookupResult lookup = ReadyToRunGenericHelperNode.GetLookupSignature(_nodeFactory, lookupKind, targetOfLookup);
int dictionarySlot = dictionaryLayout.GetSlotForEntry(lookup);
int dictionaryOffset = dictionarySlot * pointerSize;

if (contextSource == GenericContextSource.MethodParameter)
{
return GenericDictionaryLookup.CreateFixedLookup(contextSource, dictionaryOffset);
}
else
{
int vtableSlot = VirtualMethodSlotHelper.GetGenericDictionarySlot(_nodeFactory, contextMethod.OwningType);
int vtableOffset = EETypeNode.GetVTableOffset(pointerSize) + vtableSlot * pointerSize;
return GenericDictionaryLookup.CreateFixedLookup(contextSource, vtableOffset, dictionaryOffset);
}
}
}

// Fixed lookup not possible - use helper.
return GenericDictionaryLookup.CreateHelperLookup(contextSource);
}

CompilationResults ICompilation.Compile(string outputFile, ObjectDumper dumper)
{
if (dumper != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public ReadyToRunGenericHelperNode(NodeFactory factory, ReadyToRunHelperId helpe
_lookupSignature = GetLookupSignature(factory, helperId, target);
}

private static GenericLookupResult GetLookupSignature(NodeFactory factory, ReadyToRunHelperId id, object target)
public static GenericLookupResult GetLookupSignature(NodeFactory factory, ReadyToRunHelperId id, object target)
{
// Necessary type handle is not something you can put in a dictionary - someone should have normalized to TypeHandle
Debug.Assert(id != ReadyToRunHelperId.NecessaryTypeHandle);

switch (id)
{
case ReadyToRunHelperId.TypeHandle:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum ReadyToRunHelperId

// The following helpers are used for generic lookups only
TypeHandle,
NecessaryTypeHandle,
MethodHandle,
FieldHandle,
MethodDictionary,
Expand Down
98 changes: 98 additions & 0 deletions src/ILCompiler.Compiler/src/Compiler/GenericDictionaryLookup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

using Debug = System.Diagnostics.Debug;

namespace ILCompiler
{
/// <summary>
/// Structure that specifies how a generic dictionary lookup should be performed.
/// </summary>
public struct GenericDictionaryLookup
{
private const short UseHelperOffset = -1;

private short _offset1;
private short _offset2;

public readonly GenericContextSource ContextSource;

public bool UseHelper
{
get
{
return _offset1 == UseHelperOffset;
}
}

public int NumberOfIndirections
{
get
{
Debug.Assert(!UseHelper);
return ContextSource == GenericContextSource.MethodParameter ? 1 : 2;
}
}

public int this[int index]
{
get
{
Debug.Assert(!UseHelper);
Debug.Assert(index < NumberOfIndirections);
switch (index)
{
case 0:
return _offset1;
case 1:
return _offset2;
}

// Should be unreachable.
throw new NotSupportedException();
}
}

private GenericDictionaryLookup(GenericContextSource contextSource, int offset1, int offset2)
{
ContextSource = contextSource;
_offset1 = checked((short)offset1);
_offset2 = checked((short)offset2);
}

public static GenericDictionaryLookup CreateFixedLookup(GenericContextSource contextSource, int offset1, int offset2 = UseHelperOffset)
{
Debug.Assert(offset1 != UseHelperOffset);
return new GenericDictionaryLookup(contextSource, offset1, offset2);
}

public static GenericDictionaryLookup CreateHelperLookup(GenericContextSource contextSource)
{
return new GenericDictionaryLookup(contextSource, UseHelperOffset, UseHelperOffset);
}
}

/// <summary>
/// Specifies to source of the generic context.
/// </summary>
public enum GenericContextSource
{
/// <summary>
/// Generic context is specified by a hidden parameter that has a method dictionary.
/// </summary>
MethodParameter,

/// <summary>
/// Generic context is specified by a hidden parameter that has a type.
/// </summary>
TypeParameter,

/// <summary>
/// Generic context is specified implicitly by the `this` object.
/// </summary>
ThisObject,
}
}
1 change: 1 addition & 0 deletions src/ILCompiler.Compiler/src/ILCompiler.Compiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
<Compile Include="Compiler\DependencyAnalysis\Target_ARM\ARMReadyToRunGenericHelperNode.cs" />
<Compile Include="Compiler\DependencyAnalysis\Target_ARM\ARMDebug.cs" />
<Compile Include="Compiler\ExportedMethodsRootProvider.cs" />
<Compile Include="Compiler\GenericDictionaryLookup.cs" />
<Compile Include="Compiler\IRootingServiceProvider.cs" />
<Compile Include="Compiler\JitHelper.cs" />
<Compile Include="Compiler\LazyGenericsPolicy.cs" />
Expand Down
Loading