暫無描述
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.

035-Functions.cs 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Runtime.CompilerServices;
  2. using Unity.Burst;
  3. using Unity.Burst.CompilerServices;
  4. using UnityBenchShared;
  5. namespace Burst.Compiler.IL.Tests
  6. {
  7. internal class Functions
  8. {
  9. [TestCompiler]
  10. public static int CheckFunctionCall()
  11. {
  12. return AnotherFunction();
  13. }
  14. private static int AnotherFunction()
  15. {
  16. return 150;
  17. }
  18. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_UnableToAccessManagedMethod)]
  19. public static void Boxing()
  20. {
  21. var a = new CustomStruct();
  22. // This will box CustomStruct, so this method should fail when compiling
  23. a.GetType();
  24. }
  25. private struct CustomStruct
  26. {
  27. }
  28. public static int NotDiscardable()
  29. {
  30. return 3;
  31. }
  32. [BurstDiscard]
  33. public static void Discardable()
  34. {
  35. }
  36. [TestCompiler]
  37. public static int TestCallsOfDiscardedMethodRegression()
  38. {
  39. // The regression was that we would queue all calls of a method, but if we encountered a discardable one
  40. // We would stop visiting pending methods. This resulting in method bodies not being visited.
  41. Discardable();
  42. return NotDiscardable();
  43. }
  44. [MethodImpl(MethodImplOptions.NoInlining)]
  45. public static int NoInline(int x)
  46. {
  47. return x;
  48. }
  49. [TestCompiler(42)]
  50. public static int TestNoInline(int x)
  51. {
  52. return NoInline(x);
  53. }
  54. [MethodImpl(MethodImplOptions.NoOptimization)]
  55. public static int NoOptimization(int x)
  56. {
  57. return x;
  58. }
  59. [TestCompiler(42)]
  60. public static int TestNoOptimization(int x)
  61. {
  62. return NoOptimization(x);
  63. }
  64. [TestCompiler(42)]
  65. public static int TestImplicitCapture(int x)
  66. {
  67. return SomeFunction();
  68. int SomeFunction()
  69. {
  70. return x;
  71. }
  72. }
  73. public struct Pair
  74. {
  75. public int X;
  76. public int Y;
  77. public struct Provider : IArgumentProvider
  78. {
  79. public object Value => new Pair { X = 13, Y = 42 };
  80. }
  81. }
  82. [TestCompiler(42, typeof(Pair.Provider))]
  83. public static int TestImplicitCaptureInLoop(int x, ref Pair rp)
  84. {
  85. int total = 0;
  86. Pair p = rp;
  87. for (int i = 0; i < x; i++)
  88. {
  89. total += SomeFunction(42, 42, 42, 42, 42, i);
  90. int SomeFunction(int a, int b, int c, int d, int e, int otherI)
  91. {
  92. if (p.Y != 0)
  93. {
  94. return (otherI == i) ? 56 : -13;
  95. }
  96. return 0;
  97. }
  98. }
  99. return total;
  100. }
  101. [TestCompiler(42)]
  102. [IgnoreWarning((int)DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  103. public static void NoWarningsWithSingle(int i)
  104. {
  105. if ((6 * 8) == i)
  106. {
  107. throw new System.Exception("Not the meaning of life!");
  108. }
  109. }
  110. [TestCompiler(42)]
  111. [IgnoreWarning((int)DiagnosticId.WRN_LoopIntrinsicCalledButLoopOptimizedAway)]
  112. [IgnoreWarning((int)DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  113. public static void NoWarningsWithMultiple(int i)
  114. {
  115. if ((6 * 8) == i)
  116. {
  117. throw new System.Exception("Not the meaning of life!");
  118. }
  119. }
  120. }
  121. }