Skip to content

Commit

Permalink
JIT: import static readonly fields holding frozen objects as const ha…
Browse files Browse the repository at this point in the history
…ndles (#76112)

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Co-authored-by: Jakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
  • Loading branch information
3 people authored Oct 10, 2022
1 parent ef6bc67 commit 12caa8f
Show file tree
Hide file tree
Showing 27 changed files with 873 additions and 353 deletions.
47 changes: 47 additions & 0 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,32 @@ class ICorStaticInfo
CORINFO_CLASS_HANDLE cls
) = 0;

//------------------------------------------------------------------------------
// isObjectImmutable: checks whether given object is known to be immutable or not
//
// Arguments:
// objPtr - Direct object handle
//
// Return Value:
// Returns true if object is known to be immutable
//
virtual bool isObjectImmutable(
void* objPtr
) = 0;

//------------------------------------------------------------------------------
// getObjectType: obtains type handle for given object
//
// Arguments:
// objPtr - Direct object handle
//
// Return Value:
// Returns CORINFO_CLASS_HANDLE handle that represents given object's type
//
virtual CORINFO_CLASS_HANDLE getObjectType(
void* objPtr
) = 0;

virtual bool getReadyToRunHelper(
CORINFO_RESOLVED_TOKEN * pResolvedToken,
CORINFO_LOOKUP_KIND * pGenericLookupKind,
Expand Down Expand Up @@ -3167,6 +3193,27 @@ class ICorDynamicInfo : public ICorStaticInfo
void **ppIndirection = NULL
) = 0;

//------------------------------------------------------------------------------
// getReadonlyStaticFieldValue: returns true and the actual field's value if the given
// field represents a statically initialized readonly field of any type, it might be:
// * integer/floating point primitive
// * null
// * frozen object reference (string, array or object)
//
// Arguments:
// field - field handle
// buffer - buffer field's value will be stored to
// bufferSize - size of buffer
//
// Return Value:
// Returns true if field's constant value was available and successfully copied to buffer
//
virtual bool getReadonlyStaticFieldValue(
CORINFO_FIELD_HANDLE field,
uint8_t *buffer,
int bufferSize
) = 0;

// If pIsSpeculative is NULL, return the class handle for the value of ref-class typed
// static readonly fields, if there is a unique location for the static and the class
// is already initialized.
Expand Down
11 changes: 11 additions & 0 deletions src/coreclr/inc/icorjitinfoimpl_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ CorInfoHelpFunc getUnBoxHelper(
void* getRuntimeTypePointer(
CORINFO_CLASS_HANDLE cls) override;

bool isObjectImmutable(
void* objPtr) override;

CORINFO_CLASS_HANDLE getObjectType(
void* objPtr) override;

bool getReadyToRunHelper(
CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_LOOKUP_KIND* pGenericLookupKind,
Expand Down Expand Up @@ -606,6 +612,11 @@ void* getFieldAddress(
CORINFO_FIELD_HANDLE field,
void** ppIndirection) override;

bool getReadonlyStaticFieldValue(
CORINFO_FIELD_HANDLE field,
uint8_t* buffer,
int bufferSize) override;

CORINFO_CLASS_HANDLE getStaticFieldCurrentClass(
CORINFO_FIELD_HANDLE field,
bool* pIsSpeculative) override;
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 = { /* 3f5e4630-b29a-4aeb-bab7-07bdff43a156 */
0x3f5e4630,
0xb29a,
0x4aeb,
{0xba, 0xb7, 0x7, 0xbd, 0xff, 0x43, 0xa1, 0x56}
constexpr GUID JITEEVersionIdentifier = { /* 982ed1fd-7bf3-425b-8b8a-902873151e79 */
0x982ed1fd,
0x7bf3,
0x425b,
{0x8b, 0x8a, 0x90, 0x28, 0x73, 0x15, 0x1e, 0x79}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/jit/ICorJitInfo_API_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ DEF_CLR_API(getTypeForBox)
DEF_CLR_API(getBoxHelper)
DEF_CLR_API(getUnBoxHelper)
DEF_CLR_API(getRuntimeTypePointer)
DEF_CLR_API(isObjectImmutable)
DEF_CLR_API(getObjectType)
DEF_CLR_API(getReadyToRunHelper)
DEF_CLR_API(getReadyToRunDelegateCtorHelper)
DEF_CLR_API(getHelperName)
Expand Down Expand Up @@ -152,6 +154,7 @@ DEF_CLR_API(canAccessFamily)
DEF_CLR_API(isRIDClassDomainID)
DEF_CLR_API(getClassDomainID)
DEF_CLR_API(getFieldAddress)
DEF_CLR_API(getReadonlyStaticFieldValue)
DEF_CLR_API(getStaticFieldCurrentClass)
DEF_CLR_API(getVarArgsHandle)
DEF_CLR_API(canGetVarArgsHandle)
Expand Down
29 changes: 29 additions & 0 deletions src/coreclr/jit/ICorJitInfo_API_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,24 @@ void* WrapICorJitInfo::getRuntimeTypePointer(
return temp;
}

bool WrapICorJitInfo::isObjectImmutable(
void* objPtr)
{
API_ENTER(isObjectImmutable);
bool temp = wrapHnd->isObjectImmutable(objPtr);
API_LEAVE(isObjectImmutable);
return temp;
}

CORINFO_CLASS_HANDLE WrapICorJitInfo::getObjectType(
void* objPtr)
{
API_ENTER(getObjectType);
CORINFO_CLASS_HANDLE temp = wrapHnd->getObjectType(objPtr);
API_LEAVE(getObjectType);
return temp;
}

bool WrapICorJitInfo::getReadyToRunHelper(
CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_LOOKUP_KIND* pGenericLookupKind,
Expand Down Expand Up @@ -1451,6 +1469,17 @@ void* WrapICorJitInfo::getFieldAddress(
return temp;
}

bool WrapICorJitInfo::getReadonlyStaticFieldValue(
CORINFO_FIELD_HANDLE field,
uint8_t* buffer,
int bufferSize)
{
API_ENTER(getReadonlyStaticFieldValue);
bool temp = wrapHnd->getReadonlyStaticFieldValue(field, buffer, bufferSize);
API_LEAVE(getReadonlyStaticFieldValue);
return temp;
}

CORINFO_CLASS_HANDLE WrapICorJitInfo::getStaticFieldCurrentClass(
CORINFO_FIELD_HANDLE field,
bool* pIsSpeculative)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3631,7 +3631,7 @@ class Compiler

GenTree* impInitClass(CORINFO_RESOLVED_TOKEN* pResolvedToken);

GenTree* impImportStaticReadOnlyField(void* fldAddr, var_types lclTyp);
GenTree* impImportStaticReadOnlyField(uint8_t* buffer, int bufferSize, var_types valueType);

GenTree* impImportStaticFieldAccess(CORINFO_RESOLVED_TOKEN* pResolvedToken,
CORINFO_ACCESS_FLAGS access,
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/gcinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ GCInfo::WriteBarrierForm GCInfo::gcIsWriteBarrierCandidate(GenTreeStoreInd* stor
return WBF_NoBarrier;
}

// Write-barriers are no-op for frozen objects (as values)
if (store->Data()->IsIconHandle(GTF_ICON_OBJ_HDL))
{
// Ignore frozen objects
Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17502,6 +17502,20 @@ CORINFO_CLASS_HANDLE Compiler::gtGetClassHandle(GenTree* tree, bool* pIsExact, b
break;
}

case GT_CNS_INT:
{
if (tree->IsIconHandle(GTF_ICON_OBJ_HDL))
{
objClass = info.compCompHnd->getObjectType((void*)tree->AsIntCon()->IconValue());
if (objClass != NO_CLASS_HANDLE)
{
// if we managed to get a class handle it's definitely not null
*pIsNonNull = true;
}
}
break;
}

case GT_RET_EXPR:
{
// If we see a RET_EXPR, recurse through to examine the
Expand Down
Loading

0 comments on commit 12caa8f

Please sign in to comment.