Brak opisu
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.

BurstLoader.cs 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Unity.Burst.LowLevel;
  10. #if UNITY_2020_1_OR_NEWER
  11. using Unity.Profiling;
  12. using Unity.Profiling.LowLevel;
  13. using Unity.Profiling.LowLevel.Unsafe;
  14. #endif
  15. using UnityEditor;
  16. using UnityEditor.Compilation;
  17. namespace Unity.Burst.Editor
  18. {
  19. /// <summary>
  20. /// Main entry point for initializing the burst compiler service for both JIT and AOT
  21. /// </summary>
  22. [InitializeOnLoad]
  23. internal class BurstLoader
  24. {
  25. // Cache the delegate to make sure it doesn't get collected.
  26. private static readonly BurstCompilerService.ExtractCompilerFlags TryGetOptionsFromMemberDelegate = TryGetOptionsFromMember;
  27. private static readonly object EagerCompilationLockObject = new object();
  28. private static readonly CancellationTokenSource EagerCompilationTokenSource = new CancellationTokenSource();
  29. private static List<BurstCompileTarget> _cachedCompileTargets;
  30. /// <summary>
  31. /// Gets the location to the runtime path of burst.
  32. /// </summary>
  33. public static string RuntimePath { get; private set; }
  34. public static bool IsDebugging { get; private set; }
  35. public static int DebuggingLevel { get; private set; }
  36. public static bool SafeShutdown { get; private set; }
  37. private static void VersionUpdateCheck()
  38. {
  39. var seek = "com.unity.burst@";
  40. var first = RuntimePath.LastIndexOf(seek);
  41. var last = RuntimePath.LastIndexOf(".Runtime");
  42. string version;
  43. if (first == -1 || last == -1 || last <= first)
  44. {
  45. version = "Unknown";
  46. }
  47. else
  48. {
  49. first += seek.Length;
  50. last -= 1;
  51. version = RuntimePath.Substring(first, last - first);
  52. }
  53. var result = BurstCompiler.VersionNotify(version);
  54. // result will be empty if we are shutting down, and thus we shouldn't popup a dialog
  55. if (!String.IsNullOrEmpty(result) && result != version)
  56. {
  57. if (IsDebugging)
  58. {
  59. UnityEngine.Debug.LogWarning($"[com.unity.burst] - '{result}' != '{version}'");
  60. }
  61. OnVersionChangeDetected();
  62. }
  63. }
  64. private static bool UnityBurstRuntimePathOverwritten(out string path)
  65. {
  66. path = Environment.GetEnvironmentVariable("UNITY_BURST_RUNTIME_PATH");
  67. return Directory.Exists(path);
  68. }
  69. private static void OnVersionChangeDetected()
  70. {
  71. // Write marker file to tell Burst to delete the cache at next startup.
  72. try
  73. {
  74. File.Create(Path.Combine(BurstCompilerOptions.DefaultCacheFolder, BurstCompilerOptions.DeleteCacheMarkerFileName)).Dispose();
  75. }
  76. catch (IOException)
  77. {
  78. // In the unlikely scenario that two processes are creating this marker file at the same time,
  79. // and one of them fails, do nothing because the other one has hopefully succeeded.
  80. }
  81. // Skip checking if we are using an explicit runtime path.
  82. if (!UnityBurstRuntimePathOverwritten(out var _))
  83. {
  84. EditorUtility.DisplayDialog("Burst Package Update Detected", "The version of Burst used by your project has changed. Please restart the Editor to continue.", "OK");
  85. BurstCompiler.Shutdown();
  86. }
  87. }
  88. static BurstLoader()
  89. {
  90. if (BurstCompilerOptions.ForceDisableBurstCompilation)
  91. {
  92. if (!BurstCompilerOptions.IsSecondaryUnityProcess)
  93. {
  94. UnityEngine.Debug.LogWarning("[com.unity.burst] Burst is disabled entirely from the command line");
  95. }
  96. return;
  97. }
  98. // This can be setup to get more diagnostics
  99. var debuggingStr = Environment.GetEnvironmentVariable("UNITY_BURST_DEBUG");
  100. IsDebugging = debuggingStr != null;
  101. if (IsDebugging)
  102. {
  103. UnityEngine.Debug.LogWarning("[com.unity.burst] Extra debugging is turned on.");
  104. int debuggingLevel;
  105. int.TryParse(debuggingStr, out debuggingLevel);
  106. if (debuggingLevel <= 0) debuggingLevel = 1;
  107. DebuggingLevel = debuggingLevel;
  108. }
  109. // Try to load the runtime through an environment variable
  110. if (!UnityBurstRuntimePathOverwritten(out var path))
  111. {
  112. // Otherwise try to load it from the package itself
  113. path = Path.GetFullPath("Packages/com.unity.burst/.Runtime");
  114. }
  115. RuntimePath = path;
  116. if (IsDebugging)
  117. {
  118. UnityEngine.Debug.LogWarning($"[com.unity.burst] Runtime directory set to {RuntimePath}");
  119. }
  120. BurstCompilerService.Initialize(RuntimePath, TryGetOptionsFromMemberDelegate);
  121. // It's important that this call comes *after* BurstCompilerService.Initialize,
  122. // otherwise any calls from within EnsureSynchronized to BurstCompilerService,
  123. // such as BurstCompiler.Disable(), will silently fail.
  124. BurstEditorOptions.EnsureSynchronized();
  125. EditorApplication.quitting += OnEditorApplicationQuitting;
  126. #if UNITY_2019_1_OR_NEWER
  127. CompilationPipeline.compilationStarted += OnCompilationStarted;
  128. #endif
  129. #if !UNITY_2021_1_OR_NEWER
  130. UnityEditor.Compilation.CompilationPipeline.assemblyCompilationStarted += OnAssemblyCompilationStarted;
  131. #endif
  132. UnityEditor.Compilation.CompilationPipeline.assemblyCompilationFinished += OnAssemblyCompilationFinished;
  133. EditorApplication.playModeStateChanged += EditorApplicationOnPlayModeStateChanged;
  134. AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
  135. SafeShutdown = false;
  136. #if UNITY_2020_2_OR_NEWER
  137. UnityEditor.PackageManager.Events.registeringPackages += PackageRegistrationEvent;
  138. SafeShutdown = BurstCompiler.IsApiAvailable("SafeShutdown");
  139. #endif
  140. if (!SafeShutdown)
  141. {
  142. VersionUpdateCheck();
  143. }
  144. BurstReflection.EnsureInitialized();
  145. #if !UNITY_2019_3_OR_NEWER
  146. // Workaround to update the list of assembly folders as soon as possible
  147. // in order for the JitCompilerService to not fail with AssemblyResolveExceptions.
  148. // This workaround is only necessary for editors prior to 2019.3 (i.e. 2018.4),
  149. // because 2019.3+ include a fix on the Unity side.
  150. try
  151. {
  152. var assemblyList = BurstReflection.AllEditorAssemblies;
  153. var assemblyFolders = new HashSet<string>();
  154. foreach (var assembly in assemblyList)
  155. {
  156. try
  157. {
  158. var fullPath = Path.GetFullPath(assembly.Location);
  159. var assemblyFolder = Path.GetDirectoryName(fullPath);
  160. if (!string.IsNullOrEmpty(assemblyFolder))
  161. {
  162. assemblyFolders.Add(assemblyFolder);
  163. }
  164. }
  165. catch
  166. {
  167. // ignore
  168. }
  169. }
  170. // Notify the compiler
  171. var assemblyFolderList = assemblyFolders.ToList();
  172. if (IsDebugging)
  173. {
  174. UnityEngine.Debug.Log($"Burst - Change of list of assembly folders:\n{string.Join("\n", assemblyFolderList)}");
  175. }
  176. BurstCompiler.UpdateAssemblerFolders(assemblyFolderList);
  177. }
  178. catch
  179. {
  180. // ignore
  181. }
  182. #endif
  183. // Notify the compiler about a domain reload
  184. if (IsDebugging)
  185. {
  186. UnityEngine.Debug.Log("Burst - Domain Reload");
  187. }
  188. // Notify the JitCompilerService about a domain reload
  189. BurstCompiler.DomainReload();
  190. #if UNITY_2020_1_OR_NEWER
  191. BurstCompiler.OnProgress += OnProgress;
  192. BurstCompiler.SetProgressCallback();
  193. BurstCompiler.OnProfileBegin += OnProfileBegin;
  194. BurstCompiler.OnProfileEnd += OnProfileEnd;
  195. BurstCompiler.SetProfilerCallbacks();
  196. #endif
  197. // Make sure BurstRuntime is initialized
  198. BurstRuntime.Initialize();
  199. // Schedule upfront compilation of all methods in all assemblies,
  200. // with the goal of having as many methods as possible Burst-compiled
  201. // by the time the user enters PlayMode.
  202. if (!EditorApplication.isPlayingOrWillChangePlaymode)
  203. {
  204. MaybeTriggerEagerCompilation();
  205. }
  206. #if UNITY_2020_1_OR_NEWER
  207. // Can't call Menu.AddMenuItem immediately, presumably because the menu controller isn't initialized yet.
  208. EditorApplication.CallDelayed(() => CreateDynamicMenuItems());
  209. #endif
  210. }
  211. private static bool _isQuitting;
  212. private static void OnEditorApplicationQuitting()
  213. {
  214. _isQuitting = true;
  215. }
  216. #if UNITY_2020_2_OR_NEWER
  217. public static Action OnBurstShutdown;
  218. private static void PackageRegistrationEvent(UnityEditor.PackageManager.PackageRegistrationEventArgs obj)
  219. {
  220. bool requireCleanup = false;
  221. if (SafeShutdown)
  222. {
  223. foreach (var changed in obj.changedFrom)
  224. {
  225. if (changed.name.Contains("com.unity.burst"))
  226. {
  227. requireCleanup = true;
  228. break;
  229. }
  230. }
  231. }
  232. foreach (var removed in obj.removed)
  233. {
  234. if (removed.name.Contains("com.unity.burst"))
  235. {
  236. requireCleanup = true;
  237. }
  238. }
  239. if (requireCleanup)
  240. {
  241. OnBurstShutdown?.Invoke();
  242. if (!SafeShutdown)
  243. {
  244. EditorUtility.DisplayDialog("Burst Package Has Been Removed", "Please restart the Editor to continue.", "OK");
  245. }
  246. BurstCompiler.Shutdown();
  247. }
  248. }
  249. #endif
  250. #if UNITY_2020_1_OR_NEWER
  251. // Don't initialize to 0 because that could be a valid progress ID.
  252. private static int BurstProgressId = -1;
  253. // If this enum changes, update the benchmarks tool accordingly as we rely on integer value related to this enum
  254. internal enum BurstEagerCompilationStatus
  255. {
  256. NotScheduled,
  257. Scheduled,
  258. Completed
  259. }
  260. // For the time being, this field is only read through reflection
  261. internal static BurstEagerCompilationStatus EagerCompilationStatus;
  262. private static void OnProgress(int current, int total)
  263. {
  264. if (current == total)
  265. {
  266. EagerCompilationStatus = BurstEagerCompilationStatus.Completed;
  267. }
  268. // OnProgress is called from a background thread,
  269. // but we need to update the progress UI on the main thread.
  270. EditorApplication.CallDelayed(() =>
  271. {
  272. if (current == total)
  273. {
  274. // We've finished - remove progress bar.
  275. if (Progress.Exists(BurstProgressId))
  276. {
  277. Progress.Remove(BurstProgressId);
  278. BurstProgressId = -1;
  279. }
  280. }
  281. else
  282. {
  283. // Do we need to create the progress bar?
  284. if (!Progress.Exists(BurstProgressId))
  285. {
  286. BurstProgressId = Progress.Start(
  287. "Burst",
  288. "Compiling...",
  289. Progress.Options.Unmanaged);
  290. }
  291. Progress.Report(
  292. BurstProgressId,
  293. current / (float)total,
  294. $"Compiled {current} / {total} methods");
  295. }
  296. });
  297. }
  298. [ThreadStatic]
  299. private static Dictionary<string, IntPtr> ProfilerMarkers;
  300. private static unsafe void OnProfileBegin(string markerName, string metadataName, string metadataValue)
  301. {
  302. if (ProfilerMarkers == null)
  303. {
  304. // Initialize thread-static dictionary.
  305. ProfilerMarkers = new Dictionary<string, IntPtr>();
  306. }
  307. if (!ProfilerMarkers.TryGetValue(markerName, out var markerPtr))
  308. {
  309. ProfilerMarkers.Add(markerName, markerPtr = ProfilerUnsafeUtility.CreateMarker(
  310. markerName,
  311. ProfilerUnsafeUtility.CategoryScripts,
  312. MarkerFlags.Script,
  313. metadataName != null ? 1 : 0));
  314. // metadataName is assumed to be consistent for a given markerName.
  315. if (metadataName != null)
  316. {
  317. ProfilerUnsafeUtility.SetMarkerMetadata(
  318. markerPtr,
  319. 0,
  320. metadataName,
  321. (byte)ProfilerMarkerDataType.String16,
  322. (byte)ProfilerMarkerDataUnit.Undefined);
  323. }
  324. }
  325. if (metadataName != null && metadataValue != null)
  326. {
  327. fixed (char* methodNamePtr = metadataValue)
  328. {
  329. var metadata = new ProfilerMarkerData
  330. {
  331. Type = (byte)ProfilerMarkerDataType.String16,
  332. Size = ((uint)metadataValue.Length + 1) * 2,
  333. Ptr = methodNamePtr
  334. };
  335. ProfilerUnsafeUtility.BeginSampleWithMetadata(markerPtr, 1, &metadata);
  336. }
  337. }
  338. else
  339. {
  340. ProfilerUnsafeUtility.BeginSample(markerPtr);
  341. }
  342. }
  343. private static void OnProfileEnd(string markerName)
  344. {
  345. if (ProfilerMarkers == null)
  346. {
  347. // If we got here it means we had a domain reload between when we called profile begin and
  348. // now profile end, and so we need to bail out.
  349. return;
  350. }
  351. if (!ProfilerMarkers.TryGetValue(markerName, out var markerPtr))
  352. {
  353. return;
  354. }
  355. ProfilerUnsafeUtility.EndSample(markerPtr);
  356. }
  357. #endif
  358. private static void EditorApplicationOnPlayModeStateChanged(PlayModeStateChange state)
  359. {
  360. if (DebuggingLevel > 2)
  361. {
  362. UnityEngine.Debug.Log($"Burst - Change of Editor State: {state}");
  363. }
  364. switch (state)
  365. {
  366. case PlayModeStateChange.ExitingEditMode:
  367. if (BurstCompiler.Options.RequiresSynchronousCompilation)
  368. {
  369. if (DebuggingLevel > 2)
  370. {
  371. UnityEngine.Debug.Log("Burst - Exiting EditMode - waiting for any pending synchronous jobs");
  372. }
  373. EditorUtility.DisplayProgressBar("Burst", "Waiting for synchronous compilation to finish", -1);
  374. try
  375. {
  376. BurstCompiler.WaitUntilCompilationFinished();
  377. }
  378. finally
  379. {
  380. EditorUtility.ClearProgressBar();
  381. }
  382. if (DebuggingLevel > 2)
  383. {
  384. UnityEngine.Debug.Log("Burst - Exiting EditMode - finished waiting for any pending synchronous jobs");
  385. }
  386. }
  387. else
  388. {
  389. BurstCompiler.ClearEagerCompilationQueues();
  390. if (DebuggingLevel > 2)
  391. {
  392. UnityEngine.Debug.Log("Burst - Exiting EditMode - cleared eager-compilation queues");
  393. }
  394. }
  395. break;
  396. case PlayModeStateChange.ExitingPlayMode:
  397. // If Synchronous Compilation is checked, then we will already have waited for eager-compilation to finish
  398. // before entering playmode. But if it was unchecked, we may have cancelled in-progress eager-compilation.
  399. // We start it again here.
  400. if (!BurstCompiler.Options.RequiresSynchronousCompilation)
  401. {
  402. if (DebuggingLevel > 2)
  403. {
  404. UnityEngine.Debug.Log("Burst - Exiting PlayMode - triggering eager-compilation");
  405. }
  406. MaybeTriggerEagerCompilation();
  407. }
  408. // Cleanup any loaded burst natives so users have a clean point to update the libraries.
  409. BurstCompiler.UnloadAdditionalLibraries();
  410. break;
  411. }
  412. }
  413. private static void CancelEagerCompilationPriorToAssemblyCompilation()
  414. {
  415. BurstCompiler.CancelEagerCompilation();
  416. if (DebuggingLevel > 2)
  417. {
  418. UnityEngine.Debug.Log("Burst - Cancelled eager-compilation prior to assembly compilation");
  419. }
  420. }
  421. #if UNITY_2019_1_OR_NEWER
  422. private static void OnCompilationStarted(object value)
  423. {
  424. if (DebuggingLevel > 2)
  425. {
  426. UnityEngine.Debug.Log($"{DateTime.UtcNow} Burst - compilation started for '{value}'");
  427. }
  428. CancelEagerCompilationPriorToAssemblyCompilation();
  429. }
  430. #endif
  431. private static void OnAssemblyCompilationFinished(string arg1, CompilerMessage[] arg2)
  432. {
  433. if (DebuggingLevel > 2)
  434. {
  435. UnityEngine.Debug.Log($"{DateTime.UtcNow} Burst - Assembly compilation finished for '{arg1}'");
  436. }
  437. }
  438. #if !UNITY_2019_1_OR_NEWER
  439. private static bool _hasCompilationStarted;
  440. #endif
  441. #if !UNITY_2021_1_OR_NEWER
  442. // This callback has been deprecated on 2021_1 and above
  443. private static void OnAssemblyCompilationStarted(string obj)
  444. {
  445. if (DebuggingLevel > 2)
  446. {
  447. UnityEngine.Debug.Log($"{DateTime.UtcNow} Burst - Assembly compilation started for '{obj}'");
  448. }
  449. #if !UNITY_2019_1_OR_NEWER
  450. // This is a workaround for 2018.4 not having the CompilationPipeline.compilationStarted event.
  451. if (!_hasCompilationStarted)
  452. {
  453. CancelEagerCompilationPriorToAssemblyCompilation();
  454. _hasCompilationStarted = true;
  455. }
  456. #endif
  457. }
  458. #endif
  459. private static bool TryGetOptionsFromMember(MemberInfo member, out string flagsOut)
  460. {
  461. return BurstCompiler.Options.TryGetOptions(member, true, out flagsOut);
  462. }
  463. private static void MaybeTriggerEagerCompilation()
  464. {
  465. var isEagerCompilationEnabled =
  466. BurstCompiler.Options.IsEnabled
  467. && Environment.GetEnvironmentVariable("UNITY_BURST_EAGER_COMPILATION_DISABLED") == null
  468. && (!UnityEngine.Application.isBatchMode || Environment.GetEnvironmentVariable("UNITY_BURST_EAGER_COMPILATION_ENABLED") != null);
  469. if (!isEagerCompilationEnabled)
  470. {
  471. return;
  472. }
  473. // Trigger compilation only if one of the following is true:
  474. // 1. Unity version is 2020.1 or older, AND the CompilationPipeline.IsCodegenComplete() API exists and returns true
  475. // 2. Unity version is 2020.1 or older, AND the CompilationPipeline.IsCodegenComplete() API does not exist
  476. // 3. Unity version is 2020.2+
  477. //
  478. // Eager-compilation logging is only enabled if one of the following is true:
  479. // 1. Unity version is 2020.2+
  480. // 2. Unity version is 2020.1 or older, AND the CompilationPipeline.IsCodegenComplete() API exists and returns true
  481. #if UNITY_2020_2_OR_NEWER
  482. var shouldTriggerEagerCompilation = true;
  483. var loggingEnabled = true;
  484. #else
  485. var isCodegenCompleteMethod = typeof(CompilationPipeline).GetMethod("IsCodegenComplete", BindingFlags.NonPublic | BindingFlags.Static);
  486. var hasValidCodegenCompleteMethod =
  487. isCodegenCompleteMethod != null &&
  488. isCodegenCompleteMethod.GetParameters().Length == 0 &&
  489. isCodegenCompleteMethod.ReturnType == typeof(bool);
  490. var shouldTriggerEagerCompilation = true;
  491. var loggingEnabled = false;
  492. if (hasValidCodegenCompleteMethod)
  493. {
  494. try
  495. {
  496. shouldTriggerEagerCompilation = (bool)isCodegenCompleteMethod.Invoke(null, Array.Empty<object>());
  497. loggingEnabled = shouldTriggerEagerCompilation;
  498. if (shouldTriggerEagerCompilation && DebuggingLevel > 2)
  499. {
  500. UnityEngine.Debug.Log("CompilationPipeline.IsCodegenComplete() exists and returned true");
  501. }
  502. }
  503. catch (Exception ex)
  504. {
  505. if (DebuggingLevel > 2)
  506. {
  507. UnityEngine.Debug.Log("CompilationPipeline.IsCodegenComplete() exists but there was an error calling it: " + ex);
  508. }
  509. }
  510. }
  511. #endif
  512. BurstCompiler.EagerCompilationLoggingEnabled = loggingEnabled;
  513. if (shouldTriggerEagerCompilation)
  514. {
  515. TriggerEagerCompilation();
  516. }
  517. }
  518. private static void TriggerEagerCompilation()
  519. {
  520. if (_cachedCompileTargets != null)
  521. {
  522. Task.Run(ScheduleEagerCompilation, EagerCompilationTokenSource.Token);
  523. }
  524. else
  525. {
  526. if (DebuggingLevel > 2)
  527. {
  528. UnityEngine.Debug.Log("Burst - Finding methods for eager-compilation");
  529. }
  530. var assemblyList = BurstReflection.EditorAssembliesThatCanPossiblyContainJobsExcludingTestAssemblies;
  531. Task.Run(
  532. () =>
  533. {
  534. _cachedCompileTargets = BurstReflection.FindExecuteMethods(assemblyList, BurstReflectionAssemblyOptions.ExcludeTestAssemblies).CompileTargets;
  535. ScheduleEagerCompilation();
  536. },
  537. EagerCompilationTokenSource.Token);
  538. }
  539. }
  540. private static void ScheduleEagerCompilation()
  541. {
  542. lock (EagerCompilationLockObject)
  543. {
  544. if (EagerCompilationTokenSource.IsCancellationRequested)
  545. {
  546. return;
  547. }
  548. if (_cachedCompileTargets == null)
  549. {
  550. throw new InvalidOperationException();
  551. }
  552. if (DebuggingLevel > 2)
  553. {
  554. UnityEngine.Debug.Log($"Burst - Starting scheduling eager-compilation");
  555. }
  556. var methodsToCompile = new List<EagerCompilationRequest>();
  557. foreach (var compileTarget in _cachedCompileTargets)
  558. {
  559. var member = compileTarget.IsStaticMethod
  560. ? (MemberInfo)compileTarget.Method
  561. : compileTarget.JobType;
  562. if (BurstCompiler.Options.TryGetOptions(member, true, out var optionsString, isForEagerCompilation: true))
  563. {
  564. if (compileTarget.IsStaticMethod)
  565. {
  566. optionsString += "\n--" + BurstCompilerOptions.OptionJitIsForFunctionPointer;
  567. }
  568. var encodedMethod = BurstCompilerService.GetMethodSignature(compileTarget.Method);
  569. methodsToCompile.Add(new EagerCompilationRequest(encodedMethod, optionsString));
  570. }
  571. }
  572. BurstCompiler.EagerCompileMethods(methodsToCompile);
  573. #if UNITY_2020_1_OR_NEWER
  574. EagerCompilationStatus = BurstEagerCompilationStatus.Scheduled;
  575. #endif
  576. if (DebuggingLevel > 2)
  577. {
  578. UnityEngine.Debug.Log($"Burst - Finished scheduling eager-compilation of {methodsToCompile.Count} methods");
  579. }
  580. }
  581. }
  582. private static void OnDomainUnload(object sender, EventArgs e)
  583. {
  584. if (DebuggingLevel > 2)
  585. {
  586. UnityEngine.Debug.Log($"Burst - OnDomainUnload");
  587. }
  588. lock (EagerCompilationLockObject)
  589. {
  590. EagerCompilationTokenSource.Cancel();
  591. }
  592. BurstCompiler.Cancel();
  593. // This check here is to execute shutdown after all OnDisable's. EditorApplication.quitting event is called before OnDisable's, so we need to shutdown in here.
  594. if (_isQuitting)
  595. {
  596. BurstCompiler.Shutdown();
  597. }
  598. #if UNITY_2020_1_OR_NEWER
  599. // Because of a check in Unity (specifically SCRIPTINGAPI_THREAD_AND_SERIALIZATION_CHECK),
  600. // we are not allowed to call thread-unsafe methods (like Progress.Exists) after the
  601. // kApplicationTerminating bit has been set. And because the domain is unloaded
  602. // (thus triggering AppDomain.DomainUnload) *after* that bit is set, we can't call Progress.Exists
  603. // during shutdown. So we check _isQuitting here. When quitting, it's fine for the progress item
  604. // not to be removed since it's all being torn down anyway.
  605. if (!_isQuitting && Progress.Exists(BurstProgressId))
  606. {
  607. Progress.Remove(BurstProgressId);
  608. BurstProgressId = -1;
  609. }
  610. #endif
  611. }
  612. #if UNITY_2020_1_OR_NEWER
  613. private static void CreateDynamicMenuItems()
  614. {
  615. if (Unsupported.IsDeveloperMode())
  616. {
  617. Menu.AddMenuItem(
  618. "Jobs/Burst/Clear JIT Cache",
  619. "",
  620. false,
  621. 1001, // Add at bottom of Burst menu, below standard items which have default priority of 1000
  622. () =>
  623. {
  624. BurstEditorUtility.RequestClearJitCache();
  625. EditorUtility.RequestScriptReload();
  626. },
  627. () => !EditorApplication.isPlayingOrWillChangePlaymode);
  628. }
  629. }
  630. #endif
  631. }
  632. }
  633. #endif