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

Unpin locals, attempt 2 #70655

Merged
merged 2 commits into from
Jun 17, 2022
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
2 changes: 2 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ class LclVarDsc
unsigned char lvSingleDefDisqualifyReason = 'H';
#endif

unsigned char lvAllDefsAreNoGc : 1; // For pinned locals: true if all defs of this local are no-gc

#if FEATURE_MULTIREG_ARGS
regNumber lvRegNumForSlot(unsigned slotNum)
{
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,11 @@ struct GenTree
return true;
}

bool IsNotGcDef() const
{
return IsIntegralConst(0) || IsLocalAddrExpr();
}

// LIR flags
// These helper methods, along with the flag values they manipulate, are defined in lir.h
//
Expand Down
78 changes: 48 additions & 30 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4181,49 +4181,57 @@ void Compiler::lvaMarkLclRefs(GenTree* tree, BasicBlock* block, Statement* stmt,

/* Is this an assignment to a local variable? */

if (op1->gtOper == GT_LCL_VAR && op2->gtType != TYP_BOOL)
if (op1->gtOper == GT_LCL_VAR)
{
/* Only simple assignments allowed for booleans */
LclVarDsc* varDsc = lvaGetDesc(op1->AsLclVarCommon());

if (tree->gtOper != GT_ASG)
if (varDsc->lvPinned && varDsc->lvAllDefsAreNoGc)
{
goto NOT_BOOL;
if (!op2->IsNotGcDef())
{
varDsc->lvAllDefsAreNoGc = false;
}
}

/* Is the RHS clearly a boolean value? */

switch (op2->gtOper)
if (op2->gtType != TYP_BOOL)
{
unsigned lclNum;
/* Only simple assignments allowed for booleans */

case GT_CNS_INT:
if (tree->gtOper != GT_ASG)
{
goto NOT_BOOL;
}

if (op2->AsIntCon()->gtIconVal == 0)
{
break;
}
if (op2->AsIntCon()->gtIconVal == 1)
{
break;
}
/* Is the RHS clearly a boolean value? */

// Not 0 or 1, fall through ....
FALLTHROUGH;
switch (op2->gtOper)
{
case GT_CNS_INT:

default:
if (op2->AsIntCon()->gtIconVal == 0)
{
break;
}
if (op2->AsIntCon()->gtIconVal == 1)
{
break;
}

if (op2->OperIsCompare())
{
break;
}
// Not 0 or 1, fall through ....
FALLTHROUGH;

NOT_BOOL:
default:

lclNum = op1->AsLclVarCommon()->GetLclNum();
noway_assert(lclNum < lvaCount);
if (op2->OperIsCompare())
{
break;
}

lvaTable[lclNum].lvIsBoolean = false;
break;
NOT_BOOL:

varDsc->lvIsBoolean = false;
break;
}
}
}
}
Expand Down Expand Up @@ -4278,7 +4286,8 @@ void Compiler::lvaMarkLclRefs(GenTree* tree, BasicBlock* block, Statement* stmt,
{
if (lvaVarAddrExposed(lclNum))
{
varDsc->lvIsBoolean = false;
varDsc->lvIsBoolean = false;
varDsc->lvAllDefsAreNoGc = false;
}

if (tree->gtOper == GT_LCL_FLD)
Expand Down Expand Up @@ -4750,6 +4759,8 @@ void Compiler::lvaComputeRefCounts(bool isRecompute, bool setSlotNumbers)
{
varDsc->lvSingleDef = varDsc->lvIsParam;
varDsc->lvSingleDefRegCandidate = varDsc->lvIsParam;

varDsc->lvAllDefsAreNoGc = (varDsc->lvImplicitlyReferenced == false);
aromaa marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
varDsc->lvAllDefsAreNoGc = (varDsc->lvImplicitlyReferenced == false);
// if there are implicit refs, assume they may be gc defs
varDsc->lvAllDefsAreNoGc = !varDsc->lvImplicitlyReferenced;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This actually gives a compiler warning of "Using logical '!' when bitwise '~' was probably intended"

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it's ok to leave this as is. For some reason all these bit fields are unsigned char and not bool.

}
}

Expand Down Expand Up @@ -4868,6 +4879,13 @@ void Compiler::lvaComputeRefCounts(bool isRecompute, bool setSlotNumbers)
varDsc->lvImplicitlyReferenced = 1;
}
}

if (varDsc->lvPinned && varDsc->lvAllDefsAreNoGc)
{
varDsc->lvPinned = 0;

JITDUMP("V%02u was unpinned as all def candidates were local.\n", lclNum);
}
}
}

Expand Down