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

Allocate RuntimeType objects on Frozen Object Heap #75573

Merged
merged 46 commits into from
Sep 22, 2022

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Sep 13, 2022

Follow up to #49576
This PR allocates Type objects on the recently-added Frozen Segments so we can bake direct objects' addresses in JIT and avoid helper calls, e.g. for:

Type Test() => typeof(int);

Old codegen:

; Method Program:Test():System.Type:this
G_M50870_IG01: 
       4883EC28             sub      rsp, 40
G_M50870_IG02: 
       48B968BB53F5FB7F0000 mov      rcx, 0x7FFBF553BB68      ; System.Int32
       E81D94505F           call     CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE
       90                   nop      
G_M50870_IG03: 
       4883C428             add      rsp, 40
       C3                   ret      
; Total bytes of code: 25

New codegen:

; Method Program:Test():System.Type:this
G_M50870_IG01: 
G_M50870_IG02:          
       48B8482A63DE29020000 mov      rax, 0x229DE632A48      ; System.Int32
G_M50870_IG03: 
       C3                   ret      
; Total bytes of code: 11

Jit-diffs (-f --pmi):

PMI CodeSize Diffs for System.Private.CoreLib.dll, framework assemblies for  default jit

Summary of Code Size diffs:
(Lower is better)

Total bytes of base: 64750613
Total bytes of diff: 64621503
Total bytes of delta: -129110 (-0.20 % of base)
Total relative delta: -715.51
    diff is an improvement.
    relative diff is an improvement.


Top file improvements (bytes):
      -14241 : System.Text.Json.dasm (-1.22% of base)
      -12215 : System.Private.Xml.dasm (-0.30% of base)
      -11900 : System.Linq.Queryable.dasm (-3.49% of base)
       -9901 : System.Private.CoreLib.dasm (-0.17% of base)
       -7878 : System.Linq.Expressions.dasm (-0.90% of base)
       -6757 : System.Data.Common.dasm (-0.39% of base)
       -6516 : System.Private.DataContractSerialization.dasm (-0.71% of base)
       -4883 : System.ComponentModel.TypeConverter.dasm (-1.59% of base)
       -4275 : Newtonsoft.Json.dasm (-0.44% of base)
       -3175 : System.ComponentModel.Composition.dasm (-0.86% of base)
       -2984 : Microsoft.CodeAnalysis.VisualBasic.dasm (-0.04% of base)
       -2625 : Microsoft.CSharp.dasm (-0.61% of base)
       -2537 : System.DirectoryServices.dasm (-0.54% of base)
       -2418 : FSharp.Core.dasm (-0.06% of base)
       -2219 : System.DirectoryServices.AccountManagement.dasm (-0.52% of base)
       -1961 : Microsoft.CodeAnalysis.CSharp.dasm (-0.04% of base)
       -1539 : System.Data.OleDb.dasm (-0.45% of base)
       -1387 : System.Drawing.Common.dasm (-0.24% of base)
       -1309 : xunit.execution.dotnet.dasm (-0.47% of base)
       -1196 : Microsoft.VisualBasic.Core.dasm (-0.22% of base)

173 total files with Code Size differences (173 improved, 0 regressed), 101 unchanged.

Top method regressions (bytes):
         122 (13.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Numerics.Vector`1[float]](System.Object):System.Numerics.Vector`1[float]
         115 (13.92% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[int](System.Object):int
         115 (13.69% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[long](System.Object):long
         115 (13.45% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[short](System.Object):short
         115 (13.67% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[ubyte](System.Object):ubyte
         112 (11.62% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[double](System.Object):double
          99 (11.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Nullable`1[int]](System.Object):System.Nullable`1[int]
          89 ( 5.45% of base) : System.Composition.TypedParts.dasm - System.Composition.TypedParts.ActivationFeatures.PropertyInjectionFeature:RewriteActivator(System.Reflection.TypeInfo,System.Composition.Hosting.Core.CompositeActivator,System.Collections.Generic.IDictionary`2[System.String,System.Object],System.Collections.Generic.IEnumerable`1[System.Composition.Hosting.Core.CompositionDependency]):System.Composition.Hosting.Core.CompositeActivator:this
          69 ( 8.18% of base) : System.Private.CoreLib.dasm - AdjustmentRule:.ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext):this
          68 ( 2.52% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Interpreter.LightCompiler:CompileLiftedLogicalBinaryExpression(System.Linq.Expressions.BinaryExpression,bool):this
          62 ( 9.67% of base) : System.Data.Odbc.dasm - System.Data.Common.ADP:UnsafeCreateTimer(System.Threading.TimerCallback,System.Object,int,int):System.Threading.Timer
          59 ( 1.45% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterCodeGen:WriteValue(System.Object):this
          58 ( 6.39% of base) : FSharp.Core.dasm - Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter:transBinOp(Microsoft.FSharp.Core.FSharpFunc`2[System.Type,bool],Microsoft.FSharp.Quotations.FSharpExpr,ConvEnv,bool,Microsoft.FSharp.Quotations.FSharpExpr,Microsoft.FSharp.Quotations.FSharpExpr,bool,Microsoft.FSharp.Core.FSharpFunc`2[System.Tuple`3[System.Linq.Expressions.Expression,System.Linq.Expressions.Expression,System.Reflection.MethodInfo],System.Linq.Expressions.BinaryExpression],System.RuntimeMethodHandle):System.Linq.Expressions.Expression
          56 ( 2.79% of base) : FSharp.Core.dasm - Microsoft.FSharp.Linq.RuntimeHelpers.Adapters:ConvImmutableTypeToMutableType(ConversionDescription,System.Type):System.Type
          55 ( 3.91% of base) : System.Private.CoreLib.dasm - System.Enum:TryParse[double](System.ReadOnlySpan`1[ushort],bool,bool,byref):bool
          55 ( 3.88% of base) : System.Private.CoreLib.dasm - System.Enum:TryParse[System.Numerics.Vector`1[float]](System.ReadOnlySpan`1[ushort],bool,bool,byref):bool
          54 ( 5.99% of base) : FSharp.Core.dasm - Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter:transShiftOp(Microsoft.FSharp.Quotations.FSharpExpr,ConvEnv,bool,Microsoft.FSharp.Quotations.FSharpExpr,Microsoft.FSharp.Quotations.FSharpExpr,bool,Microsoft.FSharp.Core.FSharpFunc`2[System.Tuple`3[System.Linq.Expressions.Expression,System.Linq.Expressions.Expression,System.Reflection.MethodInfo],System.Linq.Expressions.BinaryExpression],System.RuntimeMethodHandle):System.Linq.Expressions.Expression
          54 ( 4.81% of base) : System.Private.CoreLib.dasm - System.Decimal:.ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext):this
          54 ( 4.26% of base) : System.Private.CoreLib.dasm - System.Enum:TryParse[long](System.ReadOnlySpan`1[ushort],bool,bool,byref):bool
          54 ( 4.30% of base) : System.Private.CoreLib.dasm - System.Enum:TryParse[ubyte](System.ReadOnlySpan`1[ushort],bool,bool,byref):bool

Top method improvements (bytes):
       -2328 (-26.68% of base) : Microsoft.CodeAnalysis.VisualBasic.dasm - Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax.SyntaxFactory:GetNodeTypes():System.Collections.Generic.IEnumerable`1[System.Object]
       -1496 (-26.69% of base) : Microsoft.CodeAnalysis.CSharp.dasm - Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxFactory:GetNodeTypes():System.Collections.Generic.IEnumerable`1[System.Type]
        -933 (-12.13% of base) : System.Data.Common.dasm - System.Data.DataTable:DeserializeTableSchema(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext,bool):this
        -714 (-12.83% of base) : System.Data.Common.dasm - System.Data.DataTable:SerializeTableSchema(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext,bool):this
        -480 (-5.29% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteLiteralStructMethod(System.Xml.Serialization.StructMapping):this
        -462 (-7.88% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteElement(System.String,System.String,System.String,System.Xml.Serialization.ElementAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool,int,int):this
        -395 (-5.99% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteStructMethod(System.Xml.Serialization.StructMapping):this
        -360 (-6.65% of base) : System.ComponentModel.TypeConverter.dasm - <>c:<get_IntrinsicTypeConverters>b__24_0():System.Collections.Generic.Dictionary`2[System.Object,IntrinsicTypeConverterData]:this
        -343 (-17.03% of base) : System.Text.Json.dasm - System.Text.Json.Serialization.Converters.IEnumerableConverterFactory:CreateConverter(System.Type,System.Text.Json.JsonSerializerOptions):System.Text.Json.Serialization.JsonConverter:this
        -326 (-5.34% of base) : System.Private.DataContractSerialization.dasm - DataContractCriticalHelper:TryCreateBuiltInDataContract(System.String,System.String,byref):bool
        -305 (-6.14% of base) : System.Data.Common.dasm - System.Data.DataTableReader:GetSchemaTableFromDataTable(System.Data.DataTable):System.Data.DataTable
        -287 (-47.52% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Compiler.DelegateHelpers:GetFuncType(System.Type[]):System.Type
        -276 (-46.78% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Compiler.DelegateHelpers:GetActionType(System.Type[]):System.Type
        -264 (-11.64% of base) : System.Private.DataContractSerialization.dasm - DataContractCriticalHelper:GetBuiltInDataContract(System.String):System.Runtime.Serialization.DataContracts.DataContract
        -262 (-5.19% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteMember(System.Xml.Serialization.SourceInfo,System.Xml.Serialization.AttributeAccessor,System.Xml.Serialization.TypeDesc,System.String):this
        -240 (-6.39% of base) : System.Private.Xml.dasm - System.Xml.Serialization.SchemaGraph:Depends(System.Xml.Schema.XmlSchemaObject,System.Collections.ArrayList):this
        -233 (-6.52% of base) : Microsoft.CSharp.dasm - Microsoft.CSharp.RuntimeBinder.ComInterop.ComInvokeBinder:GenerateTryBlock():System.Linq.Expressions.Expression:this
        -232 (-6.14% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteEnumMethod(System.Xml.Serialization.EnumMapping):this
        -228 (-5.30% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteEnumAndArrayTypes():this
        -227 (-5.05% of base) : System.Private.Xml.dasm - System.Xml.Serialization.ReflectionAwareCodeGen:WriteTypeInfo(System.Xml.Serialization.TypeScope,System.Xml.Serialization.TypeDesc,System.Type):System.String:this

Top method regressions (percentages):
          35 (14.11% of base) : FSharp.Core.dasm - Microsoft.FSharp.Text.StructuredPrintfImpl.Display:isSetOrMapType(System.Type):bool
         115 (13.92% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[int](System.Object):int
         115 (13.69% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[long](System.Object):long
         115 (13.67% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[ubyte](System.Object):ubyte
         115 (13.45% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[short](System.Object):short
         122 (13.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Numerics.Vector`1[float]](System.Object):System.Numerics.Vector`1[float]
          11 (11.83% of base) : System.Composition.Hosting.dasm - System.Composition.Hosting.Util.MethodInfoExtensions:CreateStaticDelegate[System.Numerics.Vector`1[float]](System.Reflection.MethodInfo):System.Numerics.Vector`1[float]
         112 (11.62% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[double](System.Object):double
          99 (11.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Nullable`1[int]](System.Object):System.Nullable`1[int]
          62 ( 9.67% of base) : System.Data.Odbc.dasm - System.Data.Common.ADP:UnsafeCreateTimer(System.Threading.TimerCallback,System.Object,int,int):System.Threading.Timer
          44 ( 9.17% of base) : Microsoft.Extensions.DependencyInjection.Abstractions.dasm - Microsoft.Extensions.DependencyInjection.ActivatorUtilities:CreateFactory(System.Type,System.Type[]):Microsoft.Extensions.DependencyInjection.ObjectFactory
          69 ( 8.18% of base) : System.Private.CoreLib.dasm - AdjustmentRule:.ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext):this
          51 ( 7.92% of base) : FSharp.Core.dasm - Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter:transBoolOpNoWitness(Microsoft.FSharp.Core.FSharpFunc`2[System.Type,bool],ConvEnv,bool,Microsoft.FSharp.Quotations.FSharpExpr,Microsoft.FSharp.Quotations.FSharpExpr,bool,Microsoft.FSharp.Core.FSharpFunc`2[System.Tuple`4[System.Linq.Expressions.Expression,System.Linq.Expressions.Expression,bool,System.Reflection.MethodInfo],System.Linq.Expressions.BinaryExpression],System.Reflection.MethodInfo):System.Linq.Expressions.Expression
          40 ( 7.60% of base) : System.Composition.TypedParts.dasm - System.Composition.TypedParts.Discovery.DiscoveredPropertyExport:GetExportDescriptor(System.Composition.Hosting.Core.CompositeActivator):System.Composition.Hosting.Core.ExportDescriptor:this
          42 ( 7.24% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateMethodCall[System.__Canon](System.Reflection.MethodBase):Newtonsoft.Json.Utilities.MethodCall`2[System.__Canon,System.__Canon]:this
          48 ( 6.69% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[double](System.Reflection.FieldInfo):System.Action`2[double,System.Object]:this
          48 ( 6.69% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[int](System.Reflection.FieldInfo):System.Action`2[int,System.Object]:this
          48 ( 6.69% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[long](System.Reflection.FieldInfo):System.Action`2[long,System.Object]:this
          48 ( 6.69% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[short](System.Reflection.FieldInfo):System.Action`2[short,System.Object]:this
          48 ( 6.69% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[System.Nullable`1[int]](System.Reflection.FieldInfo):System.Action`2[System.Nullable`1[int],System.Object]:this

Top method improvements (percentages):
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[double]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[int]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[long]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[short]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[ubyte]()
         -40 (-71.43% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Expression:IfThenElse(System.Linq.Expressions.Expression,System.Linq.Expressions.Expression,System.Linq.Expressions.Expression):System.Linq.Expressions.ConditionalExpression
         -23 (-62.16% of base) : System.Private.CoreLib.dasm - System.Runtime.CompilerServices.TypeHandle:TypeHandleOf[double]():System.Runtime.CompilerServices.TypeHandle
         -23 (-62.16% of base) : System.Private.CoreLib.dasm - System.Runtime.CompilerServices.TypeHandle:TypeHandleOf[int]():System.Runtime.CompilerServices.TypeHandle
         -23 (-62.16% of base) : System.Private.CoreLib.dasm - System.Runtime.CompilerServices.TypeHandle:TypeHandleOf[long]():System.Runtime.CompilerServices.TypeHandle
         -23 (-62.16% of base) : System.Private.CoreLib.dasm - System.Runtime.CompilerServices.TypeHandle:TypeHandleOf[short]():System.Runtime.CompilerServices.TypeHandle
         -23 (-62.16% of base) : System.Private.CoreLib.dasm - System.Runtime.CompilerServices.TypeHandle:TypeHandleOf[System.Nullable`1[int]]():System.Runtime.CompilerServices.TypeHandle
         -23 (-62.16% of base) : System.Private.CoreLib.dasm - System.Runtime.CompilerServices.TypeHandle:TypeHandleOf[System.Numerics.Vector`1[float]]():System.Runtime.CompilerServices.TypeHandle
         -23 (-62.16% of base) : System.Private.CoreLib.dasm - System.Runtime.CompilerServices.TypeHandle:TypeHandleOf[ubyte]():System.Runtime.CompilerServices.TypeHandle
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[double](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[int](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[long](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[short](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[System.Nullable`1[int]](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[System.Numerics.Vector`1[float]](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[ubyte](long)

7846 total methods with Code Size differences (7511 improved, 335 regressed), 383629 unchanged.

Size regressions look to be CSE-related, e.g. https://www.diffchecker.com/eOr1LIyf
E.g. previously call CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE could share its const argument with other places where we only need type handle (like in this diff)

Notes:
In JIT I do the optimization in morph because a lot of other optimizations here and there (mostly in importer) rely on typeof() being a helper call so it's simpler for now, will try to perform a sort of clean up later - current change is minimalistic and doesn't produce regressions.

Closes #49429

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 13, 2022
@ghost ghost assigned EgorBo Sep 13, 2022
@ghost
Copy link

ghost commented Sep 13, 2022

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

Follow up to #49576
This PR allocates Type objects on the recently-added Frozen Segments so we can bake direct objects' addresses in JIT and avoid helper calls, e.g. for:

Type Test() => typeof(int);

Old codegen:

; Method Program:Test():System.Type:this
G_M50870_IG01: 
       4883EC28             sub      rsp, 40
G_M50870_IG02: 
       48B968BB53F5FB7F0000 mov      rcx, 0x7FFBF553BB68      ; System.Int32
       E81D94505F           call     CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE
       90                   nop      
G_M50870_IG03: 
       4883C428             add      rsp, 40
       C3                   ret      
; Total bytes of code: 25

New codegen:

; Method Program:Test():System.Type:this
G_M50870_IG01: 
G_M50870_IG02:          
       48B8482A63DE29020000 mov      rax, 0x229DE632A48      ; Type handle
G_M50870_IG03: 
       C3                   ret      
; Total bytes of code: 11

Jit-diffs (-f --pmi):

Total bytes of base: 64641078
Total bytes of diff: 64511525
Total bytes of delta: -129553 (-0.20 % of base)
Total relative delta: -714.65
    diff is an improvement.
    relative diff is an improvement.


Top file improvements (bytes):
      -14248 : System.Text.Json.dasm (-1.22% of base)
      -12304 : System.Private.Xml.dasm (-0.30% of base)
      -11900 : System.Linq.Queryable.dasm (-3.49% of base)
       -9846 : System.Private.CoreLib.dasm (-0.17% of base)
       -7846 : System.Linq.Expressions.dasm (-0.90% of base)
       -6967 : System.Data.Common.dasm (-0.41% of base)
       -6519 : System.Private.DataContractSerialization.dasm (-0.71% of base)
       -4769 : System.ComponentModel.TypeConverter.dasm (-1.55% of base)
       -4491 : Newtonsoft.Json.dasm (-0.47% of base)
       -3168 : System.ComponentModel.Composition.dasm (-0.85% of base)
       -2984 : Microsoft.CodeAnalysis.VisualBasic.dasm (-0.04% of base)
       -2951 : FSharp.Core.dasm (-0.07% of base)
       -2662 : Microsoft.CSharp.dasm (-0.62% of base)
       -2537 : System.DirectoryServices.dasm (-0.54% of base)
       -2219 : System.DirectoryServices.AccountManagement.dasm (-0.52% of base)

173 total files with Code Size differences (173 improved, 0 regressed), 101 unchanged.

Top method regressions (bytes):
         122 (13.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Numerics.Vector`1[float]](System.Object):System.Numerics.Vector`1[float]
         115 (13.92% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[int](System.Object):int
         115 (13.69% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[long](System.Object):long
         115 (13.45% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[short](System.Object):short
         115 (13.67% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[ubyte](System.Object):ubyte
         112 (11.62% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[double](System.Object):double
          99 (11.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Nullable`1[int]](System.Object):System.Nullable`1[int]
          89 ( 5.45% of base) : System.Composition.TypedParts.dasm - System.Composition.TypedParts.ActivationFeatures.PropertyInjectionFeature:RewriteActivator(System.Reflection.TypeInfo,System.Composition.Hosting.Core.CompositeActivator,System.Collections.Generic.IDictionary`2[System.String,System.Object],System.Collections.Generic.IEnumerable`1[System.Composition.Hosting.Core.CompositionDependency]):System.Composition.Hosting.Core.CompositeActivator:this
          69 ( 8.18% of base) : System.Private.CoreLib.dasm - AdjustmentRule:.ctor(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext):this
          68 ( 2.52% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Interpreter.LightCompiler:CompileLiftedLogicalBinaryExpression(System.Linq.Expressions.BinaryExpression,bool):this
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[double](System.Reflection.FieldInfo):System.Action`2[double,System.Object]:this
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[int](System.Reflection.FieldInfo):System.Action`2[int,System.Object]:this
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[long](System.Reflection.FieldInfo):System.Action`2[long,System.Object]:this
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[short](System.Reflection.FieldInfo):System.Action`2[short,System.Object]:this
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[System.Nullable`1[int]](System.Reflection.FieldInfo):System.Action`2[System.Nullable`1[int],System.Object]:this

Top method improvements (bytes):
       -2328 (-26.68% of base) : Microsoft.CodeAnalysis.VisualBasic.dasm - Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax.SyntaxFactory:GetNodeTypes():System.Collections.Generic.IEnumerable`1[System.Object]
       -1496 (-26.69% of base) : Microsoft.CodeAnalysis.CSharp.dasm - Microsoft.CodeAnalysis.CSharp.Syntax.InternalSyntax.SyntaxFactory:GetNodeTypes():System.Collections.Generic.IEnumerable`1[System.Type]
        -933 (-12.13% of base) : System.Data.Common.dasm - System.Data.DataTable:DeserializeTableSchema(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext,bool):this
        -714 (-12.83% of base) : System.Data.Common.dasm - System.Data.DataTable:SerializeTableSchema(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext,bool):this
        -575 (-2.59% of base) : FSharp.Core.dasm - Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter:ConvExprToLinqInContext(ConvEnv,Microsoft.FSharp.Quotations.FSharpExpr):System.Linq.Expressions.Expression
        -480 (-5.29% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteLiteralStructMethod(System.Xml.Serialization.StructMapping):this
        -462 (-7.88% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationReaderILGen:WriteElement(System.String,System.String,System.String,System.Xml.Serialization.ElementAccessor,System.Xml.Serialization.ChoiceIdentifierAccessor,System.String,bool,bool,int,int):this
        -395 (-5.99% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteStructMethod(System.Xml.Serialization.StructMapping):this
        -343 (-17.03% of base) : System.Text.Json.dasm - System.Text.Json.Serialization.Converters.IEnumerableConverterFactory:CreateConverter(System.Type,System.Text.Json.JsonSerializerOptions):System.Text.Json.Serialization.JsonConverter:this
        -326 (-5.34% of base) : System.Private.DataContractSerialization.dasm - DataContractCriticalHelper:TryCreateBuiltInDataContract(System.String,System.String,byref):bool
        -305 (-6.14% of base) : System.Data.Common.dasm - System.Data.DataTableReader:GetSchemaTableFromDataTable(System.Data.DataTable):System.Data.DataTable
        -287 (-47.52% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Compiler.DelegateHelpers:GetFuncType(<unnamed>):System.Type
        -276 (-46.78% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Compiler.DelegateHelpers:GetActionType(<unnamed>):System.Type
        -270 (-5.35% of base) : System.Private.Xml.dasm - System.Xml.Serialization.XmlSerializationWriterILGen:WriteMember(System.Xml.Serialization.SourceInfo,System.Xml.Serialization.AttributeAccessor,System.Xml.Serialization.TypeDesc,System.String):this
        -264 (-11.64% of base) : System.Private.DataContractSerialization.dasm - DataContractCriticalHelper:GetBuiltInDataContract(System.String):System.Runtime.Serialization.DataContracts.DataContract

Top method regressions (percentages):
          35 (14.11% of base) : FSharp.Core.dasm - Microsoft.FSharp.Text.StructuredPrintfImpl.Display:isSetOrMapType(System.Type):bool
         115 (13.92% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[int](System.Object):int
         115 (13.69% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[long](System.Object):long
         115 (13.67% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[ubyte](System.Object):ubyte
         115 (13.45% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[short](System.Object):short
         122 (13.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Numerics.Vector`1[float]](System.Object):System.Numerics.Vector`1[float]
          58 (12.08% of base) : Microsoft.Extensions.DependencyInjection.Abstractions.dasm - Microsoft.Extensions.DependencyInjection.ActivatorUtilities:CreateFactory(System.Type,<unnamed>):Microsoft.Extensions.DependencyInjection.ObjectFactory
          11 (11.83% of base) : System.Composition.Hosting.dasm - System.Composition.Hosting.Util.MethodInfoExtensions:CreateStaticDelegate[System.Numerics.Vector`1[float]](System.Reflection.MethodInfo):System.Numerics.Vector`1[float]
         112 (11.62% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[double](System.Object):double
          99 (11.22% of base) : Microsoft.VisualBasic.Core.dasm - Microsoft.VisualBasic.CompilerServices.Conversions:ToGenericParameter[System.Nullable`1[int]](System.Object):System.Nullable`1[int]
          54 (10.27% of base) : System.Composition.TypedParts.dasm - System.Composition.TypedParts.Discovery.DiscoveredPropertyExport:GetExportDescriptor(System.Composition.Hosting.Core.CompositeActivator):System.Composition.Hosting.Core.ExportDescriptor:this
          48 ( 9.62% of base) : FSharp.Core.dasm - Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter:transBinOp(Microsoft.FSharp.Quotations.FSharpExpr,ConvEnv,bool,Microsoft.FSharp.Collections.FSharpList`1[Microsoft.FSharp.Quotations.FSharpExpr],bool,Microsoft.FSharp.Core.FSharpFunc`2[System.Tuple`2[System.Linq.Expressions.Expression,System.Linq.Expressions.Expression],System.Linq.Expressions.BinaryExpression]):System.Linq.Expressions.Expression
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[double](System.Reflection.FieldInfo):System.Action`2[double,System.Object]:this
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[int](System.Reflection.FieldInfo):System.Action`2[int,System.Object]:this
          62 ( 8.65% of base) : Newtonsoft.Json.dasm - Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory:CreateSet[long](System.Reflection.FieldInfo):System.Action`2[long,System.Object]:this

Top method improvements (percentages):
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[double]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[int]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[long]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[short]()
          -8 (-88.89% of base) : System.Diagnostics.DiagnosticSource.dasm - System.Diagnostics.Metrics.Instrument:ValidateTypeParameter[ubyte]()
         -40 (-71.43% of base) : System.Linq.Expressions.dasm - System.Linq.Expressions.Expression:IfThenElse(System.Linq.Expressions.Expression,System.Linq.Expressions.Expression,System.Linq.Expressions.Expression):System.Linq.Expressions.ConditionalExpression
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[double](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[int](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[long](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[short](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[System.Nullable`1[int]](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[System.Numerics.Vector`1[float]](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.Runtime.InteropServices.Marshal:DestroyStructure[ubyte](long)
         -24 (-61.54% of base) : System.Private.CoreLib.dasm - System.RuntimeType:IsCOMObjectImpl():bool:this
         -34 (-60.71% of base) : System.Private.DataContractSerialization.dasm - System.Runtime.Serialization.DataContracts.CharDataContract:.ctor(System.Xml.XmlDictionaryString,System.Xml.XmlDictionaryString):this

7830 total methods with Code Size differences (7524 improved, 306 regressed), 383145 unchanged.

Size regressions look to be CSE-related, e.g. https://www.diffchecker.com/lDHsFUcP

Notes:
In JIT I do the optimization in morph because a lot of stuff here and there rely on typeof() being a helper call so it's simpler for now, will try to perform a sort of clean up later - current change is minimalistic and doesn't produce regressions.

Author: EgorBo
Assignees: EgorBo
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo EgorBo marked this pull request as ready for review September 14, 2022 14:33
@EgorBo
Copy link
Member Author

EgorBo commented Sep 14, 2022

Seems to be passing CI tests (SPMI failures are due to JIT-EE change), @jkotas when you have time could you review the vm side?

src/coreclr/jit/compiler.h Outdated Show resolved Hide resolved
src/coreclr/jit/morph.cpp Outdated Show resolved Hide resolved
@@ -11161,7 +11181,7 @@ GenTree* Compiler::fgMorphSmpOp(GenTree* tree, MorphAddrContext* mac, bool* optA
//
if (!tree->AsIndir()->IsVolatile())
{
if (op1->IsIconHandle(GTF_ICON_STR_HDL))
if (op1->IsIconHandle(GTF_ICON_OBJ_HDL))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we see this for cases like "" is string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet this can be removed since we now add these flags in morph anyway in case GT_IND: and the is volatile for string literal load seems to be useless - I'll check it in a follow up together with https://github.com/dotnet/runtime/pull/75573/files#r977367516

@@ -812,7 +812,7 @@ bool Compiler::optValnumCSE_Locate()
if (!enableConstCSE &&
// Unconditionally allow these constant handles to be CSE'd
!tree->IsIconHandle(GTF_ICON_STATIC_HDL) && !tree->IsIconHandle(GTF_ICON_CLASS_HDL) &&
!tree->IsIconHandle(GTF_ICON_STR_HDL))
!tree->IsIconHandle(GTF_ICON_STR_HDL) && !tree->IsIconHandle(GTF_ICON_OBJ_HDL))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't GTF_ICON_STR_HDL come with an invariant indirection on top? Do you know why we would CSE the constant but not the indir (now and also before)?

Copy link
Member Author

@EgorBo EgorBo Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jakobbotsch normally it's expected that indirs on top of GTF_ICON_STR_HDL will be CSE'd, unlike raw constants they have higher weight and CSE should handle them on x64, I don't remember why exactly I put GTF_ICON_STR_HDL here in that other PR but AFAIR it affected diffs so I left it (initially I only wanted to handle GTF_ICON_CLASS_HDL) - I'll check diffs after this PR what will happen if I remove GTF_ICON_STR_HDL from here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially it was because we weren't setting GTF_IND_INVARIANT for the string handles in R2R? That was fixed in #70986, so might be this is unnecessary today.

Copy link
Member Author

@EgorBo EgorBo Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point, will check (in a follow up)

Copy link
Member

@jakobbotsch jakobbotsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JiT changes LGTM, a few suggestions and questions.

@EgorBo
Copy link
Member Author

EgorBo commented Sep 22, 2022

Looks like this can be merged now, two Pri0 failures are from #75952, SPMI ones are due to JIT-EE guid change (we still don't know how to avoid triggering those when both JIT and JIT-EE guid are changed in YML)

I plan to look into the profiler/diag issues raised in the other PR for FOH, and test this and the string literals on OSS (and, hopefully, some 1st parties) projects to get a better understanding how many objects make it to FOH.

Then we might look into making static objects (at least those without GC fields inside) also FOH-based

@jkotas
Copy link
Member

jkotas commented Sep 22, 2022

Then we might look into making static objects (at least those without GC fields inside) also FOH-based

How do you plan to do that? I do not think it is feasible without AOT step (or without doing expensive work when JITing static constructors - I do not expect we would want to do that).

@jakobbotsch
Copy link
Member

Can we do it for static field boxes (the ones created for non-primitives)? Today, JIT needs to create

ADD byref
  IND  ref
    CNS <object slot addr>
  CNS 8

for all such accesses, I believe. We could potentially create direct pointer to the boxed contents.

@jkotas
Copy link
Member

jkotas commented Sep 22, 2022

Ah ok, yes - these boxes can be allocated on the frozen heap.

@EgorBo
Copy link
Member Author

EgorBo commented Sep 22, 2022

Good point about the static boxes, regarding other types of objects I am not sure either

Perhaps, for e.g.

static readonly string str = ...;

we could insert call object CopyToFoh(object o) call before stsfld in .cctor as people, in theory, should not rely on references for string objects but it probably sounds terrible

@EgorBo EgorBo merged commit 1f1231c into dotnet:main Sep 22, 2022
@EgorBo EgorBo mentioned this pull request Sep 25, 2022
14 tasks
jkotas added a commit to jkotas/runtime that referenced this pull request Sep 27, 2022
jkotas added a commit that referenced this pull request Sep 27, 2022
* Revert "Allocate RuntimeType objects on Frozen Object Heap (#75573)"

This reverts commit 1f1231c.

* Revert "don't use write barriers for frozen objects (#76135)"

This reverts commit 315bdd4.
@EgorBo
Copy link
Member Author

EgorBo commented Sep 27, 2022

Around ~100 improvements dotnet/perf-autofiling-issues#8700, a few regressions and some are noise

EgorBo added a commit that referenced this pull request Oct 6, 2022
…en objects" (#76649)

This PR reverts #76235 with a few manual modifications:
The main issue why the initial PRs (#75573 and #76135) were reverted has just been resolved via #76251:

GC could collect some associated (with frozen objects) objects as unreachable, e.g. it could collect a SyncBlock, WeakReferences and Dependent handles associated with frozen objects which could (e.g. for a short period of time) be indeed unreachable but return back to life after.

Co-authored-by: Jan Kotas <[email protected]>
Co-authored-by: Jakob Botsch Nielsen <[email protected]>
@ghost ghost locked as resolved and limited conversation to collaborators Oct 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RyuJIT] inline CORINFO_HELP_TYPEHANDLE_TO_RUNTIMETYPE
4 participants