From f025132b8c5698d90f82a44ad0e5cd309434dde9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 18 Jul 2022 18:02:30 +0900 Subject: [PATCH] Generate fewer type loader templates (#72350) --- .../Compiler/AnalysisBasedMetadataManager.cs | 13 +------- .../ILCompiler.Compiler/Compiler/Compilation.cs | 6 ++++ .../DependencyAnalysis/NativeLayoutVertexNode.cs | 1 + .../Compiler/GeneratingMetadataManager.cs | 29 ---------------- .../Compiler/MetadataManager.cs | 39 +++++----------------- 5 files changed, 17 insertions(+), 71 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/AnalysisBasedMetadataManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/AnalysisBasedMetadataManager.cs index 8bda4a1..1eec083 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/AnalysisBasedMetadataManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/AnalysisBasedMetadataManager.cs @@ -199,18 +199,7 @@ namespace ILCompiler if ((pair.Value & MetadataCategory.RuntimeMapping) != 0) { FieldDesc field = pair.Key; - - // We only care about static fields at this point. Instance fields don't need - // runtime artifacts generated in the image. - if (field.IsStatic && !field.IsLiteral) - { - if (field.IsThreadStatic) - rootProvider.RootThreadStaticBaseForType(field.OwningType, reason); - else if (field.HasGCStaticBase) - rootProvider.RootGCStaticBaseForType(field.OwningType, reason); - else - rootProvider.RootNonGCStaticBaseForType(field.OwningType, reason); - } + rootProvider.AddReflectionRoot(field, reason); } } diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs index e234401..bd21f15 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs @@ -666,5 +666,11 @@ namespace ILCompiler result = comparer.Compare(Method, other.Method); return result; } + public override bool Equals(object obj) => + obj is ConstrainedCallInfo other + && ConstrainedType == other.ConstrainedType + && Method == other.Method; + + public override int GetHashCode() => HashCode.Combine(ConstrainedType, Method); } } diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NativeLayoutVertexNode.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NativeLayoutVertexNode.cs index 951a12b..d3e4ec3 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NativeLayoutVertexNode.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/DependencyAnalysis/NativeLayoutVertexNode.cs @@ -862,6 +862,7 @@ namespace ILCompiler.DependencyAnalysis { _method = method; Debug.Assert(method.HasInstantiation); + Debug.Assert(!method.IsGenericMethodDefinition); Debug.Assert(method.IsCanonicalMethod(CanonicalFormKind.Any)); Debug.Assert(method.GetCanonMethodTarget(CanonicalFormKind.Specific) == method, "Assert that the canonical method passed in is in standard canonical form"); } diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/GeneratingMetadataManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/GeneratingMetadataManager.cs index 65aa138..10e0f40 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/GeneratingMetadataManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/GeneratingMetadataManager.cs @@ -218,34 +218,5 @@ namespace ILCompiler return InstantiateCanonicalDynamicInvokeMethodForMethod(thunk, method); } - - protected override void GetDependenciesDueToEETypePresence(ref DependencyList dependencies, NodeFactory factory, TypeDesc type) - { - if (!ConstructedEETypeNode.CreationAllowed(type)) - { - // Both EETypeNode and ConstructedEETypeNode call into this logic. EETypeNode will only call for unconstructable - // EETypes. We don't have templates for those. - return; - } - - DefType closestDefType = type.GetClosestDefType(); - - // TODO-SIZE: this is overly generous in the templates we create - if (_blockingPolicy is FullyBlockedMetadataBlockingPolicy) - return; - - if (closestDefType.HasInstantiation) - { - TypeDesc canonType = type.ConvertToCanonForm(CanonicalFormKind.Specific); - TypeDesc canonClosestDefType = closestDefType.ConvertToCanonForm(CanonicalFormKind.Specific); - - // Add a dependency on the template for this type, if the canonical type should be generated into this binary. - // If the type is an array type, the check should be on its underlying Array type. This is because a copy of - // an array type gets placed into each module but the Array type only exists in the defining module and only - // one template is needed for the Array type by the dynamic type loader. - if (canonType.IsCanonicalSubtype(CanonicalFormKind.Any) && !factory.NecessaryTypeSymbol(canonClosestDefType).RepresentsIndirectionCell) - dependencies.Add(factory.NativeLayout.TemplateTypeLayout(canonType), "Template Type Layout"); - } - } } } diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/MetadataManager.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/MetadataManager.cs index 489a13a..c0e4920 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/MetadataManager.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/MetadataManager.cs @@ -345,6 +345,9 @@ namespace ILCompiler ReflectionInvokeSupportDependencyAlgorithm.GetDependenciesFromParamsArray(ref dependencies, factory, method); } + + GenericMethodsTemplateMap.GetTemplateMethodDependencies(ref dependencies, factory, method); + GenericTypesTemplateMap.GetTemplateTypeDependencies(ref dependencies, factory, method.OwningType); } } @@ -359,6 +362,12 @@ namespace ILCompiler { GetMetadataDependenciesDueToReflectability(ref dependencies, factory, field); } + + if ((category & MetadataCategory.RuntimeMapping) != 0) + { + TypeDesc owningCanonicalType = field.OwningType.ConvertToCanonForm(CanonicalFormKind.Specific); + GenericTypesTemplateMap.GetTemplateTypeDependencies(ref dependencies, factory, owningCanonicalType); + } } /// @@ -393,8 +402,6 @@ namespace ILCompiler { GetMetadataDependenciesDueToReflectability(ref dependencies, factory, type); } - - GetDependenciesDueToEETypePresence(ref dependencies, factory, type); } protected virtual void GetMetadataDependenciesDueToReflectability(ref DependencyList dependencies, NodeFactory factory, TypeDesc type) @@ -404,11 +411,6 @@ namespace ILCompiler // and property setters) } - protected virtual void GetDependenciesDueToEETypePresence(ref DependencyList dependencies, NodeFactory factory, TypeDesc type) - { - // MetadataManagers can override this to provide additional dependencies caused by the emission of an MethodTable. - } - public virtual void GetConditionalDependenciesDueToEETypePresence(ref CombinedDependencyList dependencies, NodeFactory factory, TypeDesc type) { // MetadataManagers can override this to provide additional dependencies caused by the presence of @@ -464,7 +466,6 @@ namespace ILCompiler ExactMethodInstantiationsNode.GetExactMethodInstantiationDependenciesForMethod(ref dependencies, factory, method); } - GetDependenciesDueToTemplateTypeLoader(ref dependencies, factory, method); GetDependenciesDueToMethodCodePresenceInternal(ref dependencies, factory, method, methodIL); } @@ -480,28 +481,6 @@ namespace ILCompiler // method code. } - private void GetDependenciesDueToTemplateTypeLoader(ref DependencyList dependencies, NodeFactory factory, MethodDesc method) - { - // TODO-SIZE: this is overly generous in the templates we create - if (_blockingPolicy is FullyBlockedMetadataBlockingPolicy) - return; - - if (method.HasInstantiation) - { - GenericMethodsTemplateMap.GetTemplateMethodDependencies(ref dependencies, factory, method); - } - else - { - TypeDesc owningTemplateType = method.OwningType; - - // Unboxing and Instantiating stubs use a different type as their template - if (factory.TypeSystemContext.IsSpecialUnboxingThunk(method)) - owningTemplateType = factory.TypeSystemContext.GetTargetOfSpecialUnboxingThunk(method).OwningType; - - GenericTypesTemplateMap.GetTemplateTypeDependencies(ref dependencies, factory, owningTemplateType); - } - } - protected virtual void GetDependenciesDueToMethodCodePresenceInternal(ref DependencyList dependencies, NodeFactory factory, MethodDesc method, MethodIL methodIL) { // MetadataManagers can override this to provide additional dependencies caused by the presence of a -- 2.7.4