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

JIT: Handle commas in physical promotion #87401

Merged
merged 1 commit into from
Jun 13, 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
71 changes: 59 additions & 12 deletions src/coreclr/jit/promotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,8 @@ class LocalsUseVisitor : public GenTreeVisitor<LocalsUseVisitor>
public:
enum
{
DoPreOrder = true,
DoPreOrder = true,
ComputeStack = true,
};

LocalsUseVisitor(Promotion* prom) : GenTreeVisitor(prom->m_compiler), m_prom(prom)
Expand Down Expand Up @@ -609,9 +610,25 @@ class LocalsUseVisitor : public GenTreeVisitor<LocalsUseVisitor>
}
else
{
int userIndex = 1;
while (userIndex < m_ancestors.Height())
{
GenTree* ancestor = m_ancestors.Top(userIndex);
GenTree* child = m_ancestors.Top(userIndex - 1);

if (!ancestor->OperIs(GT_COMMA) || (ancestor->gtGetOp2() != child))
{
break;
}

userIndex++;
}

GenTree* effectiveUser = userIndex >= m_ancestors.Height() ? nullptr : m_ancestors.Top(userIndex);

accessType = lcl->TypeGet();
accessLayout = accessType == TYP_STRUCT ? lcl->GetLayout(m_compiler) : nullptr;
accessFlags = ClassifyLocalAccess(lcl, user);
accessFlags = ClassifyLocalAccess(lcl, effectiveUser);
}

LocalUses* uses = GetOrCreateUses(lcl->GetLclNum());
Expand Down Expand Up @@ -679,7 +696,7 @@ class LocalsUseVisitor : public GenTreeVisitor<LocalsUseVisitor>
{
for (CallArg& arg : user->AsCall()->gtArgs.Args())
{
if (arg.GetNode() == lcl)
if (arg.GetNode()->gtEffectiveVal() == lcl)
{
flags |= AccessKindFlags::IsCallArg;
break;
Expand All @@ -688,14 +705,14 @@ class LocalsUseVisitor : public GenTreeVisitor<LocalsUseVisitor>
}

#ifdef DEBUG
if (user->OperIsStore() && (user->Data() == lcl))
if (user->OperIsStore() && (user->Data()->gtEffectiveVal() == lcl))
{
flags |= AccessKindFlags::IsAssignmentSource;
}

if (user->OperIs(GT_RETURN))
{
assert(user->gtGetOp1() == lcl);
assert(user->gtGetOp1()->gtEffectiveVal() == lcl);
flags |= AccessKindFlags::IsReturned;
}
#endif
Expand Down Expand Up @@ -1364,17 +1381,20 @@ void ReplaceVisitor::LoadStoreAroundCall(GenTreeCall* call, GenTree* user)
continue;
}

if (!arg.GetNode()->OperIs(GT_LCL_VAR, GT_LCL_FLD))
GenTree** argUse = EffectiveUse(&arg.EarlyNodeRef());
GenTree* argNode = *argUse;

if (!argNode->OperIs(GT_LCL_VAR, GT_LCL_FLD))
{
continue;
}

GenTreeLclVarCommon* argNodeLcl = arg.GetNode()->AsLclVarCommon();
GenTreeLclVarCommon* argNodeLcl = argNode->AsLclVarCommon();

if (argNodeLcl->TypeIs(TYP_STRUCT))
{
unsigned size = argNodeLcl->GetLayout(m_compiler)->GetSize();
WriteBackBefore(&arg.EarlyNodeRef(), argNodeLcl->GetLclNum(), argNodeLcl->GetLclOffs(), size);
WriteBackBefore(argUse, argNodeLcl->GetLclNum(), argNodeLcl->GetLclOffs(), size);

if ((m_aggregates[argNodeLcl->GetLclNum()] != nullptr) && IsPromotedStructLocalDying(argNodeLcl))
{
Expand All @@ -1398,6 +1418,26 @@ void ReplaceVisitor::LoadStoreAroundCall(GenTreeCall* call, GenTree* user)
}
}

//------------------------------------------------------------------------
// EffectiveUse:
// Given a use, compute the "effective" use by skipping all uses of commas.
//
// Parameters:
// use - The use edge.
//
// Returns:
// A use edge that points to a non GT_COMMA value computed for the use.
//
GenTree** ReplaceVisitor::EffectiveUse(GenTree** use)
{
while ((*use)->OperIs(GT_COMMA))
{
use = &(*use)->AsOp()->gtOp2;
}

return use;
}

//------------------------------------------------------------------------
// IsPromotedStructLocalDying:
// Check if a promoted struct local is dying at its current position.
Expand Down Expand Up @@ -1475,7 +1515,7 @@ void ReplaceVisitor::ReplaceLocal(GenTree** use, GenTree* user)
{
if (lcl->OperIsLocalRead())
{
assert((user == nullptr) || user->OperIs(GT_CALL, GT_RETURN) || user->OperIsStore());
assert((user == nullptr) || user->OperIs(GT_CALL, GT_RETURN, GT_COMMA) || user->OperIsStore());
}
}
else
Expand Down Expand Up @@ -1599,16 +1639,23 @@ void ReplaceVisitor::CheckForwardSubForLastUse(unsigned lclNum)
//
void ReplaceVisitor::StoreBeforeReturn(GenTreeUnOp* ret)
{
if (ret->TypeIs(TYP_VOID) || !ret->gtGetOp1()->OperIs(GT_LCL_VAR, GT_LCL_FLD))
if (ret->TypeIs(TYP_VOID))
{
return;
}

GenTree** retUse = EffectiveUse(&ret->gtOp1);
GenTree* retNode = *retUse;
if (!retNode->OperIs(GT_LCL_VAR, GT_LCL_FLD))
{
return;
}

GenTreeLclVarCommon* retLcl = ret->gtGetOp1()->AsLclVarCommon();
GenTreeLclVarCommon* retLcl = retNode->AsLclVarCommon();
if (retLcl->TypeIs(TYP_STRUCT))
{
unsigned size = retLcl->GetLayout(m_compiler)->GetSize();
WriteBackBefore(&ret->gtOp1, retLcl->GetLclNum(), retLcl->GetLclOffs(), size);
WriteBackBefore(retUse, retLcl->GetLclNum(), retLcl->GetLclOffs(), size);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/promotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ class ReplaceVisitor : public GenTreeVisitor<ReplaceVisitor>
private:
GenTree** InsertMidTreeReadBacksIfNecessary(GenTree** use);
void LoadStoreAroundCall(GenTreeCall* call, GenTree* user);
GenTree** EffectiveUse(GenTree** use);
bool IsPromotedStructLocalDying(GenTreeLclVarCommon* structLcl);
void ReplaceLocal(GenTree** use, GenTree* user);
void CheckForwardSubForLastUse(unsigned lclNum);
Expand Down