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

Stack allocate unescaped boxes #103361

Merged
merged 50 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e333c8f
wip
AndyAyersMS May 20, 2021
5deb676
post rebase fixes
AndyAyersMS Jun 11, 2024
877927a
wip
AndyAyersMS Jun 11, 2024
0a7f3d4
revert layout changes, rely on KVP instead
AndyAyersMS Jun 11, 2024
56500c3
trying to enable promotion
AndyAyersMS Jun 12, 2024
f9e6e47
Small fixes
jakobbotsch Jun 12, 2024
67402d4
Update src/coreclr/jit/lclmorph.cpp
jakobbotsch Jun 12, 2024
5a08c84
Merge pull request #6 from jakobbotsch/stack-allocate-unescaped-boxes…
AndyAyersMS Jun 12, 2024
350d7ca
format
AndyAyersMS Jun 12, 2024
0328665
avoid escaping nullcheck addrs
AndyAyersMS Jun 12, 2024
315916d
fix nullcheck handling
AndyAyersMS Jun 13, 2024
91b83d3
fix colon retyping
AndyAyersMS Jun 13, 2024
bcc7c3a
metrics, enable for ref classes too
AndyAyersMS Jun 14, 2024
20a83b4
fix object stack allocation test
AndyAyersMS Jun 14, 2024
be657eb
merge main
AndyAyersMS Jun 14, 2024
d9d299c
handle not getting a class handle for a box
AndyAyersMS Jun 14, 2024
03ae7ee
Merge branch 'main' into StackAllocateUnescapedBoxes
AndyAyersMS Jun 15, 2024
cc1037e
don't recycle packet number
AndyAyersMS Jun 15, 2024
3964a09
fix nullcheck exposure in local morph
AndyAyersMS Jun 16, 2024
b1c986d
allow promotion of address-exposed stack allocated boxes
AndyAyersMS Jun 17, 2024
ed41d78
unbox helper only captures type
AndyAyersMS Jun 18, 2024
810abdf
merge main
AndyAyersMS Jun 18, 2024
536b2ae
restore lost guid update and related
AndyAyersMS Jun 18, 2024
5e9e3fb
boost inlining more if we pass a box as an arg
AndyAyersMS Jun 18, 2024
0685c58
try and fix promotion to write back if there are helper call struct uses
AndyAyersMS Jun 18, 2024
07f68ee
fix physical promotion writeback logic
AndyAyersMS Jun 19, 2024
2ae83e6
fix layout access for regular case
AndyAyersMS Jun 19, 2024
1cd1022
handle more address comparisons in local morph
AndyAyersMS Jun 19, 2024
40bb560
unbox type test helper
AndyAyersMS Jun 19, 2024
2beb6b3
rewrite unbox helper calls if we stack allocate the box
AndyAyersMS Jun 19, 2024
3522738
undo hacky changes to promotion
AndyAyersMS Jun 19, 2024
d88b4a9
more extensive rewriting -- anything that might be a stack allocated box
AndyAyersMS Jun 20, 2024
9c7885d
allow method table access for stack allocated objects
AndyAyersMS Jun 20, 2024
93a3f1d
Merge branch 'main' into StackAllocateUnescapedBoxes_UnboxHelper
AndyAyersMS Jun 20, 2024
06cbf8b
do indir/comma swapping in object allocator
AndyAyersMS Jun 20, 2024
946c780
fall back to relying on (lack of) exposure
AndyAyersMS Jun 21, 2024
e63a883
fix type test helper, add range control
AndyAyersMS Jun 21, 2024
41ea7bd
fix unbox helper re-write
AndyAyersMS Jun 22, 2024
337fc7e
need more capable clone
AndyAyersMS Jun 22, 2024
6cdefdf
report all object refs as byrefs for OSR (no retyping to nint)
AndyAyersMS Jun 23, 2024
2c3785e
disable ref class stack allocation for r2r/naot for now
AndyAyersMS Jun 23, 2024
7ca7027
minor TP improvement
AndyAyersMS Jun 24, 2024
351d484
dial back inliner
AndyAyersMS Jun 24, 2024
ccad461
fix spmi capture of ref class gclayout
AndyAyersMS Jun 26, 2024
14d2455
Use StackAllocatedBox<T>
AndyAyersMS Jun 26, 2024
8a077ea
Apply suggestions from code review
AndyAyersMS Jun 27, 2024
dc873e5
more review feedback
AndyAyersMS Jun 28, 2024
2597a44
fix
AndyAyersMS Jun 28, 2024
504012d
fix typo in comment
AndyAyersMS Jun 28, 2024
7af3d98
more surgical sibling type update for qmark/colon
AndyAyersMS Jun 28, 2024
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
8 changes: 8 additions & 0 deletions src/coreclr/inc/corinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,14 @@ class ICorStaticInfo
CORINFO_CLASS_HANDLE cls
) = 0;

// Get a representation for a stack-allocated boxed value type.
//
// This differs from getTypeForBox in that it includes an explicit field
// for the method table pointer.
virtual CORINFO_CLASS_HANDLE getTypeForBoxOnStack(
CORINFO_CLASS_HANDLE cls
) = 0;

// returns the correct box helper for a particular class. Note
// that if this returns CORINFO_HELP_BOX, the JIT can assume
// 'standard' boxing (allocate object and copy), and optimize
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/inc/icorjitinfoimpl_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ CorInfoHelpFunc getSharedCCtorHelper(
CORINFO_CLASS_HANDLE getTypeForBox(
CORINFO_CLASS_HANDLE cls) override;

CORINFO_CLASS_HANDLE getTypeForBoxOnStack(
CORINFO_CLASS_HANDLE cls) override;

CorInfoHelpFunc getBoxHelper(
CORINFO_CLASS_HANDLE cls) override;

Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/inc/jiteeversionguid.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ typedef const GUID *LPCGUID;
#define GUID_DEFINED
#endif // !GUID_DEFINED

constexpr GUID JITEEVersionIdentifier = { /* 6e0b439f-0d18-4836-a486-4962af0cc948 */
0x6e0b439f,
0x0d18,
0x4836,
{0xa4, 0x86, 0x49, 0x62, 0xaf, 0x0c, 0xc9, 0x48}
constexpr GUID JITEEVersionIdentifier = { /* 51839df0-d82f-4bb6-8bdb-66bd2813082c */
0x51839df0,
0xd82f,
0x4bb6,
{0x8b, 0xdb, 0x66, 0xbd, 0x28, 0x13, 0x08, 0x2c}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/ICorJitInfo_names_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ DEF_CLR_API(getNewArrHelper)
DEF_CLR_API(getCastingHelper)
DEF_CLR_API(getSharedCCtorHelper)
DEF_CLR_API(getTypeForBox)
DEF_CLR_API(getTypeForBoxOnStack)
DEF_CLR_API(getBoxHelper)
DEF_CLR_API(getUnBoxHelper)
DEF_CLR_API(getRuntimeTypePointer)
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/jit/ICorJitInfo_wrapper_generated.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,15 @@ CORINFO_CLASS_HANDLE WrapICorJitInfo::getTypeForBox(
return temp;
}

CORINFO_CLASS_HANDLE WrapICorJitInfo::getTypeForBoxOnStack(
CORINFO_CLASS_HANDLE cls)
{
API_ENTER(getTypeForBoxOnStack);
CORINFO_CLASS_HANDLE temp = wrapHnd->getTypeForBoxOnStack(cls);
API_LEAVE(getTypeForBoxOnStack);
return temp;
}

CorInfoHelpFunc WrapICorJitInfo::getBoxHelper(
CORINFO_CLASS_HANDLE cls)
{
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ class LclVarDsc
unsigned char lvSingleDefDisqualifyReason = 'H';
#endif

unsigned char lvAllDefsAreNoGc : 1; // For pinned locals: true if all defs of this local are no-gc
unsigned char lvAllDefsAreNoGc : 1; // For pinned locals: true if all defs of this local are no-gc
unsigned char lvStackAllocatedBox : 1; // Local is a stack allocated box

#if FEATURE_MULTIREG_ARGS
regNumber lvRegNumForSlot(unsigned slotNum)
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/jit/jitconfigvalues.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,9 @@ RELEASE_CONFIG_INTEGER(JitExtDefaultPolicyProfScale, W("JitExtDefaultPolicyProfS
RELEASE_CONFIG_INTEGER(JitInlinePolicyModel, W("JitInlinePolicyModel"), 0)
RELEASE_CONFIG_INTEGER(JitInlinePolicyProfile, W("JitInlinePolicyProfile"), 0)
RELEASE_CONFIG_INTEGER(JitInlinePolicyProfileThreshold, W("JitInlinePolicyProfileThreshold"), 40)
RELEASE_CONFIG_INTEGER(JitObjectStackAllocation, W("JitObjectStackAllocation"), 0)
RELEASE_CONFIG_INTEGER(JitObjectStackAllocation, W("JitObjectStackAllocation"), 1)
RELEASE_CONFIG_INTEGER(JitObjectStackAllocationRefClass, W("JitObjectStackAllocationRefClass"), 0)
RELEASE_CONFIG_INTEGER(JitObjectStackAllocationBoxedValueClass, W("JitObjectStackAllocationBoxedValueClass"), 1)

RELEASE_CONFIG_INTEGER(JitEECallTimingInfo, W("JitEECallTimingInfo"), 0)

Expand Down
7 changes: 4 additions & 3 deletions src/coreclr/jit/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,15 @@ void ClassLayout::InitializeGCPtrs(Compiler* compiler)
gcPtrs = m_gcPtrsArray;
}

// The gcPtrs array will have the right size for boxed value classes, but all
// entries will be shifted down one slot to match the unboxed rep.
//
unsigned gcPtrCount = compiler->info.compCompHnd->getClassGClayout(m_classHandle, gcPtrs);

assert((gcPtrCount == 0) || ((compiler->info.compCompHnd->getClassAttribs(m_classHandle) &
(CORINFO_FLG_CONTAINS_GC_PTR | CORINFO_FLG_BYREF_LIKE)) != 0));

// Since class size is unsigned there's no way we could have more than 2^30 slots
// so it should be safe to fit this into a 30 bits bit field.
assert(gcPtrCount < (1 << 30));
assert(gcPtrCount < (1 << 24));

m_gcPtrCount = gcPtrCount;
}
Expand Down
55 changes: 54 additions & 1 deletion src/coreclr/jit/lclmorph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ class LocalEqualsLocalAddrAssertions
//
void OnExposed(unsigned lclNum)
{
JITDUMP("On exposed: V%02u\n", lclNum);
BitVecTraits localsTraits(m_comp->lvaCount, m_comp);
BitVecOps::AddElemD(&localsTraits, m_localsToExpose, lclNum);
}
Expand Down Expand Up @@ -802,7 +803,6 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
}

PushValue(use);

return Compiler::WALK_CONTINUE;
}

Expand Down Expand Up @@ -1024,6 +1024,59 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>
SequenceCall(node->AsCall());
break;

case GT_EQ:
case GT_NE:
{
// If we see &lcl EQ/NE null, rewrite to 0/1 comparison
// to reduce overall address exposure.
//
assert(TopValue(2).Node() == node);
assert(TopValue(1).Node() == node->AsOp()->gtOp1);
assert(TopValue(0).Node() == node->AsOp()->gtOp2);

Value& lhs = TopValue(1);
Value& rhs = TopValue(0);

if ((lhs.IsAddress() && rhs.Node()->IsIntegralConst(0)) ||
(rhs.IsAddress() && lhs.Node()->IsIntegralConst(0)))
{
JITDUMP("Rewriting known address-of comparison [%06u]\n", m_compiler->dspTreeID(node));
*lhs.Use() = m_compiler->gtNewIconNode(0);
*rhs.Use() = m_compiler->gtNewIconNode(1);
m_stmtModified = true;

INDEBUG(TopValue(0).Consume());
INDEBUG(TopValue(1).Consume());
PopValue();
PopValue();
}
else
{
EscapeValue(TopValue(0), node);
PopValue();
EscapeValue(TopValue(0), node);
PopValue();
}

break;
}

case GT_NULLCHECK:
{
assert(TopValue(1).Node() == node);
assert(TopValue(0).Node() == node->AsOp()->gtOp1);
Value& op = TopValue(0);
if (op.IsAddress())
{
JITDUMP("Bashing nullcheck of local [%06u] to NOP\n", m_compiler->dspTreeID(node));
node->gtBashToNOP();
m_stmtModified = true;
}
INDEBUG(TopValue(0).Consume());
PopValue();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to call EscapeValue in the non-transformed case (that might be the reason for the asserts)

break;
}

default:
while (TopValue(0).Node() != node)
{
Expand Down
9 changes: 9 additions & 0 deletions src/coreclr/jit/lclvars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,13 @@ bool Compiler::StructPromotionHelper::CanPromoteStructVar(unsigned lclNum)

if (varDsc->GetLayout()->IsBlockLayout())
{
JITDUMP(" struct promotion of V%02u is disabled because it has block layout\n", lclNum);
return false;
}

if (varDsc->lvStackAllocatedBox)
{
JITDUMP(" struct promotion of V%02u is disabled because it is a stack allocated box\n", lclNum);
return false;
}

Expand Down Expand Up @@ -3334,6 +3341,8 @@ bool Compiler::lvaIsLocalImplicitlyAccessedByRef(unsigned lclNum) const
// this information is already available on the CallArgABIInformation, and shouldn't need to be
// recomputed.
//
// Also seems like this info could be cached in the layout.
//
bool Compiler::lvaIsMultiregStruct(LclVarDsc* varDsc, bool isVarArg)
{
if (varTypeIsStruct(varDsc->TypeGet()))
Expand Down
Loading
Loading