Incorrect calculation result in loops involving short-to-int widening under C2 optimization

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu Linux x86_64

      Intel Xeon CPU

      A DESCRIPTION OF THE PROBLEM :
      A potential JIT miscompilation was found in HotSpot C2 compiler. When performing arithmetic operations on short array elements and storing the result in an int array within a loop, the results become incorrect after the method is compiled by C2.

      The calculation involves widening short values to int after multiplication. The error only appears after several iterations, suggesting that the issue is triggered by the C2 optimizer, likely during auto-vectorization (Superword optimization).

      REGRESSION : Java version that customer using for 17.0.18, 21.0.10



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. javac Test.java.
      2. java -Xcomp Test

      ---------- BEGIN SOURCE ----------
      public class Test {
          public static void main(String[] args) {
              int[] a = new int[10000];
              short[] s = new short[10000];
              for (int i = 0; i < 10000; i++) s[i] = (short) i;
              
              int[] gold = a.clone();
              calc(gold, s);
              
              for (int i = 0; i < 10000; i++) {
                  int[] res = a.clone();
                  calc(res, s);
                  for (int j = 0; j < 10000; j++) {
                      if (res[j] != gold[j]) throw new RuntimeException("bad");
                  }
              }
          }

          static void calc(int[] a, short[] s) {
              for (int i = 0; i < 2500; i += 2) {
                  a[i] += s[2*i] * s[2*i] + s[2*i+1] * s[2*i+1];
                  a[i+1] += s[2*i+2] * s[2*i+3] + s[2*i+3] * s[2*i+2];
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY :
      ALWAYS

            Assignee:
            Unassigned
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: