1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
|
# frozen_string_literal: true
#
# This set of tests can be run with:
# make test-all TESTS='test/ruby/test_yjit.rb'
require 'test/unit'
require 'envutil'
require 'tmpdir'
require_relative '../lib/jit_support'
return unless JITSupport.yjit_supported?
require 'stringio'
# Tests for YJIT with assertions on compilation and side exits
# insipired by the RJIT tests in test/ruby/test_rjit.rb
class TestYJIT < Test::Unit::TestCase
running_with_yjit = defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?
def test_yjit_in_ruby_description
assert_includes(RUBY_DESCRIPTION, '+YJIT')
end if running_with_yjit
# Check that YJIT is in the version string
def test_yjit_in_version
[
%w(--version --yjit),
%w(--version --disable-yjit --yjit),
%w(--version --disable-yjit --enable-yjit),
%w(--version --disable-yjit --enable=yjit),
%w(--version --disable=yjit --yjit),
%w(--version --disable=yjit --enable-yjit),
%w(--version --disable=yjit --enable=yjit),
%w(--version --jit),
%w(--version --disable-jit --jit),
%w(--version --disable-jit --enable-jit),
%w(--version --disable-jit --enable=jit),
%w(--version --disable=jit --yjit),
%w(--version --disable=jit --enable-jit),
%w(--version --disable=jit --enable=jit),
].each do |version_args|
assert_in_out_err(version_args) do |stdout, stderr|
assert_equal(RUBY_DESCRIPTION, stdout.first)
assert_equal([], stderr)
end
end
end if running_with_yjit
def test_command_line_switches
assert_in_out_err('--yjit-', '', [], /invalid option --yjit-/)
assert_in_out_err('--yjithello', '', [], /invalid option --yjithello/)
#assert_in_out_err('--yjit-call-threshold', '', [], /--yjit-call-threshold needs an argument/)
#assert_in_out_err('--yjit-call-threshold=', '', [], /--yjit-call-threshold needs an argument/)
end
def test_yjit_enable
args = []
args << "--disable=yjit" if RubyVM::YJIT.enabled?
assert_separately(args, <<~'RUBY')
refute_predicate RubyVM::YJIT, :enabled?
refute_includes RUBY_DESCRIPTION, "+YJIT"
RubyVM::YJIT.enable
assert_predicate RubyVM::YJIT, :enabled?
assert_includes RUBY_DESCRIPTION, "+YJIT"
RUBY
end
def test_yjit_disable
assert_separately(["--yjit", "--yjit-disable"], <<~'RUBY')
refute_predicate RubyVM::YJIT, :enabled?
refute_includes RUBY_DESCRIPTION, "+YJIT"
RubyVM::YJIT.enable
assert_predicate RubyVM::YJIT, :enabled?
assert_includes RUBY_DESCRIPTION, "+YJIT"
RUBY
end
def test_yjit_enable_stats_false
assert_separately(["--yjit-disable", "--yjit-stats"], <<~RUBY, ignore_stderr: true)
assert_false RubyVM::YJIT.enabled?
assert_nil RubyVM::YJIT.runtime_stats
RubyVM::YJIT.enable
assert_true RubyVM::YJIT.enabled?
assert_true RubyVM::YJIT.runtime_stats[:all_stats]
RUBY
end
def test_yjit_enable_stats_true
args = []
args << "--disable=yjit" if RubyVM::YJIT.enabled?
assert_separately(args, <<~RUBY, ignore_stderr: true)
assert_false RubyVM::YJIT.enabled?
assert_nil RubyVM::YJIT.runtime_stats
RubyVM::YJIT.enable(stats: true)
assert_true RubyVM::YJIT.enabled?
assert_true RubyVM::YJIT.runtime_stats[:all_stats]
RUBY
end
def test_yjit_enable_stats_quiet
assert_in_out_err(['--yjit-disable', '-e', 'RubyVM::YJIT.enable(stats: true)']) do |_stdout, stderr, _status|
assert_not_empty stderr
end
assert_in_out_err(['--yjit-disable', '-e', 'RubyVM::YJIT.enable(stats: :quiet)']) do |_stdout, stderr, _status|
assert_empty stderr
end
end
def test_yjit_enable_with_call_threshold
assert_separately(%w[--yjit-disable --yjit-call-threshold=1], <<~RUBY)
def not_compiled = nil
def will_compile = nil
def compiled_counts = RubyVM::YJIT.runtime_stats&.dig(:compiled_iseq_count)
not_compiled
assert_nil compiled_counts
assert_false RubyVM::YJIT.enabled?
RubyVM::YJIT.enable
will_compile
assert compiled_counts > 0
assert_true RubyVM::YJIT.enabled?
RUBY
end
def test_yjit_enable_with_monkey_patch
assert_separately(%w[--yjit-disable], <<~RUBY)
# This lets rb_method_entry_at(rb_mKernel, ...) return NULL
Kernel.prepend(Module.new)
# This must not crash with "undefined optimized method!"
RubyVM::YJIT.enable
RUBY
end
def test_yjit_stats_and_v_no_error
_stdout, stderr, _status = invoke_ruby(%w(-v --yjit-stats), '', true, true)
refute_includes(stderr, "NoMethodError")
end
def test_enable_from_env_var
yjit_child_env = {'RUBY_YJIT_ENABLE' => '1'}
assert_in_out_err([yjit_child_env, '--version'], '') do |stdout, stderr|
assert_equal(RUBY_DESCRIPTION, stdout.first)
assert_equal([], stderr)
end
assert_in_out_err([yjit_child_env, '-e puts RUBY_DESCRIPTION'], '', [RUBY_DESCRIPTION])
assert_in_out_err([yjit_child_env, '-e p RubyVM::YJIT.enabled?'], '', ['true'])
end if running_with_yjit
def test_compile_setclassvariable
script = 'class Foo; def self.foo; @@foo = 1; end; end; Foo.foo'
assert_compiles(script, insns: %i[setclassvariable], result: 1)
end
def test_compile_getclassvariable
script = 'class Foo; @@foo = 1; def self.foo; @@foo; end; end; Foo.foo'
assert_compiles(script, insns: %i[getclassvariable], result: 1)
end
def test_compile_putnil
assert_compiles('nil', insns: %i[putnil], result: nil)
end
def test_compile_putobject
assert_compiles('true', insns: %i[putobject], result: true)
assert_compiles('123', insns: %i[putobject], result: 123)
assert_compiles(':foo', insns: %i[putobject], result: :foo)
end
def test_compile_opt_succ
assert_compiles('1.succ', insns: %i[opt_succ], result: 2)
end
def test_compile_opt_not
assert_compiles('!false', insns: %i[opt_not], result: true)
assert_compiles('!nil', insns: %i[opt_not], result: true)
assert_compiles('!true', insns: %i[opt_not], result: false)
assert_compiles('![]', insns: %i[opt_not], result: false)
end
def test_compile_opt_newarray
assert_compiles('[]', insns: %i[newarray], result: [])
assert_compiles('[1+1]', insns: %i[newarray opt_plus], result: [2])
assert_compiles('[1,1+1,3,4,5,6]', insns: %i[newarray opt_plus], result: [1, 2, 3, 4, 5, 6])
end
def test_compile_opt_duparray
assert_compiles('[1]', insns: %i[duparray], result: [1])
assert_compiles('[1, 2, 3]', insns: %i[duparray], result: [1, 2, 3])
end
def test_compile_newrange
assert_compiles('s = 1; (s..5)', insns: %i[newrange], result: 1..5)
assert_compiles('s = 1; e = 5; (s..e)', insns: %i[newrange], result: 1..5)
assert_compiles('s = 1; (s...5)', insns: %i[newrange], result: 1...5)
assert_compiles('s = 1; (s..)', insns: %i[newrange], result: 1..)
assert_compiles('e = 5; (..e)', insns: %i[newrange], result: ..5)
end
def test_compile_duphash
assert_compiles('{ two: 2 }', insns: %i[duphash], result: { two: 2 })
end
def test_compile_newhash
assert_compiles('{}', insns: %i[newhash], result: {})
assert_compiles('{ two: 1 + 1 }', insns: %i[newhash], result: { two: 2 })
assert_compiles('{ 1 + 1 => :two }', insns: %i[newhash], result: { 2 => :two })
end
def test_compile_opt_nil_p
assert_compiles('nil.nil?', insns: %i[opt_nil_p], result: true)
assert_compiles('false.nil?', insns: %i[opt_nil_p], result: false)
assert_compiles('true.nil?', insns: %i[opt_nil_p], result: false)
assert_compiles('(-"").nil?', insns: %i[opt_nil_p], result: false)
assert_compiles('123.nil?', insns: %i[opt_nil_p], result: false)
end
def test_compile_eq_fixnum
assert_compiles('123 == 123', insns: %i[opt_eq], result: true)
assert_compiles('123 == 456', insns: %i[opt_eq], result: false)
end
def test_compile_eq_string
assert_compiles('-"" == -""', insns: %i[opt_eq], result: true)
assert_compiles('-"foo" == -"foo"', insns: %i[opt_eq], result: true)
assert_compiles('-"foo" == -"bar"', insns: %i[opt_eq], result: false)
end
def test_compile_eq_symbol
assert_compiles(':foo == :foo', insns: %i[opt_eq], result: true)
assert_compiles(':foo == :bar', insns: %i[opt_eq], result: false)
assert_compiles(':foo == "foo".to_sym', insns: %i[opt_eq], result: true)
end
def test_compile_eq_object
assert_compiles(<<~RUBY, insns: %i[opt_eq], result: false)
def eq(a, b)
a == b
end
eq(Object.new, Object.new)
RUBY
assert_compiles(<<~RUBY, insns: %i[opt_eq], result: true)
def eq(a, b)
a == b
end
obj = Object.new
eq(obj, obj)
RUBY
end
def test_compile_eq_arbitrary_class
assert_compiles(<<~RUBY, insns: %i[opt_eq], result: "yes")
def eq(a, b)
a == b
end
class Foo
def ==(other)
"yes"
end
end
eq(Foo.new, Foo.new)
eq(Foo.new, Foo.new)
RUBY
end
def test_compile_opt_lt
assert_compiles('1 < 2', insns: %i[opt_lt])
assert_compiles('"a" < "b"', insns: %i[opt_lt])
end
def test_compile_opt_le
assert_compiles('1 <= 2', insns: %i[opt_le])
assert_compiles('"a" <= "b"', insns: %i[opt_le])
end
def test_compile_opt_gt
assert_compiles('1 > 2', insns: %i[opt_gt])
assert_compiles('"a" > "b"', insns: %i[opt_gt])
end
def test_compile_opt_ge
assert_compiles('1 >= 2', insns: %i[opt_ge])
assert_compiles('"a" >= "b"', insns: %i[opt_ge])
end
def test_compile_opt_plus
assert_compiles('1 + 2', insns: %i[opt_plus])
assert_compiles('"a" + "b"', insns: %i[opt_plus])
assert_compiles('[:foo] + [:bar]', insns: %i[opt_plus])
end
def test_compile_opt_minus
assert_compiles('1 - 2', insns: %i[opt_minus])
assert_compiles('[:foo, :bar] - [:bar]', insns: %i[opt_minus])
end
def test_compile_opt_or
assert_compiles('1 | 2', insns: %i[opt_or])
assert_compiles('[:foo] | [:bar]', insns: %i[opt_or])
end
def test_compile_opt_and
assert_compiles('1 & 2', insns: %i[opt_and])
assert_compiles('[:foo, :bar] & [:bar]', insns: %i[opt_and])
end
def test_compile_set_and_get_global
assert_compiles('$foo = 123; $foo', insns: %i[setglobal], result: 123)
end
def test_compile_putspecialobject
assert_compiles('-> {}', insns: %i[putspecialobject])
end
def test_compile_tostring
assert_no_exits('"i am a string #{true}"')
end
def test_compile_opt_aset
assert_compiles('[1,2,3][2] = 4', insns: %i[opt_aset], frozen_string_literal: false)
assert_compiles('{}[:foo] = :bar', insns: %i[opt_aset], frozen_string_literal: false)
assert_compiles('[1,2,3][0..-1] = []', insns: %i[opt_aset], frozen_string_literal: false)
assert_compiles('"foo"[3] = "d"', insns: %i[opt_aset], frozen_string_literal: false)
end
def test_compile_attr_set
assert_no_exits(<<~EORB)
class Foo
attr_accessor :bar
end
foo = Foo.new
foo.bar = 3
foo.bar = 3
foo.bar = 3
foo.bar = 3
EORB
end
def test_compile_regexp
assert_no_exits('/#{true}/')
end
def test_compile_dynamic_symbol
assert_compiles(':"#{"foo"}"', insns: %i[intern])
assert_compiles('s = "bar"; :"foo#{s}"', insns: %i[intern])
end
def test_getlocal_with_level
assert_compiles(<<~RUBY, insns: %i[getlocal opt_plus], result: [[7]])
def foo(foo, bar)
[1].map do |x|
[1].map do |y|
foo + bar
end
end
end
foo(5, 2)
RUBY
end
def test_setlocal_with_level
assert_no_exits(<<~RUBY)
def sum(arr)
sum = 0
arr.each do |x|
sum += x
end
sum
end
sum([1,2,3])
RUBY
end
def test_string_then_nil
assert_compiles(<<~RUBY, insns: %i[opt_nil_p], result: true)
def foo(val)
val.nil?
end
foo("foo")
foo(nil)
RUBY
end
def test_nil_then_string
assert_compiles(<<~RUBY, insns: %i[opt_nil_p], result: false)
def foo(val)
val.nil?
end
foo(nil)
foo("foo")
RUBY
end
def test_string_concat_utf8
assert_compiles(<<~RUBY, frozen_string_literal: true, result: true)
def str_cat_utf8
s = String.new
10.times { s << "✅" }
s
end
str_cat_utf8 == "✅" * 10
RUBY
end
def test_string_concat_ascii
# Constant-get for classes (e.g. String, Encoding) can cause a side-exit in getinlinecache. For now, ignore exits.
assert_compiles(<<~RUBY, exits: :any)
str_arg = "b".encode(Encoding::ASCII)
def str_cat_ascii(arg)
s = String.new(encoding: Encoding::ASCII)
10.times { s << arg }
s
end
str_cat_ascii(str_arg) == str_arg * 10
RUBY
end
def test_opt_length_in_method
assert_compiles(<<~RUBY, insns: %i[opt_length], result: 5)
def foo(str)
str.length
end
foo("hello, ")
foo("world")
RUBY
end
def test_opt_regexpmatch2
assert_compiles(<<~RUBY, insns: %i[opt_regexpmatch2], result: 0)
def foo(str)
str =~ /foo/
end
foo("foobar")
RUBY
end
def test_expandarray
assert_compiles(<<~'RUBY', insns: %i[expandarray], result: [1, 2])
a, b = [1, 2]
RUBY
end
def test_expandarray_nil
assert_compiles(<<~'RUBY', insns: %i[expandarray], result: [nil, nil])
a, b = nil
[a, b]
RUBY
end
def test_getspecial_backref
assert_compiles("'foo' =~ /(o)./; $&", insns: %i[getspecial], result: "oo")
assert_compiles("'foo' =~ /(o)./; $`", insns: %i[getspecial], result: "f")
assert_compiles("'foo' =~ /(o)./; $'", insns: %i[getspecial], result: "")
assert_compiles("'foo' =~ /(o)./; $+", insns: %i[getspecial], result: "o")
assert_compiles("'foo' =~ /(o)./; $1", insns: %i[getspecial], result: "o")
assert_compiles("'foo' =~ /(o)./; $2", insns: %i[getspecial], result: nil)
end
def test_compile_getconstant
assert_compiles(<<~RUBY, insns: %i[getconstant], result: [], call_threshold: 1)
def get_argv(klass)
klass::ARGV
end
get_argv(Object)
RUBY
end
def test_compile_getconstant_with_sp_offset
assert_compiles(<<~RUBY, insns: %i[getconstant], result: 2, call_threshold: 1)
class Foo
Bar = 1
end
2.times do
s = Foo # this opt_getconstant_path needs warmup, so 2.times is needed
Class.new(Foo).const_set(:Bar, s::Bar)
end
RUBY
end
def test_compile_opt_getconstant_path
assert_compiles(<<~RUBY, insns: %i[opt_getconstant_path], result: 123, call_threshold: 2)
def get_foo
FOO
end
FOO = 123
get_foo # warm inline cache
get_foo
RUBY
end
def test_opt_getconstant_path_slowpath
assert_compiles(<<~RUBY, exits: { opt_getconstant_path: 1 }, result: [42, 42, 1, 1], call_threshold: 2)
class A
FOO = 42
class << self
def foo
_foo = nil
FOO
end
end
end
result = []
result << A.foo
result << A.foo
class << A
FOO = 1
end
result << A.foo
result << A.foo
result
RUBY
end
def test_opt_getconstant_path_general
assert_compiles(<<~RUBY, result: [1, 1])
module Base
Const = 1
end
class Sub
def const
_const = nil # make a non-entry block for opt_getconstant_path
Const
end
def self.const_missing(n)
Base.const_get(n)
end
end
sub = Sub.new
result = []
result << sub.const # generate the general case
result << sub.const # const_missing does not invalidate the block
result
RUBY
end
def test_string_interpolation
assert_compiles(<<~'RUBY', insns: %i[objtostring anytostring concatstrings], result: "foobar", call_threshold: 2)
def make_str(foo, bar)
"#{foo}#{bar}"
end
make_str("foo", "bar")
make_str("foo", "bar")
RUBY
end
def test_string_interpolation_cast
assert_compiles(<<~'RUBY', insns: %i[objtostring anytostring concatstrings], result: "123")
def make_str(foo, bar)
"#{foo}#{bar}"
end
make_str(1, 23)
RUBY
end
def test_checkkeyword
assert_compiles(<<~'RUBY', insns: %i[checkkeyword], result: [2, 5])
def foo(foo: 1+1)
foo
end
[foo, foo(foo: 5)]
RUBY
end
def test_struct_aref
assert_compiles(<<~RUBY)
def foo(obj)
obj.foo
obj.bar
end
Foo = Struct.new(:foo, :bar)
foo(Foo.new(123))
foo(Foo.new(123))
RUBY
end
def test_struct_aset
assert_compiles(<<~RUBY)
def foo(obj)
obj.foo = 123
obj.bar = 123
end
Foo = Struct.new(:foo, :bar)
foo(Foo.new(123))
foo(Foo.new(123))
RUBY
end
def test_getblockparam
assert_compiles(<<~'RUBY', insns: [:getblockparam])
def foo &blk
2.times do
blk
end
end
foo {}
foo {}
RUBY
end
def test_getblockparamproxy
assert_compiles(<<~'RUBY', insns: [:getblockparamproxy], exits: {})
def foo &blk
p blk.call
p blk.call
end
foo { 1 }
foo { 2 }
RUBY
end
def test_ifunc_getblockparamproxy
assert_compiles(<<~'RUBY', insns: [:getblockparamproxy], exits: {})
class Foo
include Enumerable
def each(&block)
block.call 1
block.call 2
block.call 3
end
end
foo = Foo.new
foo.map { _1 * 2 }
foo.map { _1 * 2 }
RUBY
end
def test_send_blockarg
assert_compiles(<<~'RUBY', insns: [:getblockparamproxy, :send], exits: {})
def bar
end
def foo &blk
bar(&blk)
bar(&blk)
end
foo
foo
foo { }
foo { }
RUBY
end
def test_send_splat
assert_compiles(<<~'RUBY', result: "3#1,2,3/P", exits: {})
def internal_method(*args)
"#{args.size}##{args.join(",")}"
end
def jit_method
send(:internal_method, *[1, 2, 3]) + "/P"
end
jit_method
RUBY
end
def test_send_multiarg
assert_compiles(<<~'RUBY', result: "3#1,2,3/Q")
def internal_method(*args)
"#{args.size}##{args.join(",")}"
end
def jit_method
send(:internal_method, 1, 2, 3) + "/Q"
end
jit_method
RUBY
end
def test_send_kwargs
# For now, this side-exits when calls include keyword args
assert_compiles(<<~'RUBY', result: "2#a:1,b:2/A")
def internal_method(**kw)
"#{kw.size}##{kw.keys.map { |k| "#{k}:#{kw[k]}" }.join(",")}"
end
def jit_method
send(:internal_method, a: 1, b: 2) + "/A"
end
jit_method
RUBY
end
def test_send_kwargs_in_receiver_only
assert_compiles(<<~'RUBY', result: "0/RK", exits: {})
def internal_method(**kw)
"#{kw.size}"
end
def jit_method
send(:internal_method) + "/RK"
end
jit_method
RUBY
end
def test_send_with_underscores
assert_compiles(<<~'RUBY', result: "0/RK", exits: {})
def internal_method(**kw)
"#{kw.size}"
end
def jit_method
__send__(:internal_method) + "/RK"
end
jit_method
RUBY
end
def test_send_kwargs_splat
# For now, this side-exits when calling with a splat
assert_compiles(<<~'RUBY', result: "2#a:1,b:2/B")
def internal_method(**kw)
"#{kw.size}##{kw.keys.map { |k| "#{k}:#{kw[k]}" }.join(",")}"
end
def jit_method
send(:internal_method, **{ a: 1, b: 2 }) + "/B"
end
jit_method
RUBY
end
def test_send_block
# Setlocal_wc_0 sometimes side-exits on write barrier
assert_compiles(<<~'RUBY', result: "b:n/b:y/b:y/b:n")
def internal_method(&b)
"b:#{block_given? ? "y" : "n"}"
end
def jit_method
b7 = proc { 7 }
[
send(:internal_method),
send(:internal_method, &b7),
send(:internal_method) { 7 },
send(:internal_method, &nil),
].join("/")
end
jit_method
RUBY
end
def test_send_block_calling
assert_compiles(<<~'RUBY', result: "1a2", exits: {})
def internal_method
out = yield
"1" + out + "2"
end
def jit_method
__send__(:internal_method) { "a" }
end
jit_method
RUBY
end
def test_send_block_only_receiver
assert_compiles(<<~'RUBY', result: "b:n", exits: {})
def internal_method(&b)
"b:#{block_given? ? "y" : "n"}"
end
def jit_method
send(:internal_method)
end
jit_method
RUBY
end
def test_send_block_only_sender
assert_compiles(<<~'RUBY', result: "Y/Y/Y/Y", exits: {})
def internal_method
"Y"
end
def jit_method
b7 = proc { 7 }
[
send(:internal_method),
send(:internal_method, &b7),
send(:internal_method) { 7 },
send(:internal_method, &nil),
].join("/")
end
jit_method
RUBY
end
def test_multisend
assert_compiles(<<~'RUBY', result: "77")
def internal_method
"7"
end
def jit_method
send(:send, :internal_method) + send(:send, :send, :internal_method)
end
jit_method
RUBY
end
def test_getivar_opt_plus
assert_no_exits(<<~RUBY)
class TheClass
def initialize
@levar = 1
end
def get_sum
sum = 0
# The type of levar is unknown,
# but this still should not exit
sum += @levar
sum
end
end
obj = TheClass.new
obj.get_sum
RUBY
end
def test_super_iseq
assert_compiles(<<~'RUBY', insns: %i[invokesuper opt_plus opt_mult], result: 15)
class A
def foo
1 + 2
end
end
class B < A
def foo
super * 5
end
end
B.new.foo
RUBY
end
def test_super_with_alias
assert_compiles(<<~'RUBY', insns: %i[invokesuper opt_plus opt_mult], result: 15)
class A
def foo = 1 + 2
end
module M
def foo = super() * 5
alias bar foo
def foo = :bad
end
A.prepend M
A.new.bar
RUBY
end
def test_super_cfunc
assert_compiles(<<~'RUBY', insns: %i[invokesuper], result: "Hello")
class Gnirts < String
def initialize
super(-"olleH")
end
def to_s
super().reverse
end
end
Gnirts.new.to_s
RUBY
end
# Tests calling a variadic cfunc with many args
def test_build_large_struct
assert_compiles(<<~RUBY, insns: %i[opt_send_without_block], call_threshold: 2)
::Foo = Struct.new(:a, :b, :c, :d, :e, :f, :g, :h)
def build_foo
::Foo.new(:a, :b, :c, :d, :e, :f, :g, :h)
end
build_foo
build_foo
RUBY
end
def test_fib_recursion
assert_compiles(<<~'RUBY', insns: %i[opt_le opt_minus opt_plus opt_send_without_block], result: 34)
def fib(n)
return n if n <= 1
fib(n-1) + fib(n-2)
end
fib(9)
RUBY
end
def test_optarg_and_kwarg
assert_no_exits(<<~'RUBY')
def opt_and_kwarg(a, b=nil, c: nil)
end
2.times do
opt_and_kwarg(1, 2, c: 3)
end
RUBY
end
def test_cfunc_kwarg
assert_no_exits('{}.store(:value, foo: 123)')
assert_no_exits('{}.store(:value, foo: 123, bar: 456, baz: 789)')
assert_no_exits('{}.merge(foo: 123)')
assert_no_exits('{}.merge(foo: 123, bar: 456, baz: 789)')
end
# regression test simplified from URI::Generic#hostname=
def test_ctx_different_mappings
assert_compiles(<<~'RUBY', frozen_string_literal: true)
def foo(v)
!(v&.start_with?('[')) && v&.index(':')
end
foo(nil)
foo("example.com")
RUBY
end
def test_no_excessive_opt_getinlinecache_invalidation
assert_compiles(<<~'RUBY', exits: :any, result: :ok)
objects = [Object.new, Object.new]
objects.each do |o|
class << o
def foo
Object
end
end
end
9000.times {
objects[0].foo
objects[1].foo
}
stats = RubyVM::YJIT.runtime_stats
return :ok unless stats[:all_stats]
return :ok if stats[:invalidation_count] < 10
:fail
RUBY
end
def test_int_equal
assert_compiles(<<~'RUBY', exits: :any, result: [true, false, true, false, true, false, true, false])
def eq(a, b)
a == b
end
def eqq(a, b)
a === b
end
big1 = 2 ** 65
big2 = big1 + 1
[eq(1, 1), eq(1, 2), eq(big1, big1), eq(big1, big2), eqq(1, 1), eqq(1, 2), eqq(big1, big1), eqq(big1, big2)]
RUBY
end
def test_opt_case_dispatch
assert_compiles(<<~'RUBY', exits: :any, result: [:"1", "2", 3])
def case_dispatch(val)
case val
when 1
:"#{val}"
when 2
"#{val}"
else
val
end
end
[case_dispatch(1), case_dispatch(2), case_dispatch(3)]
RUBY
end
def test_code_gc
assert_compiles(code_gc_helpers + <<~'RUBY', exits: :any, result: :ok)
return :not_paged unless add_pages(100) # prepare freeable pages
RubyVM::YJIT.code_gc # first code GC
return :not_compiled1 unless compiles { nil } # should be JITable again
RubyVM::YJIT.code_gc # second code GC
return :not_compiled2 unless compiles { nil } # should be JITable again
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
return :"code_gc_#{code_gc_count}" if code_gc_count != 2
:ok
RUBY
end
def test_on_stack_code_gc_call
assert_compiles(code_gc_helpers + <<~'RUBY', exits: :any, result: :ok)
fiber = Fiber.new {
# Loop to call the same basic block again after Fiber.yield
while true
Fiber.yield(nil.to_i)
end
}
return :not_paged1 unless add_pages(400) # go to a page without initial ocb code
return :broken_resume1 if fiber.resume != 0 # JIT the fiber
RubyVM::YJIT.code_gc # first code GC, which should not free the fiber page
return :broken_resume2 if fiber.resume != 0 # The code should be still callable
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
return :"code_gc_#{code_gc_count}" if code_gc_count != 1
:ok
RUBY
end
def test_on_stack_code_gc_twice
assert_compiles(code_gc_helpers + <<~'RUBY', exits: :any, result: :ok)
fiber = Fiber.new {
# Loop to call the same basic block again after Fiber.yield
while Fiber.yield(nil.to_i); end
}
return :not_paged1 unless add_pages(400) # go to a page without initial ocb code
return :broken_resume1 if fiber.resume(true) != 0 # JIT the fiber
RubyVM::YJIT.code_gc # first code GC, which should not free the fiber page
return :not_paged2 unless add_pages(300) # add some stuff to be freed
# Not calling fiber.resume here to test the case that the YJIT payload loses some
# information at the previous code GC. The payload should still be there, and
# thus we could know the fiber ISEQ is still on stack on this second code GC.
RubyVM::YJIT.code_gc # second code GC, which should still not free the fiber page
return :not_paged3 unless add_pages(200) # attempt to overwrite the fiber page (it shouldn't)
return :broken_resume2 if fiber.resume(true) != 0 # The fiber code should be still fine
return :broken_resume3 if fiber.resume(false) != nil # terminate the fiber
RubyVM::YJIT.code_gc # third code GC, freeing a page that used to be on stack
return :not_paged4 unless add_pages(100) # check everything still works
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
return :"code_gc_#{code_gc_count}" if code_gc_count != 3
:ok
RUBY
end
def test_disable_code_gc_with_many_iseqs
assert_compiles(code_gc_helpers + <<~'RUBY', exits: :any, result: :ok, mem_size: 1, code_gc: false)
fiber = Fiber.new {
# Loop to call the same basic block again after Fiber.yield
while true
Fiber.yield(nil.to_i)
end
}
return :not_paged1 unless add_pages(250) # use some pages
return :broken_resume1 if fiber.resume != 0 # leave an on-stack code as well
add_pages(2000) # use a whole lot of pages to run out of 1MiB
return :broken_resume2 if fiber.resume != 0 # on-stack code should be callable
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
return :"code_gc_#{code_gc_count}" if code_gc_count != 0
:ok
RUBY
end
def test_code_gc_with_many_iseqs
assert_compiles(code_gc_helpers + <<~'RUBY', exits: :any, result: :ok, mem_size: 1, code_gc: true)
fiber = Fiber.new {
# Loop to call the same basic block again after Fiber.yield
while true
Fiber.yield(nil.to_i)
end
}
return :not_paged1 unless add_pages(250) # use some pages
return :broken_resume1 if fiber.resume != 0 # leave an on-stack code as well
add_pages(2000) # use a whole lot of pages to run out of 1MiB
return :broken_resume2 if fiber.resume != 0 # on-stack code should be callable
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
return :"code_gc_#{code_gc_count}" if code_gc_count == 0
:ok
RUBY
end
def test_code_gc_with_auto_compact
omit "compaction is not supported on this platform" unless GC.respond_to?(:compact)
assert_compiles((code_gc_helpers + <<~'RUBY'), exits: :any, result: :ok, mem_size: 1, code_gc: true)
# Test ISEQ moves in the middle of code GC
GC.auto_compact = true
fiber = Fiber.new {
# Loop to call the same basic block again after Fiber.yield
while true
Fiber.yield(nil.to_i)
end
}
return :not_paged1 unless add_pages(250) # use some pages
return :broken_resume1 if fiber.resume != 0 # leave an on-stack code as well
add_pages(2000) # use a whole lot of pages to run out of 1MiB
return :broken_resume2 if fiber.resume != 0 # on-stack code should be callable
code_gc_count = RubyVM::YJIT.runtime_stats[:code_gc_count]
return :"code_gc_#{code_gc_count}" if code_gc_count == 0
:ok
RUBY
end
def test_code_gc_partial_last_page
# call_threshold: 2 to avoid JIT-ing code_gc itself. If code_gc were JITed right before
# code_gc is called, the last page would be on stack.
assert_compiles(<<~'RUBY', exits: :any, result: :ok, call_threshold: 2)
# Leave a bunch of off-stack pages
i = 0
while i < 1000
eval("x = proc { 1.to_s }; x.call; x.call")
i += 1
end
# On Linux, memory page size != code page size. So the last code page could be partially
# mapped. This call tests that assertions and other things work fine under the situation.
RubyVM::YJIT.code_gc
:ok
RUBY
end
def test_trace_script_compiled # not ISEQ_TRACE_EVENTS
assert_compiles(<<~'RUBY', exits: :any, result: :ok)
@eval_counter = 0
def eval_script
eval('@eval_counter += 1')
end
@trace_counter = 0
trace = TracePoint.new(:script_compiled) do |t|
@trace_counter += 1
end
eval_script # JIT without TracePoint
trace.enable
eval_script # call with TracePoint
trace.disable
return :"eval_#{@eval_counter}" if @eval_counter != 2
return :"trace_#{@trace_counter}" if @trace_counter != 1
:ok
RUBY
end
def test_trace_b_call # ISEQ_TRACE_EVENTS
assert_compiles(<<~'RUBY', exits: :any, result: :ok)
@call_counter = 0
def block_call
1.times { @call_counter += 1 }
end
@trace_counter = 0
trace = TracePoint.new(:b_call) do |t|
@trace_counter += 1
end
block_call # JIT without TracePoint
trace.enable
block_call # call with TracePoint
trace.disable
return :"call_#{@call_counter}" if @call_counter != 2
return :"trace_#{@trace_counter}" if @trace_counter != 1
:ok
RUBY
end
def test_send_to_call
assert_compiles(<<~'RUBY', result: :ok)
->{ :ok }.send(:call)
RUBY
end
def test_invokeblock_many_locals
# [Bug #19299]
assert_compiles(<<~'RUBY', result: :ok)
def foo
yield
end
foo do
a1=a2=a3=a4=a5=a6=a7=a8=a9=a10=a11=a12=a13=a14=a15=a16=a17=a18=a19=a20=a21=a22=a23=a24=a25=a26=a27=a28=a29=a30 = :ok
a30
end
RUBY
end
def test_bug_19316
n = 2 ** 64
# foo's extra param and the splats are relevant
assert_compiles(<<~'RUBY', result: [[n, -n], [n, -n]], exits: :any)
def foo(_, a, b, c)
[a & b, ~c]
end
n = 2 ** 64
args = [0, -n, n, n-1]
GC.stress = true
[foo(*args), foo(*args)]
RUBY
end
def test_gc_compact_cyclic_branch
omit "compaction is not supported on this platform" unless GC.respond_to?(:compact)
assert_compiles(<<~'RUBY', result: 2)
def foo
i = 0
while i < 2
i += 1
end
i
end
foo
GC.compact
foo
RUBY
end
def test_invalidate_cyclic_branch
assert_compiles(<<~'RUBY', result: 2, exits: { opt_plus: 1 })
def foo
i = 0
while i < 2
i += 1
end
i
end
foo
class Integer
def +(x) = self - -x
end
foo
RUBY
end
def test_tracing_str_uplus
assert_compiles(<<~RUBY, frozen_string_literal: true, result: :ok, exits: { putspecialobject: 1, definemethod: 1 })
def str_uplus
_ = 1
_ = 2
ret = [+"frfr", __LINE__]
_ = 3
_ = 4
ret
end
str_uplus
require 'objspace'
ObjectSpace.trace_object_allocations_start
str, expected_line = str_uplus
alloc_line = ObjectSpace.allocation_sourceline(str)
if expected_line == alloc_line
:ok
else
[expected_line, alloc_line]
end
RUBY
end
def test_str_uplus_subclass
assert_compiles(<<~RUBY, frozen_string_literal: true, result: :subclass)
class S < String
def encoding
:subclass
end
end
def test(str)
(+str).encoding
end
test ""
test S.new
RUBY
end
def test_return_to_invalidated_block
# [Bug #19463]
assert_compiles(<<~RUBY, result: [1, 1, :ugokanai], exits: { definesmethod: 1, getlocal_WC_0: 1 })
klass = Class.new do
def self.lookup(hash, key) = hash[key]
def self.foo(a, b) = []
def self.test(hash, key)
[lookup(hash, key), key, "".freeze]
# 05 opt_send_without_block :lookup
# 07 getlocal_WC_0 :hash
# 09 opt_str_freeze ""
# 12 newarray 3
# 14 leave
#
# YJIT will put instructions (07..14) into a block.
# When String#freeze is redefined from within lookup(),
# the return address to the block is still on-stack. We rely
# on invalidation patching the code at the return address
# to service this situation correctly.
end
end
# get YJIT to compile test()
hash = { 1 => [] }
31.times { klass.test(hash, 1) }
# inject invalidation into lookup()
evil_hash = Hash.new do |_, key|
class String
undef :freeze
def freeze = :ugokanai
end
key
end
klass.test(evil_hash, 1)
RUBY
end
def test_return_to_invalidated_frame
assert_compiles(code_gc_helpers + <<~RUBY, exits: :any, result: :ok)
def jump
[] # something not inlined
end
def entry(code_gc)
jit_exception(code_gc)
jump # faulty jump after code GC. #jit_exception should not come back.
end
def jit_exception(code_gc)
if code_gc
tap do
RubyVM::YJIT.code_gc
break # jit_exec_exception catches TAG_BREAK and re-enters JIT code
end
end
end
add_pages(100)
jump # Compile #jump in a non-first page
add_pages(100)
entry(false) # Compile #entry and its call to #jump in another page
entry(true) # Free #jump but not #entry
:ok
RUBY
end
def test_setivar_on_class
# Bug in https://github.com/ruby/ruby/pull/8152
assert_compiles(<<~RUBY, result: :ok)
class Base
def self.or_equal
@or_equal ||= Object.new
end
end
Base.or_equal # ensure compiled
class Child < Base
end
200.times do |iv| # Need to be more than MAX_IVAR
Child.instance_variable_set("@_iv_\#{iv}", Object.new)
end
Child.or_equal
:ok
RUBY
end
def test_nested_send
#[Bug #19464]
assert_compiles(<<~RUBY, result: [:ok, :ok], exits: { defineclass: 1 })
klass = Class.new do
class << self
alias_method :my_send, :send
def bar = :ok
def foo = bar
end
end
with_break = -> { break klass.send(:my_send, :foo) }
wo_break = -> { klass.send(:my_send, :foo) }
[with_break[], wo_break[]]
RUBY
end
def test_str_concat_encoding_mismatch
assert_compiles(<<~'RUBY', result: "incompatible character encodings: BINARY (ASCII-8BIT) and EUC-JP")
def bar(a, b)
a << b
rescue => e
e.message
end
def foo(a, b, h)
h[nil]
bar(a, b) # Ruby call, not set cfp->pc
end
h = Hash.new { nil }
foo("\x80".b, "\xA1A1".dup.force_encoding("EUC-JP"), h)
foo("\x80".b, "\xA1A1".dup.force_encoding("EUC-JP"), h)
RUBY
end
def test_io_reopen_clobbering_singleton_class
assert_compiles(<<~RUBY, result: [:ok, :ok], exits: { definesmethod: 1, opt_eq: 2 })
def $stderr.to_i = :i
def test = $stderr.to_i
[test, test]
$stderr.reopen($stderr.dup)
[test, test].map { :ok unless _1 == :i }
RUBY
end
def test_opt_aref_with
assert_compiles(<<~RUBY, insns: %i[opt_aref_with], result: "bar", frozen_string_literal: false)
h = {"foo" => "bar"}
h["foo"]
RUBY
end
def test_proc_block_arg
assert_compiles(<<~RUBY, result: [:proc, :no_block])
def yield_if_given = block_given? ? yield : :no_block
def call(block_arg = nil) = yield_if_given(&block_arg)
[call(-> { :proc }), call]
RUBY
end
def test_opt_mult_overflow
assert_no_exits('0xfff_ffff_ffff_ffff * 0x10')
end
def test_disable_stats
assert_in_out_err(%w[--yjit-stats --yjit-disable])
end
def test_odd_calls_to_attr_reader
# Use of delegate from ActiveSupport use these kind of calls to getter methods.
assert_compiles(<<~RUBY, result: [1, 1, 1], no_send_fallbacks: true)
class One
attr_reader :one
def initialize
@one = 1
end
end
def calls(obj, empty, &)
[obj.one(*empty), obj.one(&), obj.one(*empty, &)]
end
calls(One.new, [])
RUBY
end
def test_kwrest
assert_compiles(<<~RUBY, result: true, no_send_fallbacks: true)
def req_rest(r1:, **kwrest) = [r1, kwrest]
def opt_rest(r1: 1.succ, **kwrest) = [r1, kwrest]
def kwrest(**kwrest) = kwrest
def calls
[
[1, {}] == req_rest(r1: 1),
[1, {:r2=>2, :r3=>3}] == req_rest(r1: 1, r2: 2, r3: 3),
[1, {:r2=>2, :r3=>3}] == req_rest(r2: 2, r1:1, r3: 3),
[1, {:r2=>2, :r3=>3}] == req_rest(r2: 2, r3: 3, r1: 1),
[2, {}] == opt_rest,
[2, { r2: 2, r3: 3 }] == opt_rest(r2: 2, r3: 3),
[0, { r2: 2, r3: 3 }] == opt_rest(r1: 0, r3: 3, r2: 2),
[0, { r2: 2, r3: 3 }] == opt_rest(r2: 2, r1: 0, r3: 3),
[1, { r2: 2, r3: 3 }] == opt_rest(r2: 2, r3: 3, r1: 1),
{} == kwrest,
{ r0: 88, r1: 99 } == kwrest(r0: 88, r1: 99),
]
end
calls.all?
RUBY
end
def test_send_polymorphic_method_name
assert_compiles(<<~'RUBY', result: %i[ok ok], no_send_fallbacks: true)
mid = "dynamic_mid_#{rand(100..200)}"
mid_dsym = mid.to_sym
define_method(mid) { :ok }
define_method(:send_site) { send(_1) }
[send_site(mid), send_site(mid_dsym)]
RUBY
end
def test_kw_splat_nil
assert_compiles(<<~'RUBY', result: %i[ok ok], no_send_fallbacks: true)
def id(x) = x
def kw_fw(arg, **) = id(arg, **)
def use = [kw_fw(:ok), :ok.itself(**nil)]
use
RUBY
end
def test_empty_splat
assert_compiles(<<~'RUBY', result: :ok, no_send_fallbacks: true)
def foo = :ok
def use(empty) = foo(*empty)
use([])
RUBY
end
def test_byteslice_sp_invalidation
assert_compiles(<<~'RUBY', result: 'ok', no_send_fallbacks: true)
"okng".itself.byteslice(0, 2)
RUBY
end
def test_leaf_builtin
assert_compiles(code_gc_helpers + <<~'RUBY', exits: :any, result: 1)
before = RubyVM::YJIT.runtime_stats[:num_send_iseq_leaf]
return 1 if before.nil?
def entry = self.class
entry
after = RubyVM::YJIT.runtime_stats[:num_send_iseq_leaf]
after - before
RUBY
end
def test_runtime_stats_types
assert_compiles(<<~'RUBY', exits: :any, result: true)
def test = :ok
3.times { test }
stats = RubyVM::YJIT.runtime_stats
return true unless stats[:all_stats]
[
stats[:object_shape_count].is_a?(Integer),
stats[:ratio_in_yjit].is_a?(Float),
].all?
RUBY
end
def test_runtime_stats_key_arg
assert_compiles(<<~'RUBY', exits: :any, result: true)
def test = :ok
3.times { test }
# Collect single stat.
stat = RubyVM::YJIT.runtime_stats(:ratio_in_yjit)
# Ensure this invocation had stats.
return true unless RubyVM::YJIT.runtime_stats[:all_stats]
stat > 0.0
RUBY
end
def test_runtime_stats_arg_error
assert_compiles(<<~'RUBY', exits: :any, result: true)
begin
RubyVM::YJIT.runtime_stats(Object.new)
:no_error
rescue TypeError => e
e.message == "non-symbol given"
end
RUBY
end
def test_runtime_stats_unknown_key
assert_compiles(<<~'RUBY', exits: :any, result: true)
def test = :ok
3.times { test }
RubyVM::YJIT.runtime_stats(:some_key_unlikely_to_exist).nil?
RUBY
end
def test_yjit_option_uses_array_each_in_ruby
assert_separately(["--yjit"], <<~'RUBY')
# Array#each should be implemented in Ruby for YJIT
assert_equal "<internal:array>", Array.instance_method(:each).source_location.first
# The backtrace, however, should not be `from <internal:array>:XX:in 'Array#each'`
begin
[nil].each { raise }
rescue => e
assert_equal "-:11:in 'Array#each'", e.backtrace[1]
end
RUBY
end
def test_yjit_enable_replaces_array_each
assert_separately([*("--disable=yjit" if RubyVM::YJIT.enabled?)], <<~'RUBY')
# Array#each should be implemented in C for the interpreter
assert_nil Array.instance_method(:each).source_location
# The backtrace should not be `from <internal:array>:XX:in 'Array#each'`
begin
[nil].each { raise }
rescue => e
assert_equal "-:11:in 'Array#each'", e.backtrace[1]
end
RubyVM::YJIT.enable
# Array#each should be implemented in Ruby for YJIT
assert_equal "<internal:array>", Array.instance_method(:each).source_location.first
# However, the backtrace should still not be `from <internal:array>:XX:in 'Array#each'`
begin
[nil].each { raise }
rescue => e
assert_equal "-:23:in 'Array#each'", e.backtrace[1]
end
RUBY
end
def test_yjit_enable_preserves_array_each_monkey_patch
assert_separately([*("--disable=yjit" if RubyVM::YJIT.enabled?)], <<~'RUBY')
# Array#each should be implemented in C initially
assert_nil Array.instance_method(:each).source_location
# Override Array#each
$called = false
Array.prepend(Module.new {
def each
$called = true
super
end
})
RubyVM::YJIT.enable
# The monkey-patch should still be alive
[].each {}
assert_true $called
# YJIT should not replace Array#each with the "<internal:array>" one
assert_equal "-", Array.instance_method(:each).source_location.first
RUBY
end
def test_yield_kwargs
assert_compiles(<<~RUBY, result: 3, no_send_fallbacks: true)
def req2kws = yield a: 1, b: 2
req2kws { |a:, b:| a + b }
RUBY
end
private
def code_gc_helpers
<<~'RUBY'
def compiles(&block)
failures = RubyVM::YJIT.runtime_stats[:compilation_failure]
block.call
failures == RubyVM::YJIT.runtime_stats[:compilation_failure]
end
def add_pages(num_jits)
pages = RubyVM::YJIT.runtime_stats[:live_page_count]
num_jits.times { return false unless eval('compiles { nil.to_i }') }
pages.nil? || pages < RubyVM::YJIT.runtime_stats[:live_page_count]
end
RUBY
end
def assert_no_exits(script)
assert_compiles(script)
end
ANY = Object.new
def assert_compiles(
test_script, insns: [],
call_threshold: 1,
stdout: nil,
exits: {},
result: ANY,
frozen_string_literal: nil,
mem_size: nil,
code_gc: false,
no_send_fallbacks: false
)
reset_stats = <<~RUBY
RubyVM::YJIT.runtime_stats
RubyVM::YJIT.reset_stats!
RUBY
write_results = <<~RUBY
stats = RubyVM::YJIT.runtime_stats
def collect_insns(iseq)
insns = RubyVM::YJIT.insns_compiled(iseq)
iseq.each_child { |c| insns.concat collect_insns(c) }
insns
end
iseq = RubyVM::InstructionSequence.of(_test_proc)
IO.open(3).write Marshal.dump({
result: #{result == ANY ? "nil" : "result"},
stats: stats,
insns: collect_insns(iseq),
disasm: iseq.disasm
})
RUBY
script = <<~RUBY
#{"# frozen_string_literal: " + frozen_string_literal.to_s unless frozen_string_literal.nil?}
_test_proc = -> {
#{test_script}
}
#{reset_stats}
result = _test_proc.call
#{write_results}
RUBY
status, out, err, stats = eval_with_jit(script, call_threshold:, mem_size:, code_gc:)
assert status.success?, "exited with status #{status.to_i}, stderr:\n#{err}"
assert_equal stdout.chomp, out.chomp if stdout
unless ANY.equal?(result)
assert_equal result, stats[:result]
end
runtime_stats = stats[:stats]
insns_compiled = stats[:insns]
disasm = stats[:disasm]
# Check that exit counts are as expected
# Full stats are only available when --enable-yjit=dev
if runtime_stats[:all_stats]
recorded_exits = runtime_stats.select { |k, v| k.to_s.start_with?("exit_") }
recorded_exits = recorded_exits.reject { |k, v| v == 0 }
recorded_exits.transform_keys! { |k| k.to_s.gsub("exit_", "").to_sym }
# Exits can be specified as a hash of stat-name symbol to integer for exact exits.
# or stat-name symbol to range if the number of side exits might vary (e.g. write
# barriers, cache misses.)
if exits != :any &&
exits != recorded_exits &&
(exits.keys != recorded_exits.keys || !exits.all? { |k, v| v === recorded_exits[k] }) # triple-equal checks range membership or integer equality
stats_reasons = StringIO.new
::RubyVM::YJIT.send(:_print_stats_reasons, runtime_stats, stats_reasons)
stats_reasons = stats_reasons.string
flunk <<~EOM
Expected #{exits.empty? ? "no" : exits.inspect} exits, but got:
#{recorded_exits.inspect}
Reasons:
#{stats_reasons}
EOM
end
end
if no_send_fallbacks
assert_equal(0, runtime_stats[:num_send_dynamic], "Expected no use of fallback implementation")
end
# Only available when --enable-yjit=dev
if runtime_stats[:all_stats]
missed_insns = insns.dup
insns_compiled.each do |op|
if missed_insns.include?(op)
# This instruction was compiled
missed_insns.delete(op)
end
end
unless missed_insns.empty?
flunk "Expected to compile instructions #{missed_insns.join(", ")} but didn't.\niseq:\n#{disasm}"
end
end
end
def script_shell_encode(s)
# We can't pass utf-8-encoded characters directly in a shell arg. But we can use Ruby \u constants.
s.chars.map { |c| c.ascii_only? ? c : "\\u%x" % c.codepoints[0] }.join
end
def eval_with_jit(script, call_threshold: 1, timeout: 1000, mem_size: nil, code_gc: false)
args = [
"--disable-gems",
"--yjit-call-threshold=#{call_threshold}",
"--yjit-stats=quiet"
]
args << "--yjit-exec-mem-size=#{mem_size}" if mem_size
args << "--yjit-code-gc" if code_gc
args << "-e" << script_shell_encode(script)
stats_r, stats_w = IO.pipe
# Separate thread so we don't deadlock when
# the child ruby blocks writing the stats to fd 3
stats = ''
stats_reader = Thread.new do
stats = stats_r.read
stats_r.close
end
out, err, status = invoke_ruby(args, '', true, true, timeout: timeout, ios: { 3 => stats_w })
stats_w.close
stats_reader.join(timeout)
stats = Marshal.load(stats) if !stats.empty?
[status, out, err, stats]
ensure
stats_reader&.kill
stats_reader&.join(timeout)
stats_r&.close
stats_w&.close
end
# A wrapper of EnvUtil.invoke_ruby that uses RbConfig.ruby instead of EnvUtil.ruby
# that might use a wrong Ruby depending on your environment.
def invoke_ruby(*args, **kwargs)
EnvUtil.invoke_ruby(*args, rubybin: RbConfig.ruby, **kwargs)
end
end
|