Skip to content

Commit

Permalink
CodeVersionManager updates (dotnet#102298)
Browse files Browse the repository at this point in the history
* Code Versioning - move native code versioning state

Remove the dictionary from the CodeVersionManager
and instead store the data directly on the MethodDesc.

* Remove dictionary for IL version states.

Remove the dictionary from the CodeVersionManager
and instead store the data in the Module.
  • Loading branch information
AaronRobinsonMSFT authored May 22, 2024
1 parent 4a99b64 commit b8f554d
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 166 deletions.
12 changes: 6 additions & 6 deletions src/coreclr/debug/ee/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12313,13 +12313,14 @@ HRESULT Debugger::DeoptimizeMethodHelper(Module* pModule, mdMethodDef methodDef)
ILCodeVersion ilCodeVersion;
CodeVersionManager *pCodeVersionManager = pModule->GetCodeVersionManager();

LOG((LF_TIEREDCOMPILATION, LL_INFO100, "Debugger::DeoptimizeMethodHelper Module=%p Method=0x%08x\n",
pModule, methodDef));

{
CodeVersionManager::LockHolder codeVersioningLockHolder;
if (FAILED(hr = pCodeVersionManager->AddILCodeVersion(pModule, methodDef, &ilCodeVersion, TRUE)))
{
LOG((LF_TIEREDCOMPILATION, LL_INFO100, "TieredCompilationManager::DeOptimizeMethodHelper Module=0x%x Method=0x%x, AddILCodeVersion returned hr 0x%x\n",
pModule, methodDef,
hr));
LOG((LF_TIEREDCOMPILATION, LL_INFO100, "Debugger::DeoptimizeMethodHelper AddILCodeVersion returned hr 0x%x\n", hr));
return hr;
}

Expand All @@ -12335,8 +12336,7 @@ HRESULT Debugger::DeoptimizeMethodHelper(Module* pModule, mdMethodDef methodDef)
{
if (FAILED(hr = pCodeVersionManager->SetActiveILCodeVersions(&ilCodeVersion, 1, NULL)))
{
LOG((LF_TIEREDCOMPILATION, LL_INFO100, "TieredCompilationManager::DeOptimizeMethodHelper Module=0x%x Method=0x%x, SetActiveILCodeVersions returned hr 0x%x\n",
pModule, methodDef,
LOG((LF_TIEREDCOMPILATION, LL_INFO100, "Debugger::DeoptimizeMethodHelper SetActiveILCodeVersions returned hr 0x%x\n",
hr));
return hr;
}
Expand All @@ -12358,7 +12358,7 @@ HRESULT Debugger::DeoptimizeMethod(Module* pModule, mdMethodDef methodDef)
HRESULT hr = DeoptimizeMethodHelper(pModule, methodDef);
if (FAILED(hr))
{
LOG((LF_TIEREDCOMPILATION, LL_INFO100, "TieredCompilationManager::DeOptimizeMethod Module=0x%x Method=0x%x,, initial ReJIT returned hr 0x%x, aborting\n",
LOG((LF_TIEREDCOMPILATION, LL_INFO100, "Debugger::DeoptimizeMethod Module=%p Method=0x%08x, initial ReJIT returned hr 0x%x, aborting\n",
pModule, methodDef, hr));
return hr;
}
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/inc/dacvars.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ DEFINE_DACVAR(BOOL, dac__g_isNewExceptionHandlingEnabled, ::g_isNewExceptionHand
DEFINE_DACVAR(PTR_SString, SString__s_Empty, SString::s_Empty)

DEFINE_DACVAR(INT32, ArrayBase__s_arrayBoundsZero, ArrayBase::s_arrayBoundsZero)
DEFINE_DACVAR(BOOL, CodeVersionManager__s_HasNonDefaultILVersions, CodeVersionManager::s_HasNonDefaultILVersions)

DEFINE_DACVAR(PTR_JITNotification, dac__g_pNotificationTable, ::g_pNotificationTable)
DEFINE_DACVAR(ULONG32, dac__g_dacNotificationFlags, ::g_dacNotificationFlags)
Expand Down
8 changes: 0 additions & 8 deletions src/coreclr/vm/amd64/asmconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ ASMCONSTANTS_C_ASSERT(SIZEOF__ComPrestubMethodFrame
ASMCONSTANTS_C_ASSERT(SIZEOF__ComMethodFrame
== sizeof(ComMethodFrame));

#define OFFSETOF__ComPlusCallMethodDesc__m_pComPlusCallInfo DBG_FRE(0x30, 0x08)
ASMCONSTANTS_C_ASSERT(OFFSETOF__ComPlusCallMethodDesc__m_pComPlusCallInfo
== offsetof(ComPlusCallMethodDesc, m_pComPlusCallInfo));

#define OFFSETOF__ComPlusCallInfo__m_pILStub 0x0
ASMCONSTANTS_C_ASSERT(OFFSETOF__ComPlusCallInfo__m_pILStub
== offsetof(ComPlusCallInfo, m_pILStub));

#endif // FEATURE_COMINTEROP

#define OFFSETOF__Thread__m_fPreemptiveGCDisabled 0x04
Expand Down
19 changes: 14 additions & 5 deletions src/coreclr/vm/ceeload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,7 @@ void Module::AllocateMaps()
m_TypeRefToMethodTableMap.dwCount = TYPEREF_MAP_INITIAL_SIZE;
m_MemberRefMap.dwCount = MEMBERREF_MAP_INITIAL_SIZE;
m_MethodDefToDescMap.dwCount = MEMBERDEF_MAP_INITIAL_SIZE;
m_ILCodeVersioningStateMap.dwCount = MEMBERDEF_MAP_INITIAL_SIZE;
m_FieldDefToDescMap.dwCount = MEMBERDEF_MAP_INITIAL_SIZE;
m_GenericParamToDescMap.dwCount = GENERICPARAM_MAP_INITIAL_SIZE;
m_ManifestModuleReferencesMap.dwCount = ASSEMBLYREFERENCES_MAP_INITIAL_SIZE;
Expand All @@ -1827,6 +1828,9 @@ void Module::AllocateMaps()
// Get # MethodDefs
m_MethodDefToDescMap.dwCount = pImport->GetCountWithTokenKind(mdtMethodDef)+1;

// IL code versions are relatively rare so keep small.
m_ILCodeVersioningStateMap.dwCount = 1;

// Get # FieldDefs
m_FieldDefToDescMap.dwCount = pImport->GetCountWithTokenKind(mdtFieldDef)+1;

Expand All @@ -1843,6 +1847,7 @@ void Module::AllocateMaps()
nTotal += m_TypeRefToMethodTableMap.dwCount;
nTotal += m_MemberRefMap.dwCount;
nTotal += m_MethodDefToDescMap.dwCount;
nTotal += m_ILCodeVersioningStateMap.dwCount;
nTotal += m_FieldDefToDescMap.dwCount;
nTotal += m_GenericParamToDescMap.dwCount;
nTotal += m_ManifestModuleReferencesMap.dwCount;
Expand All @@ -1869,9 +1874,13 @@ void Module::AllocateMaps()
m_MethodDefToDescMap.supportedFlags = METHOD_DEF_MAP_ALL_FLAGS;
m_MethodDefToDescMap.pTable = &m_MemberRefMap.pTable[m_MemberRefMap.dwCount];

m_ILCodeVersioningStateMap.pNext = NULL;
m_ILCodeVersioningStateMap.supportedFlags = METHOD_DEF_MAP_ALL_FLAGS;
m_ILCodeVersioningStateMap.pTable = &m_MethodDefToDescMap.pTable[m_MethodDefToDescMap.dwCount];

m_FieldDefToDescMap.pNext = NULL;
m_FieldDefToDescMap.supportedFlags = FIELD_DEF_MAP_ALL_FLAGS;
m_FieldDefToDescMap.pTable = &m_MethodDefToDescMap.pTable[m_MethodDefToDescMap.dwCount];
m_FieldDefToDescMap.pTable = &m_ILCodeVersioningStateMap.pTable[m_ILCodeVersioningStateMap.dwCount];

m_GenericParamToDescMap.pNext = NULL;
m_GenericParamToDescMap.supportedFlags = GENERIC_PARAM_MAP_ALL_FLAGS;
Expand Down Expand Up @@ -4807,7 +4816,7 @@ VASigCookie *Module::GetVASigCookieWorker(Module* pDefiningModule, Module* pLoad
INJECT_FAULT(COMPlusThrowOM());
}
CONTRACT_END;

VASigCookieBlock *pBlock;
VASigCookie *pCookie;

Expand Down Expand Up @@ -4855,7 +4864,7 @@ VASigCookie *Module::GetVASigCookieWorker(Module* pDefiningModule, Module* pLoad
}
}
}

if (!pCookie)
{
// If not, time to make a new one.
Expand Down Expand Up @@ -4911,7 +4920,7 @@ VASigCookie *Module::GetVASigCookieWorker(Module* pDefiningModule, Module* pLoad
pCookie->pLoaderModule = pLoaderModule;

AllocMemTracker amt;

if (classInstCount != 0)
{
TypeHandle* pClassInst = (TypeHandle*)(void*)amt.Track(pLoaderAllocator->GetHighFrequencyHeap()->AllocMem(S_SIZE_T(classInstCount) * S_SIZE_T(sizeof(TypeHandle))));
Expand All @@ -4931,7 +4940,7 @@ VASigCookie *Module::GetVASigCookieWorker(Module* pDefiningModule, Module* pLoad
}
pCookie->methodInst = Instantiation(pMethodInst, methodInstCount);
}

amt.SuppressRelease();

// Finally, now that it's safe for asynchronous readers to see it,
Expand Down
30 changes: 26 additions & 4 deletions src/coreclr/vm/ceeload.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#endif

#include "ilinstrumentation.h"
#include "codeversion.h"

class Stub;
class MethodDesc;
Expand All @@ -63,7 +64,6 @@ class SString;
class Pending;
class MethodTable;
class DynamicMethodTable;
class CodeVersionManager;
class TieredCompilationManager;
class JITInlineTrackingMap;

Expand Down Expand Up @@ -581,8 +581,7 @@ class ModuleBase

};

// A code:Module represents a DLL or EXE file loaded from the disk. It could either be a IL module or a
// Native code (NGEN module). A module live in a code:Assembly
// A code:Module represents a DLL or EXE file loaded from the disk. A module live in a code:Assembly
//
// Some important fields are
// * code:Module.m_pPEAssembly - this points at a code:PEAssembly that understands the layout of a PE assembly. The most
Expand Down Expand Up @@ -727,6 +726,10 @@ class Module : public ModuleBase
// For generic methods, IsGenericTypeDefinition() is true i.e. instantiation at formals
LookupMap<PTR_MethodDesc> m_MethodDefToDescMap;

// Linear mapping from MethodDef token to ILCodeVersioningState *
// This is used for Code Versioning logic
LookupMap<PTR_ILCodeVersioningState> m_ILCodeVersioningStateMap;

// Linear mapping from FieldDef token to FieldDesc*
LookupMap<PTR_FieldDesc> m_FieldDefToDescMap;

Expand Down Expand Up @@ -1215,7 +1218,7 @@ class Module : public ModuleBase
}
#endif // !DACCESS_COMPILE

MethodDesc *LookupMethodDef(mdMethodDef token);
PTR_MethodDesc LookupMethodDef(mdMethodDef token);

#ifndef DACCESS_COMPILE
void EnsureMethodDefCanBeStored(mdMethodDef token)
Expand All @@ -1233,6 +1236,25 @@ class Module : public ModuleBase
}
#endif // !DACCESS_COMPILE

PTR_ILCodeVersioningState LookupILCodeVersioningState(mdMethodDef token);

#ifndef DACCESS_COMPILE
void EnsureILCodeVersioningStateCanBeStored(mdMethodDef token)
{
WRAPPER_NO_CONTRACT; // THROWS/GC_NOTRIGGER/INJECT_FAULT()/MODE_ANY
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
m_ILCodeVersioningStateMap.EnsureElementCanBeStored(this, RidFromToken(token));
}

void EnsuredStoreILCodeVersioningState(mdMethodDef token, PTR_ILCodeVersioningState value)
{
WRAPPER_NO_CONTRACT; // NOTHROW/GC_NOTRIGGER/FORBID_FAULT/MODE_ANY
_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
_ASSERTE(TypeFromToken(token) == mdtMethodDef);
m_ILCodeVersioningStateMap.SetElement(RidFromToken(token), value);
}
#endif // !DACCESS_COMPILE

#ifndef DACCESS_COMPILE
FieldDesc *LookupFieldDef(mdFieldDef token)
{
Expand Down
18 changes: 17 additions & 1 deletion src/coreclr/vm/ceeload.inl
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ inline PTR_Assembly Module::GetAssembly() const
return m_pAssembly;
}

inline MethodDesc *Module::LookupMethodDef(mdMethodDef token)
inline PTR_MethodDesc Module::LookupMethodDef(mdMethodDef token)
{
CONTRACTL
{
Expand All @@ -274,6 +274,22 @@ inline MethodDesc *Module::LookupMethodDef(mdMethodDef token)
return m_MethodDefToDescMap.GetElement(RidFromToken(token));
}

inline PTR_ILCodeVersioningState Module::LookupILCodeVersioningState(mdMethodDef token)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
SUPPORTS_DAC;
}
CONTRACTL_END

_ASSERTE(CodeVersionManager::IsLockOwnedByCurrentThread());
_ASSERTE(TypeFromToken(token) == mdtMethodDef);
return m_ILCodeVersioningStateMap.GetElement(RidFromToken(token));
}

inline MethodDesc *Module::LookupMemberRefAsMethod(mdMemberRef token)
{
LIMITED_METHOD_DAC_CONTRACT;
Expand Down
Loading

0 comments on commit b8f554d

Please sign in to comment.