Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

055-TestStackalloc.cs 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. namespace Burst.Compiler.IL.Tests.Shared
  2. {
  3. internal class TestStackalloc
  4. {
  5. [TestCompiler]
  6. public static unsafe int Stackalloc1ByteWithInitializer()
  7. {
  8. var value = stackalloc byte[1] { 0xA4 };
  9. return value[0];
  10. }
  11. [TestCompiler]
  12. public static unsafe int Stackalloc16BytesWithInitializer()
  13. {
  14. // Roslyn generates quite different IL when the number of bytes is larger than 8.
  15. var value = stackalloc byte[16] { 0xA4, 0xA1, 0x20, 0xA5, 0x80, 0x17, 0xF6, 0x4F, 0xBD, 0x18, 0x16, 0x73, 0x43, 0xC5, 0xAF, 0x16 };
  16. return value[9];
  17. }
  18. [TestCompiler]
  19. public static unsafe int Stackalloc16IntsWithInitializer()
  20. {
  21. var value = stackalloc int[16] { 0xA4, 0xA1, 0x20, 0xA5, 0x80, 0x17, 0xF6, 0x4F, 0xBD, 0x18, 0x16, 0x73, 0x43, 0xC5, 0xAF, 0x16 };
  22. return value[9];
  23. }
  24. [TestCompiler(1)]
  25. public static unsafe int StackallocInBranch(int takeBranch)
  26. {
  27. int* array = null;
  28. if (takeBranch != 0)
  29. {
  30. int* elem = stackalloc int[1];
  31. array = elem;
  32. }
  33. if (takeBranch != 0)
  34. {
  35. int* elem = stackalloc int[1];
  36. if (array == elem)
  37. {
  38. return -1;
  39. }
  40. }
  41. return 0;
  42. }
  43. [TestCompiler(4)]
  44. public static unsafe int StackallocInLoop(int iterations)
  45. {
  46. int** array = stackalloc int*[iterations];
  47. for (int i = 0; i < iterations; i++)
  48. {
  49. #pragma warning disable CA2014 // Do not use stackalloc in loops
  50. int* elem = stackalloc int[1];
  51. #pragma warning restore CA2014 // Do not use stackalloc in loops
  52. array[i] = elem;
  53. }
  54. for (int i = 0; i < iterations; i++)
  55. {
  56. for (int k = i + 1; k < iterations; k++)
  57. {
  58. // Make sure all the stack allocations within the loop are unique addresses.
  59. if (array[i] == array[k])
  60. {
  61. return -1;
  62. }
  63. }
  64. }
  65. return 0;
  66. }
  67. [TestCompiler]
  68. public static unsafe int StackallocWithUnmanagedConstructedType()
  69. {
  70. var value = stackalloc[]
  71. {
  72. new Point<int> { X = 1, Y = 2 },
  73. new Point<int> { X = 42, Y = 5 },
  74. new Point<int> { X = 3, Y = -1 },
  75. };
  76. return value[1].X;
  77. }
  78. private struct Point<T>
  79. {
  80. public T X;
  81. public T Y;
  82. }
  83. #if UNITY_2021_2_OR_NEWER || BURST_INTERNAL
  84. [TestCompiler]
  85. public static int StackallocInNestedExpression()
  86. {
  87. return StackallocInNestedExpressionHelper(stackalloc[] { 2, 4, 6, 8 });
  88. }
  89. private static int StackallocInNestedExpressionHelper(System.Span<int> span)
  90. {
  91. return span[2];
  92. }
  93. #endif
  94. }
  95. }