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.

040-ControlFlows.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Jobs;
  6. using Unity.Mathematics;
  7. namespace Burst.Compiler.IL.Tests
  8. {
  9. internal class ControlFlows
  10. {
  11. [TestCompiler]
  12. public static int For()
  13. {
  14. var counter = 0;
  15. for (var i = 0; i < 10; i++)
  16. counter++;
  17. return counter;
  18. }
  19. [TestCompiler(10)]
  20. public static int ForBreak(int a)
  21. {
  22. int result = 0;
  23. for (int i = 0; i < a; i++)
  24. {
  25. if (i == 5)
  26. {
  27. break;
  28. }
  29. result += 2;
  30. }
  31. return result;
  32. }
  33. [TestCompiler(10, 5)]
  34. public static int ForContinue(int a, int b)
  35. {
  36. int result = 0;
  37. for (int i = 0; i < a; i++)
  38. {
  39. if (i == b)
  40. {
  41. continue;
  42. }
  43. result += i;
  44. }
  45. return result;
  46. }
  47. [TestCompiler]
  48. public static int ForBreak2()
  49. {
  50. int i = 0;
  51. while (true)
  52. {
  53. if (i == 5)
  54. {
  55. break;
  56. }
  57. i++;
  58. }
  59. return i;
  60. }
  61. [TestCompiler(10)]
  62. public static float ForDynamicCondition(ref int b)
  63. {
  64. var counter = 0.0f;
  65. for (var i = 0; i < b; i++)
  66. counter++;
  67. return counter;
  68. }
  69. [TestCompiler(5, 5)]
  70. public static int ForNestedIf(int a, int b)
  71. {
  72. var counter = 0;
  73. for (var i = 0; i < a; i++)
  74. for (var i2 = 0; i != b; i++)
  75. {
  76. counter += i;
  77. counter += i2;
  78. }
  79. return counter;
  80. }
  81. [TestCompiler(5, 5)]
  82. public static int DoWhileNested(int a, int b)
  83. {
  84. var total = 0;
  85. var counter2 = 0;
  86. do
  87. {
  88. var counter1 = 0;
  89. do
  90. {
  91. total++;
  92. counter1++;
  93. } while (counter1 < a);
  94. counter2++;
  95. } while (counter2 < b);
  96. return total;
  97. }
  98. [TestCompiler(5)]
  99. public static int While(int a)
  100. {
  101. var i = 0;
  102. var counter = 0;
  103. while (i < a)
  104. {
  105. i++;
  106. counter += i;
  107. }
  108. return counter;
  109. }
  110. [TestCompiler(5)]
  111. public static int ForForIf(int a)
  112. {
  113. var counter = 0;
  114. for (var i = 0; i != a; i++)
  115. for (var j = 0; j < 4; j++)
  116. if (j > 2)
  117. counter = counter + i;
  118. return counter;
  119. }
  120. [TestCompiler(5)]
  121. public static int ForNestedComplex1(int a)
  122. {
  123. var x = 0;
  124. var y = 0;
  125. for (var i = 0; i < a; i++)
  126. {
  127. y = y + 1;
  128. for (var j = 0; j < 4; j++)
  129. {
  130. if (y > 1)
  131. {
  132. x = x + i;
  133. if (x > 2)
  134. {
  135. for (int k = 0; k < 3; k++)
  136. {
  137. y = y + 1;
  138. if (y > 3)
  139. {
  140. x = x + 1;
  141. }
  142. else if (x > 6)
  143. {
  144. y = 1;
  145. break;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. continue;
  152. }
  153. }
  154. else
  155. {
  156. x--;
  157. }
  158. x++;
  159. }
  160. if (y > 2)
  161. {
  162. x = x + 1;
  163. }
  164. }
  165. return x;
  166. }
  167. [TestCompiler(5)]
  168. public static int ForNestedComplex2(int a)
  169. {
  170. var x = 0;
  171. for (var i = 0; i < a; i++)
  172. {
  173. var insideLoop1 = 0;
  174. for (var j = 0; j < 4; j++)
  175. {
  176. x = x + i;
  177. if (x > 2)
  178. {
  179. insideLoop1++;
  180. for (int k = 0; k < 3; k++)
  181. {
  182. if (insideLoop1 > 3)
  183. {
  184. x = x + 1;
  185. }
  186. else if (x > 6)
  187. {
  188. break;
  189. }
  190. }
  191. }
  192. }
  193. if (insideLoop1 > 2)
  194. {
  195. x = x + 1 + insideLoop1;
  196. }
  197. }
  198. return x;
  199. }
  200. [TestCompiler(5)]
  201. [TestCompiler(-5)]
  202. public static int IfReturn(int a)
  203. {
  204. if (a < 0)
  205. return 55;
  206. return 111;
  207. }
  208. [TestCompiler(5)]
  209. [TestCompiler(-5)]
  210. public static int IfElseReturn(int a)
  211. {
  212. int b = 0;
  213. if (a < 0)
  214. {
  215. b = 1;
  216. }
  217. else
  218. {
  219. b = 2;
  220. }
  221. return b;
  222. }
  223. [TestCompiler(5)]
  224. [TestCompiler(-5)]
  225. public static int IfElseReturnDynamic(int a)
  226. {
  227. int b;
  228. if (a < 0)
  229. {
  230. b = a;
  231. }
  232. else
  233. {
  234. b = a + 1;
  235. }
  236. return b;
  237. }
  238. [TestCompiler(10)]
  239. public static int WhileFunction(int a)
  240. {
  241. while (condition_helper(a))
  242. {
  243. a--;
  244. }
  245. return a;
  246. }
  247. [TestCompiler(10)]
  248. public static int WhileDynamic(ref int a)
  249. {
  250. while (a > 2)
  251. {
  252. a--;
  253. }
  254. return a;
  255. }
  256. [TestCompiler(5, 6, 7)]
  257. [TestCompiler(-5, -6, -7)]
  258. public static int IfDeep(int a, int b, int c)
  259. {
  260. int result = 0;
  261. if (a < 0)
  262. {
  263. if (b > 1)
  264. {
  265. if (c < 2)
  266. {
  267. result = 55;
  268. }
  269. else
  270. {
  271. result = 66;
  272. }
  273. }
  274. else
  275. {
  276. result = 77;
  277. }
  278. }
  279. else
  280. {
  281. if (b < 0)
  282. {
  283. if (c < -2)
  284. {
  285. result = 88;
  286. }
  287. else
  288. {
  289. result = 99;
  290. }
  291. }
  292. else
  293. {
  294. result = 100;
  295. }
  296. }
  297. return result;
  298. }
  299. [TestCompiler(5)]
  300. public static int CallRecursive(int n)
  301. {
  302. return InternalCallRecursive(n);
  303. }
  304. private static int InternalCallRecursive(int n)
  305. {
  306. if (n <= 1)
  307. return 1;
  308. return n * InternalCallRecursive(n - 1);
  309. }
  310. [TestCompiler(3f, 8f)]
  311. [TestCompiler(6f, 8f)]
  312. public static float IfCompareFloat(float a, float b)
  313. {
  314. if (a > 5f)
  315. return 10f;
  316. return b;
  317. }
  318. [TestCompiler(10)]
  319. [TestCompiler(0)]
  320. public static float TernaryCompareFloat(int input)
  321. {
  322. return input > 5 ? 2.5f : 1.2F;
  323. }
  324. [TestCompiler(0)]
  325. [TestCompiler(1)]
  326. public static int TernaryMask(int a)
  327. {
  328. return (a & 1) != 0 ? 5 : 4;
  329. }
  330. [TestCompiler(0)]
  331. [TestCompiler(1)]
  332. public static int IfElseMash(int a)
  333. {
  334. if ((a & 1) != 0)
  335. return 5;
  336. else
  337. return 4;
  338. }
  339. [TestCompiler(0)]
  340. public static int IfCallCondition(int a)
  341. {
  342. if (a > 0 && condition_helper(++a))
  343. {
  344. return a;
  345. }
  346. return -10 + a;
  347. }
  348. [TestCompiler(1)]
  349. [TestCompiler(0)]
  350. [TestCompiler(-1)]
  351. public static int IfIncrementCondition(int a)
  352. {
  353. if (a < 0 || condition_helper(++a))
  354. {
  355. return a;
  356. }
  357. return -10 + a;
  358. }
  359. private static bool condition_helper(int value)
  360. {
  361. return value > 2;
  362. }
  363. [TestCompiler(1, 8)]
  364. public static int IfWhileGotoForward(int a, int b)
  365. {
  366. if (a > 0)
  367. {
  368. while (a < 10)
  369. {
  370. a++;
  371. if (a == b)
  372. {
  373. a--;
  374. goto TestLabel;
  375. }
  376. }
  377. a++;
  378. }
  379. TestLabel:
  380. a--;
  381. return a;
  382. }
  383. [TestCompiler(1, 5)]
  384. public static int IfWhileGotoBackward(int a, int b)
  385. {
  386. RewindLabel:
  387. if (a > 0)
  388. {
  389. while (a < 10)
  390. {
  391. a++;
  392. if (a == b)
  393. {
  394. a++;
  395. goto RewindLabel;
  396. }
  397. }
  398. a++;
  399. }
  400. a--;
  401. return a;
  402. }
  403. [TestCompiler(-1, 0)]
  404. [TestCompiler(0, 0)]
  405. [TestCompiler(0, -1)]
  406. public static int IfAssignCondition(int a, int b)
  407. {
  408. int result = 0;
  409. if (++a > 0 && ++b > 0)
  410. {
  411. result = a + b;
  412. }
  413. else
  414. {
  415. result = a * 10 + b;
  416. }
  417. return result;
  418. }
  419. private static bool ProcessFirstInt(int a, out float b)
  420. {
  421. b = a + 1;
  422. return b < 10;
  423. }
  424. private static bool ProcessNextInt(int a, ref float b)
  425. {
  426. b = a + 2;
  427. return b < 20;
  428. }
  429. [TestCompiler(1, 10)]
  430. public static float ForWhileNestedCall(int a, int b)
  431. {
  432. float value = 0;
  433. for (int i = 0; i < b * 3; i++)
  434. {
  435. var flag = ProcessFirstInt(a, out value);
  436. int num2 = 0;
  437. while (flag && num2 < 2)
  438. {
  439. bool flag2 = i == a;
  440. if (flag2)
  441. {
  442. flag = ProcessNextInt(a + i, ref value);
  443. }
  444. else
  445. {
  446. value++;
  447. flag = ProcessNextInt(a + b + i, ref value);
  448. }
  449. num2++;
  450. }
  451. }
  452. return value;
  453. }
  454. #if BURST_TESTS_ONLY
  455. [TestCompiler(true)]
  456. [TestCompiler(false)]
  457. public static bool CheckDup(bool value)
  458. {
  459. return ILTestsHelper.CheckDupBeforeJump(value);
  460. }
  461. #endif
  462. [TestCompiler(1)]
  463. public static int WhileIfContinue(int a)
  464. {
  465. while (a > 10)
  466. {
  467. if (a < 5)
  468. {
  469. a++;
  470. if (a == 8)
  471. {
  472. continue;
  473. }
  474. }
  475. a++;
  476. }
  477. return a;
  478. }
  479. [TestCompiler(0)]
  480. [TestCompiler(1)]
  481. [TestCompiler(2)]
  482. [TestCompiler(3)]
  483. [TestCompiler(4)]
  484. public static int SwitchReturn(int a)
  485. {
  486. switch (a)
  487. {
  488. case 1:
  489. return 100;
  490. case 2:
  491. return 200;
  492. case 3:
  493. return 300;
  494. case 10:
  495. return 300;
  496. default:
  497. return 1000;
  498. }
  499. }
  500. [TestCompiler(0)]
  501. [TestCompiler(1)]
  502. [TestCompiler(2)]
  503. [TestCompiler(3)]
  504. [TestCompiler(4)]
  505. public static int SwitchBreak(int a)
  506. {
  507. switch (a)
  508. {
  509. case 1:
  510. return 100;
  511. case 2:
  512. break;
  513. default:
  514. return 1000;
  515. }
  516. return 200;
  517. }
  518. [TestCompiler((byte)0)]
  519. [TestCompiler((byte)1)]
  520. [TestCompiler((byte)2)]
  521. [TestCompiler((byte)3)]
  522. [TestCompiler((byte)4)]
  523. public static int SwitchBreakByte(byte a)
  524. {
  525. switch (a)
  526. {
  527. case 1:
  528. return 100;
  529. case 2:
  530. break;
  531. default:
  532. return 1000;
  533. }
  534. return 200;
  535. }
  536. public static byte GetValueAsByte(int a)
  537. {
  538. return (byte)a;
  539. }
  540. [TestCompiler(0)]
  541. [TestCompiler(1)]
  542. [TestCompiler(2)]
  543. [TestCompiler(3)]
  544. public static byte SwitchByteReturnFromFunction(int a)
  545. {
  546. switch (GetValueAsByte(a))
  547. {
  548. case 0:
  549. return 1;
  550. case 1:
  551. return 2;
  552. case 2:
  553. return 3;
  554. default:
  555. return 0;
  556. }
  557. }
  558. [TestCompiler(long.MaxValue)]
  559. [TestCompiler(long.MinValue)]
  560. [TestCompiler(0)]
  561. public static byte SwitchOnLong(long a)
  562. {
  563. switch (a)
  564. {
  565. case long.MaxValue:
  566. return 1;
  567. case long.MinValue:
  568. return 2;
  569. default:
  570. return 0;
  571. }
  572. }
  573. public static byte TestSwitchByteReturn(NativeArray<byte> _results, int a)
  574. {
  575. if (_results.Length > a)
  576. {
  577. switch (_results[a])
  578. {
  579. case 0:
  580. return 1;
  581. case 1:
  582. return 2;
  583. case 2:
  584. return 3;
  585. default:
  586. return 0;
  587. }
  588. }
  589. return 99;
  590. }
  591. [TestCompiler(EnumSwitch.Case1)]
  592. [TestCompiler(EnumSwitch.Case2)]
  593. [TestCompiler(EnumSwitch.Case3)]
  594. public static int SwitchEnum(EnumSwitch a)
  595. {
  596. switch (a)
  597. {
  598. case EnumSwitch.Case1:
  599. return 100;
  600. case EnumSwitch.Case3:
  601. break;
  602. default:
  603. return 1000;
  604. }
  605. return 200;
  606. }
  607. public enum EnumSwitch
  608. {
  609. Case1,
  610. Case2,
  611. Case3,
  612. }
  613. [TestCompiler(long.MaxValue)]
  614. [TestCompiler(long.MinValue)]
  615. [TestCompiler(0)]
  616. public static byte SwitchExpression(long a)
  617. {
  618. return a switch
  619. {
  620. long.MaxValue => 1,
  621. long.MinValue => 2,
  622. _ => 0,
  623. };
  624. }
  625. [TestCompiler(ExpectedException = typeof(InvalidOperationException), ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  626. [MonoOnly(".NET CLR does not support burst.abort correctly")]
  627. public static int ExceptionReachedReturn()
  628. {
  629. throw new InvalidOperationException("This is bad 1");
  630. }
  631. [TestCompiler(ExpectedException = typeof(InvalidOperationException), ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  632. [MonoOnly(".NET CLR does not support burst.abort correctly")]
  633. public static void ExceptionReached()
  634. {
  635. throw new InvalidOperationException("This is bad 2");
  636. }
  637. [TestCompiler(1, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  638. [TestCompiler(2, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  639. public static void ExceptionNotReached(int a)
  640. {
  641. if (a > 10)
  642. {
  643. throw new InvalidOperationException("This is bad 2");
  644. }
  645. }
  646. [TestCompiler(1, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  647. [TestCompiler(2, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  648. public static void ExceptionMultipleNotReached(int a)
  649. {
  650. if (a > 10)
  651. {
  652. if (a > 15)
  653. {
  654. throw new InvalidOperationException("This is bad 2");
  655. }
  656. else
  657. {
  658. if (a < 8)
  659. {
  660. throw new NotSupportedException();
  661. }
  662. else
  663. {
  664. a = a + 1;
  665. }
  666. }
  667. }
  668. }
  669. private struct SmallStruct
  670. {
  671. public int I;
  672. public float F;
  673. }
  674. private static SmallStruct UnreachedException(bool b)
  675. {
  676. if (b)
  677. {
  678. throw new Exception("Never here!");
  679. }
  680. return new SmallStruct { I = 42, F = 42.0f };
  681. }
  682. [TestCompiler(0, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  683. public static double UnreachedExceptionInCalledFunction(int a)
  684. {
  685. var result = UnreachedException(a != 0);
  686. return result.I + result.F;
  687. }
  688. [TestCompiler(1, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  689. public static int ExceptionNotReachedReturn(int a)
  690. {
  691. int b = a;
  692. if (a > 10)
  693. {
  694. b = 5;
  695. throw new InvalidOperationException("This is bad 2");
  696. }
  697. return b;
  698. }
  699. [TestCompiler(13, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  700. [TestCompiler(1, ExpectedDiagnosticId = DiagnosticId.WRN_ExceptionThrownInNonSafetyCheckGuardedFunction)]
  701. public static int ExceptionMultipleNotReachedReturn(int a)
  702. {
  703. if (a > 10)
  704. {
  705. if (a > 15)
  706. {
  707. throw new InvalidOperationException("This is bad 2");
  708. }
  709. else
  710. {
  711. if (a < 12)
  712. {
  713. throw new NotSupportedException();
  714. }
  715. else
  716. {
  717. a = a + 1;
  718. }
  719. }
  720. }
  721. return a;
  722. }
  723. [TestCompiler]
  724. public static void TestInternalError()
  725. {
  726. var job = new InternalErrorVariableNotFound();
  727. job.Execute();
  728. }
  729. public struct InternalErrorVariableNotFound : IJob
  730. {
  731. public void Execute()
  732. {
  733. CausesError(3);
  734. }
  735. static int CausesError(int x)
  736. {
  737. int y = 0;
  738. while (y != 0 && y != 1)
  739. {
  740. if (x > 0)
  741. x = y++;
  742. }
  743. return y;
  744. }
  745. }
  746. [TestCompiler(true)]
  747. public static int TestPopNonInitialTrailingPush(bool x)
  748. {
  749. return (x ? 1 : -1) * math.min(16, 1);
  750. }
  751. [TestCompiler]
  752. // Check unsigned ternary comparison (Bxx_Un) opcodes
  753. public static ulong TestUnsignedTernary()
  754. {
  755. ulong a = 0;
  756. ulong b = ~0UL;
  757. ulong c = (a < b) ? 1UL : 0;
  758. ulong d = (a <= b) ? 1UL : 0;
  759. ulong e = (a > b) ? 0: 1UL;
  760. ulong f = (a >= b) ? 0: 1UL;
  761. return c + d + e + f;
  762. }
  763. [TestCompiler((byte)0)]
  764. [TestCompiler((byte) 1)]
  765. public static int TestByteAndIntFlow(byte value)
  766. {
  767. var position = value == 0 ? -1 : value;
  768. if (position < 0)
  769. {
  770. position = 17;
  771. }
  772. return position;
  773. }
  774. }
  775. }