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

Fix Unnecessary sign-extension for (nuint)span.Length #88136

Merged
merged 19 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ enum CorInfoFlag
// CORINFO_FLG_UNUSED = 0x40000000,

// These are internal flags that can only be on Classes
CORINFO_FLG_SPAN = 0x00008000, // is the class a Span<T>/ReadOnlySpan<T>
Copy link
Member

Choose a reason for hiding this comment

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

The default pattern for special casing types in the JIT is to mark the type with [Intrinsic], call isIntrinsicType on the class handle, and check the name if isIntrinsicType returns true. It is how we are special casing the SIMD Vector types. My guess that the default pattern would cheaper overall than computing all type flags just to figure out whether the type is a Span or ReadOnlySpan.

If this check is very hot and we cannot afford to use the default pattern for it, it should be a dedicated isSpanOrReadOnlySpanType method on JIT/EE interface so that we do not pay the full cost of getting all flags that getClassAttributes returns.

Copy link
Member

@jakobbotsch jakobbotsch Jul 5, 2023

Choose a reason for hiding this comment

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

I suggested to @En3Tho to add CORINFO_FLG_SPAN mainly based on the fact that it is on the same level of information as CORINFO_FLG_ARRAY and CORINFO_FLG_DELEGATE, but it is reasonable to me to switch this to isSpanOrReadOnlySpanType. I am not so sure that we want to switch lvaSetStruct to be doing the same style of recognition as the SIMD recognition we do today.

My guess that the default pattern would cheaper overall than computing all type flags just to figure out whether the type is a Span or ReadOnlySpan.

Note that we have been calling getClassAttribs unconditionally from lvaSetStruct since forever. It was only recently removed in #87967.

Copy link
Member

Choose a reason for hiding this comment

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

CORINFO_FLG_SPAN mainly based on the fact that it is on the same level of information as CORINFO_FLG_ARRAY and CORINFO_FLG_DELEGATE

Nit: Arrays or Delegates are core type system entities that one cannot identify via a simple name check. Span/ReadOnlySpan is just an ordinary type that the JIT is recognizing as intrinsic.

I am not so sure that we want to switch lvaSetStruct to be doing the same style of recognition as the SIMD recognition we do today.

Are you worried about throughput? The name check behind the isIntrinsicType early out is fairly cheap overall. We have intentionally switched to allow JIT to identify intrinsics by name, so that the JIT/EE interface does not need to know about all ordinary types and methods that the JIT wants to special case as an intrinsic.

If we are worried about throughout, a dedicated isSpanOrReadOnlySpanType method would be the way to go.

Copy link
Member

Choose a reason for hiding this comment

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

Nit: Arrays or Delegates are core type system entities that one cannot identify via a simple name check. Span/ReadOnlySpan is just an ordinary type that the JIT is recognizing as intrinsic.

Fair point.

Are you worried about throughput? The name check behind the isIntrinsicType early out is fairly cheap overall. We have intentionally switched to allow JIT to identify intrinsics by name, so that the JIT/EE interface does not need to know about all ordinary types and methods that the JIT wants to special case as an intrinsic.

Yes, I was mainly worried about throughput -- we have been quite careful in the JIT to only do SIMD recognition from the paths where we know it is relevant, instead of, say, a general function like lvaSetStruct. But SIMD recognition also has much more to recognize, so if you say that the isIntrinsic + metadata lookup is cheap, then I think we should just do it this way and avoid the JIT-EE change.

Copy link
Member

Choose a reason for hiding this comment

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

We can keep track of it only when optimizing.

CORINFO_FLG_VALUECLASS = 0x00010000, // is the class a value class
// This flag is define din the Methods section, but is also valid on classes.
// CORINFO_FLG_SHAREDINST = 0x00020000, // This class is satisfies TypeHandle::IsCanonicalSubtype
Expand Down
30 changes: 30 additions & 0 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,36 @@ bool IntegralRange::Contains(int64_t value) const
}
break;

case GT_IND:
{
GenTree* const addr = node->AsIndir()->Addr();
if (node->TypeIs(TYP_INT) && addr->OperIs(GT_ADD) && addr->gtGetOp1()->OperIs(GT_LCL_VAR) &&
addr->gtGetOp2()->IsIntegralConst(OFFSETOF__CORINFO_Span__length))
{
GenTreeLclVar* const lclVar = addr->gtGetOp1()->AsLclVar();

if (lclVar->IsSpan(compiler))
{
assert(compiler->lvaIsImplicitByRefLocal(lclVar->GetLclNum()));
return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}
}
break;
}

case GT_LCL_FLD:
{
GenTreeLclFld* const lclFld = node->AsLclFld();
LclVarDsc* const varDsc = compiler->lvaGetDesc(lclFld);

if (node->TypeIs(TYP_INT) && varDsc->IsSpan() && lclFld->GetLclOffs() == OFFSETOF__CORINFO_Span__length)
{
return {SymbolicIntegerValue::Zero, UpperBoundForType(rangeType)};
}

break;
}

case GT_LCL_VAR:
{
LclVarDsc* const varDsc = compiler->lvaGetDesc(node->AsLclVar());
Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ class LclVarDsc
private:
unsigned char lvIsNeverNegative : 1; // The local is known to be never negative

unsigned char lvIsSpan : 1; // The local is a Span<T>
EgorBo marked this conversation as resolved.
Show resolved Hide resolved

public:
union {
unsigned lvFieldLclStart; // The index of the local var representing the first field in the promoted struct
Expand Down Expand Up @@ -956,6 +958,18 @@ class LclVarDsc
lvIsNeverNegative = value;
}

// Is this is local a Span<T>?
bool IsSpan() const
{
return lvIsSpan;
}

// Is this is local a Span<T>?
void SetIsSpan(bool value)
{
lvIsSpan = value;
}

/////////////////////

regNumber GetArgInitReg() const
Expand Down
15 changes: 15 additions & 0 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26020,6 +26020,21 @@ bool GenTreeLclVar::IsNeverNegative(Compiler* comp) const
return comp->lvaGetDesc(GetLclNum())->IsNeverNegative();
}

//------------------------------------------------------------------------
// GenTreeLclVar::IsSpan: Gets true if the lcl var is a Span<T>; otherwise false.
//
// Arguments:
// comp - the compiler instance
//
// Return Value:
// true if the lcl var is a Span<T>; otherwise false.
//
bool GenTreeLclVar::IsSpan(Compiler* comp) const
{
assert(OperIs(GT_LCL_VAR));
return comp->lvaGetDesc(GetLclNum())->IsSpan();
}
En3Tho marked this conversation as resolved.
Show resolved Hide resolved

#if defined(TARGET_XARCH) && defined(FEATURE_HW_INTRINSICS)
//------------------------------------------------------------------------
// GetResultOpNumForRmwIntrinsic: check if the result is written into one of the operands.
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,8 @@ struct GenTreeLclVar : public GenTreeLclVarCommon

bool IsNeverNegative(Compiler* comp) const;

bool IsSpan(Compiler* comp) const;

//-------------------------------------------------------------------
// clearOtherRegFlags: clear GTF_* flags associated with gtOtherRegs
//
Expand Down
19 changes: 13 additions & 6 deletions src/coreclr/jit/lclmorph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,13 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>

if (addr->IsLclVarAddr())
{
const LclVarDsc* varDsc = m_compiler->lvaGetDesc(addr->AsLclVarCommon());
LclVarDsc* varDsc = m_compiler->lvaGetDesc(addr->AsLclVarCommon());

// addr is a span field so this lclVar must be a span
if (isSpanLength)
{
varDsc->SetIsSpan(true);
}
En3Tho marked this conversation as resolved.
Show resolved Hide resolved

if (varDsc->lvPromoted)
{
Expand All @@ -1256,18 +1262,19 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>

LclVarDsc* fieldVarDsc = m_compiler->lvaGetDesc(fieldLclNum);

// Span's Length is never negative unconditionally
if (isSpanLength && (accessSize == genTypeSize(TYP_INT)))
{
fieldVarDsc->SetIsNeverNegative(true);
}

// Retargeting the indirection to reference the promoted field would make it "wide", exposing
// the whole parent struct (with all of its fields).
if (accessSize > genTypeSize(fieldVarDsc))
{
return BAD_VAR_NUM;
}

if (isSpanLength && (accessSize == genTypeSize(TYP_INT)))
{
fieldVarDsc->SetIsNeverNegative(true);
}

JITDUMP("Replacing the field in promoted struct with local var V%02u\n", fieldLclNum);
m_stmtModified = true;

Expand Down
8 changes: 5 additions & 3 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2914,15 +2914,17 @@ void Compiler::lvaSetStruct(unsigned varNum, ClassLayout* layout, bool unsafeVal

if (layout->IsValueClass())
{
varDsc->lvType = layout->GetType();
varDsc->lvType = layout->GetType();
CORINFO_CLASS_HANDLE const classHandle = layout->GetClassHandle();
unsigned const corinfoFlags = this->info.compCompHnd->getClassAttribs(classHandle);
varDsc->SetIsSpan(corinfoFlags & CORINFO_FLG_SPAN);
En3Tho marked this conversation as resolved.
Show resolved Hide resolved

#if FEATURE_IMPLICIT_BYREFS
// Mark implicit byref struct parameters
if (varDsc->lvIsParam && !varDsc->lvIsStructField)
{
structPassingKind howToReturnStruct;
getArgTypeForStruct(layout->GetClassHandle(), &howToReturnStruct, this->info.compIsVarArgs,
varDsc->lvExactSize());
getArgTypeForStruct(classHandle, &howToReturnStruct, this->info.compIsVarArgs, varDsc->lvExactSize());

if (howToReturnStruct == SPK_ByReference)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ std::string SpmiDumpHelper::DumpCorInfoFlag(CorInfoFlag flags)
AddFlag(CORINFO_FLG_BEFOREFIELDINIT);
AddFlag(CORINFO_FLG_GENERIC_TYPE_VARIABLE);
AddFlag(CORINFO_FLG_UNSAFE_VALUECLASS);
AddFlag(CORINFO_FLG_SPAN);

#undef AddFlag

Expand Down
7 changes: 7 additions & 0 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3767,8 +3767,15 @@ uint32_t CEEInfo::getClassAttribsInternal (CORINFO_CLASS_HANDLE clsHnd)
ret |= CORINFO_FLG_VALUECLASS;

if (pMT->IsByRefLike())
{
ret |= CORINFO_FLG_BYREF_LIKE;

if (pMT == CoreLibBinder::GetClass(CLASS__SPAN) || pMT == CoreLibBinder::GetClass(CLASS__READONLY_SPAN))
En3Tho marked this conversation as resolved.
Show resolved Hide resolved
{
ret |= CORINFO_FLG_SPAN;
}
}

if ((pClass->IsNotTightlyPacked() && (!pClass->IsManagedSequential() || pClass->HasExplicitSize())) ||
pMT == g_TypedReferenceMT ||
VMClsHnd.IsNativeValueType())
Expand Down