2022-09-22 08:56:07 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using HybridCLR.Editor.Meta;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace HybridCLR.Editor.AOT
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class Analyzer
|
|
|
|
|
{
|
|
|
|
|
public class Options
|
|
|
|
|
{
|
|
|
|
|
public AssemblyReferenceDeepCollector Collector { get; set; }
|
|
|
|
|
|
|
|
|
|
public int MaxIterationCount { get; set; }
|
2024-05-30 12:11:05 +08:00
|
|
|
|
|
|
|
|
|
public bool ComputeAotAssembly { get; set; }
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly int _maxInterationCount;
|
|
|
|
|
|
|
|
|
|
private readonly AssemblyReferenceDeepCollector _assemblyCollector;
|
|
|
|
|
|
2024-05-30 12:11:05 +08:00
|
|
|
|
private readonly bool _computeAotAssembly;
|
|
|
|
|
|
2022-09-22 08:56:07 +08:00
|
|
|
|
private readonly HashSet<GenericClass> _genericTypes = new HashSet<GenericClass>();
|
|
|
|
|
private readonly HashSet<GenericMethod> _genericMethods = new HashSet<GenericMethod>();
|
|
|
|
|
|
|
|
|
|
private List<GenericMethod> _processingMethods = new List<GenericMethod>();
|
|
|
|
|
private List<GenericMethod> _newMethods = new List<GenericMethod>();
|
|
|
|
|
|
|
|
|
|
public IReadOnlyCollection<GenericClass> GenericTypes => _genericTypes;
|
|
|
|
|
|
|
|
|
|
public IReadOnlyCollection<GenericMethod> GenericMethods => _genericMethods;
|
|
|
|
|
|
|
|
|
|
private readonly MethodReferenceAnalyzer _methodReferenceAnalyzer;
|
|
|
|
|
|
|
|
|
|
private readonly HashSet<string> _hotUpdateAssemblyFiles;
|
|
|
|
|
|
2022-09-23 15:16:43 +08:00
|
|
|
|
public ConstraintContext ConstraintContext { get; } = new ConstraintContext();
|
|
|
|
|
|
2022-09-23 14:26:28 +08:00
|
|
|
|
public List<GenericClass> AotGenericTypes { get; } = new List<GenericClass>();
|
|
|
|
|
|
|
|
|
|
public List<GenericMethod> AotGenericMethods { get; } = new List<GenericMethod>();
|
|
|
|
|
|
2022-09-22 08:56:07 +08:00
|
|
|
|
public Analyzer(Options options)
|
|
|
|
|
{
|
|
|
|
|
_assemblyCollector = options.Collector;
|
|
|
|
|
_maxInterationCount = options.MaxIterationCount;
|
2024-05-30 12:11:05 +08:00
|
|
|
|
_computeAotAssembly = options.ComputeAotAssembly;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
_methodReferenceAnalyzer = new MethodReferenceAnalyzer(this.OnNewMethod);
|
|
|
|
|
_hotUpdateAssemblyFiles = new HashSet<string>(options.Collector.GetRootAssemblyNames().Select(assName => assName + ".dll"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TryAddAndWalkGenericType(GenericClass gc)
|
|
|
|
|
{
|
|
|
|
|
if (gc == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
gc = gc.ToGenericShare();
|
2023-08-13 13:07:37 +08:00
|
|
|
|
if (_genericTypes.Add(gc) && NeedWalk(null, gc.Type))
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
WalkType(gc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 13:07:37 +08:00
|
|
|
|
private bool NeedWalk(MethodDef callFrom, TypeDef type)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
2023-08-13 13:07:37 +08:00
|
|
|
|
return _hotUpdateAssemblyFiles.Contains(type.Module.Name) || callFrom == null || callFrom.HasGenericParameters;
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 14:26:28 +08:00
|
|
|
|
private bool IsAotType(TypeDef type)
|
|
|
|
|
{
|
2024-05-30 12:11:05 +08:00
|
|
|
|
return _computeAotAssembly || !_hotUpdateAssemblyFiles.Contains(type.Module.Name);
|
2022-09-23 14:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsAotGenericMethod(MethodDef method)
|
|
|
|
|
{
|
|
|
|
|
return IsAotType(method.DeclaringType) && method.HasGenericParameters;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 11:47:17 +08:00
|
|
|
|
private void OnNewMethod(MethodDef methodDef, List<TypeSig> klassGenericInst, List<TypeSig> methodGenericInst, GenericMethod method)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
if(method == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-13 13:07:37 +08:00
|
|
|
|
if (NeedWalk(methodDef, method.Method.DeclaringType) && _genericMethods.Add(method))
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
_newMethods.Add(method);
|
|
|
|
|
}
|
|
|
|
|
if (method.KlassInst != null)
|
|
|
|
|
{
|
|
|
|
|
TryAddAndWalkGenericType(new GenericClass(method.Method.DeclaringType, method.KlassInst));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TryAddMethodNotWalkType(GenericMethod method)
|
|
|
|
|
{
|
|
|
|
|
if (method == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-13 13:07:37 +08:00
|
|
|
|
if (NeedWalk(null, method.Method.DeclaringType) && _genericMethods.Add(method))
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
_newMethods.Add(method);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WalkType(GenericClass gc)
|
|
|
|
|
{
|
|
|
|
|
//Debug.Log($"typespec:{sig} {sig.GenericType} {sig.GenericType.TypeDefOrRef.ResolveTypeDef()}");
|
|
|
|
|
//Debug.Log($"== walk generic type:{new GenericInstSig(gc.Type.ToTypeSig().ToClassOrValueTypeSig(), gc.KlassInst)}");
|
|
|
|
|
ITypeDefOrRef baseType = gc.Type.BaseType;
|
|
|
|
|
if (baseType != null && baseType.TryGetGenericInstSig() != null)
|
|
|
|
|
{
|
|
|
|
|
GenericClass parentType = GenericClass.ResolveClass((TypeSpec)baseType, new GenericArgumentContext(gc.KlassInst, null));
|
|
|
|
|
TryAddAndWalkGenericType(parentType);
|
|
|
|
|
}
|
|
|
|
|
foreach (var method in gc.Type.Methods)
|
|
|
|
|
{
|
|
|
|
|
if (method.HasGenericParameters || !method.HasBody || method.Body.Instructions == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
var gm = new GenericMethod(method, gc.KlassInst, null).ToGenericShare();
|
|
|
|
|
//Debug.Log($"add method:{gm.Method} {gm.KlassInst}");
|
|
|
|
|
TryAddMethodNotWalkType(gm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WalkType(TypeDef typeDef)
|
|
|
|
|
{
|
|
|
|
|
if (typeDef.HasGenericParameters)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
ITypeDefOrRef baseType = typeDef.BaseType;
|
|
|
|
|
if (baseType != null && baseType.TryGetGenericInstSig() != null)
|
|
|
|
|
{
|
|
|
|
|
GenericClass gc = GenericClass.ResolveClass((TypeSpec)baseType, null);
|
|
|
|
|
TryAddAndWalkGenericType(gc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Prepare()
|
|
|
|
|
{
|
|
|
|
|
// 将所有非泛型函数全部加入函数列表,同时立马walk这些method。
|
|
|
|
|
// 后续迭代中将只遍历MethodSpec
|
|
|
|
|
foreach (var ass in _assemblyCollector.GetLoadedModulesOfRootAssemblies())
|
|
|
|
|
{
|
|
|
|
|
foreach (TypeDef typeDef in ass.GetTypes())
|
|
|
|
|
{
|
|
|
|
|
WalkType(typeDef);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (uint rid = 1, n = ass.Metadata.TablesStream.TypeSpecTable.Rows; rid <= n; rid++)
|
|
|
|
|
{
|
|
|
|
|
var ts = ass.ResolveTypeSpec(rid);
|
2023-08-13 13:07:37 +08:00
|
|
|
|
var cs = GenericClass.ResolveClass(ts, null)?.ToGenericShare();
|
|
|
|
|
if (cs != null)
|
2022-09-22 08:56:07 +08:00
|
|
|
|
{
|
|
|
|
|
TryAddAndWalkGenericType(cs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (uint rid = 1, n = ass.Metadata.TablesStream.MethodSpecTable.Rows; rid <= n; rid++)
|
|
|
|
|
{
|
|
|
|
|
var ms = ass.ResolveMethodSpec(rid);
|
|
|
|
|
var gm = GenericMethod.ResolveMethod(ms, null)?.ToGenericShare();
|
|
|
|
|
TryAddMethodNotWalkType(gm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Debug.Log($"PostPrepare genericTypes:{_genericTypes.Count} genericMethods:{_genericMethods.Count} newMethods:{_newMethods.Count}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RecursiveCollect()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _maxInterationCount && _newMethods.Count > 0; i++)
|
|
|
|
|
{
|
|
|
|
|
var temp = _processingMethods;
|
|
|
|
|
_processingMethods = _newMethods;
|
|
|
|
|
_newMethods = temp;
|
|
|
|
|
_newMethods.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var method in _processingMethods)
|
|
|
|
|
{
|
|
|
|
|
_methodReferenceAnalyzer.WalkMethod(method.Method, method.KlassInst, method.MethodInst);
|
|
|
|
|
}
|
|
|
|
|
Debug.Log($"iteration:[{i}] genericClass:{_genericTypes.Count} genericMethods:{_genericMethods.Count} newMethods:{_newMethods.Count}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 21:36:04 +08:00
|
|
|
|
private bool IsNotShareableAOTGenericType(TypeDef typeDef)
|
|
|
|
|
{
|
|
|
|
|
if (!IsAotType(typeDef))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return typeDef.GenericParameters.Any(c => !c.HasReferenceTypeConstraint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsNotShareableAOTGenericMethod(MethodDef method)
|
|
|
|
|
{
|
|
|
|
|
if (!IsAotGenericMethod(method))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return method.GenericParameters.Concat(method.DeclaringType.GenericParameters).Any(c => !c.HasReferenceTypeConstraint);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 14:26:28 +08:00
|
|
|
|
private void FilterAOTGenericTypeAndMethods()
|
|
|
|
|
{
|
2022-09-23 15:16:43 +08:00
|
|
|
|
ConstraintContext cc = this.ConstraintContext;
|
2024-01-12 21:36:04 +08:00
|
|
|
|
AotGenericTypes.AddRange(_genericTypes.Where(type => IsNotShareableAOTGenericType(type.Type)).Select(gc => cc.ApplyConstraints(gc)));
|
|
|
|
|
AotGenericMethods.AddRange(_genericMethods.Where(method => IsNotShareableAOTGenericMethod(method.Method)).Select(gm => cc.ApplyConstraints(gm)));
|
2022-09-23 14:26:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-22 08:56:07 +08:00
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
Prepare();
|
|
|
|
|
RecursiveCollect();
|
2022-09-23 14:26:28 +08:00
|
|
|
|
FilterAOTGenericTypeAndMethods();
|
2022-09-22 08:56:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|