Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: Pass field's owner to getFieldType #96794

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3616,7 +3616,7 @@ class Compiler
// Get a class handle from a helper call argument
CORINFO_CLASS_HANDLE gtGetHelperArgClassHandle(GenTree* array);
// Get the class handle for a field
CORINFO_CLASS_HANDLE gtGetFieldClassHandle(CORINFO_FIELD_HANDLE fieldHnd, bool* pIsExact, bool* pIsNonNull);
CORINFO_CLASS_HANDLE gtGetFieldClassHandle(CORINFO_FIELD_HANDLE fieldHnd, CORINFO_CLASS_HANDLE fieldOwnerCls, bool* pIsExact, bool* pIsNonNull);
// Check if this tree is a typeof()
bool gtIsTypeof(GenTree* tree, CORINFO_CLASS_HANDLE* handle = nullptr);

Expand Down
23 changes: 19 additions & 4 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18542,12 +18542,23 @@ CORINFO_CLASS_HANDLE Compiler::gtGetClassHandle(GenTree* tree, bool* pIsExact, b
if ((fldSeq != nullptr) && (fldSeq->GetOffset() == base->AsIntCon()->IconValue()))
{
CORINFO_FIELD_HANDLE fldHandle = base->AsIntCon()->gtFieldSeq->GetFieldHandle();
objClass = gtGetFieldClassHandle(fldHandle, pIsExact, pIsNonNull);
objClass = gtGetFieldClassHandle(fldHandle, NO_CLASS_HANDLE, pIsExact, pIsNonNull);
}
}
else if (base->OperIs(GT_FIELD_ADDR))
{
objClass = gtGetFieldClassHandle(base->AsFieldAddr()->gtFldHnd, pIsExact, pIsNonNull);
CORINFO_FIELD_HANDLE fldHandle = base->AsFieldAddr()->gtFldHnd;
CORINFO_CLASS_HANDLE fldOwnerCls = NO_CLASS_HANDLE;

if (base->gtGetOp1() != nullptr)
{
// If it's an instance field, try to grab the class handle from the instance.
// we're going to use it as a hint for getFieldType
bool isExactObj = false;
bool isNonNullObj = false;
fldOwnerCls = gtGetClassHandle(base->gtGetOp1(), &isExactObj, &isNonNullObj);
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
}
objClass = gtGetFieldClassHandle(fldHandle, fldOwnerCls, pIsExact, pIsNonNull);
}
break;
}
Expand Down Expand Up @@ -18778,6 +18789,7 @@ CORINFO_CLASS_HANDLE Compiler::gtGetArrayElementClassHandle(GenTree* array)
//
// Arguments:
// fieldHnd - field handle for field in question
// fieldOwnerCls - field's owner class handle, if known.
// pIsExact - [OUT] true if type is known exactly
// pIsNonNull - [OUT] true if field value is not null
//
Expand All @@ -18787,10 +18799,13 @@ CORINFO_CLASS_HANDLE Compiler::gtGetArrayElementClassHandle(GenTree* array)
//
// May examine runtime state of static field instances.

CORINFO_CLASS_HANDLE Compiler::gtGetFieldClassHandle(CORINFO_FIELD_HANDLE fieldHnd, bool* pIsExact, bool* pIsNonNull)
CORINFO_CLASS_HANDLE Compiler::gtGetFieldClassHandle(CORINFO_FIELD_HANDLE fieldHnd,
CORINFO_CLASS_HANDLE fieldOwnerCls,
bool* pIsExact,
bool* pIsNonNull)
{
CORINFO_CLASS_HANDLE fieldClass = nullptr;
CorInfoType fieldCorType = info.compCompHnd->getFieldType(fieldHnd, &fieldClass);
CorInfoType fieldCorType = info.compCompHnd->getFieldType(fieldHnd, &fieldClass, fieldOwnerCls);

if (fieldCorType == CORINFO_TYPE_CLASS)
{
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9150,6 +9150,12 @@ CorInfoType CEEInfo::getFieldType (CORINFO_FIELD_HANDLE fieldHnd,

JIT_TO_EE_TRANSITION();

PTR_MethodTable fldType = ((FieldDesc*)fieldHnd)->GetApproxEnclosingMethodTable();
if ((owner != nullptr) && !TypeHandle(owner).AsMethodTable()->HasSameTypeDefAs(fldType))
{
owner = nullptr;
}

result = getFieldTypeInternal(fieldHnd, pTypeHnd, owner);

EE_TO_JIT_TRANSITION();
Expand Down