Skip to content

Commit

Permalink
Pass exact context to getMethodInfo (#88025)
Browse files Browse the repository at this point in the history
* Modify JIT-EE API

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
EgorBo and jkotas authored Jul 3, 2023
1 parent 9a3673a commit 19887f7
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 52 deletions.
3 changes: 2 additions & 1 deletion src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2078,7 +2078,8 @@ class ICorStaticInfo
// This method is used to fetch data needed to inline functions
virtual bool getMethodInfo (
CORINFO_METHOD_HANDLE ftn, /* IN */
CORINFO_METHOD_INFO* info /* OUT */
CORINFO_METHOD_INFO* info, /* OUT */
CORINFO_CONTEXT_HANDLE context = NULL /* IN */
) = 0;

// Decides if you have any limitations for inlining. If everything's OK, it will return
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/inc/icorjitinfoimpl_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ void getMethodSig(

bool getMethodInfo(
CORINFO_METHOD_HANDLE ftn,
CORINFO_METHOD_INFO* info) override;
CORINFO_METHOD_INFO* info,
CORINFO_CONTEXT_HANDLE context) override;

CorInfoInline canInline(
CORINFO_METHOD_HANDLE callerHnd,
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/inc/jiteeversionguid.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ typedef const GUID *LPCGUID;
#define GUID_DEFINED
#endif // !GUID_DEFINED

constexpr GUID JITEEVersionIdentifier = { /* 878d63a7-ffc9-421f-81f7-db4729f0ed5c */
0x878d63a7,
0xffc9,
0x421f,
{0x81, 0xf7, 0xdb, 0x47, 0x29, 0xf0, 0xed, 0x5c}
constexpr GUID JITEEVersionIdentifier = { /* 2335da47-1d6c-4844-96b6-025558a525ba */
0x2335da47,
0x1d6c,
0x4844,
{0x96, 0xb6, 0x02, 0x55, 0x58, 0xa5, 0x25, 0xba}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/jit/ICorJitInfo_wrapper_generated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ void WrapICorJitInfo::getMethodSig(

bool WrapICorJitInfo::getMethodInfo(
CORINFO_METHOD_HANDLE ftn,
CORINFO_METHOD_INFO* info)
CORINFO_METHOD_INFO* info,
CORINFO_CONTEXT_HANDLE context)
{
API_ENTER(getMethodInfo);
bool temp = wrapHnd->getMethodInfo(ftn, info);
bool temp = wrapHnd->getMethodInfo(ftn, info, context);
API_LEAVE(getMethodInfo);
return temp;
}
Expand Down
26 changes: 25 additions & 1 deletion src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7775,11 +7775,35 @@ void Compiler::impCheckCanInline(GenTreeCall* call,
return;
}
#endif
JITDUMP("\nCheckCanInline: fetching method info for inline candidate %s -- context %p\n",
compiler->eeGetMethodName(ftn), pParam->exactContextHnd);

if (pParam->exactContextHnd == nullptr)
{
JITDUMP("NULL context\n");
}
else if (pParam->exactContextHnd == METHOD_BEING_COMPILED_CONTEXT())
{
JITDUMP("Current method context\n");
}
else if ((((size_t)pParam->exactContextHnd & CORINFO_CONTEXTFLAGS_MASK) == CORINFO_CONTEXTFLAGS_METHOD))
{
JITDUMP("Method context: %s\n",
compiler->eeGetMethodName((CORINFO_METHOD_HANDLE)pParam->exactContextHnd));
}
else
{
JITDUMP("Class context: %s\n", compiler->eeGetClassName((CORINFO_CLASS_HANDLE)(
(size_t)pParam->exactContextHnd & ~CORINFO_CONTEXTFLAGS_MASK)));
}

const bool isGdv = pParam->call->IsGuardedDevirtualizationCandidate();

// Fetch method info. This may fail, if the method doesn't have IL.
// NOTE: For GDV we're expected to use a different context (per candidate)
//
CORINFO_METHOD_INFO methInfo;
if (!compCompHnd->getMethodInfo(ftn, &methInfo))
if (!compCompHnd->getMethodInfo(ftn, &methInfo, isGdv ? nullptr : pParam->exactContextHnd))
{
inlineResult->NoteFatal(InlineObservation::CALLEE_NO_METHOD_INFO);
return;
Expand Down
22 changes: 21 additions & 1 deletion src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,9 +1179,29 @@ private void getMethodSig(CORINFO_METHOD_STRUCT_* ftn, CORINFO_SIG_INFO* sig, CO
Get_CORINFO_SIG_INFO(method, sig: sig, scope: null);
}

private bool getMethodInfo(CORINFO_METHOD_STRUCT_* ftn, CORINFO_METHOD_INFO* info)
private bool getMethodInfo(CORINFO_METHOD_STRUCT_* ftn, CORINFO_METHOD_INFO* info, CORINFO_CONTEXT_STRUCT* context)
{
MethodDesc method = HandleToObject(ftn);

if (context != null && method.IsSharedByGenericInstantiations)
{
TypeSystemEntity ctx = entityFromContext(context);
if (ctx is MethodDesc methodFromCtx && context != contextFromMethodBeingCompiled())
{
Debug.Assert(method.GetTypicalMethodDefinition() == methodFromCtx.GetTypicalMethodDefinition());
method = methodFromCtx;
}
else if (ctx is InstantiatedType instantiatedCtxType)
{
MethodDesc instantiatedMethod = _compilation.TypeSystemContext.GetMethodForInstantiatedType(method.GetTypicalMethodDefinition(), instantiatedCtxType);
if (method.HasInstantiation)
{
instantiatedMethod = _compilation.TypeSystemContext.GetInstantiatedMethod(instantiatedMethod, method.Instantiation);
}
method = instantiatedMethod;
}
}

#if READYTORUN
// Add an early CanInline check to see if referring to the IL of the target methods is
// permitted from within this MethodBeingCompiled, the full CanInline check will be performed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ private static void _getMethodSig(IntPtr thisHandle, IntPtr* ppException, CORINF
}

[UnmanagedCallersOnly]
private static byte _getMethodInfo(IntPtr thisHandle, IntPtr* ppException, CORINFO_METHOD_STRUCT_* ftn, CORINFO_METHOD_INFO* info)
private static byte _getMethodInfo(IntPtr thisHandle, IntPtr* ppException, CORINFO_METHOD_STRUCT_* ftn, CORINFO_METHOD_INFO* info, CORINFO_CONTEXT_STRUCT* context)
{
var _this = GetThis(thisHandle);
try
{
return _this.getMethodInfo(ftn, info) ? (byte)1 : (byte)0;
return _this.getMethodInfo(ftn, info, context) ? (byte)1 : (byte)0;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -2513,7 +2513,7 @@ private static IntPtr GetUnmanagedCallbacks()
callbacks[1] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, uint>)&_getMethodAttribs;
callbacks[2] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, CorInfoMethodRuntimeFlags, void>)&_setMethodAttribs;
callbacks[3] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, CORINFO_SIG_INFO*, CORINFO_CLASS_STRUCT_*, void>)&_getMethodSig;
callbacks[4] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, CORINFO_METHOD_INFO*, byte>)&_getMethodInfo;
callbacks[4] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, CORINFO_METHOD_INFO*, CORINFO_CONTEXT_STRUCT*, byte>)&_getMethodInfo;
callbacks[5] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, CORINFO_METHOD_STRUCT_*, CorInfoInline>)&_canInline;
callbacks[6] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, CORINFO_METHOD_STRUCT_*, void>)&_beginInlining;
callbacks[7] = (delegate* unmanaged<IntPtr, IntPtr*, CORINFO_METHOD_STRUCT_*, CORINFO_METHOD_STRUCT_*, CorInfoInline, byte*, void>)&_reportInliningDecision;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ FUNCTIONS
uint32_t getMethodAttribs( CORINFO_METHOD_HANDLE ftn );
void setMethodAttribs( CORINFO_METHOD_HANDLE ftn, CorInfoMethodRuntimeFlags attribs );
void getMethodSig( CORINFO_METHOD_HANDLE ftn, CORINFO_SIG_INFO *sig, CORINFO_CLASS_HANDLE memberParent );
bool getMethodInfo( CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info );
bool getMethodInfo(CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, CORINFO_CONTEXT_HANDLE context);
CorInfoInline canInline( CORINFO_METHOD_HANDLE callerHnd, CORINFO_METHOD_HANDLE calleeHnd);
void beginInlining(CORINFO_METHOD_HANDLE inlinerHnd, CORINFO_METHOD_HANDLE inlineeHnd);
void reportInliningDecision(CORINFO_METHOD_HANDLE inlinerHnd, CORINFO_METHOD_HANDLE inlineeHnd, CorInfoInline inlineResult, const char * reason);
Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/tools/aot/jitinterface/jitinterface_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct JitInterfaceCallbacks
uint32_t (* getMethodAttribs)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE ftn);
void (* setMethodAttribs)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE ftn, CorInfoMethodRuntimeFlags attribs);
void (* getMethodSig)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE ftn, CORINFO_SIG_INFO* sig, CORINFO_CLASS_HANDLE memberParent);
bool (* getMethodInfo)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info);
bool (* getMethodInfo)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, CORINFO_CONTEXT_HANDLE context);
CorInfoInline (* canInline)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE callerHnd, CORINFO_METHOD_HANDLE calleeHnd);
void (* beginInlining)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE inlinerHnd, CORINFO_METHOD_HANDLE inlineeHnd);
void (* reportInliningDecision)(void * thisHandle, CorInfoExceptionClass** ppException, CORINFO_METHOD_HANDLE inlinerHnd, CORINFO_METHOD_HANDLE inlineeHnd, CorInfoInline inlineResult, const char* reason);
Expand Down Expand Up @@ -234,10 +234,11 @@ class JitInterfaceWrapper : public ICorJitInfo

virtual bool getMethodInfo(
CORINFO_METHOD_HANDLE ftn,
CORINFO_METHOD_INFO* info)
CORINFO_METHOD_INFO* info,
CORINFO_CONTEXT_HANDLE context)
{
CorInfoExceptionClass* pException = nullptr;
bool temp = _callbacks->getMethodInfo(_thisHandle, &pException, ftn, info);
bool temp = _callbacks->getMethodInfo(_thisHandle, &pException, ftn, info, context);
if (pException != nullptr) throw pException;
return temp;
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/superpmi/superpmi-shared/lwmlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ LWM(GetAssemblyName, DWORDLONG, DWORD)
LWM(GetMethodClass, DWORDLONG, DWORDLONG)
LWM(GetMethodDefFromMethod, DWORDLONG, DWORD)
LWM(GetMethodHash, DWORDLONG, DWORD)
LWM(GetMethodInfo, DWORDLONG, Agnostic_GetMethodInfo)
LWM(GetMethodInfo, DLDL, Agnostic_GetMethodInfo)
LWM(GetMethodNameFromMetadata, Agnostic_CORINFO_METHODNAME_TOKENin, Agnostic_CORINFO_METHODNAME_TOKENout)
LWM(GetMethodSig, DLDL, Agnostic_CORINFO_SIG_INFO)
LWM(GetMethodSync, DWORDLONG, DLDL)
Expand Down
33 changes: 19 additions & 14 deletions src/coreclr/tools/superpmi/superpmi-shared/methodcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2939,13 +2939,14 @@ CorInfoHFAElemType MethodContext::repGetHFAType(CORINFO_CLASS_HANDLE clsHnd)
return (CorInfoHFAElemType)value;
}

void MethodContext::recGetMethodInfo(CORINFO_METHOD_HANDLE ftn,
CORINFO_METHOD_INFO* info,
bool result,
DWORD exceptionCode)
void MethodContext::recGetMethodInfo(CORINFO_METHOD_HANDLE ftn,
CORINFO_METHOD_INFO* info,
CORINFO_CONTEXT_HANDLE context,
bool result,
DWORD exceptionCode)
{
if (GetMethodInfo == nullptr)
GetMethodInfo = new LightWeightMap<DWORDLONG, Agnostic_GetMethodInfo>();
GetMethodInfo = new LightWeightMap<DLDL, Agnostic_GetMethodInfo>();

Agnostic_GetMethodInfo value;
ZeroMemory(&value, sizeof(value));
Expand All @@ -2967,31 +2968,35 @@ void MethodContext::recGetMethodInfo(CORINFO_METHOD_HANDLE ftn,
value.result = result;
value.exceptionCode = (DWORD)exceptionCode;

DWORDLONG key = CastHandle(ftn);
DLDL key;
key.A = CastHandle(ftn);
key.B = CastHandle(context);
GetMethodInfo->Add(key, value);
DEBUG_REC(dmpGetMethodInfo(key, value));
}
void MethodContext::dmpGetMethodInfo(DWORDLONG key, const Agnostic_GetMethodInfo& value)
void MethodContext::dmpGetMethodInfo(DLDL key, const Agnostic_GetMethodInfo& value)
{
if (value.result)
{
printf("GetMethodInfo key ftn-%016" PRIX64 ", value res-%u ftn-%016" PRIX64 " scp-%016" PRIX64 " ilo-%u ils-%u ms-%u ehc-%u opt-%08X rk-%u args-%s locals-%s excp-%08X",
key, value.result, value.info.ftn, value.info.scope, value.info.ILCode_offset, value.info.ILCodeSize,
printf("GetMethodInfo key ftn-%016" PRIX64 " ctx-%016" PRIX64 ", value res-%u ftn-%016" PRIX64 " scp-%016" PRIX64 " ilo-%u ils-%u ms-%u ehc-%u opt-%08X rk-%u args-%s locals-%s excp-%08X",
key.A, key.B, value.result, value.info.ftn, value.info.scope, value.info.ILCode_offset, value.info.ILCodeSize,
value.info.maxStack, value.info.EHcount, value.info.options, value.info.regionKind,
SpmiDumpHelper::DumpAgnostic_CORINFO_SIG_INFO(value.info.args, GetMethodInfo, SigInstHandleMap).c_str(),
SpmiDumpHelper::DumpAgnostic_CORINFO_SIG_INFO(value.info.locals, GetMethodInfo, SigInstHandleMap).c_str(),
value.exceptionCode);
}
else
{
printf("GetMethodInfo key ftn-%016" PRIX64 ", value res-%u excp-%08X",
key, value.result, value.exceptionCode);
printf("GetMethodInfo key ftn-%016" PRIX64 " ctx-%016" PRIX64 ", value res-%u excp-%08X",
key.A, key.B, value.result, value.exceptionCode);
}
}
bool MethodContext::repGetMethodInfo(CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, DWORD* exceptionCode)
bool MethodContext::repGetMethodInfo(CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, CORINFO_CONTEXT_HANDLE context, DWORD* exceptionCode)
{
DWORDLONG key = CastHandle(ftn);
Agnostic_GetMethodInfo value = LookupByKeyOrMiss(GetMethodInfo, key, ": key %016" PRIX64 "", key);
DLDL key;
key.A = CastHandle(ftn);
key.B = CastHandle(context);
Agnostic_GetMethodInfo value = LookupByKeyOrMiss(GetMethodInfo, key, ": key %016" PRIX64 "", key.A);

DEBUG_REP(dmpGetMethodInfo(key, value));

Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/tools/superpmi/superpmi-shared/methodcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ class MethodContext
void dmpGetHFAType(DWORDLONG key, DWORD value);
CorInfoHFAElemType repGetHFAType(CORINFO_CLASS_HANDLE clsHnd);

void recGetMethodInfo(CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, bool result, DWORD exceptionCode);
void dmpGetMethodInfo(DWORDLONG key, const Agnostic_GetMethodInfo& value);
bool repGetMethodInfo(CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, DWORD* exceptionCode);
void recGetMethodInfo(CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, CORINFO_CONTEXT_HANDLE context, bool result, DWORD exceptionCode);
void dmpGetMethodInfo(DLDL key, const Agnostic_GetMethodInfo& value);
bool repGetMethodInfo(CORINFO_METHOD_HANDLE ftn, CORINFO_METHOD_INFO* info, CORINFO_CONTEXT_HANDLE context, DWORD* exceptionCode);

void recGetNewHelper(CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_METHOD_HANDLE callerHandle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ void interceptor_ICJI::getMethodSig(CORINFO_METHOD_HANDLE ftn, /* IN */
// return information about a method private to the implementation
// returns false if method is not IL, or is otherwise unavailable.
// This method is used to fetch data needed to inline functions
bool interceptor_ICJI::getMethodInfo(CORINFO_METHOD_HANDLE ftn, /* IN */
CORINFO_METHOD_INFO* info /* OUT */
bool interceptor_ICJI::getMethodInfo(CORINFO_METHOD_HANDLE ftn, /* IN */
CORINFO_METHOD_INFO* info, /* OUT */
CORINFO_CONTEXT_HANDLE context /* IN */
)
{
bool temp = false;
Expand All @@ -78,11 +79,11 @@ bool interceptor_ICJI::getMethodInfo(CORINFO_METHOD_HANDLE ftn, /* IN */
[&]()
{
mc->cr->AddCall("getMethodInfo");
temp = original_ICorJitInfo->getMethodInfo(ftn, info);
temp = original_ICorJitInfo->getMethodInfo(ftn, info, context);
},
[&](DWORD exceptionCode)
{
this->mc->recGetMethodInfo(ftn, info, temp, exceptionCode);
this->mc->recGetMethodInfo(ftn, info, context, temp, exceptionCode);
});

return temp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ void interceptor_ICJI::getMethodSig(

bool interceptor_ICJI::getMethodInfo(
CORINFO_METHOD_HANDLE ftn,
CORINFO_METHOD_INFO* info)
CORINFO_METHOD_INFO* info,
CORINFO_CONTEXT_HANDLE context)
{
mcs->AddCall("getMethodInfo");
return original_ICorJitInfo->getMethodInfo(ftn, info);
return original_ICorJitInfo->getMethodInfo(ftn, info, context);
}

CorInfoInline interceptor_ICJI::canInline(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ void interceptor_ICJI::getMethodSig(

bool interceptor_ICJI::getMethodInfo(
CORINFO_METHOD_HANDLE ftn,
CORINFO_METHOD_INFO* info)
CORINFO_METHOD_INFO* info,
CORINFO_CONTEXT_HANDLE context)
{
return original_ICorJitInfo->getMethodInfo(ftn, info);
return original_ICorJitInfo->getMethodInfo(ftn, info, context);
}

CorInfoInline interceptor_ICJI::canInline(
Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/tools/superpmi/superpmi/icorjitinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,14 @@ void MyICJI::getMethodSig(CORINFO_METHOD_HANDLE ftn, /* IN */
// return information about a method private to the implementation
// returns false if method is not IL, or is otherwise unavailable.
// This method is used to fetch data needed to inline functions
bool MyICJI::getMethodInfo(CORINFO_METHOD_HANDLE ftn, /* IN */
CORINFO_METHOD_INFO* info /* OUT */
bool MyICJI::getMethodInfo(CORINFO_METHOD_HANDLE ftn, /* IN */
CORINFO_METHOD_INFO* info, /* OUT */
CORINFO_CONTEXT_HANDLE context /* IN */
)
{
jitInstance->mc->cr->AddCall("getMethodInfo");
DWORD exceptionCode = 0;
bool value = jitInstance->mc->repGetMethodInfo(ftn, info, &exceptionCode);
bool value = jitInstance->mc->repGetMethodInfo(ftn, info, context, &exceptionCode);
if (exceptionCode != 0)
ThrowException(exceptionCode);
return value;
Expand Down
Loading

0 comments on commit 19887f7

Please sign in to comment.