Skip to content

Commit

Permalink
JIT: Disallow implicit byref args and return buffers from aliasing (#…
Browse files Browse the repository at this point in the history
…104616)

The recent work to allow more retbuf definitions uncovered cases where
we would end up with the retbuffer aliasing an implicit byref argument.
The JIT does not handle this aliasing, which would require proper
`GTF_GLOB_REF` flags on the local nodes accesses implicit byrefs. Fix
the problem by disallowing last-use copy elision when the local would
alias the return buffer.

Fix #104613
  • Loading branch information
jakobbotsch committed Jul 10, 2024
1 parent d586986 commit 6c55362
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/coreclr/jit/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17974,11 +17974,14 @@ GenTreeLclVarCommon* GenTree::IsImplicitByrefParameterValuePreMorph(Compiler* co
// compiler - compiler instance
// addr - [out] tree representing the address computation on top of the implicit byref.
// Will be the same as the return value if the whole implicit byref is used, for example.
// offset - [out] Offset that "addr" is adding on top of the returned local.
//
// Return Value:
// Node for the local, or nullptr.
//
GenTreeLclVar* GenTree::IsImplicitByrefParameterValuePostMorph(Compiler* compiler, GenTree** addr)
GenTreeLclVar* GenTree::IsImplicitByrefParameterValuePostMorph(Compiler* compiler,
GenTree** addr,
target_ssize_t* offset)
{
#if FEATURE_IMPLICIT_BYREFS && !defined(TARGET_LOONGARCH64) // TODO-LOONGARCH64-CQ: enable this.

Expand All @@ -17987,13 +17990,10 @@ GenTreeLclVar* GenTree::IsImplicitByrefParameterValuePostMorph(Compiler* compile
return nullptr;
}

*addr = AsIndir()->Addr();
GenTree* innerAddr = *addr;
*addr = AsIndir()->Addr();

while (innerAddr->OperIs(GT_ADD) && innerAddr->gtGetOp2()->IsCnsIntOrI())
{
innerAddr = innerAddr->gtGetOp1();
}
GenTree* innerAddr = *addr;
compiler->gtPeelOffsets(&innerAddr, offset);

if (innerAddr->OperIs(GT_LCL_VAR))
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,7 @@ struct GenTree
unsigned* pSize = nullptr);

GenTreeLclVarCommon* IsImplicitByrefParameterValuePreMorph(Compiler* compiler);
GenTreeLclVar* IsImplicitByrefParameterValuePostMorph(Compiler* compiler, GenTree** addr);
GenTreeLclVar* IsImplicitByrefParameterValuePostMorph(Compiler* compiler, GenTree** addr, target_ssize_t* offset);

unsigned IsLclVarUpdateTree(GenTree** otherTree, genTreeOps* updateOper);

Expand Down
29 changes: 27 additions & 2 deletions src/coreclr/jit/morph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3157,13 +3157,15 @@ void Compiler::fgMakeOutgoingStructArgCopy(GenTreeCall* call, CallArg* arg)
if (opts.OptimizationEnabled() && arg->AbiInfo.PassedByRef)
{
GenTree* implicitByRefLclAddr;
target_ssize_t implicitByRefLclOffs;
GenTreeLclVarCommon* implicitByRefLcl =
argx->IsImplicitByrefParameterValuePostMorph(this, &implicitByRefLclAddr);
argx->IsImplicitByrefParameterValuePostMorph(this, &implicitByRefLclAddr, &implicitByRefLclOffs);

GenTreeLclVarCommon* lcl = implicitByRefLcl;
if ((lcl == nullptr) && argx->OperIsLocal())
{
lcl = argx->AsLclVarCommon();
lcl = argx->AsLclVarCommon();
implicitByRefLclOffs = lcl->GetLclOffs();
}

if (lcl != nullptr)
Expand Down Expand Up @@ -3191,6 +3193,28 @@ void Compiler::fgMakeOutgoingStructArgCopy(GenTreeCall* call, CallArg* arg)
!varDsc->lvPromoted && !varDsc->lvIsStructField && ((lcl->gtFlags & GTF_VAR_DEATH) != 0);
}

// Disallow the argument from potentially aliasing the return
// buffer.
if (omitCopy)
{
GenTreeLclVarCommon* retBuffer = gtCallGetDefinedRetBufLclAddr(call);
if ((retBuffer != nullptr) && (retBuffer->GetLclNum() == varNum))
{
unsigned retBufferSize = typGetObjLayout(call->gtRetClsHnd)->GetSize();
target_ssize_t retBufferStart = retBuffer->GetLclOffs();
target_ssize_t retBufferEnd = retBufferStart + static_cast<target_ssize_t>(retBufferSize);

unsigned argSize = arg->GetSignatureType() == TYP_STRUCT
? typGetObjLayout(arg->GetSignatureClassHandle())->GetSize()
: genTypeSize(arg->GetSignatureType());
target_ssize_t implByrefStart = implicitByRefLclOffs;
target_ssize_t implByrefEnd = implByrefStart + static_cast<target_ssize_t>(argSize);

bool disjoint = (retBufferEnd <= implByrefStart) || (implByrefEnd <= retBufferStart);
omitCopy = disjoint;
}
}

if (omitCopy)
{
if (implicitByRefLcl != nullptr)
Expand Down Expand Up @@ -3218,6 +3242,7 @@ void Compiler::fgMakeOutgoingStructArgCopy(GenTreeCall* call, CallArg* arg)
#endif

JITDUMP("making an outgoing copy for struct arg\n");
assert(!call->IsTailCall() || !arg->AbiInfo.PassedByRef);

CORINFO_CLASS_HANDLE copyBlkClass = arg->GetSignatureClassHandle();
unsigned tmp = 0;
Expand Down

0 comments on commit 6c55362

Please sign in to comment.