Skip to content

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

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

Merged
merged 19 commits into from
Oct 10, 2023
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
31 changes: 31 additions & 0 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,37 @@ 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 (compiler->lvaGetDesc(lclVar->GetLclNum())->IsSpan())
{
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
26 changes: 26 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,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>

public:
union {
unsigned lvFieldLclStart; // The index of the local var representing the first field in the promoted struct
Expand Down Expand Up @@ -964,6 +966,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 Expand Up @@ -8549,6 +8563,18 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
return strcmp(ns, "System.Runtime.Intrinsics") == 0;
}

bool isSpanClass(const CORINFO_CLASS_HANDLE clsHnd)
{
if (isIntrinsicType(clsHnd))
{
const char* namespaceName = nullptr;
const char* className = getClassNameFromMetadata(clsHnd, &namespaceName);
return strcmp(namespaceName, "System") == 0 &&
(strcmp(className, "Span`1") == 0 || strcmp(className, "ReadOnlySpan`1") == 0);
}
return false;
}

#ifdef FEATURE_SIMD
// Have we identified any SIMD types?
// This is currently used by struct promotion to avoid getting type information for a struct
Expand Down
11 changes: 6 additions & 5 deletions src/coreclr/jit/lclmorph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,18 +1331,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
7 changes: 7 additions & 0 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,11 @@ void Compiler::StructPromotionHelper::PromoteStructVar(unsigned lclNum)
fieldVarDsc->lvIsOSRLocal = varDsc->lvIsOSRLocal;
fieldVarDsc->lvIsOSRExposedLocal = varDsc->lvIsOSRExposedLocal;

if (varDsc->IsSpan() && fieldVarDsc->lvFldOffset == OFFSETOF__CORINFO_Span__length)
{
fieldVarDsc->SetIsNeverNegative(true);
}

// This new local may be the first time we've seen a long typed local.
if (fieldVarDsc->lvType == TYP_LONG)
{
Expand Down Expand Up @@ -2933,6 +2938,8 @@ void Compiler::lvaSetStruct(unsigned varNum, ClassLayout* layout, bool unsafeVal
}
#endif // not TARGET_64BIT

varDsc->SetIsSpan(this->isSpanClass(layout->GetClassHandle()));

// Check whether this local is an unsafe value type and requires GS cookie protection.
// GS checks require the stack to be re-ordered, which can't be done with EnC.
if (unsafeValueClsCheck)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace System
#pragma warning disable SYSLIB1056 // Specified native type is invalid
[NativeMarshalling(typeof(ReadOnlySpanMarshaller<,>))]
#pragma warning restore SYSLIB1056 // Specified native type is invalid
[Intrinsic]
public readonly ref struct ReadOnlySpan<T>
{
/// <summary>A byref or a native ptr.</summary>
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Private.CoreLib/src/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace System
#pragma warning disable SYSLIB1056 // Specified native type is invalid
[NativeMarshalling(typeof(SpanMarshaller<,>))]
#pragma warning restore SYSLIB1056 // Specified native type is invalid
[Intrinsic]
public readonly ref struct Span<T>
{
/// <summary>A byref or a native ptr.</summary>
Expand Down