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

Gc arrays #111686

Draft
wants to merge 99 commits into
base: main
Choose a base branch
from
Draft

Gc arrays #111686

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 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
66e98cd
Merge branch 'main' into value-array-stack-alloc
AndyAyersMS Jan 9, 2025
de30bdc
Alternative take on array stack allocation
AndyAyersMS Jan 9, 2025
5137a5c
basic VN support
AndyAyersMS Jan 10, 2025
315f884
Merge branch 'main' into value-array-stack-alloc
AndyAyersMS Jan 10, 2025
aa09187
restore complexity change
AndyAyersMS Jan 11, 2025
c451435
pass address of stack local to new helper
AndyAyersMS Jan 14, 2025
18d3677
Merge branch 'main' into value-array-stack-alloc-alt
AndyAyersMS Jan 14, 2025
f6b012a
temp hack to boost SPMI coverage
AndyAyersMS Jan 15, 2025
a6e7bd5
avoid pessimizing tail calls. implement configurable size limit
AndyAyersMS Jan 15, 2025
b3edc07
add missing well known arg string
AndyAyersMS Jan 15, 2025
9b63e2d
fix array length check
AndyAyersMS Jan 16, 2025
e535657
use ClrSafeInt directly
AndyAyersMS Jan 17, 2025
07cd310
bypass for R2R for now, since it may inhibit prejitting
AndyAyersMS Jan 17, 2025
2e2bdf6
Merge branch 'main' into value-array-stack-alloc
AndyAyersMS Jan 17, 2025
7f95162
Merge remote-tracking branch 'hez/value-array-stack-alloc' into value…
AndyAyersMS Jan 17, 2025
1915450
Merge branch 'main' into value-array-stack-alloc
hez2010 Jan 18, 2025
210f009
Merge branch 'main' into value-array-stack-alloc
AndyAyersMS Jan 20, 2025
71ebf24
revise helper expansion
AndyAyersMS Jan 21, 2025
97eba58
revise remainder
AndyAyersMS Jan 21, 2025
860f552
JIT: support stack allocation of fixed-sized arrays with GC elements
AndyAyersMS Jan 16, 2025
dfbc3e0
merge main
AndyAyersMS Jan 22, 2025
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
4 changes: 4 additions & 0 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11056,6 +11056,10 @@ class Compiler
ClassLayout* typGetObjLayout(CORINFO_CLASS_HANDLE classHandle);
// Get the number of a layout for the specified class handle.
unsigned typGetObjLayoutNum(CORINFO_CLASS_HANDLE classHandle);
// Get the layout for the specified array of known length
ClassLayout* typGetArrayLayout(CORINFO_CLASS_HANDLE classHandle, unsigned length);
// Get the number of a layout for the specified array of known length
unsigned typGetArrayLayoutNum(CORINFO_CLASS_HANDLE classHandle, unsigned length);

var_types TypeHandleToVarType(CORINFO_CLASS_HANDLE handle, ClassLayout** pLayout = nullptr);
var_types TypeHandleToVarType(CorInfoType jitType, CORINFO_CLASS_HANDLE handle, ClassLayout** pLayout = nullptr);
Expand Down
236 changes: 236 additions & 0 deletions src/coreclr/jit/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,38 @@ class ClassLayoutTable
static constexpr unsigned ZeroSizedBlockLayoutNum = TYP_UNKNOWN + 1;
static constexpr unsigned FirstLayoutNum = TYP_UNKNOWN + 2;

struct ArrKey
{
CORINFO_CLASS_HANDLE m_arrayType;
unsigned m_arrayLen;

ArrKey()
: m_arrayType(NO_CLASS_HANDLE)
, m_arrayLen(0)
{
}

ArrKey(CORINFO_CLASS_HANDLE hnd, unsigned len)
: m_arrayType(hnd)
, m_arrayLen(len)
{
}

static bool Equals(const ArrKey& x, const ArrKey& y)
{
return (x.m_arrayType == y.m_arrayType) && (x.m_arrayLen == y.m_arrayLen);
}

static unsigned GetHashCode(const ArrKey& x)
{
unsigned typeHash = static_cast<unsigned>(reinterpret_cast<uintptr_t>(x.m_arrayType));
return x.m_arrayLen ^ typeHash;
}
};

typedef JitHashTable<unsigned, JitSmallPrimitiveKeyFuncs<unsigned>, unsigned> BlkLayoutIndexMap;
typedef JitHashTable<CORINFO_CLASS_HANDLE, JitPtrKeyFuncs<CORINFO_CLASS_STRUCT_>, unsigned> ObjLayoutIndexMap;
typedef JitHashTable<ArrKey, ArrKey, unsigned> ArrLayoutIndexMap;

union
{
Expand All @@ -32,6 +62,7 @@ class ClassLayoutTable
ClassLayout** m_layoutLargeArray;
BlkLayoutIndexMap* m_blkLayoutMap;
ObjLayoutIndexMap* m_objLayoutMap;
ArrLayoutIndexMap* m_arrLayoutMap;
};
};
// The number of layout objects stored in this table.
Expand Down Expand Up @@ -109,6 +140,25 @@ class ClassLayoutTable
return GetObjLayoutIndex(compiler, classHandle) + FirstLayoutNum;
}

// Get the layout for an on-stack known-length array
ClassLayout* GetArrayLayout(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle, unsigned length)
{
CORINFO_CLASS_HANDLE elementHandle = NO_CLASS_HANDLE;
CorInfoType elementCorType = compiler->info.compCompHnd->getChildType(classHandle, &elementHandle);
var_types elementType = JITtype2varType(elementCorType);

return GetLayoutByIndex(GetArrayLayoutIndex(compiler, classHandle, elementType, elementHandle, length));
}

unsigned GetArrayLayoutNum(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle, unsigned length)
{
CORINFO_CLASS_HANDLE elementHandle = NO_CLASS_HANDLE;
CorInfoType elementCorType = compiler->info.compCompHnd->getChildType(classHandle, &elementHandle);
var_types elementType = JITtype2varType(elementCorType);

return GetArrayLayoutIndex(compiler, classHandle, elementType, elementHandle, length) + FirstLayoutNum;
}

private:
bool HasSmallCapacity() const
{
Expand Down Expand Up @@ -246,6 +296,62 @@ class ClassLayoutTable
return index;
}

ClassLayout* CreateArrayLayout(Compiler* compiler,
CORINFO_CLASS_HANDLE classHandle,
var_types elementType,
CORINFO_CLASS_HANDLE elementHandle,
unsigned elementCount)
{
return ClassLayout::CreateArray(compiler, classHandle, elementType, elementHandle, elementCount);
}

unsigned GetArrayLayoutIndex(Compiler* compiler,
CORINFO_CLASS_HANDLE classHandle,
var_types elementType,
CORINFO_CLASS_HANDLE elementHandle,
unsigned elementCount)
{
assert(classHandle != NO_CLASS_HANDLE);

if (HasSmallCapacity())
{
for (unsigned i = 0; i < m_layoutCount; i++)
{
if ((m_layoutArray[i]->GetClassHandle() == classHandle) &&
(m_layoutArray[i]->GetElementCount() == elementCount))
{
return i;
}
}
}
else
{
ArrKey key(classHandle, elementCount);
unsigned index;
if (m_arrLayoutMap->Lookup(key, &index))
{
return index;
}
}

return AddArrayLayout(compiler,
CreateArrayLayout(compiler, classHandle, elementType, elementHandle, elementCount));
}

unsigned AddArrayLayout(Compiler* compiler, ClassLayout* layout)
{
if (m_layoutCount < ArrLen(m_layoutArray))
{
m_layoutArray[m_layoutCount] = layout;
return m_layoutCount++;
}

unsigned index = AddLayoutLarge(compiler, layout);
ArrKey key(layout->GetClassHandle(), layout->GetElementCount());
m_arrLayoutMap->Set(key, index);
return index;
}

unsigned AddLayoutLarge(Compiler* compiler, ClassLayout* layout)
{
if (m_layoutCount >= m_layoutLargeCapacity)
Expand All @@ -258,6 +364,7 @@ class ClassLayoutTable
{
BlkLayoutIndexMap* blkLayoutMap = new (alloc) BlkLayoutIndexMap(alloc);
ObjLayoutIndexMap* objLayoutMap = new (alloc) ObjLayoutIndexMap(alloc);
ArrLayoutIndexMap* arrLayoutMap = new (alloc) ArrLayoutIndexMap(alloc);

for (unsigned i = 0; i < m_layoutCount; i++)
{
Expand All @@ -268,6 +375,11 @@ class ClassLayoutTable
{
blkLayoutMap->Set(l->GetSize(), i);
}
else if (l->IsArrayLayout())
{
ArrKey k(l->GetClassHandle(), l->GetElementCount());
arrLayoutMap->Set(k, i);
}
else
{
objLayoutMap->Set(l->GetClassHandle(), i);
Expand All @@ -276,6 +388,7 @@ class ClassLayoutTable

m_blkLayoutMap = blkLayoutMap;
m_objLayoutMap = objLayoutMap;
m_arrLayoutMap = arrLayoutMap;
}
else
{
Expand Down Expand Up @@ -354,6 +467,125 @@ ClassLayout* Compiler::typGetObjLayout(CORINFO_CLASS_HANDLE classHandle)
return typGetClassLayoutTable()->GetObjLayout(this, classHandle);
}

unsigned Compiler::typGetArrayLayoutNum(CORINFO_CLASS_HANDLE classHandle, unsigned length)
{
return typGetClassLayoutTable()->GetArrayLayoutNum(this, classHandle, length);
}

ClassLayout* Compiler::typGetArrayLayout(CORINFO_CLASS_HANDLE classHandle, unsigned length)
{
return typGetClassLayoutTable()->GetArrayLayout(this, classHandle, length);
}

ClassLayout* ClassLayout::CreateArray(Compiler* compiler,
CORINFO_CLASS_HANDLE classHandle,
var_types elemType,
CORINFO_CLASS_HANDLE elemHandle,
unsigned elemCount)
{
assert(elemCount <= CORINFO_Array_MaxLength);

unsigned elemLayoutIndex = 0;
ClassLayout* elemLayout = nullptr;
unsigned elemSize = 0;
unsigned elemGCPtrCount = 0;

if (elemType == TYP_STRUCT)
{
assert(elemHandle != NO_CLASS_HANDLE);
elemLayoutIndex = compiler->typGetObjLayoutNum(elemHandle);
elemLayout = compiler->typGetLayoutByNum(elemLayoutIndex);
elemSize = elemLayout->GetSize();
elemGCPtrCount = elemLayout->GetGCPtrCount();
}
else
{
// No byref arrays (yet)
//
assert(elemType != TYP_BYREF);
elemSize = genTypeSize(elemType);
if (varTypeIsGC(elemType))
{
elemGCPtrCount = 1;
}
}

// compute size
//
ClrSafeInt<unsigned> totalSize(elemSize);
totalSize *= static_cast<unsigned>(elemCount);
totalSize += static_cast<unsigned>(OFFSETOF__CORINFO_Array__data);

assert(!totalSize.IsOverflow());

INDEBUG(const char* className = compiler->eeGetClassName(classHandle));
INDEBUG(const char* shortClassName = compiler->eeGetShortClassName(classHandle));

// Note we bias elementCount up by 1 here, so that only layouts with nonzero element
// counts represent arrays.
//
ClassLayout* const layout =
new (compiler, CMK_ClassLayout) ClassLayout(classHandle, totalSize.Value(), elemCount + 1,
elemLayoutIndex DEBUGARG(className) DEBUGARG(shortClassName));

unsigned const arrayGCPtrCount = elemGCPtrCount * elemCount;

if (arrayGCPtrCount > 0)
{
assert((elemType == TYP_REF) || (elemLayout != nullptr));

unsigned const elemSlotCount = (elemLayout == nullptr) ? 1 : elemLayout->GetSlotCount();
unsigned const arraySlotCount = layout->GetSlotCount();
assert(arraySlotCount == 2 + elemSlotCount * elemCount);

BYTE* gcPtrs = nullptr;

if (arraySlotCount > sizeof(m_gcPtrsArray))
{
gcPtrs = layout->m_gcPtrs = new (compiler, CMK_ClassLayout) BYTE[arraySlotCount];
}
else
{
gcPtrs = layout->m_gcPtrsArray;
}

gcPtrs[0] = TYPE_GC_NONE;
gcPtrs[1] = TYPE_GC_NONE;

unsigned slot = 2;

for (unsigned i = 0; i < elemCount; i++)
{
for (unsigned j = 0; j < elemSlotCount; j++)
{
CorInfoGCType g = TYPE_GC_NONE;

if (elemLayout == nullptr)
{
g = TYPE_GC_REF;
}
else
{
g = elemLayout->GetGCPtr(j);
}

JITDUMP("Slot %u gc type %u\n", slot, g);
gcPtrs[slot++] = (BYTE)g;
}
}

assert(slot == arraySlotCount);
}

// 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(arrayGCPtrCount < (1 << 30));
layout->m_gcPtrCount = arrayGCPtrCount;
INDEBUG(layout->m_gcPtrsInitialized = true);

return layout;
}

ClassLayout* ClassLayout::Create(Compiler* compiler, CORINFO_CLASS_HANDLE classHandle)
{
bool isValueClass = compiler->eeIsValueClass(classHandle);
Expand All @@ -375,6 +607,7 @@ ClassLayout* ClassLayout::Create(Compiler* compiler, CORINFO_CLASS_HANDLE classH

ClassLayout* layout = new (compiler, CMK_ClassLayout)
ClassLayout(classHandle, isValueClass, size, type DEBUGARG(className) DEBUGARG(shortClassName));

layout->InitializeGCPtrs(compiler);

return layout;
Expand All @@ -384,6 +617,7 @@ void ClassLayout::InitializeGCPtrs(Compiler* compiler)
{
assert(!m_gcPtrsInitialized);
assert(!IsBlockLayout());
assert(!IsArrayLayout());

if (m_size < TARGET_POINTER_SIZE)
{
Expand Down Expand Up @@ -497,6 +731,8 @@ bool ClassLayout::AreCompatible(const ClassLayout* layout1, const ClassLayout* l
CORINFO_CLASS_HANDLE clsHnd1 = layout1->GetClassHandle();
CORINFO_CLASS_HANDLE clsHnd2 = layout2->GetClassHandle();

// validate elemcount

if ((clsHnd1 != NO_CLASS_HANDLE) && (clsHnd1 == clsHnd2))
{
return true;
Expand Down
Loading
Loading