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: Add a simple store-forwarding optimization for structs returned in registers #86388

Closed
Closed
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
93 changes: 90 additions & 3 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4107,9 +4107,9 @@ void Lowering::LowerRet(GenTreeUnOp* ret)
{
assert(ret->OperGet() == GT_RETURN);

JITDUMP("lowering GT_RETURN\n");
JITDUMP("Lowering RETURN\n");
DISPNODE(ret);
JITDUMP("============");
JITDUMP("============\n");

GenTree* retVal = ret->gtGetOp1();
// There are two kinds of retyping:
Expand Down Expand Up @@ -4436,11 +4436,17 @@ void Lowering::LowerRetStruct(GenTreeUnOp* ret)
assert(ret->OperIs(GT_RETURN));
assert(varTypeIsStruct(ret));

GenTree* retVal = ret->gtGetOp1();
var_types nativeReturnType = comp->info.compRetNativeType;
// Note: small types are returned as INT.
ret->ChangeType(genActualType(nativeReturnType));

if (comp->opts.OptimizationEnabled())
{
TryStoreForwardStructReturn(ret);
}

GenTree* retVal = ret->gtGetOp1();

switch (retVal->OperGet())
{
case GT_CNS_INT:
Expand Down Expand Up @@ -4511,6 +4517,87 @@ void Lowering::LowerRetStruct(GenTreeUnOp* ret)
}
}

//----------------------------------------------------------------------------------------------
// TryStoreForwardStructReturn: See if we can store forward a primitive store to a struct return.
//
// Arguments:
// ret - The return node.
//
// Remarks:
// Struct returns may be immediately preceded by IR that writes a primitive
// field into the struct. Particularly physical promotion produces this
// pattern. This optimization gets rid of the store when possible.
//
// Before:
// t21 = CNS_INT int 0 $41
// ┌──▌ t21 int
// ▌ STORE_LCL_FLD ushort V01 tmp1 d:1[+0] $VN.Void
// t6 = LCL_VAR struct<System.Half, 2> V01 tmp1 u:1 (last use) $c0
// ┌──▌ t6 struct
// ▌ RETURN struct $VN.Void
//
// After:
// t21 = CNS_INT int 0 $41
// ┌──▌ t21 int
// ▌ RETURN int $VN.Void
//
void Lowering::TryStoreForwardStructReturn(GenTreeUnOp* ret)
{
GenTree* retVal = ret->gtGetOp1();
if (!retVal->OperIsLocalRead())
{
return;
}

GenTreeLclVarCommon* retLcl = retVal->AsLclVarCommon();

GenTree* prev = retVal->gtPrev;
while ((prev != nullptr) && prev->OperIs(GT_IL_OFFSET))
{
prev = prev->gtPrev;
}

if ((prev == nullptr) || !prev->OperIsLocalStore())
{
return;
}

GenTreeLclVarCommon* store = prev->AsLclVarCommon();

if (store->GetLclNum() != retLcl->GetLclNum())
{
return;
}

if (store->GetLclOffs() != retLcl->GetLclOffs())
{
return;
}

// Note: No normalization concerns for primitives here since we don't
// guarantee any normalization for struct returns.
if (genActualType(store) != genActualType(comp->info.compRetNativeType))
{
return;
}

if (genTypeSize(store) < genTypeSize(comp->info.compRetNativeType))
{
return;
}

JITDUMP("Store-forwarding struct with preceding primitive store. Before:\n");
DISPTREERANGE(BlockRange(), ret);

ret->gtOp1 = store->Data();
store->Data()->ClearContained();
BlockRange().Remove(store);
BlockRange().Remove(retLcl);

JITDUMP("\nAfter:\n");
DISPTREERANGE(BlockRange(), ret);
}

//----------------------------------------------------------------------------------------------
// LowerRetSingleRegStructLclVar: Lowers a return node with a struct lclVar as a source.
//
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Lowering final : public Phase
void LowerRet(GenTreeUnOp* ret);
void LowerStoreLocCommon(GenTreeLclVarCommon* lclVar);
void LowerRetStruct(GenTreeUnOp* ret);
void TryStoreForwardStructReturn(GenTreeUnOp* ret);
void LowerRetSingleRegStructLclVar(GenTreeUnOp* ret);
void LowerCallStruct(GenTreeCall* call);
void LowerStoreSingleRegCallStruct(GenTreeBlk* store);
Expand Down