Geen omschrijving
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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 string GetDisplayName()
  63. {
  64. var displayName = IsStaticMethod ? Pretty(Method) : $"{Pretty(JobType)} - ({Pretty(JobInterfaceType)})";
  65. // Remove the '<>c__DisplayClass_' part of the name - this is only added for C# Entities.ForEach jobs to trick the C# debugging tools into
  66. // treating them like lambdas. This is removed wherever possible from user facing tools (like the Unity profiler), so we should do the same.
  67. return displayName.Replace("<>c__DisplayClass_", "");
  68. }
  69. private static string Pretty(MethodInfo method)
  70. {
  71. var builder = new StringBuilder();
  72. builder.Append(Pretty(method.DeclaringType));
  73. builder.Append(".");
  74. builder.Append(method.Name);
  75. builder.Append("(");
  76. var parameters = method.GetParameters();
  77. for (var i = 0; i < parameters.Length; i++)
  78. {
  79. var param = parameters[i];
  80. if (i > 0) builder.Append(", ");
  81. builder.Append(Pretty(param.ParameterType));
  82. }
  83. builder.Append(")");
  84. return builder.ToString();
  85. }
  86. private static string Pretty(Type type)
  87. {
  88. if (type == typeof(bool))
  89. {
  90. return "bool";
  91. }
  92. if (type == typeof(int))
  93. {
  94. return "int";
  95. }
  96. if (type == typeof(long))
  97. {
  98. return "long";
  99. }
  100. if (type == typeof(uint))
  101. {
  102. return "uint";
  103. }
  104. if (type == typeof(ulong))
  105. {
  106. return "ulong";
  107. }
  108. if (type == typeof(short))
  109. {
  110. return "short";
  111. }
  112. if (type == typeof(ushort))
  113. {
  114. return "ushort";
  115. }
  116. if (type == typeof(byte))
  117. {
  118. return "byte";
  119. }
  120. if (type == typeof(sbyte))
  121. {
  122. return "sbyte";
  123. }
  124. if (type == typeof(float))
  125. {
  126. return "float";
  127. }
  128. if (type == typeof(double))
  129. {
  130. return "double";
  131. }
  132. if (type == typeof(string))
  133. {
  134. return "string";
  135. }
  136. if (type == typeof(object))
  137. {
  138. return "object";
  139. }
  140. if (type == typeof(char))
  141. {
  142. return "char";
  143. }
  144. // When displaying job interface type, display the interface name of Unity.Jobs namespace
  145. var typeName = type.IsInterface && type.Name.StartsWith("IJob") ? type.Name : type.ToString();
  146. return typeName.Replace("+", ".");
  147. }
  148. }
  149. internal enum DisassemblyKind
  150. {
  151. Asm = 0,
  152. IL = 1,
  153. UnoptimizedIR = 2,
  154. OptimizedIR = 3,
  155. IRPassAnalysis = 4
  156. }
  157. }
  158. #endif