Skip to content

Commit

Permalink
Enable "Allocate on stack" for NAOT/R2R (#104411)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
EgorBo and jkotas authored Jul 14, 2024
1 parent fda63b4 commit af5e715
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\CastHelpers.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\ICastableHelpers.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\RuntimeHelpers.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\CompilerServices\StackAllocatedBox.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\ControlledExecution.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Runtime\DependentHandle.cs" />
<Compile Include="$(MSBuildThisFileDirectory)..\nativeaot\Common\src\System\Runtime\RhFailFastReason.cs" />
Expand Down
24 changes: 12 additions & 12 deletions src/coreclr/inc/corinfoinstructionset.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,18 +608,18 @@ inline CORINFO_InstructionSetFlags EnsureInstructionSetFlagsAreValid(CORINFO_Ins
resultflags.RemoveInstructionSet(InstructionSet_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet_AVX512F_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW);
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512BW))
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512DQ);
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512DQ))
Expand Down Expand Up @@ -714,18 +714,18 @@ inline CORINFO_InstructionSetFlags EnsureInstructionSetFlagsAreValid(CORINFO_Ins
resultflags.RemoveInstructionSet(InstructionSet_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet_AVX512F_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW);
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512BW))
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512BW_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
resultflags.RemoveInstructionSet(InstructionSet_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512CD))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512CD_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512F_VL))
resultflags.RemoveInstructionSet(InstructionSet_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ) && !resultflags.HasInstructionSet(InstructionSet_AVX512F))
resultflags.RemoveInstructionSet(InstructionSet_AVX512DQ);
if (resultflags.HasInstructionSet(InstructionSet_AVX512DQ_VL) && !resultflags.HasInstructionSet(InstructionSet_AVX512DQ))
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/inc/readytorun.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// If you update this, ensure you run `git grep MINIMUM_READYTORUN_MAJOR_VERSION`
// and handle pending work.
#define READYTORUN_MAJOR_VERSION 10
#define READYTORUN_MINOR_VERSION 0x0000
#define READYTORUN_MINOR_VERSION 0x0001

#define MINIMUM_READYTORUN_MAJOR_VERSION 10

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

struct READYTORUN_CORE_HEADER
{
Expand Down Expand Up @@ -357,6 +358,7 @@ enum ReadyToRunHelper
READYTORUN_HELPER_Unbox = 0x5A,
READYTORUN_HELPER_Unbox_Nullable = 0x5B,
READYTORUN_HELPER_NewMultiDimArr = 0x5C,
READYTORUN_HELPER_Unbox_TypeTest = 0x5D,

// Helpers used with generic handle lookup cases
READYTORUN_HELPER_NewObject = 0x60,
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/inc/readytorunhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ HELPER(READYTORUN_HELPER_Box_Nullable, CORINFO_HELP_BOX_NULLABLE,
HELPER(READYTORUN_HELPER_Unbox, CORINFO_HELP_UNBOX, )
HELPER(READYTORUN_HELPER_Unbox_Nullable, CORINFO_HELP_UNBOX_NULLABLE, )
HELPER(READYTORUN_HELPER_NewMultiDimArr, CORINFO_HELP_NEW_MDARR, )
HELPER(READYTORUN_HELPER_Unbox_TypeTest, CORINFO_HELP_UNBOX_TYPETEST, )

HELPER(READYTORUN_HELPER_NewObject, CORINFO_HELP_NEWFAST, )
HELPER(READYTORUN_HELPER_NewArray, CORINFO_HELP_NEWARR_1_DIRECT, )
Expand Down
9 changes: 0 additions & 9 deletions src/coreclr/jit/objectalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,6 @@ inline bool ObjectAllocator::CanAllocateLclVarOnStack(unsigned int lclNu
return false;
}

#ifdef FEATURE_READYTORUN
if (comp->opts.IsReadyToRun())
{
// Need to fix getClassGClayout and maybe more
*reason = "[R2R/NAOT support NYI]";
return false;
}
#endif

classSize = comp->info.compCompHnd->getHeapClassSize(clsHnd);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ public static unsafe void RhUnboxNullable(ref byte data, MethodTable* pUnboxToEE
RhUnbox(obj, ref data, pUnboxToEEType);
}

[RuntimeExport("RhUnboxTypeTest")]
public static unsafe void RhUnboxTypeTest(MethodTable* pType, MethodTable* pBoxType)
{
Debug.Assert(pType->IsValueType);

if (!UnboxAnyTypeCompare(pType, pBoxType))
{
throw pType->GetClasslibException(ExceptionIDs.InvalidCast);
}
}

[RuntimeExport("RhUnbox")]
public static unsafe void RhUnbox(object? obj, ref byte data, MethodTable* pUnboxToEEType)
{
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/inc/ModuleHeaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct ReadyToRunHeaderConstants
static const uint32_t Signature = 0x00525452; // 'RTR'

static const uint32_t CurrentMajorVersion = 10;
static const uint32_t CurrentMinorVersion = 0;
static const uint32_t CurrentMinorVersion = 1;
};

struct ReadyToRunHeader
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/tools/Common/Internal/Runtime/ModuleHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal struct ReadyToRunHeaderConstants
public const uint Signature = 0x00525452; // 'RTR'

public const ushort CurrentMajorVersion = 10;
public const ushort CurrentMinorVersion = 0;
public const ushort CurrentMinorVersion = 1;
}
#if READYTORUN
#pragma warning disable 0169
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public enum ReadyToRunHelper
Unbox = 0x5A,
Unbox_Nullable = 0x5B,
NewMultiDimArr = 0x5C,
Unbox_TypeTest = 0x5D,

// Helpers used with generic handle lookup cases
NewObject = 0x60,
Expand Down
33 changes: 22 additions & 11 deletions src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2352,14 +2352,8 @@ private uint getClassGClayout(CORINFO_CLASS_STRUCT_* cls, byte* gcPtrs)
uint result = 0;

MetadataType type = (MetadataType)HandleToObject(cls);

int pointerSize = PointerSize;

int ptrsCount = AlignmentHelper.AlignUp(type.InstanceFieldSize.AsInt, pointerSize) / pointerSize;

// Assume no GC pointers at first
for (int i = 0; i < ptrsCount; i++)
gcPtrs[i] = (byte)CorInfoGCType.TYPE_GC_NONE;
uint size = type.IsValueType ? getClassSize(cls) : getHeapClassSize(cls);
new Span<byte>(gcPtrs, (int)((size + PointerSize - 1) / PointerSize)).Clear();

if (type.ContainsGCPointers || type.IsByRefLike)
{
Expand Down Expand Up @@ -2606,9 +2600,26 @@ private CorInfoHelpFunc getSharedCCtorHelper(CORINFO_CLASS_STRUCT_* clsHnd)

private CORINFO_CLASS_STRUCT_* getTypeForBoxOnStack(CORINFO_CLASS_STRUCT_* cls)
{
// Todo: implement...
_ = HandleToObject(cls);
return null;
TypeDesc clsTypeDesc = HandleToObject(cls);
if (clsTypeDesc.IsNullable)
{
clsTypeDesc = clsTypeDesc.Instantiation[0];
}

if (clsTypeDesc.RequiresAlign8())
{
// Conservatively give up on such types (32bit)
return null;
}

// Instantiate StackAllocatedBox<T> helper type with the type we're boxing
MetadataType placeholderType = _compilation.TypeSystemContext.SystemModule.GetType("System.Runtime.CompilerServices", "StackAllocatedBox`1", throwIfNotFound: false);
if (placeholderType == null)
{
// Give up if corelib does not have support for stackallocation
return null;
}
return ObjectToHandle(placeholderType.MakeInstantiatedType(clsTypeDesc));
}

private CorInfoHelpFunc getBoxHelper(CORINFO_CLASS_STRUCT_* cls)
Expand Down
48 changes: 24 additions & 24 deletions src/coreclr/tools/Common/JitInterface/CorInfoInstructionSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -746,18 +746,18 @@ public static InstructionSetFlags ExpandInstructionSetByImplicationHelper(Target
resultflags.AddInstructionSet(InstructionSet.X64_EVEX);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512BW))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512BW_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512BW);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512BW_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512DQ))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512DQ_VL))
Expand Down Expand Up @@ -853,18 +853,18 @@ public static InstructionSetFlags ExpandInstructionSetByImplicationHelper(Target
resultflags.AddInstructionSet(InstructionSet.X86_EVEX);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512BW))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512BW_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512BW);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512BW_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512DQ))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512DQ_VL))
Expand Down Expand Up @@ -1087,18 +1087,18 @@ private static InstructionSetFlags ExpandInstructionSetByReverseImplicationHelpe
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512BW);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512BW))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X64_AVX512DQ);
if (resultflags.HasInstructionSet(InstructionSet.X64_AVX512DQ))
Expand Down Expand Up @@ -1194,18 +1194,18 @@ private static InstructionSetFlags ExpandInstructionSetByReverseImplicationHelpe
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512F_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512BW);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512BW))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512BW_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512CD))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F_VL))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512CD_VL);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512F))
resultflags.AddInstructionSet(InstructionSet.X86_AVX512DQ);
if (resultflags.HasInstructionSet(InstructionSet.X86_AVX512DQ))
Expand Down
Loading

0 comments on commit af5e715

Please sign in to comment.