Skip to content

Commit

Permalink
Repurpose genHomeSwiftStructParameters for reassembling structs on RI…
Browse files Browse the repository at this point in the history
…SC-V

This is what RISC-V did with old ABI classifiers
  • Loading branch information
tomeksowi committed Apr 22, 2024
1 parent 5423299 commit 06e9d65
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 30 deletions.
14 changes: 8 additions & 6 deletions src/coreclr/jit/abi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,16 @@ bool ABIPassingInformation::HasExactlyOneStackSegment() const
//
bool ABIPassingInformation::IsSplitAcrossRegistersAndStack() const
{
bool anyReg = false;
bool anyStack = false;
for (unsigned i = 0; i < NumSegments; i++)
if (NumSegments < 2)
return false;

bool isFirstInReg = Segments[0].IsPassedInRegister();
for (unsigned i = 1; i < NumSegments; i++)
{
anyReg |= Segments[i].IsPassedInRegister();
anyStack |= Segments[i].IsPassedOnStack();
if (isFirstInReg != Segments[i].IsPassedInRegister())
return true;
}
return anyReg && anyStack;
return false;
}

//-----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ class CodeGen final : public CodeGenInterface
void genEnregisterOSRArgsAndLocals();
#endif

void genHomeSwiftStructParameters(bool handleStack);
void genHomeSplitStructParameters(bool handleStack);

void genCheckUseBlockInit();
#if defined(UNIX_AMD64_ABI) && defined(FEATURE_SIMD)
Expand Down
18 changes: 10 additions & 8 deletions src/coreclr/jit/codegencommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4119,32 +4119,34 @@ void CodeGen::genEnregisterOSRArgsAndLocals()
}
}

#ifdef SWIFT_SUPPORT
#if defined(SWIFT_SUPPORT) || defined(TARGET_RISCV64)

//-----------------------------------------------------------------------------
// genHomeSwiftStructParameters:
// Reassemble Swift struct parameters if necessary.
// genHomeSplitStructParameters:
// Reassemble split struct parameters if necessary.
//
// Parameters:
// handleStack - If true, reassemble the segments that were passed on the stack.
// If false, reassemble the segments that were passed in registers.
//
void CodeGen::genHomeSwiftStructParameters(bool handleStack)
void CodeGen::genHomeSplitStructParameters(bool handleStack)
{
for (unsigned lclNum = 0; lclNum < compiler->info.compArgsCount; lclNum++)
{
#ifdef SWIFT_SUPPORT
if (lclNum == compiler->lvaSwiftSelfArg)
{
continue;
}
#endif

LclVarDsc* dsc = compiler->lvaGetDesc(lclNum);
if ((dsc->TypeGet() != TYP_STRUCT) || compiler->lvaIsImplicitByRefLocal(lclNum) || !dsc->lvOnFrame)
{
continue;
}

JITDUMP("Homing Swift parameter V%02u: ", lclNum);
JITDUMP("Homing split parameter V%02u: ", lclNum);
const ABIPassingInformation& abiInfo = compiler->lvaGetParameterABIInfo(lclNum);
DBEXEC(VERBOSE, abiInfo.Dump());

Expand Down Expand Up @@ -4174,7 +4176,7 @@ void CodeGen::genHomeSwiftStructParameters(bool handleStack)
else
{
var_types loadType = TYP_UNDEF;
switch (seg.Size)
switch (RISCV64_ONLY(RoundUpToPower2)(seg.Size))
{
case 1:
loadType = TYP_UBYTE;
Expand Down Expand Up @@ -4221,7 +4223,7 @@ void CodeGen::genHomeSwiftStructParameters(bool handleStack)
}
}
}
#endif
#endif // defined(SWIFT_SUPPORT) || defined(TARGET_RISCV64)

/*-----------------------------------------------------------------------------
*
Expand Down Expand Up @@ -5520,7 +5522,7 @@ void CodeGen::genFnProlog()
intRegState.rsCalleeRegArgMaskLiveIn &= ~RBM_SWIFT_ERROR;
}

genHomeSwiftStructParameters(/* handleStack */ false);
genHomeSplitStructParameters(/* handleStack */ false);
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/jit/codegenlinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ void CodeGen::genCodeForBBlist()
compiler->compCurStmt = nullptr;
compiler->compCurLifeTree = nullptr;

#ifdef SWIFT_SUPPORT
// Reassemble Swift struct parameters on the local stack frame in the
#if defined(SWIFT_SUPPORT) || defined(TARGET_RISCV64)
// Reassemble split struct parameters on the local stack frame in the
// scratch BB right after the prolog. There can be arbitrary amounts of
// codegen related to doing this, so it cannot be done in the prolog.
if (compiler->fgBBisScratch(block) && compiler->lvaHasAnySwiftStackParamToReassemble())
if (compiler->fgBBisScratch(block) && compiler->lvaHasAnyStackParamToReassemble())
{
genHomeSwiftStructParameters(/* handleStack */ true);
genHomeSplitStructParameters(/* handleStack */ true);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4002,7 +4002,7 @@ class Compiler
void lvaClassifyParameterABI();

bool lvaInitSpecialSwiftParam(CORINFO_ARG_LIST_HANDLE argHnd, InitVarDscInfo* varDscInfo, CorInfoType type, CORINFO_CLASS_HANDLE typeHnd);
bool lvaHasAnySwiftStackParamToReassemble();
bool lvaHasAnyStackParamToReassemble();

var_types lvaGetActualType(unsigned lclNum);
var_types lvaGetRealType(unsigned lclNum);
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,9 +2296,9 @@ PhaseStatus Compiler::fgAddInternal()
madeChanges |= fgCreateFiltersForGenericExceptions();

// The backend requires a scratch BB into which it can safely insert a P/Invoke method prolog if one is
// required. Similarly, we need a scratch BB for poisoning and when we have Swift parameters to reassemble.
// required. Similarly, we need a scratch BB for poisoning and when we have split parameters to reassemble.
// Create it here.
if (compMethodRequiresPInvokeFrame() || compShouldPoisonFrame() || lvaHasAnySwiftStackParamToReassemble())
if (compMethodRequiresPInvokeFrame() || compShouldPoisonFrame() || lvaHasAnyStackParamToReassemble())
{
madeChanges |= fgEnsureFirstBBisScratch();
fgFirstBB->SetFlags(BBF_DONT_REMOVE);
Expand Down
16 changes: 10 additions & 6 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1889,31 +1889,35 @@ void Compiler::lvaClassifyParameterABI()
}

//--------------------------------------------------------------------------------------------
// lvaHaveSwiftStructStackParamsToReassemble:
// Check if this compilation has any Swift parameters that are passed on the
// stack and that need to be reassembled on the local stack frame.
// lvaHasAnyStackParamToReassemble:
// Check if this compilation has any parameters split that need to be reassembled on the local stack frame.
//
// Return value:
// True if so.
//
bool Compiler::lvaHasAnySwiftStackParamToReassemble()
bool Compiler::lvaHasAnyStackParamToReassemble()
{
#if defined(SWIFT_SUPPORT) || defined(TARGET_RISCV64)
#ifdef SWIFT_SUPPORT
if (info.compCallConv != CorInfoCallConvExtension::Swift)
{
return false;
}
#endif

for (unsigned lclNum = 0; lclNum < info.compArgsCount; lclNum++)
{
const ABIPassingInformation& abiInfo = lvaGetParameterABIInfo(lclNum);
#ifdef SWIFT_SUPPORT
if (abiInfo.HasAnyStackSegment() && !abiInfo.HasExactlyOneStackSegment())
#else // TARGET_RISCV64
if (abiInfo.IsSplitAcrossRegistersAndStack())
#endif
{
return true;
}
}
#endif

#endif // defined(SWIFT_SUPPORT) || defined(TARGET_RISCV64)
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/jit/lsrabuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2542,12 +2542,12 @@ void LinearScan::buildIntervals()
// assert(block->isRunRarely());
}

// For Swift calls there can be an arbitrary amount of codegen related
// For Swift and RISC-V calls there can be an arbitrary amount of codegen related
// to homing of decomposed struct parameters passed on stack. We cannot
// do that in the prolog. We handle registers in the prolog and the
// stack args in the scratch BB that we have ensured exists. The
// handling clobbers REG_SCRATCH, so kill it here.
if ((block == compiler->fgFirstBB) && compiler->lvaHasAnySwiftStackParamToReassemble())
if ((block == compiler->fgFirstBB) && compiler->lvaHasAnyStackParamToReassemble())
{
assert(compiler->fgFirstBBisScratch());
addRefsForPhysRegMask(genRegMask(REG_SCRATCH), currentLoc + 1, RefTypeKill, true);
Expand Down

0 comments on commit 06e9d65

Please sign in to comment.