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: Optimize out write barriers for fields in ref-like structs #103503

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8166,6 +8166,7 @@ class Compiler
// Get the flags

bool eeIsValueClass(CORINFO_CLASS_HANDLE clsHnd);
bool eeIsByrefLike(CORINFO_CLASS_HANDLE clsHnd);
bool eeIsIntrinsic(CORINFO_METHOD_HANDLE ftn);
bool eeIsFieldStatic(CORINFO_FIELD_HANDLE fldHnd);

Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/jit/ee_il_dll.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ bool Compiler::eeIsValueClass(CORINFO_CLASS_HANDLE clsHnd)
return info.compCompHnd->isValueClass(clsHnd);
}

FORCEINLINE
bool Compiler::eeIsByrefLike(CORINFO_CLASS_HANDLE clsHnd)
{
return (info.compCompHnd->getClassAttribs(clsHnd) & CORINFO_FLG_BYREF_LIKE) != 0;
}

FORCEINLINE
bool Compiler::eeIsIntrinsic(CORINFO_METHOD_HANDLE ftn)
{
Expand Down
6 changes: 2 additions & 4 deletions src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2672,8 +2672,7 @@ bool Compiler::verCheckTailCallConstraint(OPCODE opcode,
}

// Check that the argument is not a byref-like for tailcalls.
if ((ciType == CORINFO_TYPE_VALUECLASS) &&
((info.compCompHnd->getClassAttribs(classHandle) & CORINFO_FLG_BYREF_LIKE) != 0))
if ((ciType == CORINFO_TYPE_VALUECLASS) && eeIsByrefLike(classHandle))
{
return false;
}
Expand Down Expand Up @@ -10121,8 +10120,7 @@ void Compiler::impImportBlockCode(BasicBlock* block)
break;
}

bool isByRefLike =
(info.compCompHnd->getClassAttribs(resolvedToken.hClass) & CORINFO_FLG_BYREF_LIKE) != 0;
bool isByRefLike = eeIsByrefLike(resolvedToken.hClass);
if (isByRefLike)
{
// For ByRefLike types we are required to either fold the
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3885,8 +3885,7 @@ GenTree* Compiler::impIntrinsic(CORINFO_CLASS_HANDLE clsHnd,
retNode = gtNewIconNode(eeIsValueClass(hClass) ? 1 : 0);
break;
case NI_System_Type_get_IsByRefLike:
retNode = gtNewIconNode(
(info.compCompHnd->getClassAttribs(hClass) & CORINFO_FLG_BYREF_LIKE) ? 1 : 0);
retNode = gtNewIconNode(eeIsByrefLike(hClass) ? 1 : 0);
break;
case NI_System_Type_get_IsPrimitive:
// getTypeForPrimitiveValueClass returns underlying type for enums, so we check it first
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/jit/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ void ClassLayout::InitializeGCPtrs(Compiler* compiler)
bool ClassLayout::IsStackOnly(Compiler* comp) const
{
// Byref-like structs are stack only
if ((m_classHandle != NO_CLASS_HANDLE) &&
(((comp->info.compCompHnd->getClassAttribs(m_classHandle)) & CORINFO_FLG_BYREF_LIKE) != 0))
if ((m_classHandle != NO_CLASS_HANDLE) && comp->eeIsByrefLike(m_classHandle))
{
return true;
}
Expand Down
13 changes: 13 additions & 0 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7735,6 +7735,19 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
}
break;

case GT_STOREIND:
if (op1->OperIs(GT_FIELD_ADDR) && varTypeIsGC(tree))
{
CORINFO_FIELD_HANDLE fieldHandle = op1->AsFieldAddr()->gtFldHnd;
if (eeIsByrefLike(info.compCompHnd->getFieldClass(fieldHandle)))
{
JITDUMP("Marking [%06u] STOREIND as GTF_IND_TGT_NOT_HEAP: field's owner is a byref-like struct\n",
dspTreeID(tree));
tree->gtFlags |= GTF_IND_TGT_NOT_HEAP;
}
}
break;

case GT_DIV:
// Replace "val / dcon" with "val * (1.0 / dcon)" if dcon is a power of two.
// Powers of two within range are always exactly represented,
Expand Down
Loading