RFR: 8346989: Deoptimization and re-compilation cycle with C2 compiled code

Marc Chevalier duke at openjdk.org
Fri Mar 7 14:22:29 UTC 2025


`Math.*Exact` intrinsics can cause many deopt when used repeatedly with problematic arguments.
This fix proposes not to rely on intrinsics after `too_many_traps()` has been reached.

Benchmark show that this issue affects every Math.*Exact functions. And this fix improve them all.

tl;dr:
- C1: no problem, no change
- C2:
  - with intrinsics:
    - with overflow: clear improvement. Was way worse than C1, now is similar (~4s => ~600ms)
    - without overflow: no problem, no change
  - without intrinsics: no problem, no change

Before the fix:

Benchmark                                           (SIZE)  Mode  Cnt     Score      Error  Units
MathExact.C1_1.loopAddIInBounds                    1000000  avgt    3     1.272 ±    0.048  ms/op
MathExact.C1_1.loopAddIOverflow                    1000000  avgt    3   641.917 ±   58.238  ms/op
MathExact.C1_1.loopAddLInBounds                    1000000  avgt    3     1.402 ±    0.842  ms/op
MathExact.C1_1.loopAddLOverflow                    1000000  avgt    3   671.013 ±  229.425  ms/op
MathExact.C1_1.loopDecrementIInBounds              1000000  avgt    3     3.722 ±   22.244  ms/op
MathExact.C1_1.loopDecrementIOverflow              1000000  avgt    3   653.341 ±  279.003  ms/op
MathExact.C1_1.loopDecrementLInBounds              1000000  avgt    3     2.525 ±    0.810  ms/op
MathExact.C1_1.loopDecrementLOverflow              1000000  avgt    3   656.750 ±  141.792  ms/op
MathExact.C1_1.loopIncrementIInBounds              1000000  avgt    3     4.621 ±   12.822  ms/op
MathExact.C1_1.loopIncrementIOverflow              1000000  avgt    3   651.608 ±  274.396  ms/op
MathExact.C1_1.loopIncrementLInBounds              1000000  avgt    3     2.576 ±    3.316  ms/op
MathExact.C1_1.loopIncrementLOverflow              1000000  avgt    3   662.216 ±   71.879  ms/op
MathExact.C1_1.loopMultiplyIInBounds               1000000  avgt    3     1.402 ±    0.587  ms/op
MathExact.C1_1.loopMultiplyIOverflow               1000000  avgt    3   615.836 ±  252.137  ms/op
MathExact.C1_1.loopMultiplyLInBounds               1000000  avgt    3     2.906 ±    5.718  ms/op
MathExact.C1_1.loopMultiplyLOverflow               1000000  avgt    3   655.576 ±  147.432  ms/op
MathExact.C1_1.loopNegateIInBounds                 1000000  avgt    3     2.023 ±    0.027  ms/op
MathExact.C1_1.loopNegateIOverflow                 1000000  avgt    3   639.136 ±   30.841  ms/op
MathExact.C1_1.loopNegateLInBounds                 1000000  avgt    3     2.422 ±    3.590  ms/op
MathExact.C1_1.loopNegateLOverflow                 1000000  avgt    3   638.837 ±   49.512  ms/op
MathExact.C1_1.loopSubtractIInBounds               1000000  avgt    3     1.255 ±    0.799  ms/op
MathExact.C1_1.loopSubtractIOverflow               1000000  avgt    3   637.857 ±  231.804  ms/op
MathExact.C1_1.loopSubtractLInBounds               1000000  avgt    3     1.412 ±    0.602  ms/op
MathExact.C1_1.loopSubtractLOverflow               1000000  avgt    3   642.113 ±  251.349  ms/op
MathExact.C1_2.loopAddIInBounds                    1000000  avgt    3     1.748 ±    1.095  ms/op
MathExact.C1_2.loopAddIOverflow                    1000000  avgt    3   654.617 ±  287.678  ms/op
MathExact.C1_2.loopAddLInBounds                    1000000  avgt    3     2.004 ±    1.655  ms/op
MathExact.C1_2.loopAddLOverflow                    1000000  avgt    3   670.791 ±   93.689  ms/op
MathExact.C1_2.loopDecrementIInBounds              1000000  avgt    3     5.306 ±   65.215  ms/op
MathExact.C1_2.loopDecrementIOverflow              1000000  avgt    3   650.425 ±  461.740  ms/op
MathExact.C1_2.loopDecrementLInBounds              1000000  avgt    3     5.484 ±   42.778  ms/op
MathExact.C1_2.loopDecrementLOverflow              1000000  avgt    3   656.747 ±  333.281  ms/op
MathExact.C1_2.loopIncrementIInBounds              1000000  avgt    3     3.077 ±    1.677  ms/op
MathExact.C1_2.loopIncrementIOverflow              1000000  avgt    3   634.510 ±   51.365  ms/op
MathExact.C1_2.loopIncrementLInBounds              1000000  avgt    3     3.902 ±   18.471  ms/op
MathExact.C1_2.loopIncrementLOverflow              1000000  avgt    3   656.465 ±  227.014  ms/op
MathExact.C1_2.loopMultiplyIInBounds               1000000  avgt    3     2.384 ±   10.045  ms/op
MathExact.C1_2.loopMultiplyIOverflow               1000000  avgt    3   624.029 ±  342.084  ms/op
MathExact.C1_2.loopMultiplyLInBounds               1000000  avgt    3     3.247 ±    0.735  ms/op
MathExact.C1_2.loopMultiplyLOverflow               1000000  avgt    3   661.427 ±  100.744  ms/op
MathExact.C1_2.loopNegateIInBounds                 1000000  avgt    3     3.061 ±    1.148  ms/op
MathExact.C1_2.loopNegateIOverflow                 1000000  avgt    3   645.241 ±  323.824  ms/op
MathExact.C1_2.loopNegateLInBounds                 1000000  avgt    3     3.211 ±    0.068  ms/op
MathExact.C1_2.loopNegateLOverflow                 1000000  avgt    3   658.846 ±  204.524  ms/op
MathExact.C1_2.loopSubtractIInBounds               1000000  avgt    3     1.717 ±    0.161  ms/op
MathExact.C1_2.loopSubtractIOverflow               1000000  avgt    3   644.287 ±  301.787  ms/op
MathExact.C1_2.loopSubtractLInBounds               1000000  avgt    3     3.976 ±   11.982  ms/op
MathExact.C1_2.loopSubtractLOverflow               1000000  avgt    3   660.871 ±   16.538  ms/op
MathExact.C1_3.loopAddIInBounds                    1000000  avgt    3     4.380 ±   42.598  ms/op
MathExact.C1_3.loopAddIOverflow                    1000000  avgt    3   686.766 ±  511.146  ms/op
MathExact.C1_3.loopAddLInBounds                    1000000  avgt    3     5.445 ±   49.738  ms/op
MathExact.C1_3.loopAddLOverflow                    1000000  avgt    3   641.936 ±   32.769  ms/op
MathExact.C1_3.loopDecrementIInBounds              1000000  avgt    3     8.340 ±   69.455  ms/op
MathExact.C1_3.loopDecrementIOverflow              1000000  avgt    3   682.239 ±  212.017  ms/op
MathExact.C1_3.loopDecrementLInBounds              1000000  avgt    3     6.048 ±    0.651  ms/op
MathExact.C1_3.loopDecrementLOverflow              1000000  avgt    3   670.924 ±   42.037  ms/op
MathExact.C1_3.loopIncrementIInBounds              1000000  avgt    3     7.970 ±   63.664  ms/op
MathExact.C1_3.loopIncrementIOverflow              1000000  avgt    3   684.490 ±  197.407  ms/op
MathExact.C1_3.loopIncrementLInBounds              1000000  avgt    3     8.780 ±   86.737  ms/op
MathExact.C1_3.loopIncrementLOverflow              1000000  avgt    3   660.941 ±  172.305  ms/op
MathExact.C1_3.loopMultiplyIInBounds               1000000  avgt    3     3.241 ±    0.567  ms/op
MathExact.C1_3.loopMultiplyIOverflow               1000000  avgt    3   630.455 ±  138.458  ms/op
MathExact.C1_3.loopMultiplyLInBounds               1000000  avgt    3     5.906 ±    0.662  ms/op
MathExact.C1_3.loopMultiplyLOverflow               1000000  avgt    3   693.248 ±  539.146  ms/op
MathExact.C1_3.loopNegateIInBounds                 1000000  avgt    3     6.394 ±    7.757  ms/op
MathExact.C1_3.loopNegateIOverflow                 1000000  avgt    3   644.722 ±   56.929  ms/op
MathExact.C1_3.loopNegateLInBounds                 1000000  avgt    3     7.610 ±   41.533  ms/op
MathExact.C1_3.loopNegateLOverflow                 1000000  avgt    3   670.166 ±   14.496  ms/op
MathExact.C1_3.loopSubtractIInBounds               1000000  avgt    3     3.345 ±    1.977  ms/op
MathExact.C1_3.loopSubtractIOverflow               1000000  avgt    3   677.317 ±   22.878  ms/op
MathExact.C1_3.loopSubtractLInBounds               1000000  avgt    3     3.226 ±    0.122  ms/op
MathExact.C1_3.loopSubtractLOverflow               1000000  avgt    3   643.642 ±   65.217  ms/op
MathExact.C2.loopAddIInBounds                      1000000  avgt    3     1.217 ±    1.694  ms/op
MathExact.C2.loopAddIOverflow                      1000000  avgt    3  3995.424 ± 1177.165  ms/op
MathExact.C2.loopAddLInBounds                      1000000  avgt    3     2.404 ±    0.053  ms/op
MathExact.C2.loopAddLOverflow                      1000000  avgt    3  3997.984 ±  612.558  ms/op
MathExact.C2.loopDecrementIInBounds                1000000  avgt    3     2.014 ±    0.176  ms/op
MathExact.C2.loopDecrementIOverflow                1000000  avgt    3  3828.615 ±  260.670  ms/op
MathExact.C2.loopDecrementLInBounds                1000000  avgt    3     1.986 ±    1.536  ms/op
MathExact.C2.loopDecrementLOverflow                1000000  avgt    3  4075.934 ±  263.798  ms/op
MathExact.C2.loopIncrementIInBounds                1000000  avgt    3     2.238 ±    6.380  ms/op
MathExact.C2.loopIncrementIOverflow                1000000  avgt    3  3927.929 ±  837.162  ms/op
MathExact.C2.loopIncrementLInBounds                1000000  avgt    3     1.971 ±    1.232  ms/op
MathExact.C2.loopIncrementLOverflow                1000000  avgt    3  3915.202 ± 1024.956  ms/op
MathExact.C2.loopMultiplyIInBounds                 1000000  avgt    3     1.175 ±    0.509  ms/op
MathExact.C2.loopMultiplyIOverflow                 1000000  avgt    3  3803.719 ± 1583.828  ms/op
MathExact.C2.loopMultiplyLInBounds                 1000000  avgt    3     0.937 ±    0.631  ms/op
MathExact.C2.loopMultiplyLOverflow                 1000000  avgt    3  4023.742 ±  967.498  ms/op
MathExact.C2.loopNegateIInBounds                   1000000  avgt    3     2.129 ±    1.094  ms/op
MathExact.C2.loopNegateIOverflow                   1000000  avgt    3  3850.484 ±  464.979  ms/op
MathExact.C2.loopNegateLInBounds                   1000000  avgt    3     2.247 ±    9.714  ms/op
MathExact.C2.loopNegateLOverflow                   1000000  avgt    3  3911.853 ±  362.961  ms/op
MathExact.C2.loopSubtractIInBounds                 1000000  avgt    3     1.141 ±    1.579  ms/op
MathExact.C2.loopSubtractIOverflow                 1000000  avgt    3  3917.533 ±  628.485  ms/op
MathExact.C2.loopSubtractLInBounds                 1000000  avgt    3     2.232 ±   22.329  ms/op
MathExact.C2.loopSubtractLOverflow                 1000000  avgt    3  3995.088 ±  302.549  ms/op
MathExact.C2_no_intrinsics.loopAddIInBounds        1000000  avgt    3     1.488 ±   12.243  ms/op
MathExact.C2_no_intrinsics.loopAddIOverflow        1000000  avgt    3   585.568 ±  106.360  ms/op
MathExact.C2_no_intrinsics.loopAddLInBounds        1000000  avgt    3     2.234 ±   23.010  ms/op
MathExact.C2_no_intrinsics.loopAddLOverflow        1000000  avgt    3   602.290 ±  212.146  ms/op
MathExact.C2_no_intrinsics.loopDecrementIInBounds  1000000  avgt    3     4.705 ±   36.814  ms/op
MathExact.C2_no_intrinsics.loopDecrementIOverflow  1000000  avgt    3   590.212 ±  280.334  ms/op
MathExact.C2_no_intrinsics.loopDecrementLInBounds  1000000  avgt    3     2.374 ±   13.667  ms/op
MathExact.C2_no_intrinsics.loopDecrementLOverflow  1000000  avgt    3   583.053 ±   50.535  ms/op
MathExact.C2_no_intrinsics.loopIncrementIInBounds  1000000  avgt    3     3.966 ±   15.366  ms/op
MathExact.C2_no_intrinsics.loopIncrementIOverflow  1000000  avgt    3   591.683 ±  171.580  ms/op
MathExact.C2_no_intrinsics.loopIncrementLInBounds  1000000  avgt    3     3.682 ±   23.147  ms/op
MathExact.C2_no_intrinsics.loopIncrementLOverflow  1000000  avgt    3   601.325 ±   10.597  ms/op
MathExact.C2_no_intrinsics.loopMultiplyIInBounds   1000000  avgt    3     1.307 ±    0.235  ms/op
MathExact.C2_no_intrinsics.loopMultiplyIOverflow   1000000  avgt    3   570.615 ±   50.808  ms/op
MathExact.C2_no_intrinsics.loopMultiplyLInBounds   1000000  avgt    3     1.087 ±    0.486  ms/op
MathExact.C2_no_intrinsics.loopMultiplyLOverflow   1000000  avgt    3   595.713 ±  162.773  ms/op
MathExact.C2_no_intrinsics.loopNegateIInBounds     1000000  avgt    3     1.874 ±    0.954  ms/op
MathExact.C2_no_intrinsics.loopNegateIOverflow     1000000  avgt    3   596.588 ±   68.081  ms/op
MathExact.C2_no_intrinsics.loopNegateLInBounds     1000000  avgt    3     2.337 ±   12.164  ms/op
MathExact.C2_no_intrinsics.loopNegateLOverflow     1000000  avgt    3   573.711 ±   63.243  ms/op
MathExact.C2_no_intrinsics.loopSubtractIInBounds   1000000  avgt    3     1.085 ±    0.815  ms/op
MathExact.C2_no_intrinsics.loopSubtractIOverflow   1000000  avgt    3   579.489 ±   61.399  ms/op
MathExact.C2_no_intrinsics.loopSubtractLInBounds   1000000  avgt    3     1.020 ±    0.161  ms/op
MathExact.C2_no_intrinsics.loopSubtractLOverflow   1000000  avgt    3   580.578 ±  167.454  ms/op


After:

Benchmark                                           (SIZE)  Mode  Cnt    Score     Error  Units
MathExact.C1_1.loopAddIInBounds                    1000000  avgt    3    1.369 ±   0.462  ms/op
MathExact.C1_1.loopAddIOverflow                    1000000  avgt    3  635.020 ± 106.156  ms/op
MathExact.C1_1.loopAddLInBounds                    1000000  avgt    3    1.371 ±   0.020  ms/op
MathExact.C1_1.loopAddLOverflow                    1000000  avgt    3  633.864 ±  72.176  ms/op
MathExact.C1_1.loopDecrementIInBounds              1000000  avgt    3    2.053 ±   0.330  ms/op
MathExact.C1_1.loopDecrementIOverflow              1000000  avgt    3  634.675 ±  79.427  ms/op
MathExact.C1_1.loopDecrementLInBounds              1000000  avgt    3    3.798 ±  38.502  ms/op
MathExact.C1_1.loopDecrementLOverflow              1000000  avgt    3  650.880 ± 123.220  ms/op
MathExact.C1_1.loopIncrementIInBounds              1000000  avgt    3    2.305 ±   4.829  ms/op
MathExact.C1_1.loopIncrementIOverflow              1000000  avgt    3  648.231 ±  39.012  ms/op
MathExact.C1_1.loopIncrementLInBounds              1000000  avgt    3    2.627 ±   3.129  ms/op
MathExact.C1_1.loopIncrementLOverflow              1000000  avgt    3  663.671 ± 446.140  ms/op
MathExact.C1_1.loopMultiplyIInBounds               1000000  avgt    3    1.479 ±   0.102  ms/op
MathExact.C1_1.loopMultiplyIOverflow               1000000  avgt    3  627.959 ± 297.291  ms/op
MathExact.C1_1.loopMultiplyLInBounds               1000000  avgt    3    2.718 ±   0.806  ms/op
MathExact.C1_1.loopMultiplyLOverflow               1000000  avgt    3  655.310 ± 112.686  ms/op
MathExact.C1_1.loopNegateIInBounds                 1000000  avgt    3    2.079 ±   2.166  ms/op
MathExact.C1_1.loopNegateIOverflow                 1000000  avgt    3  640.530 ± 152.489  ms/op
MathExact.C1_1.loopNegateLInBounds                 1000000  avgt    3    3.168 ±  16.524  ms/op
MathExact.C1_1.loopNegateLOverflow                 1000000  avgt    3  650.823 ±  58.420  ms/op
MathExact.C1_1.loopSubtractIInBounds               1000000  avgt    3    2.325 ±  27.865  ms/op
MathExact.C1_1.loopSubtractIOverflow               1000000  avgt    3  632.198 ± 280.799  ms/op
MathExact.C1_1.loopSubtractLInBounds               1000000  avgt    3    1.478 ±   0.281  ms/op
MathExact.C1_1.loopSubtractLOverflow               1000000  avgt    3  626.481 ±  47.028  ms/op
MathExact.C1_2.loopAddIInBounds                    1000000  avgt    3    1.850 ±   0.462  ms/op
MathExact.C1_2.loopAddIOverflow                    1000000  avgt    3  640.668 ± 217.610  ms/op
MathExact.C1_2.loopAddLInBounds                    1000000  avgt    3    1.823 ±   0.123  ms/op
MathExact.C1_2.loopAddLOverflow                    1000000  avgt    3  643.123 ± 174.505  ms/op
MathExact.C1_2.loopDecrementIInBounds              1000000  avgt    3    6.435 ±  54.316  ms/op
MathExact.C1_2.loopDecrementIOverflow              1000000  avgt    3  649.622 ±  15.314  ms/op
MathExact.C1_2.loopDecrementLInBounds              1000000  avgt    3    4.315 ±  26.421  ms/op
MathExact.C1_2.loopDecrementLOverflow              1000000  avgt    3  649.018 ± 386.320  ms/op
MathExact.C1_2.loopIncrementIInBounds              1000000  avgt    3    3.444 ±   1.375  ms/op
MathExact.C1_2.loopIncrementIOverflow              1000000  avgt    3  628.711 ±  51.292  ms/op
MathExact.C1_2.loopIncrementLInBounds              1000000  avgt    3    3.351 ±   0.483  ms/op
MathExact.C1_2.loopIncrementLOverflow              1000000  avgt    3  653.560 ± 160.718  ms/op
MathExact.C1_2.loopMultiplyIInBounds               1000000  avgt    3    1.860 ±   0.633  ms/op
MathExact.C1_2.loopMultiplyIOverflow               1000000  avgt    3  620.883 ±  54.516  ms/op
MathExact.C1_2.loopMultiplyLInBounds               1000000  avgt    3    3.998 ±  16.269  ms/op
MathExact.C1_2.loopMultiplyLOverflow               1000000  avgt    3  671.956 ±  93.092  ms/op
MathExact.C1_2.loopNegateIInBounds                 1000000  avgt    3    4.415 ±  44.105  ms/op
MathExact.C1_2.loopNegateIOverflow                 1000000  avgt    3  661.902 ± 224.843  ms/op
MathExact.C1_2.loopNegateLInBounds                 1000000  avgt    3    3.492 ±   0.738  ms/op
MathExact.C1_2.loopNegateLOverflow                 1000000  avgt    3  634.946 ± 150.491  ms/op
MathExact.C1_2.loopSubtractIInBounds               1000000  avgt    3    1.712 ±   0.066  ms/op
MathExact.C1_2.loopSubtractIOverflow               1000000  avgt    3  651.508 ±  76.022  ms/op
MathExact.C1_2.loopSubtractLInBounds               1000000  avgt    3    1.949 ±   0.201  ms/op
MathExact.C1_2.loopSubtractLOverflow               1000000  avgt    3  627.459 ±  26.817  ms/op
MathExact.C1_3.loopAddIInBounds                    1000000  avgt    3    7.378 ±   4.301  ms/op
MathExact.C1_3.loopAddIOverflow                    1000000  avgt    3  647.275 ± 177.062  ms/op
MathExact.C1_3.loopAddLInBounds                    1000000  avgt    3    3.427 ±   0.037  ms/op
MathExact.C1_3.loopAddLOverflow                    1000000  avgt    3  643.735 ± 227.934  ms/op
MathExact.C1_3.loopDecrementIInBounds              1000000  avgt    3    5.680 ±   0.497  ms/op
MathExact.C1_3.loopDecrementIOverflow              1000000  avgt    3  666.431 ±   8.006  ms/op
MathExact.C1_3.loopDecrementLInBounds              1000000  avgt    3    6.897 ±  24.615  ms/op
MathExact.C1_3.loopDecrementLOverflow              1000000  avgt    3  683.691 ±  52.892  ms/op
MathExact.C1_3.loopIncrementIInBounds              1000000  avgt    3    5.743 ±   0.602  ms/op
MathExact.C1_3.loopIncrementIOverflow              1000000  avgt    3  670.027 ± 175.208  ms/op
MathExact.C1_3.loopIncrementLInBounds              1000000  avgt    3    6.157 ±   2.876  ms/op
MathExact.C1_3.loopIncrementLOverflow              1000000  avgt    3  673.410 ± 245.939  ms/op
MathExact.C1_3.loopMultiplyIInBounds               1000000  avgt    3    3.220 ±   0.165  ms/op
MathExact.C1_3.loopMultiplyIOverflow               1000000  avgt    3  640.165 ± 505.006  ms/op
MathExact.C1_3.loopMultiplyLInBounds               1000000  avgt    3    7.986 ±  62.547  ms/op
MathExact.C1_3.loopMultiplyLOverflow               1000000  avgt    3  681.282 ± 107.856  ms/op
MathExact.C1_3.loopNegateIInBounds                 1000000  avgt    3    7.133 ±  18.111  ms/op
MathExact.C1_3.loopNegateIOverflow                 1000000  avgt    3  680.976 ± 285.486  ms/op
MathExact.C1_3.loopNegateLInBounds                 1000000  avgt    3    7.405 ±  37.040  ms/op
MathExact.C1_3.loopNegateLOverflow                 1000000  avgt    3  681.574 ± 173.484  ms/op
MathExact.C1_3.loopSubtractIInBounds               1000000  avgt    3    3.971 ±  16.942  ms/op
MathExact.C1_3.loopSubtractIOverflow               1000000  avgt    3  655.780 ± 230.793  ms/op
MathExact.C1_3.loopSubtractLInBounds               1000000  avgt    3    3.369 ±   3.844  ms/op
MathExact.C1_3.loopSubtractLOverflow               1000000  avgt    3  634.824 ±  20.350  ms/op
MathExact.C2.loopAddIInBounds                      1000000  avgt    3    2.461 ±   2.936  ms/op
MathExact.C2.loopAddIOverflow                      1000000  avgt    3  589.095 ± 151.126  ms/op
MathExact.C2.loopAddLInBounds                      1000000  avgt    3    0.978 ±   0.604  ms/op
MathExact.C2.loopAddLOverflow                      1000000  avgt    3  590.511 ±  64.618  ms/op
MathExact.C2.loopDecrementIInBounds                1000000  avgt    3    1.981 ±   0.443  ms/op
MathExact.C2.loopDecrementIOverflow                1000000  avgt    3  593.578 ±  32.752  ms/op
MathExact.C2.loopDecrementLInBounds                1000000  avgt    3    2.924 ±  29.455  ms/op
MathExact.C2.loopDecrementLOverflow                1000000  avgt    3  601.392 ± 936.568  ms/op
MathExact.C2.loopIncrementIInBounds                1000000  avgt    3    2.697 ±  22.142  ms/op
MathExact.C2.loopIncrementIOverflow                1000000  avgt    3  602.418 ± 199.763  ms/op
MathExact.C2.loopIncrementLInBounds                1000000  avgt    3    1.954 ±   0.396  ms/op
MathExact.C2.loopIncrementLOverflow                1000000  avgt    3  601.183 ± 156.439  ms/op
MathExact.C2.loopMultiplyIInBounds                 1000000  avgt    3    1.530 ±   7.954  ms/op
MathExact.C2.loopMultiplyIOverflow                 1000000  avgt    3  566.677 ±  45.992  ms/op
MathExact.C2.loopMultiplyLInBounds                 1000000  avgt    3    2.184 ±  22.242  ms/op
MathExact.C2.loopMultiplyLOverflow                 1000000  avgt    3  600.233 ± 234.648  ms/op
MathExact.C2.loopNegateIInBounds                   1000000  avgt    3    2.130 ±   1.028  ms/op
MathExact.C2.loopNegateIOverflow                   1000000  avgt    3  593.145 ± 337.886  ms/op
MathExact.C2.loopNegateLInBounds                   1000000  avgt    3    2.600 ±  20.795  ms/op
MathExact.C2.loopNegateLOverflow                   1000000  avgt    3  592.288 ± 138.321  ms/op
MathExact.C2.loopSubtractIInBounds                 1000000  avgt    3    1.081 ±   0.265  ms/op
MathExact.C2.loopSubtractIOverflow                 1000000  avgt    3  575.884 ± 200.113  ms/op
MathExact.C2.loopSubtractLInBounds                 1000000  avgt    3    1.016 ±   0.792  ms/op
MathExact.C2.loopSubtractLOverflow                 1000000  avgt    3  589.873 ±  52.521  ms/op
MathExact.C2_no_intrinsics.loopAddIInBounds        1000000  avgt    3    2.166 ±  10.999  ms/op
MathExact.C2_no_intrinsics.loopAddIOverflow        1000000  avgt    3  586.660 ± 229.451  ms/op
MathExact.C2_no_intrinsics.loopAddLInBounds        1000000  avgt    3    1.054 ±   0.528  ms/op
MathExact.C2_no_intrinsics.loopAddLOverflow        1000000  avgt    3  572.511 ±  76.440  ms/op
MathExact.C2_no_intrinsics.loopDecrementIInBounds  1000000  avgt    3    1.907 ±   0.149  ms/op
MathExact.C2_no_intrinsics.loopDecrementIOverflow  1000000  avgt    3  599.262 ± 600.992  ms/op
MathExact.C2_no_intrinsics.loopDecrementLInBounds  1000000  avgt    3    1.820 ±   0.106  ms/op
MathExact.C2_no_intrinsics.loopDecrementLOverflow  1000000  avgt    3  570.464 ±  44.418  ms/op
MathExact.C2_no_intrinsics.loopIncrementIInBounds  1000000  avgt    3    1.914 ±   0.131  ms/op
MathExact.C2_no_intrinsics.loopIncrementIOverflow  1000000  avgt    3  575.143 ± 160.185  ms/op
MathExact.C2_no_intrinsics.loopIncrementLInBounds  1000000  avgt    3    1.818 ±   0.288  ms/op
MathExact.C2_no_intrinsics.loopIncrementLOverflow  1000000  avgt    3  589.998 ±  33.029  ms/op
MathExact.C2_no_intrinsics.loopMultiplyIInBounds   1000000  avgt    3    1.960 ±  10.135  ms/op
MathExact.C2_no_intrinsics.loopMultiplyIOverflow   1000000  avgt    3  571.497 ± 264.484  ms/op
MathExact.C2_no_intrinsics.loopMultiplyLInBounds   1000000  avgt    3    1.061 ±   0.198  ms/op
MathExact.C2_no_intrinsics.loopMultiplyLOverflow   1000000  avgt    3  585.139 ± 317.175  ms/op
MathExact.C2_no_intrinsics.loopNegateIInBounds     1000000  avgt    3    2.611 ±  22.325  ms/op
MathExact.C2_no_intrinsics.loopNegateIOverflow     1000000  avgt    3  579.911 ± 140.426  ms/op
MathExact.C2_no_intrinsics.loopNegateLInBounds     1000000  avgt    3    2.233 ±   2.774  ms/op
MathExact.C2_no_intrinsics.loopNegateLOverflow     1000000  avgt    3  572.368 ±  81.851  ms/op
MathExact.C2_no_intrinsics.loopSubtractIInBounds   1000000  avgt    3    3.162 ±  38.115  ms/op
MathExact.C2_no_intrinsics.loopSubtractIOverflow   1000000  avgt    3  582.794 ±  65.622  ms/op
MathExact.C2_no_intrinsics.loopSubtractLInBounds   1000000  avgt    3    1.028 ±   0.255  ms/op
MathExact.C2_no_intrinsics.loopSubtractLOverflow   1000000  avgt    3  577.491 ±  69.778  ms/op


Is it worth having intrinsics at all? @eme64 wondered, so I tried with this code:

public class Test {
    final static int N = 500_000_000;

    public static int test(int i) {
        try{
            return Math.multiplyExact(i, i);
        } catch (Throwable e){
            return 0;
        }
    }

    public static void loop() {
        for(int i = 0; i < N; i++) {
            test(i % 32_768);
        }
    }

    public static void main(String[] args) {
        loop();
    }
}

and with much more runs (50 instead of 3), and in a more stable load for the rest of the system.

No intrinsic (inlined Java implem):

Benchmark 1: ~/jdk/build/linux-x64/jdk/bin/java -XX:CompileCommand=compileonly,"Test*::test*" -XX:-UseOnStackReplacement Test.java
  Time (mean ± σ):      8.651 s ±  0.902 s    [User: 8.517 s, System: 0.155 s]
  Range (min … max):    6.853 s … 10.439 s    50 runs


Always intrinsic (current behavior, and new behavior in absence of overflow, like in this example):

Benchmark 1: ~/jdk/build/linux-x64/jdk/bin/java -XX:CompileCommand=compileonly,"Test*::test*" -XX:-UseOnStackReplacement Test.java
  Time (mean ± σ):      8.222 s ±  1.024 s    [User: 8.090 s, System: 0.155 s]
  Range (min … max):    6.667 s … 10.406 s    50 runs


So it's... not very conclusive, but likely to be a bit useful. The gap between the means is about 0.4s, which is less than half the standard deviation.
Still, it seems good to have.

>From a more theoretical point of view, we can see that the code generated for the instrinsics is mostly a `mul` and a `jo`, while it is much more complicated for inlined java (with many `mov`, `movsx`, `cmp` and conditional jumps, looking a lot like the Java code).

Thanks,
Marc

-------------

Commit messages:
 - More exhaustive bench
 - Limit inlining of math Exact operations in case of too many deopts

Changes: https://git.openjdk.org/jdk/pull/23916/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=23916&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8346989
  Stats: 405 lines in 2 files changed: 404 ins; 0 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/23916.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/23916/head:pull/23916

PR: https://git.openjdk.org/jdk/pull/23916


More information about the hotspot-compiler-dev mailing list