No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BurstILPostProcessor.cs 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using Mono.Cecil;
  7. using Mono.Cecil.Cil;
  8. using Unity.CompilationPipeline.Common.Diagnostics;
  9. using Unity.CompilationPipeline.Common.ILPostProcessing;
  10. // TODO: Once DOTS has the latest 2022.2 editor merged into their fork, this can be UNITY_2022_2_OR_NEWER!
  11. #if UNITY_2023_1_OR_NEWER
  12. using ILPostProcessorToUse = zzzUnity.Burst.CodeGen.ILPostProcessing;
  13. #else
  14. using ILPostProcessorToUse = zzzUnity.Burst.CodeGen.ILPostProcessingLegacy;
  15. #endif
  16. /// Deliberately named zzzUnity.Burst.CodeGen, as we need to ensure its last in the chain
  17. namespace zzzUnity.Burst.CodeGen
  18. {
  19. /// <summary>
  20. /// Postprocessor used to replace calls from C# to [BurstCompile] functions to direct calls to
  21. /// Burst native code functions without having to go through a C# delegate.
  22. /// </summary>
  23. internal class BurstILPostProcessor : ILPostProcessor
  24. {
  25. private sealed class CachedAssemblyResolver : AssemblyResolver
  26. {
  27. private Dictionary<string, AssemblyDefinition> _cache = new Dictionary<string, AssemblyDefinition>();
  28. private Dictionary<string, string> _knownLocations = new Dictionary<string, string>();
  29. public void RegisterKnownLocation(string path)
  30. {
  31. var k = Path.GetFileNameWithoutExtension(path);
  32. // If an assembly is referenced multiple times, resolve to the first one
  33. if (!_knownLocations.ContainsKey(k))
  34. {
  35. _knownLocations.Add(k, path);
  36. }
  37. }
  38. public override AssemblyDefinition Resolve(AssemblyNameReference name)
  39. {
  40. if (!_cache.TryGetValue(name.FullName, out var definition))
  41. {
  42. if (_knownLocations.TryGetValue(name.Name, out var path))
  43. {
  44. definition = LoadFromFile(path);
  45. }
  46. else
  47. {
  48. definition = base.Resolve(name);
  49. }
  50. _cache.Add(name.FullName, definition);
  51. }
  52. return definition;
  53. }
  54. }
  55. public bool IsDebugging;
  56. public int DebuggingLevel;
  57. private void SetupDebugging()
  58. {
  59. // This can be setup to get more diagnostics
  60. var debuggingStr = Environment.GetEnvironmentVariable("UNITY_BURST_DEBUG");
  61. var debugLevel = 0;
  62. IsDebugging = debuggingStr != null && int.TryParse(debuggingStr, out debugLevel) && debugLevel > 0;
  63. if (IsDebugging)
  64. {
  65. Log("[com.unity.burst] Extra debugging is turned on.");
  66. DebuggingLevel = debugLevel;
  67. }
  68. }
  69. private static SequencePoint FindBestSequencePointFor(MethodDefinition method, Instruction instruction)
  70. {
  71. var sequencePoints = method.DebugInformation?.GetSequencePointMapping().Values.OrderBy(s => s.Offset).ToList();
  72. if (sequencePoints == null || !sequencePoints.Any())
  73. return null;
  74. for (int i = 0; i != sequencePoints.Count-1; i++)
  75. {
  76. if (sequencePoints[i].Offset < instruction.Offset &&
  77. sequencePoints[i + 1].Offset > instruction.Offset)
  78. return sequencePoints[i];
  79. }
  80. return sequencePoints.FirstOrDefault();
  81. }
  82. private static DiagnosticMessage MakeDiagnosticError(MethodDefinition method, Instruction errorLocation, string message)
  83. {
  84. var m = new DiagnosticMessage { DiagnosticType = DiagnosticType.Error };
  85. var sPoint = errorLocation != null ? FindBestSequencePointFor(method, errorLocation) : null;
  86. if (sPoint!=null)
  87. {
  88. m.Column = sPoint.StartColumn;
  89. m.Line = sPoint.StartLine;
  90. m.File = sPoint.Document.Url;
  91. }
  92. m.MessageData = message;
  93. return m;
  94. }
  95. public override unsafe ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
  96. {
  97. var diagnostics = new List<DiagnosticMessage>();
  98. if (!WillProcess(compiledAssembly))
  99. return new ILPostProcessResult(null, diagnostics);
  100. bool wasModified = false;
  101. SetupDebugging();
  102. bool debugging = IsDebugging && DebuggingLevel >= 2;
  103. var inMemoryAssembly = compiledAssembly.InMemoryAssembly;
  104. var peData = inMemoryAssembly.PeData;
  105. var pdbData = inMemoryAssembly.PdbData;
  106. var loader = new CachedAssemblyResolver();
  107. var folders = new HashSet<string>();
  108. var isForEditor = compiledAssembly.Defines?.Contains("UNITY_EDITOR") ?? false;
  109. foreach (var reference in compiledAssembly.References)
  110. {
  111. loader.RegisterKnownLocation(reference);
  112. folders.Add(Path.Combine(Environment.CurrentDirectory, Path.GetDirectoryName(reference)));
  113. }
  114. var folderList = folders.OrderBy(x => x).ToList();
  115. foreach (var folder in folderList)
  116. {
  117. loader.AddSearchDirectory(folder);
  118. }
  119. var clock = Stopwatch.StartNew();
  120. if (debugging)
  121. {
  122. Log($"Start processing assembly {compiledAssembly.Name}, IsForEditor: {isForEditor}, Folders: {string.Join("\n", folderList)}");
  123. }
  124. var ilPostProcessing = new ILPostProcessorToUse(loader, isForEditor,
  125. (m,i,s) => { diagnostics.Add(MakeDiagnosticError(m, i, s)); },
  126. IsDebugging ? Log : (LogDelegate)null, DebuggingLevel);
  127. var functionPointerProcessing = new FunctionPointerInvokeTransform(loader,
  128. (m,i,s) => { diagnostics.Add(MakeDiagnosticError(m, i, s)); },
  129. IsDebugging ? Log : (LogDelegate)null, DebuggingLevel);
  130. try
  131. {
  132. // For IL Post Processing, use the builtin symbol reader provider
  133. var assemblyDefinition = loader.LoadFromStream(new MemoryStream(peData), new MemoryStream(pdbData), new PortablePdbReaderProvider() );
  134. wasModified |= ilPostProcessing.Run(assemblyDefinition);
  135. wasModified |= functionPointerProcessing.Run(assemblyDefinition);
  136. if (wasModified)
  137. {
  138. var peStream = new MemoryStream();
  139. var pdbStream = new MemoryStream();
  140. var writeParameters = new WriterParameters
  141. {
  142. SymbolWriterProvider = new PortablePdbWriterProvider(),
  143. WriteSymbols = true,
  144. SymbolStream = pdbStream
  145. };
  146. assemblyDefinition.Write(peStream, writeParameters);
  147. peStream.Flush();
  148. pdbStream.Flush();
  149. peData = peStream.ToArray();
  150. pdbData = pdbStream.ToArray();
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. throw new InvalidOperationException($"Internal compiler error for Burst ILPostProcessor on {compiledAssembly.Name}. Exception: {ex}");
  156. }
  157. if (debugging)
  158. {
  159. Log($"End processing assembly {compiledAssembly.Name} in {clock.Elapsed.TotalMilliseconds}ms.");
  160. }
  161. if (wasModified && !diagnostics.Any(d => d.DiagnosticType == DiagnosticType.Error))
  162. {
  163. return new ILPostProcessResult(new InMemoryAssembly(peData, pdbData), diagnostics);
  164. }
  165. return new ILPostProcessResult(null, diagnostics);
  166. }
  167. private static void Log(string message)
  168. {
  169. Console.WriteLine($"{nameof(BurstILPostProcessor)}: {message}");
  170. }
  171. public override ILPostProcessor GetInstance()
  172. {
  173. return this;
  174. }
  175. public override bool WillProcess(ICompiledAssembly compiledAssembly)
  176. {
  177. return compiledAssembly.References.Any(f => Path.GetFileName(f) == "Unity.Burst.dll");
  178. }
  179. }
  180. }