This commit is contained in:
2025-03-31 14:55:24 +08:00
parent 613e0f7f61
commit c5ead42ade
207 changed files with 10338 additions and 108 deletions

View File

@@ -0,0 +1,208 @@
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; }
}
private readonly int _maxInterationCount;
private readonly AssemblyReferenceDeepCollector _assemblyCollector;
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;
public ConstraintContext ConstraintContext { get; } = new ConstraintContext();
public List<GenericClass> AotGenericTypes { get; } = new List<GenericClass>();
public List<GenericMethod> AotGenericMethods { get; } = new List<GenericMethod>();
public Analyzer(Options options)
{
_assemblyCollector = options.Collector;
_maxInterationCount = options.MaxIterationCount;
_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();
if (_genericTypes.Add(gc) && NeedWalk(null, gc.Type))
{
WalkType(gc);
}
}
private bool NeedWalk(MethodDef callFrom, TypeDef type)
{
return _hotUpdateAssemblyFiles.Contains(type.Module.Name) || callFrom == null || callFrom.HasGenericParameters;
}
private bool IsAotType(TypeDef type)
{
return !_hotUpdateAssemblyFiles.Contains(type.Module.Name);
}
private bool IsAotGenericMethod(MethodDef method)
{
return IsAotType(method.DeclaringType) && method.HasGenericParameters;
}
private void OnNewMethod(MethodDef methodDef, List<TypeSig> klassGenericInst, List<TypeSig> methodGenericInst, GenericMethod method)
{
if(method == null)
{
return;
}
if (NeedWalk(methodDef, method.Method.DeclaringType) && _genericMethods.Add(method))
{
_newMethods.Add(method);
}
if (method.KlassInst != null)
{
TryAddAndWalkGenericType(new GenericClass(method.Method.DeclaringType, method.KlassInst));
}
}
private void TryAddMethodNotWalkType(GenericMethod method)
{
if (method == null)
{
return;
}
if (NeedWalk(null, method.Method.DeclaringType) && _genericMethods.Add(method))
{
_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);
var cs = GenericClass.ResolveClass(ts, null)?.ToGenericShare();
if (cs != null)
{
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}");
}
}
private void FilterAOTGenericTypeAndMethods()
{
ConstraintContext cc = this.ConstraintContext;
AotGenericTypes.AddRange(_genericTypes.Where(type => IsAotType(type.Type)).Select(gc => cc.ApplyConstraints(gc)));
AotGenericMethods.AddRange(_genericMethods.Where(method => IsAotGenericMethod(method.Method)).Select(gm => cc.ApplyConstraints(gm)));
}
public void Run()
{
Prepare();
RecursiveCollect();
FilterAOTGenericTypeAndMethods();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 30bbf4a80a6cf3a43b3f489747d9dd6a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,73 @@
using dnlib.DotNet;
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HybridCLR.Editor.AOT
{
public class ConstraintContext
{
public class ImplType
{
public TypeSig BaseType { get; }
public List<TypeSig> Interfaces { get; }
public bool ValueType { get; }
private readonly int _hash;
public ImplType(TypeSig baseType, List<TypeSig> interfaces, bool valueType)
{
BaseType = baseType;
Interfaces = interfaces;
ValueType = valueType;
_hash = ComputHash();
}
public override bool Equals(object obj)
{
ImplType o = (ImplType)obj;
return MetaUtil.EqualsTypeSig(this.BaseType, o.BaseType)
&& MetaUtil.EqualsTypeSigArray(this.Interfaces, o.Interfaces)
&& this.ValueType == o.ValueType;
}
public override int GetHashCode()
{
return _hash;
}
private int ComputHash()
{
int hash = 0;
if (BaseType != null)
{
hash = HashUtil.CombineHash(hash, TypeEqualityComparer.Instance.GetHashCode(BaseType));
}
if (Interfaces.Count > 0)
{
hash = HashUtil.CombineHash(hash, HashUtil.ComputHash(Interfaces));
}
return hash;
}
}
public HashSet<ImplType> ImplTypes { get; } = new HashSet<ImplType>();
public GenericClass ApplyConstraints(GenericClass gc)
{
return gc;
}
public GenericMethod ApplyConstraints(GenericMethod gm)
{
return gm;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 812d81a75b690394bbe16ef5f0bcbc46
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,139 @@
using HybridCLR.Editor.Meta;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UnityEngine;
namespace HybridCLR.Editor.AOT
{
public class GenericReferenceWriter
{
private static readonly Dictionary<Type, string> _typeNameMapping = new Dictionary<Type, string>
{
{typeof(bool), "bool" },
{typeof(byte), "byte" },
{typeof(sbyte), "sbyte" },
{typeof(short), "short" },
{typeof(ushort), "ushort" },
{typeof(int), "int" },
{typeof(uint), "uint" },
{typeof(long), "long" },
{typeof(ulong), "ulong" },
{typeof(float), "float" },
{typeof(double), "double" },
{typeof(object), "object" },
{typeof(string), "string" },
};
private readonly Dictionary<string, string> _typeSimpleNameMapping = new Dictionary<string, string>();
private readonly Regex _systemTypePattern;
private readonly Regex _genericPattern = new Regex(@"`\d+");
public GenericReferenceWriter()
{
foreach (var e in _typeNameMapping)
{
_typeSimpleNameMapping.Add(e.Key.FullName, e.Value);
}
_systemTypePattern = new Regex(string.Join("|", _typeSimpleNameMapping.Keys.Select (k => $@"\b{k}\b")));
}
public string PrettifyTypeSig(string typeSig)
{
string s = _genericPattern.Replace(typeSig, "").Replace('/', '.');
return _systemTypePattern.Replace(s, m => _typeSimpleNameMapping[m.Groups[0].Value]);
}
public string PrettifyMethodSig(string methodSig)
{
string s = PrettifyTypeSig(methodSig).Replace("::", ".");
if (s.Contains(".ctor("))
{
s = "new " + s.Replace(".ctor(", "(");
}
return s;
}
public void Write(List<GenericClass> types, List<GenericMethod> methods, string outputFile)
{
string parentDir = Directory.GetParent(outputFile).FullName;
Directory.CreateDirectory(parentDir);
List<string> codes = new List<string>();
codes.Add("using System.Collections.Generic;");
codes.Add("public class AOTGenericReferences : UnityEngine.MonoBehaviour");
codes.Add("{");
codes.Add("");
codes.Add("\t// {{ AOT assemblies");
codes.Add("\tpublic static readonly IReadOnlyList<string> PatchedAOTAssemblyList = new List<string>");
codes.Add("\t{");
List<dnlib.DotNet.ModuleDef> modules = new HashSet<dnlib.DotNet.ModuleDef>(
types.Select(t => t.Type.Module).Concat(methods.Select(m => m.Method.Module))).ToList();
modules.Sort((a, b) => a.Name.CompareTo(b.Name));
foreach (dnlib.DotNet.ModuleDef module in modules)
{
codes.Add($"\t\t\"{module.Name}\",");
}
codes.Add("\t};");
codes.Add("\t// }}");
codes.Add("");
codes.Add("\t// {{ constraint implement type");
codes.Add("\t// }} ");
codes.Add("");
codes.Add("\t// {{ AOT generic types");
List<string> typeNames = types.Select(t => PrettifyTypeSig(t.ToTypeSig().ToString())).ToList();
typeNames.Sort(string.CompareOrdinal);
foreach(var typeName in typeNames)
{
codes.Add($"\t// {typeName}");
}
codes.Add("\t// }}");
codes.Add("");
codes.Add("\tpublic void RefMethods()");
codes.Add("\t{");
List<(string, string, string)> methodTypeAndNames = methods.Select(m =>
(PrettifyTypeSig(m.Method.DeclaringType.ToString()), PrettifyMethodSig(m.Method.Name), PrettifyMethodSig(m.ToMethodSpec().ToString())))
.ToList();
methodTypeAndNames.Sort((a, b) =>
{
int c = String.Compare(a.Item1, b.Item1, StringComparison.Ordinal);
if (c != 0)
{
return c;
}
c = String.Compare(a.Item2, b.Item2, StringComparison.Ordinal);
if (c != 0)
{
return c;
}
return String.Compare(a.Item3, b.Item3, StringComparison.Ordinal);
});
foreach(var method in methodTypeAndNames)
{
codes.Add($"\t\t// {PrettifyMethodSig(method.Item3)}");
}
codes.Add("\t}");
codes.Add("}");
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
File.WriteAllText(outputFile, string.Join("\n", codes), utf8WithoutBom);
Debug.Log($"[GenericReferenceWriter] write {outputFile}");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d1243cf04685361478972f93b5ca868a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: