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: Fold string fields in structs stored in static readonly fields #80431

Merged
merged 23 commits into from Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/coreclr/jit/valuenum.cpp
Expand Up @@ -5126,6 +5126,43 @@ void Compiler::fgValueNumberFieldLoad(GenTree* loadTree, GenTree* baseAddr, Fiel
{
noway_assert(fieldSeq != nullptr);

// Check if the load represents a frozen gc object located in a struct which is stored in a static readonly field,
// e.g.:
//
// struct MyStruct { string Name; }
// static readonly MyStruct MyStr = new() { Name = "Hey!" };
//
// string GetName() => MyStr.Name; // <- loadTree
//
if ((baseAddr == nullptr) && loadTree->TypeIs(TYP_REF) &&
// it should be a static field
(fieldSeq->GetKind() == FieldSeq::FieldKind::SimpleStatic) &&
// boxed static to be precise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fragile condition. We are assuming that the runtime uses boxed statics for this pattern. Is there a way to write this in a more general way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void Compiler::fgValueNumberFieldLoad(GenTree* loadTree, GenTree* baseAddr, FieldSeq* fieldSeq, ssize_t offset)
{
noway_assert(fieldSeq != nullptr);
// Check if the load represents a frozen gc object located in a struct which is stored in a static readonly field,
// e.g.:
//
// struct MyStruct { string Name; }
// static readonly MyStruct MyStr = new() { Name = "Hey!" };
//
// string GetName() => MyStr.Name; // <- loadTree
//
if ((baseAddr == nullptr) && loadTree->TypeIs(TYP_REF) &&
// it should be a static field
(fieldSeq->GetKind() == FieldSeq::FieldKind::SimpleStatic) &&
// boxed static to be precise
(fieldSeq->GetOffset() == TARGET_POINTER_SIZE))
{
uint8_t buffer[TARGET_POINTER_SIZE] = {0};
if (((UINT)offset < INT_MAX) &&
info.compCompHnd->getReadonlyStaticFieldValue(fieldSeq->GetFieldHandle(), buffer, TARGET_POINTER_SIZE,

we accept loadTree that looks like this:
image

and the fieldseq that represents that load. So technically I don't need to check that tree at all. @SingleAccretion @jakobbotsch could you please validate JIT part of this PR?

(fieldSeq->GetOffset() == TARGET_POINTER_SIZE))
{
uint8_t buffer[TARGET_POINTER_SIZE] = {0};
if (((UINT)offset < INT_MAX) &&
info.compCompHnd->getReadonlyStaticFieldValue(fieldSeq->GetFieldHandle(), buffer, TARGET_POINTER_SIZE,
(int)offset))
{
// In case of 64bit jit emitting 32bit codegen this handle will be 64bit
// value holding 32bit handle with upper half zeroed (hence, "= NULL").
// It's done to match the current crossgen/ILC behavior.
ssize_t objHandle = 0;
memcpy(&objHandle, buffer, TARGET_POINTER_SIZE);
if (objHandle == 0)
{
loadTree->gtVNPair.SetBoth(vnStore->VNForNull());
}
else
{
loadTree->gtVNPair.SetBoth(vnStore->VNForHandle(objHandle, GTF_ICON_OBJ_HDL));
setMethodHasFrozenObjects();
}
return;
}
}

// Two cases:
//
// 1) Instance field / "complex" static: heap[field][baseAddr][offset + load size].
Expand Down
47 changes: 45 additions & 2 deletions src/coreclr/vm/jitinterface.cpp
Expand Up @@ -11723,8 +11723,51 @@ bool CEEInfo::getReadonlyStaticFieldValue(CORINFO_FIELD_HANDLE fieldHnd, uint8_t

if (size >= (UINT)bufferSize && valueOffset >= 0 && (UINT)valueOffset <= size - (UINT)bufferSize)
{
memcpy(buffer, (uint8_t*)baseAddr + valueOffset, bufferSize);
result = true;
// For structs containing GC pointers we want to make sure those GC pointers belong to FOH
// so we expect valueOffset to be a real field offset (same for bufferSize)
if (!field->IsRVA() && field->GetFieldType() == ELEMENT_TYPE_VALUETYPE)
{
PTR_MethodTable structType = field->GetFieldTypeHandleThrowing().GetMethodTable();
if (structType->ContainsPointers())
{
for (WORD i = 0; i < structType->GetNumInstanceFields(); i++)
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
FieldDesc* subField = (FieldDesc*)((structType->GetApproxFieldDescListRaw()) + i);
// TODO: If subField is also a struct we might want to inspect its fields too
if (subField->GetOffset() == (DWORD)valueOffset && subField->GetSize() == (UINT)bufferSize && subField->IsObjRef() &&
subField->GetFieldType() != ELEMENT_TYPE_VALUETYPE)
{
GCX_COOP();
Object* subFieldValue = nullptr;
memcpy(&subFieldValue, (uint8_t*)baseAddr + valueOffset, bufferSize);
if (subFieldValue == nullptr || GCHeapUtilities::GetGCHeap()->IsInFrozenSegment(subFieldValue))
{
// GC handle from FOH or null
memcpy(buffer, (uint8_t*)baseAddr + valueOffset, bufferSize);
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
result = true;
}
break;
}

if (subField->GetOffset() >= (DWORD)valueOffset)
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
break;
}
}
}
else
{
// No gc pointers in the struct
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
memcpy(buffer, (uint8_t*)baseAddr + valueOffset, bufferSize);
result = true;
}
}
else
{
// Primitive or RVA
memcpy(buffer, (uint8_t*)baseAddr + valueOffset, bufferSize);
result = true;
}
}
}
}
Expand Down