Skip to content

Commit af5e715

Browse files
EgorBojkotas
andauthored
Enable "Allocate on stack" for NAOT/R2R (#104411)
Co-authored-by: Jan Kotas <[email protected]>
1 parent fda63b4 commit af5e715

File tree

20 files changed

+159
-95
lines changed

20 files changed

+159
-95
lines changed

src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@
197197
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\CastHelpers.cs" />
198198
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\ICastableHelpers.cs" />
199199
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeHelpers.CoreCLR.cs" />
200-
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\StackAllocatedBox.cs" />
201200
<Compile Include="$(BclSourcesRoot)\System\Runtime\ControlledExecution.CoreCLR.cs" />
202201
<Compile Include="$(BclSourcesRoot)\System\Runtime\DependentHandle.cs" />
203202
<Compile Include="$(MSBuildThisFileDirectory)..\nativeaot\Common\src\System\Runtime\RhFailFastReason.cs" />

src/coreclr/inc/corinfoinstructionset.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,18 +608,18 @@ inline CORINFO_InstructionSetFlags EnsureInstructionSetFlagsAreValid(CORINFO_Ins
608608
resultflags.RemoveInstructionSet(InstructionSet_AVX512F);
609609
if (resultflags.HasInstructionSet(InstructionSet_AVX512F_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
610610
resultflags.RemoveInstructionSet(InstructionSet_AVX512F_VL);
611-
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
612-
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
613-
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
614-
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
615-
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
616-
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
617611
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
618612
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW);
619613
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512BW))
620614
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
621615
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
622616
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
617+
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
618+
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
619+
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
620+
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
621+
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
622+
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
623623
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
624624
resultflags.RemoveInstructionSet(InstructionSet_AVX512DQ);
625625
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512DQ))
@@ -714,18 +714,18 @@ inline CORINFO_InstructionSetFlags EnsureInstructionSetFlagsAreValid(CORINFO_Ins
714714
resultflags.RemoveInstructionSet(InstructionSet_AVX512F);
715715
if (resultflags.HasInstructionSet(InstructionSet_AVX512F_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
716716
resultflags.RemoveInstructionSet(InstructionSet_AVX512F_VL);
717-
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
718-
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
719-
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
720-
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
721-
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
722-
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
723717
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
724718
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW);
725719
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512BW))
726720
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
727721
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
728722
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
723+
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
724+
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
725+
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
726+
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
727+
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
728+
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
729729
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
730730
resultflags.RemoveInstructionSet(InstructionSet_AVX512DQ);
731731
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512DQ))

src/coreclr/inc/readytorun.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// If you update this, ensure you run `git grep MINIMUM_READYTORUN_MAJOR_VERSION`
2121
// and handle pending work.
2222
#define READYTORUN_MAJOR_VERSION 10
23-
#define READYTORUN_MINOR_VERSION 0x0000
23+
#define READYTORUN_MINOR_VERSION 0x0001
2424

2525
#define MINIMUM_READYTORUN_MAJOR_VERSION 10
2626

@@ -37,6 +37,7 @@
3737
// R2R Version 9.3 adds BulkWriteBarrier helper
3838
// uses GCInfo v3, which makes safe points in partially interruptible code interruptible.
3939
// R2R Version 10.0 adds support for the statics being allocated on a per type basis instead of on a per module basis
40+
// R2R Version 10.1 adds Unbox_TypeTest helper
4041

4142
struct READYTORUN_CORE_HEADER
4243
{
@@ -357,6 +358,7 @@ enum ReadyToRunHelper
357358
READYTORUN_HELPER_Unbox = 0x5A,
358359
READYTORUN_HELPER_Unbox_Nullable = 0x5B,
359360
READYTORUN_HELPER_NewMultiDimArr = 0x5C,
361+
READYTORUN_HELPER_Unbox_TypeTest = 0x5D,
360362

361363
// Helpers used with generic handle lookup cases
362364
READYTORUN_HELPER_NewObject = 0x60,

src/coreclr/inc/readytorunhelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ HELPER(READYTORUN_HELPER_Box_Nullable, CORINFO_HELP_BOX_NULLABLE,
4545
HELPER(READYTORUN_HELPER_Unbox, CORINFO_HELP_UNBOX, )
4646
HELPER(READYTORUN_HELPER_Unbox_Nullable, CORINFO_HELP_UNBOX_NULLABLE, )
4747
HELPER(READYTORUN_HELPER_NewMultiDimArr, CORINFO_HELP_NEW_MDARR, )
48+
HELPER(READYTORUN_HELPER_Unbox_TypeTest, CORINFO_HELP_UNBOX_TYPETEST, )
4849

4950
HELPER(READYTORUN_HELPER_NewObject, CORINFO_HELP_NEWFAST, )
5051
HELPER(READYTORUN_HELPER_NewArray, CORINFO_HELP_NEWARR_1_DIRECT, )

src/coreclr/jit/objectalloc.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,6 @@ inline bool ObjectAllocator::CanAllocateLclVarOnStack(unsigned int lclNu
164164
return false;
165165
}
166166

167-
#ifdef FEATURE_READYTORUN
168-
if (comp->opts.IsReadyToRun())
169-
{
170-
// Need to fix getClassGClayout and maybe more
171-
*reason = "[R2R/NAOT support NYI]";
172-
return false;
173-
}
174-
#endif
175-
176167
classSize = comp->info.compCompHnd->getHeapClassSize(clsHnd);
177168
}
178169

src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,17 @@ public static unsafe void RhUnboxNullable(ref byte data, MethodTable* pUnboxToEE
227227
RhUnbox(obj, ref data, pUnboxToEEType);
228228
}
229229

230+
[RuntimeExport("RhUnboxTypeTest")]
231+
public static unsafe void RhUnboxTypeTest(MethodTable* pType, MethodTable* pBoxType)
232+
{
233+
Debug.Assert(pType->IsValueType);
234+
235+
if (!UnboxAnyTypeCompare(pType, pBoxType))
236+
{
237+
throw pType->GetClasslibException(ExceptionIDs.InvalidCast);
238+
}
239+
}
240+
230241
[RuntimeExport("RhUnbox")]
231242
public static unsafe void RhUnbox(object? obj, ref byte data, MethodTable* pUnboxToEEType)
232243
{

src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct ReadyToRunHeaderConstants
1212
static const uint32_t Signature = 0x00525452; // 'RTR'
1313

1414
static const uint32_t CurrentMajorVersion = 10;
15-
static const uint32_t CurrentMinorVersion = 0;
15+
static const uint32_t CurrentMinorVersion = 1;
1616
};
1717

1818
struct ReadyToRunHeader

src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal struct ReadyToRunHeaderConstants
1616
public const uint Signature = 0x00525452; // 'RTR'
1717

1818
public const ushort CurrentMajorVersion = 10;
19-
public const ushort CurrentMinorVersion = 0;
19+
public const ushort CurrentMinorVersion = 1;
2020
}
2121
#if READYTORUN
2222
#pragma warning disable 0169

src/coreclr/tools/Common/Internal/Runtime/ReadyToRunConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public enum ReadyToRunHelper
267267
Unbox = 0x5A,
268268
Unbox_Nullable = 0x5B,
269269
NewMultiDimArr = 0x5C,
270+
Unbox_TypeTest = 0x5D,
270271

271272
// Helpers used with generic handle lookup cases
272273
NewObject = 0x60,

src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,14 +2352,8 @@ private uint getClassGClayout(CORINFO_CLASS_STRUCT_* cls, byte* gcPtrs)
23522352
uint result = 0;
23532353

23542354
MetadataType type = (MetadataType)HandleToObject(cls);
2355-
2356-
int pointerSize = PointerSize;
2357-
2358-
int ptrsCount = AlignmentHelper.AlignUp(type.InstanceFieldSize.AsInt, pointerSize) / pointerSize;
2359-
2360-
// Assume no GC pointers at first
2361-
for (int i = 0; i < ptrsCount; i++)
2362-
gcPtrs[i] = (byte)CorInfoGCType.TYPE_GC_NONE;
2355+
uint size = type.IsValueType ? getClassSize(cls) : getHeapClassSize(cls);
2356+
new Span<byte>(gcPtrs, (int)((size + PointerSize - 1) / PointerSize)).Clear();
23632357

23642358
if (type.ContainsGCPointers || type.IsByRefLike)
23652359
{
@@ -2606,9 +2600,26 @@ private CorInfoHelpFunc getSharedCCtorHelper(CORINFO_CLASS_STRUCT_* clsHnd)
26062600

26072601
private CORINFO_CLASS_STRUCT_* getTypeForBoxOnStack(CORINFO_CLASS_STRUCT_* cls)
26082602
{
2609-
// Todo: implement...
2610-
_ = HandleToObject(cls);
2611-
return null;
2603+
TypeDesc clsTypeDesc = HandleToObject(cls);
2604+
if (clsTypeDesc.IsNullable)
2605+
{
2606+
clsTypeDesc = clsTypeDesc.Instantiation[0];
2607+
}
2608+
2609+
if (clsTypeDesc.RequiresAlign8())
2610+
{
2611+
// Conservatively give up on such types (32bit)
2612+
return null;
2613+
}
2614+
2615+
// Instantiate StackAllocatedBox<T> helper type with the type we're boxing
2616+
MetadataType placeholderType = _compilation.TypeSystemContext.SystemModule.GetType("System.Runtime.CompilerServices", "StackAllocatedBox`1", throwIfNotFound: false);
2617+
if (placeholderType == null)
2618+
{
2619+
// Give up if corelib does not have support for stackallocation
2620+
return null;
2621+
}
2622+
return ObjectToHandle(placeholderType.MakeInstantiatedType(clsTypeDesc));
26122623
}
26132624

26142625
private CorInfoHelpFunc getBoxHelper(CORINFO_CLASS_STRUCT_* cls)

0 commit comments

Comments
 (0)