loopyieldnested.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // -lic:10 -bgjit-
  6. const mod = new WebAssembly.Module(WebAssembly.wabt.convertWast2Wasm(`
  7. (module
  8. (func (export "foo") (param $i i32) (result i32)
  9. (local $l i32)
  10. (block $b1 (result i32)
  11. (loop (result i32)
  12. (i32.const -1) (br_if 2 (i32.eq (get_local $i) (i32.const 0))) (drop)
  13. (i32.const 0) (br_if $b1 (i32.eq (get_local $i) (i32.const 1))) (drop)
  14. (block $b2 (result i32)
  15. (set_local $l (i32.const 0))
  16. (loop
  17. (i32.const -2) (br_if 4 (i32.eq (get_local $i) (i32.const 2))) (drop)
  18. (i32.const 1) (br_if $b1 (i32.eq (get_local $i) (i32.const 3))) (drop)
  19. (i32.const 2) (br_if $b2 (i32.eq (get_local $i) (i32.const 4))) (drop)
  20. ;; loop 100 times to trigger jit loop body on inner loops first
  21. (i32.add (get_local $l) (i32.const 1))
  22. (tee_local $l)
  23. (br_if 0 (i32.lt_u (i32.const 100)))
  24. )
  25. (block $b3 (result i32)
  26. (set_local $l (i32.const 0))
  27. (loop
  28. (i32.const -3) (br_if 5 (i32.eq (get_local $i) (i32.const 5))) (drop)
  29. (i32.const 3) (br_if $b1 (i32.eq (get_local $i) (i32.const 6))) (drop)
  30. (i32.const 4) (br_if $b2 (i32.eq (get_local $i) (i32.const 7))) (drop)
  31. (i32.const 5) (br_if $b3 (i32.eq (get_local $i) (i32.const 8))) (drop)
  32. ;; loop 100 times to trigger jit loop body on inner loops first
  33. (i32.add (get_local $l) (i32.const 1))
  34. (tee_local $l)
  35. (br_if 0 (i32.lt_u (i32.const 100)))
  36. )
  37. (i32.const 6)
  38. )
  39. )
  40. )
  41. )
  42. (i32.add (i32.const 1))
  43. )
  44. )`));
  45. const {exports: {foo}} = new WebAssembly.Instance(mod);
  46. const expected = [-1, 1, -2, 2, 3, -3, 4, 5, 6, 7];
  47. // Do this a few times to try to cover all possible jited loop
  48. for (let l = 0; l < 100; ++l) {
  49. for (let i = 9; i >= 0; --i) {
  50. const res = foo(i);
  51. if (res !== expected[i]) {
  52. console.log(`Failed foo(${i}). Expected ${expected[i]}, got ${res}`);
  53. }
  54. }
  55. }
  56. console.log("pass");