Skip to content

JIT: run local struct to field update in morph both pre and post order for returns #75304

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 2 commits into from
Sep 13, 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
1 change: 1 addition & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5768,6 +5768,7 @@ class Compiler
GenTree* fgMorphStoreDynBlock(GenTreeStoreDynBlk* tree);
GenTree* fgMorphForRegisterFP(GenTree* tree);
GenTree* fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optAssertionPropDone = nullptr);
void fgTryReplaceStructLocalWithField(GenTree* tree);
GenTree* fgOptimizeCast(GenTreeCast* cast);
GenTree* fgOptimizeCastOnAssignment(GenTreeOp* asg);
GenTree* fgOptimizeEqualityComparisonWithConst(GenTreeOp* cmp);
Expand Down
81 changes: 55 additions & 26 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10243,32 +10243,8 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
{
op1 = fgMorphRetInd(tree->AsUnOp());
}
if (op1->OperIs(GT_LCL_VAR))
{
// With a `genReturnBB` this `RETURN(src)` tree will be replaced by a `ASG(genReturnLocal, src)`
// and `ASG` will be transformed into field by field copy without parent local referencing if
// possible.
GenTreeLclVar* lclVar = op1->AsLclVar();
unsigned lclNum = lclVar->GetLclNum();
if ((genReturnLocal == BAD_VAR_NUM) || (genReturnLocal == lclNum))
{
LclVarDsc* varDsc = lvaGetDesc(lclVar);
if (varDsc->CanBeReplacedWithItsField(this))
{
// We can replace the struct with its only field and allow copy propagation to replace
// return value that was written as a field.
unsigned fieldLclNum = varDsc->lvFieldLclStart;
LclVarDsc* fieldDsc = lvaGetDesc(fieldLclNum);

JITDUMP("Replacing an independently promoted local var V%02u with its only field "
"V%02u for "
"the return [%06u]\n",
lclVar->GetLclNum(), fieldLclNum, dspTreeID(tree));
lclVar->SetLclNum(fieldLclNum);
lclVar->ChangeType(fieldDsc->lvType);
}
}
}

fgTryReplaceStructLocalWithField(op1);
}

// normalize small integer return values
Expand Down Expand Up @@ -11681,6 +11657,17 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
}
break;

case GT_RETURN:

// Retry updating op1 to a field -- assertion
// prop done when morphing op1 changed the local.
//
if (op1 != nullptr)
{
fgTryReplaceStructLocalWithField(op1);
}
break;

default:
break;
}
Expand Down Expand Up @@ -11725,6 +11712,48 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
return tree;
}

//------------------------------------------------------------------------
// fgTryReplaceStructLocalWithField: see if a struct use can be replaced
// with an equivalent field use
//
// Arguments:
// tree - tree to examine and possibly modify
//
// Notes:
// Currently only called when the tree parent is a GT_RETURN.
//
void Compiler::fgTryReplaceStructLocalWithField(GenTree* tree)
{
if (!tree->OperIs(GT_LCL_VAR))
{
return;
}

// With a `genReturnBB` this `RETURN(src)` tree will be replaced by a `ASG(genReturnLocal, src)`
// and `ASG` will be transformed into field by field copy without parent local referencing if
// possible.
GenTreeLclVar* lclVar = tree->AsLclVar();
unsigned lclNum = lclVar->GetLclNum();
if ((genReturnLocal == BAD_VAR_NUM) || (genReturnLocal == lclNum))
{
LclVarDsc* const varDsc = lvaGetDesc(lclVar);
if (varDsc->CanBeReplacedWithItsField(this))
{
// We can replace the struct with its only field and allow copy propagation to replace
// return value that was written as a field.
unsigned const fieldLclNum = varDsc->lvFieldLclStart;
LclVarDsc* const fieldDsc = lvaGetDesc(fieldLclNum);

JITDUMP("Replacing an independently promoted local var V%02u with its only field "
"V%02u for "
"the return [%06u]\n",
lclVar->GetLclNum(), fieldLclNum, dspTreeID(tree));
lclVar->SetLclNum(fieldLclNum);
lclVar->ChangeType(fieldDsc->lvType);
}
}
}

//------------------------------------------------------------------------
// fgOptimizeCast: Optimizes the supplied GT_CAST tree.
//
Expand Down