Skip to content

Commit

Permalink
Use function pointers in crossgen2 (dotnet#42655)
Browse files Browse the repository at this point in the history
Co-authored-by: Adeel Mujahid <adeelbm@outlook.com>
  • Loading branch information
jkotas and am11 authored Sep 24, 2020
1 parent 11350c5 commit 7c3d238
Show file tree
Hide file tree
Showing 7 changed files with 724 additions and 1,331 deletions.
1,805 changes: 646 additions & 1,159 deletions src/coreclr/src/tools/Common/JitInterface/CorInfoBase.cs

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions src/coreclr/src/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ private enum ImageFileMachine
private IntPtr _jit;

private IntPtr _unmanagedCallbacks; // array of pointers to JIT-EE interface callbacks
private Object _keepAlive; // Keeps delegates for the callbacks alive

private ExceptionDispatchInfo _lastException;

Expand Down Expand Up @@ -122,7 +121,7 @@ public CorInfoImpl()
throw new IOException("Failed to initialize JIT");
}

_unmanagedCallbacks = GetUnmanagedCallbacks(out _keepAlive);
_unmanagedCallbacks = GetUnmanagedCallbacks();
}

public TextWriter Log
Expand Down Expand Up @@ -1043,7 +1042,7 @@ private CorInfoUnmanagedCallConv getUnmanagedCallConv(CORINFO_METHOD_STRUCT_* me

private bool satisfiesMethodConstraints(CORINFO_CLASS_STRUCT_* parent, CORINFO_METHOD_STRUCT_* method)
{ throw new NotImplementedException("satisfiesMethodConstraints"); }
private bool isCompatibleDelegate(CORINFO_CLASS_STRUCT_* objCls, CORINFO_CLASS_STRUCT_* methodParentCls, CORINFO_METHOD_STRUCT_* method, CORINFO_CLASS_STRUCT_* delegateCls, ref bool pfIsOpenDelegate)
private bool isCompatibleDelegate(CORINFO_CLASS_STRUCT_* objCls, CORINFO_CLASS_STRUCT_* methodParentCls, CORINFO_METHOD_STRUCT_* method, CORINFO_CLASS_STRUCT_* delegateCls, BOOL* pfIsOpenDelegate)
{ throw new NotImplementedException("isCompatibleDelegate"); }
private void setPatchpointInfo(PatchpointInfo* patchpointInfo)
{ throw new NotImplementedException("setPatchpointInfo"); }
Expand Down Expand Up @@ -1529,7 +1528,7 @@ private void LongLifetimeFree(void* obj)
Marshal.FreeCoTaskMem((IntPtr)obj);
}

private byte* getClassModuleIdForStatics(CORINFO_CLASS_STRUCT_* cls, CORINFO_MODULE_STRUCT_** pModule, void** ppIndirection)
private UIntPtr getClassModuleIdForStatics(CORINFO_CLASS_STRUCT_* cls, CORINFO_MODULE_STRUCT_** pModule, void** ppIndirection)
{ throw new NotImplementedException("getClassModuleIdForStatics"); }

private uint getClassSize(CORINFO_CLASS_STRUCT_* cls)
Expand Down Expand Up @@ -2525,7 +2524,7 @@ private uint getMethodHash(CORINFO_METHOD_STRUCT_* ftn)
return (uint)HandleToObject(ftn).GetHashCode();
}

private byte* findNameOfToken(CORINFO_MODULE_STRUCT_* moduleHandle, mdToken token, byte* szFQName, UIntPtr FQNameCapacity)
private UIntPtr findNameOfToken(CORINFO_MODULE_STRUCT_* moduleHandle, mdToken token, byte* szFQName, UIntPtr FQNameCapacity)
{ throw new NotImplementedException("findNameOfToken"); }

private bool getSystemVAmd64PassStructInRegisterDescriptor(CORINFO_CLASS_STRUCT_* structHnd, SYSTEMV_AMD64_CORINFO_STRUCT_REG_PASSING_DESCRIPTOR* structPassInRegDescPtr)
Expand Down Expand Up @@ -2615,7 +2614,7 @@ private void getLocationOfThisType(CORINFO_METHOD_STRUCT_* context, ref CORINFO_
ppIndirection = null;
return null;
}
private void GetProfilingHandle(ref bool pbHookFunction, ref void* pProfilerHandle, ref bool pbIndirectedHandles)
private void GetProfilingHandle(BOOL* pbHookFunction, ref void* pProfilerHandle, BOOL* pbIndirectedHandles)
{ throw new NotImplementedException("GetProfilingHandle"); }

/// <summary>
Expand Down
40 changes: 6 additions & 34 deletions src/coreclr/src/tools/Common/JitInterface/CorInfoTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

namespace Internal.JitInterface
{
public enum BOOL : int
{
FALSE = 0,
TRUE = 1,
}

public static class CORINFO
{
// CORINFO_MAXINDIRECTIONS is the maximum number of
Expand All @@ -20,40 +26,14 @@ public static class CORINFO

public struct CORINFO_METHOD_STRUCT_
{
internal static unsafe CORINFO_METHOD_STRUCT_* Construct(int i)
{
return (CORINFO_METHOD_STRUCT_*)((i + 1) << 4);
}

internal static unsafe int GetValue(CORINFO_METHOD_STRUCT_* val)
{
return ((int)val - 1) >> 4;
}
}

public struct CORINFO_FIELD_STRUCT_
{
internal static unsafe CORINFO_FIELD_STRUCT_* Construct(int i)
{
return (CORINFO_FIELD_STRUCT_*)((i + 1) << 4);
}
internal static unsafe int GetValue(CORINFO_FIELD_STRUCT_* val)
{
return ((int)val - 1) >> 4;
}
}

public struct CORINFO_CLASS_STRUCT_
{
internal static unsafe CORINFO_CLASS_STRUCT_* Construct(int i)
{
return (CORINFO_CLASS_STRUCT_*)((i + 1) << 4);
}

internal static unsafe int GetValue(CORINFO_CLASS_STRUCT_* val)
{
return ((int)val - 1) >> 4;
}
}

public struct CORINFO_ARG_LIST_STRUCT_
Expand All @@ -62,14 +42,6 @@ public struct CORINFO_ARG_LIST_STRUCT_

public struct CORINFO_MODULE_STRUCT_
{
internal static unsafe CORINFO_MODULE_STRUCT_* Construct(int i)
{
return (CORINFO_MODULE_STRUCT_*)((i + 1) << 4);
}
internal static unsafe int GetValue(CORINFO_MODULE_STRUCT_* val)
{
return ((int)val - 1) >> 4;
}
}

public struct CORINFO_ASSEMBLY_STRUCT_
Expand Down
34 changes: 12 additions & 22 deletions src/coreclr/src/tools/Common/JitInterface/JitConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static JitConfigProvider Instance

private CorJitFlag[] _jitFlags;
private Dictionary<string, string> _config = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
private object _keepAlive; // Keeps callback delegates alive

public static void Initialize(
TargetDetails target,
Expand Down Expand Up @@ -151,43 +150,34 @@ private static string GetTargetSpec(TargetDetails target)

#region Unmanaged instance

private unsafe IntPtr CreateUnmanagedInstance()
private static unsafe IntPtr CreateUnmanagedInstance()
{
// TODO: this potentially leaks memory, but since we only expect to have one per compilation,
// This potentially leaks memory, but since we only expect to have one per compilation,
// it shouldn't matter...

const int numCallbacks = 2;

IntPtr* callbacks = (IntPtr*)Marshal.AllocCoTaskMem(sizeof(IntPtr) * numCallbacks);
object[] delegates = new object[numCallbacks];
void** callbacks = (void**)Marshal.AllocCoTaskMem(sizeof(IntPtr) * numCallbacks);

var d0 = new __getIntConfigValue(getIntConfigValue);
callbacks[0] = Marshal.GetFunctionPointerForDelegate(d0);
delegates[0] = d0;
callbacks[0] = (delegate* <IntPtr, char*, int, int>)&getIntConfigValue;
callbacks[1] = (delegate* <IntPtr, char*, char*, int, int>)&getStringConfigValue;

var d1 = new __getStringConfigValue(getStringConfigValue);
callbacks[1] = Marshal.GetFunctionPointerForDelegate(d1);
delegates[1] = d1;

_keepAlive = delegates;
IntPtr instance = Marshal.AllocCoTaskMem(sizeof(IntPtr));
*(IntPtr**)instance = callbacks;
*(IntPtr*)instance = (IntPtr)callbacks;

return instance;
}

[UnmanagedFunctionPointer(default(CallingConvention))]
private unsafe delegate int __getIntConfigValue(IntPtr thisHandle, [MarshalAs(UnmanagedType.LPWStr)] string name, int defaultValue);
private unsafe int getIntConfigValue(IntPtr thisHandle, string name, int defaultValue)
[UnmanagedCallersOnly]
private static unsafe int getIntConfigValue(IntPtr thisHandle, char* name, int defaultValue)
{
return GetIntConfigValue(name, defaultValue);
return s_instance.GetIntConfigValue(new string(name), defaultValue);
}

[UnmanagedFunctionPointer(default(CallingConvention))]
private unsafe delegate int __getStringConfigValue(IntPtr thisHandle, [MarshalAs(UnmanagedType.LPWStr)] string name, char* retBuffer, int retBufferLength);
private unsafe int getStringConfigValue(IntPtr thisHandle, string name, char* retBuffer, int retBufferLength)
[UnmanagedCallersOnly]
private static unsafe int getStringConfigValue(IntPtr thisHandle, char* name, char* retBuffer, int retBufferLength)
{
string result = GetStringConfigValue(name);
string result = s_instance.GetStringConfigValue(new string(name));

for (int i = 0; i < Math.Min(retBufferLength, result.Length); i++)
retBuffer[i] = result[i];
Expand Down
Loading

0 comments on commit 7c3d238

Please sign in to comment.