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.

BurstCompileTarget.cs 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace Unity.Burst.Editor
  7. {
  8. [DebuggerDisplay("{GetDisplayName(),nq}")]
  9. internal class BurstCompileTarget
  10. {
  11. public BurstCompileTarget(MethodInfo method, Type jobType, Type interfaceType, bool isStaticMethod)
  12. {
  13. Method = method ?? throw new ArgumentNullException(nameof(method));
  14. JobType = jobType ?? throw new ArgumentNullException(nameof(jobType));
  15. JobInterfaceType = interfaceType; // can be null
  16. // This is important to clone the options as we don't want to modify the global instance
  17. Options = BurstCompiler.Options.Clone();
  18. Options.EnableBurstCompilation = true;
  19. // Enable safety checks by default to match inspector default behavior
  20. Options.EnableBurstSafetyChecks = true;
  21. TargetCpu = BurstTargetCpu.Auto;
  22. // The BurstCompilerAttribute can be either on the type or on the method
  23. IsStaticMethod = isStaticMethod;
  24. }
  25. /// <summary>
  26. /// <c>true</c> if the <see cref="Method"/> is directly tagged with a [BurstCompile] attribute
  27. /// </summary>
  28. public readonly bool IsStaticMethod;
  29. /// <summary>
  30. /// The Execute method of the target's producer type.
  31. /// </summary>
  32. public readonly MethodInfo Method;
  33. /// <summary>
  34. /// The type of the actual job (i.e. BoidsSimulationJob).
  35. /// </summary>
  36. public readonly Type JobType;
  37. /// <summary>
  38. /// The interface of the job (IJob, IJobParallelFor...)
  39. /// </summary>
  40. public readonly Type JobInterfaceType;
  41. /// <summary>
  42. /// The default compiler options
  43. /// </summary>
  44. public readonly BurstCompilerOptions Options;
  45. public BurstTargetCpu TargetCpu { get; set; }
  46. /// <summary>
  47. /// Set to true if burst compilation is actually requested via proper `[BurstCompile]` attribute:
  48. /// - On the job if it is a job only
  49. /// - On the method and parent class it if is a static method
  50. /// </summary>
  51. public bool HasRequiredBurstCompileAttributes => BurstCompilerOptions.HasBurstCompileAttribute(JobType) && (!IsStaticMethod || BurstCompilerOptions.HasBurstCompileAttribute(Method));
  52. /// <summary>
  53. /// Generated raw disassembly (IR, IL, ASM...), or null if disassembly failed (only valid for the current TargetCpu)
  54. /// </summary>
  55. public string RawDisassembly;
  56. /// <summary>
  57. /// Formatted disassembly for the associated <see cref="RawDisassembly"/>, currently only valid for <see cref="Unity.Burst.Editor.DisassemblyKind.Asm"/>
  58. /// </summary>
  59. public string FormattedDisassembly;
  60. public DisassemblyKind DisassemblyKind;
  61. public bool IsDarkMode { get; set; }
  62. public bool IsBurstError { get; set; }
  63. public bool IsLoading = false;
  64. public bool JustLoaded = false;
  65. public string GetDisplayName()
  66. {
  67. var displayName = IsStaticMethod ? Pretty(Method) : $"{Pretty(JobType)} - ({Pretty(JobInterfaceType)})";
  68. // Remove the '<>c__DisplayClass_' part of the name - this is only added for C# Entities.ForEach jobs to trick the C# debugging tools into
  69. // treating them like lambdas. This is removed wherever possible from user facing tools (like the Unity profiler), so we should do the same.
  70. return displayName.Replace("<>c__DisplayClass_", "");
  71. }
  72. private static string Pretty(MethodInfo method)
  73. {
  74. var builder = new StringBuilder();
  75. builder.Append(Pretty(method.DeclaringType));
  76. builder.Append(".");
  77. builder.Append(method.Name);
  78. builder.Append("(");
  79. var parameters = method.GetParameters();
  80. for (var i = 0; i < parameters.Length; i++)
  81. {
  82. var param = parameters[i];
  83. if (i > 0) builder.Append(", ");
  84. builder.Append(Pretty(param.ParameterType));
  85. }
  86. builder.Append(")");
  87. return builder.ToString();
  88. }
  89. internal static string Pretty(Type type)
  90. {
  91. if (type == typeof(bool))
  92. {
  93. return "bool";
  94. }
  95. if (type == typeof(int))
  96. {
  97. return "int";
  98. }
  99. if (type == typeof(long))
  100. {
  101. return "long";
  102. }
  103. if (type == typeof(uint))
  104. {
  105. return "uint";
  106. }
  107. if (type == typeof(ulong))
  108. {
  109. return "ulong";
  110. }
  111. if (type == typeof(short))
  112. {
  113. return "short";
  114. }
  115. if (type == typeof(ushort))
  116. {
  117. return "ushort";
  118. }
  119. if (type == typeof(byte))
  120. {
  121. return "byte";
  122. }
  123. if (type == typeof(sbyte))
  124. {
  125. return "sbyte";
  126. }
  127. if (type == typeof(float))
  128. {
  129. return "float";
  130. }
  131. if (type == typeof(double))
  132. {
  133. return "double";
  134. }
  135. if (type == typeof(string))
  136. {
  137. return "string";
  138. }
  139. if (type == typeof(object))
  140. {
  141. return "object";
  142. }
  143. if (type == typeof(char))
  144. {
  145. return "char";
  146. }
  147. // When displaying job interface type, display the interface name of Unity.Jobs namespace
  148. var typeName = type.IsInterface && type.Name.StartsWith("IJob") ? type.Name : type.ToString();
  149. return typeName.Replace("+", ".");
  150. }
  151. }
  152. internal enum DisassemblyKind
  153. {
  154. Asm = 0,
  155. IL = 1,
  156. UnoptimizedIR = 2,
  157. OptimizedIR = 3,
  158. IRPassAnalysis = 4
  159. }
  160. }
  161. #endif