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: Extend escape analysis to account for arrays with non-gcref elements #104906

Open
wants to merge 78 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
1b0e3d3
initial prototype
hez2010 Jul 15, 2024
57b7e42
Morph ARR_LENGTH and INDEX_ADDR
hez2010 Jul 15, 2024
1b5b25e
Fix incorrect array length storage
hez2010 Jul 15, 2024
395b735
Use offset and correct type
hez2010 Jul 15, 2024
17de70b
handle reassignment
hez2010 Jul 15, 2024
5443c42
range check
hez2010 Jul 15, 2024
b2d07da
throw range check failure
hez2010 Jul 15, 2024
b5ae9e7
update comments
hez2010 Jul 15, 2024
87b29de
add metrics
hez2010 Jul 15, 2024
eeb681d
minor cleanup
hez2010 Jul 15, 2024
dee9f38
Introduce new temp and implement local address morphing
hez2010 Jul 16, 2024
94c103b
handle index out-of-range
hez2010 Jul 16, 2024
12b297b
Refactor to remove duplicates
hez2010 Jul 16, 2024
e0fa91e
Remove invalid asserts
hez2010 Jul 16, 2024
9e0a04f
make compiler happy
hez2010 Jul 16, 2024
ae822f8
Address review feedbacks
hez2010 Jul 16, 2024
a4588bb
Fix INDEX_ADDR and add Sub
hez2010 Jul 16, 2024
32b9e26
Support IsAddressLessThan and its friends
hez2010 Jul 16, 2024
39d1ad9
Fix assertions
hez2010 Jul 16, 2024
0df0d58
Merge remote-tracking branch 'origin/main' into value-array-stack-alloc
hez2010 Jul 16, 2024
9f408b2
Use new overload
hez2010 Jul 16, 2024
418a62b
JIT: Remove GTF_IND_INVARIANT and GTF_IND_NONFAULTING flags checking
jakobbotsch Jul 16, 2024
4572408
Remove old comment
jakobbotsch Jul 16, 2024
9255762
Expose jitconfig
hez2010 Jul 16, 2024
1af84b9
Remove another assert
jakobbotsch Jul 16, 2024
629c793
Count
jakobbotsch Jul 16, 2024
b578203
Try 2 at counting
jakobbotsch Jul 16, 2024
b4445f6
Introduce BBF_HAS_NEWARR
hez2010 Jul 16, 2024
af9c40e
Early exit on debug as well
hez2010 Jul 16, 2024
8b54f5a
Update computed flags
hez2010 Jul 16, 2024
cba4d2c
Merge remote-tracking branch 'jakobbotsch/indir-flags' into value-arr…
hez2010 Jul 17, 2024
b497fae
Merge remote-tracking branch 'origin/main' into value-array-stack-alloc
hez2010 Jul 17, 2024
6eca58d
Partially revert 39d1ad9
hez2010 Jul 17, 2024
49d8509
Reuse existing comma node
hez2010 Jul 17, 2024
4c6e359
Respect IsBoundsChecked
hez2010 Jul 17, 2024
4d84379
Check lowerbound too
hez2010 Jul 17, 2024
c0cad85
Fix assertion take 2
hez2010 Jul 18, 2024
d28553a
Remove redundant jit-ee calls
hez2010 Jul 18, 2024
f42e78f
Merge branch 'main' into value-array-stack-alloc
hez2010 Jul 18, 2024
c21c4f7
Fix assertion again
hez2010 Jul 18, 2024
18ec558
Check array length
hez2010 Jul 18, 2024
eadb4ad
Fix assertion in another way
hez2010 Jul 18, 2024
9d4021c
Unset the flag to avoid unnecessary assert
hez2010 Jul 18, 2024
1cba8c0
Merge branch 'main' into value-array-stack-alloc
hez2010 Jul 19, 2024
1fff53e
Add tests
hez2010 Jul 19, 2024
d521a94
sigh
hez2010 Jul 19, 2024
97ee2be
Support R2R/NativeAOT
hez2010 Jul 19, 2024
c66bdb8
Merge branch 'main' into value-array-stack-alloc
hez2010 Jul 19, 2024
5bcb786
Fix building
hez2010 Jul 19, 2024
a01562e
cleanup
hez2010 Jul 19, 2024
e728d4f
remove invalid assert
hez2010 Jul 19, 2024
d73c5c5
double align on 32bit platform
hez2010 Jul 19, 2024
c9fea23
Use correct alignment for align8
hez2010 Jul 19, 2024
772bee6
Fix intrinsic expansion
hez2010 Jul 20, 2024
499ac0b
Merge branch 'main' into value-array-stack-alloc
hez2010 Jul 20, 2024
ffcbd85
Merge remote-tracking branch 'origin/main' into value-array-stack-alloc
hez2010 Jul 22, 2024
4789090
Merge branch 'main' into value-array-stack-alloc
hez2010 Aug 24, 2024
1b33170
Merge branch 'main' into value-array-stack-alloc
hez2010 Sep 20, 2024
9c81c04
Address some review feedback
hez2010 Sep 21, 2024
ed6c83c
Merge branch 'main' into value-array-stack-alloc
hez2010 Oct 4, 2024
6a7e6cb
Revert workaround to fgMorphSmpOp
hez2010 Oct 4, 2024
e4d91ed
Merge branch 'main' into value-array-stack-alloc
hez2010 Nov 26, 2024
aeeb9e3
Fix build failure
hez2010 Nov 26, 2024
0e2a01d
Merge branch 'main' into value-array-stack-alloc
hez2010 Dec 6, 2024
1914e80
Try fixing remorph issue
hez2010 Dec 6, 2024
f671085
Minimize asmdiff for tier-0
hez2010 Dec 6, 2024
d4fd6ae
Check CI
hez2010 Dec 6, 2024
b3ff72d
Take INDEX as non-escape
hez2010 Dec 7, 2024
49c39b9
Early exit when optimization is disabled
hez2010 Dec 7, 2024
a696f87
Format
hez2010 Dec 7, 2024
ce2de99
Merge branch 'main' into value-array-stack-alloc
hez2010 Dec 14, 2024
cc88979
Revert "Try fixing remorph issue"
hez2010 Dec 14, 2024
8d38e0c
Merge branch 'main' into value-array-stack-alloc
AndyAyersMS Dec 18, 2024
52b32f2
Introduce a flag for stack allocated arrays
hez2010 Dec 20, 2024
1f342bd
Handle in global morph
hez2010 Dec 21, 2024
5b51919
Some special cases
hez2010 Dec 21, 2024
ce41a8d
Avoid extensive remorph
hez2010 Dec 21, 2024
47114c6
Oops
hez2010 Dec 21, 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
3 changes: 1 addition & 2 deletions src/coreclr/jit/gentree.h
Original file line number Diff line number Diff line change
Expand Up @@ -7458,12 +7458,11 @@ struct GenTreeArrAddr : GenTreeUnOp

public:
GenTreeArrAddr(GenTree* addr, var_types elemType, CORINFO_CLASS_HANDLE elemClassHandle, uint8_t firstElemOffset)
: GenTreeUnOp(GT_ARR_ADDR, TYP_BYREF, addr DEBUGARG(/* largeNode */ false))
: GenTreeUnOp(GT_ARR_ADDR, addr->TypeGet(), addr DEBUGARG(/* largeNode */ false))
, m_elemClassHandle(elemClassHandle)
, m_elemType(elemType)
, m_firstElemOffset(firstElemOffset)
{
assert(addr->TypeIs(TYP_BYREF));
assert(((elemType == TYP_STRUCT) && (elemClassHandle != NO_CLASS_HANDLE)) ||
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
(elemClassHandle == NO_CLASS_HANDLE));
}
Expand Down
15 changes: 14 additions & 1 deletion src/coreclr/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9533,9 +9533,22 @@ void Compiler::impImportBlockCode(BasicBlock* block)
// Remember that this function contains 'new' of an SD array.
optMethodFlags |= OMF_HAS_NEWARRAY;

// We assign the newly allocated object (by a GT_CALL to newarr node)
// to a temp. Note that the pattern "temp = allocArr" is required
// by ObjectAllocator phase to be able to determine newarr nodes
// without exhaustive walk over all expressions.
lclNum = lvaGrabTemp(true DEBUGARG("NewArr temp"));

impStoreToTemp(lclNum, op1, CHECK_SPILL_NONE);

assert(lvaTable[lclNum].lvSingleDef == 0);
lvaTable[lclNum].lvSingleDef = 1;
JITDUMP("Marked V%02u as a single def local\n", lclNum);
lvaSetClass(lclNum, resolvedToken.hClass, true /* is Exact */);

/* Push the result of the call on the stack */

impPushOnStack(op1, tiRetVal);
impPushOnStack(gtNewLclvNode(lclNum, TYP_REF), tiRetVal);

callTyp = TYP_REF;
}
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/jitmetadatalist.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ JITMETADATAMETRIC(NewRefClassHelperCalls, int, 0)
JITMETADATAMETRIC(StackAllocatedRefClasses, int, 0)
JITMETADATAMETRIC(NewBoxedValueClassHelperCalls, int, 0)
JITMETADATAMETRIC(StackAllocatedBoxedValueClasses, int, 0)
JITMETADATAMETRIC(NewArrayHelperCalls, int, 0)
JITMETADATAMETRIC(StackAllocatedArrays, int, 0)

#undef JITMETADATA
#undef JITMETADATAINFO
Expand Down
68 changes: 68 additions & 0 deletions src/coreclr/jit/lclmorph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,74 @@ class LocalAddressVisitor final : public GenTreeVisitor<LocalAddressVisitor>

switch (node->OperGet())
{
case GT_INDEX_ADDR:
{
assert(TopValue(2).Node() == node);
assert(TopValue(1).Node() == node->gtGetOp1());
assert(TopValue(0).Node() == node->gtGetOp2());

if (node->gtGetOp1()->OperIs(GT_LCL_VAR, GT_LCL_ADDR) && node->gtGetOp2()->IsCnsIntOrI())
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
if (TopValue(1).IsAddress() &&
m_compiler->lvaGetDesc(TopValue(1).LclNum())->GetLayout()->IsBlockLayout())
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
ssize_t offset = node->AsIndexAddr()->gtElemOffset +
node->gtGetOp2()->AsIntCon()->IconValue() * node->AsIndexAddr()->gtElemSize;

if (offset < static_cast<ssize_t>(m_compiler->lvaLclSize(TopValue(1).LclNum())))
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
if (FitsIn<unsigned>(offset) &&
TopValue(2).AddOffset(TopValue(1), static_cast<unsigned>(offset)))
{
INDEBUG(TopValue(0).Consume());
PopValue();
PopValue();
break;
}
}
else
{
GenTree* gtThrow =
m_compiler->gtNewMustThrowException(CORINFO_HELP_RNGCHKFAIL, node->TypeGet(),
node->AsIndexAddr()->gtStructElemClass);
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
m_compiler->lvaGetDesc(gtThrow->AsOp()->gtOp2->AsLclVarCommon())->incLvRefCnt(1, RCS_EARLY);
*use = gtThrow;
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
m_stmtModified = true;
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

EscapeValue(TopValue(0), node);
PopValue();
EscapeValue(TopValue(0), node);
PopValue();
break;
}
case GT_ARR_LENGTH:
{
assert(TopValue(1).Node() == node);
assert(TopValue(0).Node() == node->gtGetOp1());

if (node->gtGetOp1()->OperIs(GT_LCL_VAR, GT_LCL_ADDR))
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
if (TopValue(0).IsAddress() &&
m_compiler->lvaGetDesc(TopValue(0).LclNum())->GetLayout()->IsBlockLayout())
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
GenTree* gtLclFld =
m_compiler->gtNewLclFldNode(TopValue(0).LclNum(), TYP_INT, OFFSETOF__CORINFO_Array__length);
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
SequenceLocal(gtLclFld->AsLclVarCommon());
*use = gtLclFld;
m_stmtModified = true;
INDEBUG(TopValue(0).Consume());
PopValue();
break;
}
}

EscapeValue(TopValue(0), node);
PopValue();
break;
}
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
case GT_STORE_LCL_FLD:
if (node->IsPartialLclFld(m_compiler))
{
Expand Down
Loading
Loading