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

[RISC-V] Avoid using native layout info to calculate register flags for small structs where possible #97877

Merged
merged 13 commits into from
Mar 15, 2024
Merged
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
99 changes: 63 additions & 36 deletions src/coreclr/vm/methodtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3420,18 +3420,12 @@ bool MethodTable::IsRiscV64OnlyOneField(MethodTable * pMT)
{
TypeHandle th(pMT);

bool useNativeLayout = false;
bool ret = false;
MethodTable* pMethodTable = nullptr;
bool ret = false;

if (!th.IsTypeDesc())
{
pMethodTable = th.AsMethodTable();
if (pMethodTable->HasLayout())
{
useNativeLayout = true;
}
else if (th.GetSize() <= 16 /*MAX_PASS_MULTIREG_BYTES*/)
MethodTable* pMethodTable = th.AsMethodTable();
if (th.GetSize() <= 16 /*MAX_PASS_MULTIREG_BYTES*/)
{
DWORD numIntroducedFields = pMethodTable->GetNumIntroducedInstanceFields();

Expand All @@ -3441,6 +3435,19 @@ bool MethodTable::IsRiscV64OnlyOneField(MethodTable * pMT)

CorElementType fieldType = pFieldStart[0].GetFieldType();

// InlineArray types and fixed buffer types have implied repeated fields.
// Checking if a type is an InlineArray type is cheap, so we'll do that first.
bool hasImpliedRepeatedFields = HasImpliedRepeatedFields(pMethodTable);

if (hasImpliedRepeatedFields)
{
numIntroducedFields = pMethodTable->GetNumInstanceFieldBytes() / pFieldStart->GetSize();
if (numIntroducedFields != 1)
{
goto _End_arg;
}
}

if (CorTypeInfo::IsPrimitiveType_NoThrow(fieldType))
{
ret = true;
Expand All @@ -3454,20 +3461,12 @@ bool MethodTable::IsRiscV64OnlyOneField(MethodTable * pMT)
}
}
}
goto _End_arg;
}
}
else
{
_ASSERTE(th.IsNativeValueType());

useNativeLayout = true;
pMethodTable = th.AsNativeValueType();
}
_ASSERTE(pMethodTable != nullptr);
MethodTable* pMethodTable = th.AsNativeValueType();

if (useNativeLayout)
{
if (th.GetSize() <= 16 /*MAX_PASS_MULTIREG_BYTES*/)
{
DWORD numIntroducedFields = pMethodTable->GetNativeLayoutInfo()->GetNumFields();
Expand Down Expand Up @@ -3526,18 +3525,12 @@ int MethodTable::GetRiscV64PassStructInRegisterFlags(CORINFO_CLASS_HANDLE cls)
{
TypeHandle th(cls);

bool useNativeLayout = false;
int size = STRUCT_NO_FLOAT_FIELD;
MethodTable* pMethodTable = nullptr;

if (!th.IsTypeDesc())
{
pMethodTable = th.AsMethodTable();
if (pMethodTable->HasLayout())
{
useNativeLayout = true;
}
else if (th.GetSize() <= 16 /*MAX_PASS_MULTIREG_BYTES*/)
MethodTable* pMethodTable = th.AsMethodTable();
if (th.GetSize() <= 16 /*MAX_PASS_MULTIREG_BYTES*/)
{
DWORD numIntroducedFields = pMethodTable->GetNumIntroducedInstanceFields();

Expand All @@ -3547,6 +3540,44 @@ int MethodTable::GetRiscV64PassStructInRegisterFlags(CORINFO_CLASS_HANDLE cls)

CorElementType fieldType = pFieldStart[0].GetFieldType();

// InlineArray types and fixed buffer types have implied repeated fields.
// Checking if a type is an InlineArray type is cheap, so we'll do that first.
bool hasImpliedRepeatedFields = HasImpliedRepeatedFields(pMethodTable);

if (hasImpliedRepeatedFields)
{
numIntroducedFields = pMethodTable->GetNumInstanceFieldBytes() / pFieldStart->GetSize();
if (numIntroducedFields > 2)
{
goto _End_arg;
}

if (fieldType == ELEMENT_TYPE_R4)
{
if (numIntroducedFields == 1)
{
size = STRUCT_FLOAT_FIELD_ONLY_ONE;
}
else if (numIntroducedFields == 2)
{
size = STRUCT_FLOAT_FIELD_ONLY_TWO;
}
goto _End_arg;
}
else if (fieldType == ELEMENT_TYPE_R8)
{
if (numIntroducedFields == 1)
{
size = STRUCT_FLOAT_FIELD_ONLY_ONE | STRUCT_FIRST_FIELD_SIZE_IS8;
}
else if (numIntroducedFields == 2)
{
size = STRUCT_FIELD_TWO_DOUBLES;
}
goto _End_arg;
}
}

if (CorTypeInfo::IsPrimitiveType_NoThrow(fieldType))
{
if (fieldType == ELEMENT_TYPE_R4)
Expand Down Expand Up @@ -3584,6 +3615,11 @@ int MethodTable::GetRiscV64PassStructInRegisterFlags(CORINFO_CLASS_HANDLE cls)
goto _End_arg;
}

if (pFieldFirst->GetSize() > pFieldSecond->GetOffset())
{
goto _End_arg;
}

CorElementType fieldType = pFieldFirst[0].GetFieldType();
if (CorTypeInfo::IsPrimitiveType_NoThrow(fieldType))
{
Expand Down Expand Up @@ -3701,21 +3737,12 @@ int MethodTable::GetRiscV64PassStructInRegisterFlags(CORINFO_CLASS_HANDLE cls)
size |= STRUCT_SECOND_FIELD_SIZE_IS8;
}
}

goto _End_arg;
}
}
else
{
_ASSERTE(th.IsNativeValueType());
MethodTable* pMethodTable = th.AsNativeValueType();

useNativeLayout = true;
pMethodTable = th.AsNativeValueType();
}
_ASSERTE(pMethodTable != nullptr);
jkotas marked this conversation as resolved.
Show resolved Hide resolved

if (useNativeLayout)
{
if (th.GetSize() <= 16 /*MAX_PASS_MULTIREG_BYTES*/)
{
DWORD numIntroducedFields = pMethodTable->GetNativeLayoutInfo()->GetNumFields();
Expand Down
Loading