diff --git a/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs b/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs index a701d39ead25a3..d3f95bb05377a7 100644 --- a/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs +++ b/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs @@ -732,7 +732,14 @@ internal bool RequiresAlign8 { get { - return (RareFlags & EETypeRareFlags.RequiresAlign8Flag) != 0; + // NOTE: Does not work for types with HasComponentSize, ie. arrays and strings. + // Since this is called early through RhNewObject we cannot use regular Debug.Assert + // here to enforce the assumption. +#if DEBUG + if (HasComponentSize) + Debug.Fail("RequiresAlign8 called for array or string"); +#endif + return (_uFlags & (uint)EETypeFlagsEx.RequiresAlign8Flag) != 0; } } diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs index a1e7ab60f30da6..b7a1863d3727db 100644 --- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs +++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/RuntimeExports.cs @@ -61,7 +61,8 @@ public static unsafe object RhNewArray(MethodTable* pEEType, int length) Debug.Assert(pEEType->IsArray || pEEType->IsString); #if FEATURE_64BIT_ALIGNMENT - if (pEEType->RequiresAlign8) + MethodTable* pEEElementType = pEEType->RelatedParameterType; + if (pEEElementType->IsValueType && pEEElementType->RequiresAlign8) { return InternalCalls.RhpNewArrayAlign8(pEEType, length); } @@ -381,7 +382,8 @@ internal static unsafe IntPtr RhGetRuntimeHelperForType(MethodTable* pEEType, Ru case RuntimeHelperKind.AllocateArray: #if FEATURE_64BIT_ALIGNMENT - if (pEEType->RequiresAlign8) + MethodTable* pEEElementType = pEEType->RelatedParameterType; + if (pEEElementType->IsValueType && pEEElementType->RequiresAlign8) return (IntPtr)(delegate*)&InternalCalls.RhpNewArrayAlign8; #endif // FEATURE_64BIT_ALIGNMENT diff --git a/src/coreclr/nativeaot/Runtime/inc/MethodTable.h b/src/coreclr/nativeaot/Runtime/inc/MethodTable.h index f33a5d066dc3ec..ca7f09feb1d811 100644 --- a/src/coreclr/nativeaot/Runtime/inc/MethodTable.h +++ b/src/coreclr/nativeaot/Runtime/inc/MethodTable.h @@ -161,6 +161,9 @@ class MethodTable // GC depends on this bit, this type has a critical finalizer HasCriticalFinalizerFlag = 0x0002, IsTrackedReferenceWithFinalizerFlag = 0x0004, + + // This type requires 8-byte alignment for its fields on certain platforms (ARM32, WASM) + RequiresAlign8Flag = 0x1000 }; public: diff --git a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs index 2f6d2c7f04d6fe..2e008b9a1debe4 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/EETypeBuilderHelpers.cs @@ -128,6 +128,11 @@ mdType.Name is "WeakReference" or "WeakReference`1" && flagsEx |= (ushort)EETypeFlagsEx.IDynamicInterfaceCastableFlag; } + if (type.RequiresAlign8()) + { + flagsEx |= (ushort)EETypeFlagsEx.RequiresAlign8Flag; + } + return flagsEx; } diff --git a/src/coreclr/tools/Common/Internal/Runtime/MethodTable.Constants.cs b/src/coreclr/tools/Common/Internal/Runtime/MethodTable.Constants.cs index 1d2834a4e5bee4..bfa142befb24bc 100644 --- a/src/coreclr/tools/Common/Internal/Runtime/MethodTable.Constants.cs +++ b/src/coreclr/tools/Common/Internal/Runtime/MethodTable.Constants.cs @@ -84,6 +84,11 @@ internal enum EETypeFlagsEx : ushort /// This type implements IDynamicInterfaceCastable to allow dynamic resolution of interface casts. /// IDynamicInterfaceCastableFlag = 0x0008, + + /// + /// This type requires 8-byte alignment for its fields on certain platforms (ARM32, WASM) + /// + RequiresAlign8Flag = 0x1000 } internal enum EETypeKind : uint @@ -116,10 +121,7 @@ internal enum EETypeKind : uint [Flags] internal enum EETypeRareFlags : int { - /// - /// This type requires 8-byte alignment for its fields on certain platforms (only ARM currently). - /// - RequiresAlign8Flag = 0x00000001, + // UNUSED = 0x00000001, // UNUSED1 = 0x00000002, diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs index 7c637812029c73..934a69e505c500 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/EETypeNode.cs @@ -1352,11 +1352,6 @@ private void ComputeRareFlags() { uint flags = 0; - if (_type.RequiresAlign8()) - { - flags |= (uint)EETypeRareFlags.RequiresAlign8Flag; - } - if (_type.IsByRefLike) { flags |= (uint)EETypeRareFlags.IsByRefLikeFlag;