Skip to content

Commit 12caa8f

Browse files
EgorBojkotasjakobbotsch
authored
JIT: import static readonly fields holding frozen objects as const handles (#76112)
Co-authored-by: Jan Kotas <jkotas@microsoft.com> Co-authored-by: Jakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
1 parent ef6bc67 commit 12caa8f

File tree

27 files changed

+873
-353
lines changed

27 files changed

+873
-353
lines changed

src/coreclr/inc/corinfo.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2503,6 +2503,32 @@ class ICorStaticInfo
25032503
CORINFO_CLASS_HANDLE cls
25042504
) = 0;
25052505

2506+
//------------------------------------------------------------------------------
2507+
// isObjectImmutable: checks whether given object is known to be immutable or not
2508+
//
2509+
// Arguments:
2510+
// objPtr - Direct object handle
2511+
//
2512+
// Return Value:
2513+
// Returns true if object is known to be immutable
2514+
//
2515+
virtual bool isObjectImmutable(
2516+
void* objPtr
2517+
) = 0;
2518+
2519+
//------------------------------------------------------------------------------
2520+
// getObjectType: obtains type handle for given object
2521+
//
2522+
// Arguments:
2523+
// objPtr - Direct object handle
2524+
//
2525+
// Return Value:
2526+
// Returns CORINFO_CLASS_HANDLE handle that represents given object's type
2527+
//
2528+
virtual CORINFO_CLASS_HANDLE getObjectType(
2529+
void* objPtr
2530+
) = 0;
2531+
25062532
virtual bool getReadyToRunHelper(
25072533
CORINFO_RESOLVED_TOKEN * pResolvedToken,
25082534
CORINFO_LOOKUP_KIND * pGenericLookupKind,
@@ -3167,6 +3193,27 @@ class ICorDynamicInfo : public ICorStaticInfo
31673193
void **ppIndirection = NULL
31683194
) = 0;
31693195

3196+
//------------------------------------------------------------------------------
3197+
// getReadonlyStaticFieldValue: returns true and the actual field's value if the given
3198+
// field represents a statically initialized readonly field of any type, it might be:
3199+
// * integer/floating point primitive
3200+
// * null
3201+
// * frozen object reference (string, array or object)
3202+
//
3203+
// Arguments:
3204+
// field - field handle
3205+
// buffer - buffer field's value will be stored to
3206+
// bufferSize - size of buffer
3207+
//
3208+
// Return Value:
3209+
// Returns true if field's constant value was available and successfully copied to buffer
3210+
//
3211+
virtual bool getReadonlyStaticFieldValue(
3212+
CORINFO_FIELD_HANDLE field,
3213+
uint8_t *buffer,
3214+
int bufferSize
3215+
) = 0;
3216+
31703217
// If pIsSpeculative is NULL, return the class handle for the value of ref-class typed
31713218
// static readonly fields, if there is a unique location for the static and the class
31723219
// is already initialized.

src/coreclr/inc/icorjitinfoimpl_generated.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,12 @@ CorInfoHelpFunc getUnBoxHelper(
289289
void* getRuntimeTypePointer(
290290
CORINFO_CLASS_HANDLE cls) override;
291291

292+
bool isObjectImmutable(
293+
void* objPtr) override;
294+
295+
CORINFO_CLASS_HANDLE getObjectType(
296+
void* objPtr) override;
297+
292298
bool getReadyToRunHelper(
293299
CORINFO_RESOLVED_TOKEN* pResolvedToken,
294300
CORINFO_LOOKUP_KIND* pGenericLookupKind,
@@ -606,6 +612,11 @@ void* getFieldAddress(
606612
CORINFO_FIELD_HANDLE field,
607613
void** ppIndirection) override;
608614

615+
bool getReadonlyStaticFieldValue(
616+
CORINFO_FIELD_HANDLE field,
617+
uint8_t* buffer,
618+
int bufferSize) override;
619+
609620
CORINFO_CLASS_HANDLE getStaticFieldCurrentClass(
610621
CORINFO_FIELD_HANDLE field,
611622
bool* pIsSpeculative) override;

src/coreclr/inc/jiteeversionguid.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ typedef const GUID *LPCGUID;
4343
#define GUID_DEFINED
4444
#endif // !GUID_DEFINED
4545

46-
constexpr GUID JITEEVersionIdentifier = { /* 3f5e4630-b29a-4aeb-bab7-07bdff43a156 */
47-
0x3f5e4630,
48-
0xb29a,
49-
0x4aeb,
50-
{0xba, 0xb7, 0x7, 0xbd, 0xff, 0x43, 0xa1, 0x56}
46+
constexpr GUID JITEEVersionIdentifier = { /* 982ed1fd-7bf3-425b-8b8a-902873151e79 */
47+
0x982ed1fd,
48+
0x7bf3,
49+
0x425b,
50+
{0x8b, 0x8a, 0x90, 0x28, 0x73, 0x15, 0x1e, 0x79}
5151
};
5252

5353
//////////////////////////////////////////////////////////////////////////////////////////////////////////

src/coreclr/jit/ICorJitInfo_API_names.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ DEF_CLR_API(getTypeForBox)
7272
DEF_CLR_API(getBoxHelper)
7373
DEF_CLR_API(getUnBoxHelper)
7474
DEF_CLR_API(getRuntimeTypePointer)
75+
DEF_CLR_API(isObjectImmutable)
76+
DEF_CLR_API(getObjectType)
7577
DEF_CLR_API(getReadyToRunHelper)
7678
DEF_CLR_API(getReadyToRunDelegateCtorHelper)
7779
DEF_CLR_API(getHelperName)
@@ -152,6 +154,7 @@ DEF_CLR_API(canAccessFamily)
152154
DEF_CLR_API(isRIDClassDomainID)
153155
DEF_CLR_API(getClassDomainID)
154156
DEF_CLR_API(getFieldAddress)
157+
DEF_CLR_API(getReadonlyStaticFieldValue)
155158
DEF_CLR_API(getStaticFieldCurrentClass)
156159
DEF_CLR_API(getVarArgsHandle)
157160
DEF_CLR_API(canGetVarArgsHandle)

src/coreclr/jit/ICorJitInfo_API_wrapper.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,24 @@ void* WrapICorJitInfo::getRuntimeTypePointer(
673673
return temp;
674674
}
675675

676+
bool WrapICorJitInfo::isObjectImmutable(
677+
void* objPtr)
678+
{
679+
API_ENTER(isObjectImmutable);
680+
bool temp = wrapHnd->isObjectImmutable(objPtr);
681+
API_LEAVE(isObjectImmutable);
682+
return temp;
683+
}
684+
685+
CORINFO_CLASS_HANDLE WrapICorJitInfo::getObjectType(
686+
void* objPtr)
687+
{
688+
API_ENTER(getObjectType);
689+
CORINFO_CLASS_HANDLE temp = wrapHnd->getObjectType(objPtr);
690+
API_LEAVE(getObjectType);
691+
return temp;
692+
}
693+
676694
bool WrapICorJitInfo::getReadyToRunHelper(
677695
CORINFO_RESOLVED_TOKEN* pResolvedToken,
678696
CORINFO_LOOKUP_KIND* pGenericLookupKind,
@@ -1451,6 +1469,17 @@ void* WrapICorJitInfo::getFieldAddress(
14511469
return temp;
14521470
}
14531471

1472+
bool WrapICorJitInfo::getReadonlyStaticFieldValue(
1473+
CORINFO_FIELD_HANDLE field,
1474+
uint8_t* buffer,
1475+
int bufferSize)
1476+
{
1477+
API_ENTER(getReadonlyStaticFieldValue);
1478+
bool temp = wrapHnd->getReadonlyStaticFieldValue(field, buffer, bufferSize);
1479+
API_LEAVE(getReadonlyStaticFieldValue);
1480+
return temp;
1481+
}
1482+
14541483
CORINFO_CLASS_HANDLE WrapICorJitInfo::getStaticFieldCurrentClass(
14551484
CORINFO_FIELD_HANDLE field,
14561485
bool* pIsSpeculative)

src/coreclr/jit/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3631,7 +3631,7 @@ class Compiler
36313631

36323632
GenTree* impInitClass(CORINFO_RESOLVED_TOKEN* pResolvedToken);
36333633

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

36363636
GenTree* impImportStaticFieldAccess(CORINFO_RESOLVED_TOKEN* pResolvedToken,
36373637
CORINFO_ACCESS_FLAGS access,

src/coreclr/jit/gcinfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ GCInfo::WriteBarrierForm GCInfo::gcIsWriteBarrierCandidate(GenTreeStoreInd* stor
252252
return WBF_NoBarrier;
253253
}
254254

255+
// Write-barriers are no-op for frozen objects (as values)
255256
if (store->Data()->IsIconHandle(GTF_ICON_OBJ_HDL))
256257
{
257258
// Ignore frozen objects

src/coreclr/jit/gentree.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17502,6 +17502,20 @@ CORINFO_CLASS_HANDLE Compiler::gtGetClassHandle(GenTree* tree, bool* pIsExact, b
1750217502
break;
1750317503
}
1750417504

17505+
case GT_CNS_INT:
17506+
{
17507+
if (tree->IsIconHandle(GTF_ICON_OBJ_HDL))
17508+
{
17509+
objClass = info.compCompHnd->getObjectType((void*)tree->AsIntCon()->IconValue());
17510+
if (objClass != NO_CLASS_HANDLE)
17511+
{
17512+
// if we managed to get a class handle it's definitely not null
17513+
*pIsNonNull = true;
17514+
}
17515+
}
17516+
break;
17517+
}
17518+
1750517519
case GT_RET_EXPR:
1750617520
{
1750717521
// If we see a RET_EXPR, recurse through to examine the

0 commit comments

Comments
 (0)