@@ -103,7 +103,6 @@ public override ComputedInstanceFieldLayout ComputeInstanceLayout(DefType defTyp
103
103
type . Context . Target . GetWellKnownTypeSize ( type ) ,
104
104
type . Context . Target . GetWellKnownTypeAlignment ( type ) ,
105
105
0 ,
106
- alignUpInstanceByteSize : true ,
107
106
out instanceByteSizeAndAlignment
108
107
) ;
109
108
@@ -291,8 +290,6 @@ protected virtual void FinalizeRuntimeSpecificStaticFieldLayout(TypeSystemContex
291
290
{
292
291
}
293
292
294
- protected virtual bool AlignUpInstanceByteSizeForExplicitFieldLayoutCompatQuirk ( TypeDesc type ) => true ;
295
-
296
293
protected ComputedInstanceFieldLayout ComputeExplicitFieldLayout ( MetadataType type , int numInstanceFields )
297
294
{
298
295
// Instance slice size is the total size of instance not including the base type.
@@ -312,7 +309,7 @@ protected ComputedInstanceFieldLayout ComputeExplicitFieldLayout(MetadataType ty
312
309
foreach ( var fieldAndOffset in layoutMetadata . Offsets )
313
310
{
314
311
TypeDesc fieldType = fieldAndOffset . Field . FieldType ;
315
- var fieldSizeAndAlignment = ComputeFieldSizeAndAlignment ( fieldType , hasLayout : true , packingSize , out bool fieldLayoutAbiStable ) ;
312
+ var fieldSizeAndAlignment = ComputeFieldSizeAndAlignment ( fieldType . UnderlyingType , hasLayout : true , packingSize , out bool fieldLayoutAbiStable ) ;
316
313
if ( ! fieldLayoutAbiStable )
317
314
layoutAbiStable = false ;
318
315
@@ -355,7 +352,6 @@ protected ComputedInstanceFieldLayout ComputeExplicitFieldLayout(MetadataType ty
355
352
instanceSize ,
356
353
largestAlignmentRequired ,
357
354
layoutMetadata . Size ,
358
- alignUpInstanceByteSize : AlignUpInstanceByteSizeForExplicitFieldLayoutCompatQuirk ( type ) ,
359
355
out instanceByteSizeAndAlignment ) ;
360
356
361
357
ComputedInstanceFieldLayout computedLayout = new ComputedInstanceFieldLayout ( ) ;
@@ -398,7 +394,7 @@ protected ComputedInstanceFieldLayout ComputeSequentialFieldLayout(MetadataType
398
394
if ( field . IsStatic )
399
395
continue ;
400
396
401
- var fieldSizeAndAlignment = ComputeFieldSizeAndAlignment ( field . FieldType , hasLayout : true , packingSize , out bool fieldLayoutAbiStable ) ;
397
+ var fieldSizeAndAlignment = ComputeFieldSizeAndAlignment ( field . FieldType . UnderlyingType , hasLayout : true , packingSize , out bool fieldLayoutAbiStable ) ;
402
398
if ( ! fieldLayoutAbiStable )
403
399
layoutAbiStable = false ;
404
400
@@ -417,7 +413,6 @@ protected ComputedInstanceFieldLayout ComputeSequentialFieldLayout(MetadataType
417
413
cumulativeInstanceFieldPos + offsetBias ,
418
414
largestAlignmentRequirement ,
419
415
layoutMetadata . Size ,
420
- alignUpInstanceByteSize : true ,
421
416
out instanceByteSizeAndAlignment ) ;
422
417
423
418
ComputedInstanceFieldLayout computedLayout = new ComputedInstanceFieldLayout ( ) ;
@@ -442,8 +437,8 @@ protected ComputedInstanceFieldLayout ComputeAutoFieldLayout(MetadataType type,
442
437
bool hasLayout = type . HasLayout ( ) ;
443
438
var layoutMetadata = type . GetClassLayout ( ) ;
444
439
445
- int packingSize = ComputePackingSize ( type , layoutMetadata ) ;
446
- packingSize = Math . Min ( context . Target . MaximumAutoLayoutPackingSize , packingSize ) ;
440
+ // Auto-layout in CoreCLR does not respect packing size.
441
+ int packingSize = type . Context . Target . MaximumAlignment ;
447
442
448
443
var offsets = new FieldAndOffset [ numInstanceFields ] ;
449
444
int fieldOrdinal = 0 ;
@@ -543,7 +538,7 @@ protected ComputedInstanceFieldLayout ComputeAutoFieldLayout(MetadataType type,
543
538
LayoutInt offsetBias = LayoutInt . Zero ;
544
539
if ( ! type . IsValueType && cumulativeInstanceFieldPos != LayoutInt . Zero && type . Context . Target . Architecture == TargetArchitecture . X86 )
545
540
{
546
- offsetBias = new LayoutInt ( type . Context . Target . PointerSize ) ;
541
+ offsetBias = type . Context . Target . LayoutPointerSize ;
547
542
cumulativeInstanceFieldPos -= offsetBias ;
548
543
}
549
544
@@ -650,29 +645,14 @@ protected ComputedInstanceFieldLayout ComputeAutoFieldLayout(MetadataType type,
650
645
// Place value class fields last
651
646
for ( int i = 0 ; i < instanceValueClassFieldsArr . Length ; i ++ )
652
647
{
653
- // If the field has an indeterminate alignment, align the cumulative field offset to the indeterminate value
654
- // Otherwise, align the cumulative field offset to the PointerSize
655
- // This avoids issues with Universal Generic Field layouts whose fields may have Indeterminate sizes or alignments
648
+ // Align the cumulative field offset to the indeterminate value
656
649
var fieldSizeAndAlignment = ComputeFieldSizeAndAlignment ( instanceValueClassFieldsArr [ i ] . FieldType , hasLayout , packingSize , out bool fieldLayoutAbiStable ) ;
657
650
if ( ! fieldLayoutAbiStable )
658
651
layoutAbiStable = false ;
659
652
660
- if ( fieldSizeAndAlignment . Alignment . IsIndeterminate )
661
- {
662
- cumulativeInstanceFieldPos = AlignUpInstanceFieldOffset ( type , cumulativeInstanceFieldPos , fieldSizeAndAlignment . Alignment , context . Target ) ;
663
- }
664
- else
665
- {
666
- LayoutInt AlignmentRequired = LayoutInt . Max ( fieldSizeAndAlignment . Alignment , context . Target . LayoutPointerSize ) ;
667
- cumulativeInstanceFieldPos = AlignUpInstanceFieldOffset ( type , cumulativeInstanceFieldPos , AlignmentRequired , context . Target ) ;
668
- }
653
+ cumulativeInstanceFieldPos = AlignUpInstanceFieldOffset ( type , cumulativeInstanceFieldPos , fieldSizeAndAlignment . Alignment , context . Target ) ;
669
654
offsets [ fieldOrdinal ] = new FieldAndOffset ( instanceValueClassFieldsArr [ i ] , cumulativeInstanceFieldPos + offsetBias ) ;
670
-
671
- // If the field has an indeterminate size, align the cumulative field offset to the indeterminate value
672
- // Otherwise, align the cumulative field offset to the aligned-instance field size
673
- // This avoids issues with Universal Generic Field layouts whose fields may have Indeterminate sizes or alignments
674
- LayoutInt alignedInstanceFieldBytes = fieldSizeAndAlignment . Size . IsIndeterminate ? fieldSizeAndAlignment . Size : GetAlignedNumInstanceFieldBytes ( fieldSizeAndAlignment . Size ) ;
675
- cumulativeInstanceFieldPos = checked ( cumulativeInstanceFieldPos + alignedInstanceFieldBytes ) ;
655
+ cumulativeInstanceFieldPos = checked ( cumulativeInstanceFieldPos + fieldSizeAndAlignment . Size ) ;
676
656
677
657
fieldOrdinal ++ ;
678
658
}
@@ -687,11 +667,18 @@ protected ComputedInstanceFieldLayout ComputeAutoFieldLayout(MetadataType type,
687
667
}
688
668
else if ( cumulativeInstanceFieldPos . AsInt > context . Target . PointerSize )
689
669
{
690
- minAlign = context . Target . LayoutPointerSize ;
691
- if ( requiresAlign8 && minAlign . AsInt == 4 )
670
+ if ( requiresAlign8 )
692
671
{
693
672
minAlign = new LayoutInt ( 8 ) ;
694
673
}
674
+ else if ( type . ContainsGCPointers )
675
+ {
676
+ minAlign = context . Target . LayoutPointerSize ;
677
+ }
678
+ else
679
+ {
680
+ minAlign = largestAlignmentRequired ;
681
+ }
695
682
}
696
683
else
697
684
{
@@ -705,8 +692,7 @@ protected ComputedInstanceFieldLayout ComputeAutoFieldLayout(MetadataType type,
705
692
cumulativeInstanceFieldPos + offsetBias ,
706
693
minAlign ,
707
694
classLayoutSize : 0 ,
708
- alignUpInstanceByteSize : true ,
709
- out instanceByteSizeAndAlignment ) ;
695
+ byteCount : out instanceByteSizeAndAlignment ) ;
710
696
711
697
ComputedInstanceFieldLayout computedLayout = new ComputedInstanceFieldLayout ( ) ;
712
698
computedLayout . FieldAlignment = instanceSizeAndAlignment . Alignment ;
@@ -790,10 +776,10 @@ private static SizeAndAlignment ComputeFieldSizeAndAlignment(TypeDesc fieldType,
790
776
{
791
777
if ( fieldType . IsValueType )
792
778
{
793
- DefType metadataType = ( DefType ) fieldType ;
794
- result . Size = metadataType . InstanceFieldSize ;
795
- result . Alignment = metadataType . InstanceFieldAlignment ;
796
- layoutAbiStable = metadataType . LayoutAbiStable ;
779
+ DefType defType = ( DefType ) fieldType ;
780
+ result . Size = defType . InstanceFieldSize ;
781
+ result . Alignment = defType . InstanceFieldAlignment ;
782
+ layoutAbiStable = defType . LayoutAbiStable ;
797
783
}
798
784
else
799
785
{
@@ -816,27 +802,24 @@ private static SizeAndAlignment ComputeFieldSizeAndAlignment(TypeDesc fieldType,
816
802
result . Alignment = fieldType . Context . Target . LayoutPointerSize ;
817
803
}
818
804
805
+ // For non-auto layouts, we need to respect tighter packing requests for alignment.
819
806
if ( hasLayout )
820
807
{
821
808
result . Alignment = LayoutInt . Min ( result . Alignment , new LayoutInt ( packingSize ) ) ;
822
809
}
823
- else
824
- {
825
- result . Alignment = LayoutInt . Min ( result . Alignment , fieldType . Context . Target . GetObjectAlignment ( result . Alignment ) ) ;
826
- }
827
810
828
811
return result ;
829
812
}
830
813
831
814
private static int ComputePackingSize ( MetadataType type , ClassLayoutMetadata layoutMetadata )
832
815
{
833
816
if ( layoutMetadata . PackingSize == 0 )
834
- return type . Context . Target . DefaultPackingSize ;
817
+ return type . Context . Target . MaximumAlignment ;
835
818
else
836
819
return layoutMetadata . PackingSize ;
837
820
}
838
821
839
- private static SizeAndAlignment ComputeInstanceSize ( MetadataType type , LayoutInt instanceSize , LayoutInt alignment , int classLayoutSize , bool alignUpInstanceByteSize , out SizeAndAlignment byteCount )
822
+ private static SizeAndAlignment ComputeInstanceSize ( MetadataType type , LayoutInt instanceSize , LayoutInt alignment , int classLayoutSize , out SizeAndAlignment byteCount )
840
823
{
841
824
SizeAndAlignment result ;
842
825
@@ -864,9 +847,7 @@ private static SizeAndAlignment ComputeInstanceSize(MetadataType type, LayoutInt
864
847
{
865
848
if ( type . IsValueType )
866
849
{
867
- instanceSize = LayoutInt . AlignUp ( instanceSize ,
868
- alignUpInstanceByteSize ? alignment : LayoutInt . Min ( alignment , target . LayoutPointerSize ) ,
869
- target ) ;
850
+ instanceSize = LayoutInt . AlignUp ( instanceSize , alignment , target ) ;
870
851
}
871
852
}
872
853
0 commit comments