hotspot/test/compiler/loopopts/TestLogSum.java
author duke
Wed, 05 Jul 2017 22:24:41 +0200
changeset 41867 d62173b931bf
parent 40059 c2304140ed64
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24954
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     1
/*
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     4
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     8
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    13
 * accompanied this code).
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    14
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    18
 *
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    21
 * questions.
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    22
 */
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    23
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    24
/*
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    25
 * @test
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    26
 * @bug 8046516
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    27
 * @summary Segmentation fault in JVM (easily reproducible)
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24954
diff changeset
    28
 *
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24954
diff changeset
    29
 * @run main/othervm -XX:-TieredCompilation -Xbatch compiler.loopopts.TestLogSum
24954
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    30
 * @author jackkamm@gmail.com
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    31
 */
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    32
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24954
diff changeset
    33
package compiler.loopopts;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24954
diff changeset
    34
24954
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    35
import java.util.Arrays;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    36
import java.util.HashMap;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    37
import java.util.List;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    38
import java.util.Map;
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24954
diff changeset
    39
24954
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    40
public class TestLogSum {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    41
  public static void main(String[] args) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    42
    double sum;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    43
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    44
    for (int i = 0; i < 6; i++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    45
        for (int n = 2; n < 30; n++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    46
           for (int j = 1; j <= n; j++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    47
              for (int k = 1; k <= j; k++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    48
                // System.out.println(computeSum(k, j));
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    49
                sum = computeSum(k, j);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    50
              }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    51
           }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    52
        }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    53
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    54
   }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    55
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    56
   private static Map<List<Integer>, Double> cache = new HashMap<List<Integer>, Double>();
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    57
   public static double computeSum(int x, int y) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    58
      List<Integer> key = Arrays.asList(new Integer[] {x, y});
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    59
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    60
      if (!cache.containsKey(key)) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    61
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    62
        // explicitly creating/updating a double[] array, instead of using the LogSumArray wrapper object, will prevent the error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    63
        LogSumArray toReturn = new LogSumArray(x);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    64
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    65
        // changing loop indices will prevent the error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    66
        // in particular, for(z=0; z<x-1; z++), and then using z+1 in place of z, will not produce error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    67
        for (int z = 1; z < x+1; z++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    68
           double logSummand = Math.log(z + x + y);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    69
           toReturn.addLogSummand(logSummand);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    70
        }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    71
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    72
        // returning the value here without cacheing it will prevent the segfault
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    73
        cache.put(key, toReturn.retrieveLogSum());
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    74
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    75
      return cache.get(key);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    76
   }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    77
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    78
   /*
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    79
    * Given a bunch of logarithms log(X),log(Y),log(Z),...
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    80
    * This class is used to compute the log of the sum, log(X+Y+Z+...)
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    81
    */
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    82
   private static class LogSumArray {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    83
      private double[] logSummandArray;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    84
      private int currSize;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    85
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    86
      private double maxLogSummand;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    87
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    88
      public LogSumArray(int maxEntries) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    89
        this.logSummandArray = new double[maxEntries];
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    90
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    91
        this.currSize = 0;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    92
        this.maxLogSummand = Double.NEGATIVE_INFINITY;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    93
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    94
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    95
      public void addLogSummand(double logSummand) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    96
        logSummandArray[currSize] = logSummand;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    97
        currSize++;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    98
        // removing this line will prevent the error
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
    99
        maxLogSummand = Math.max(maxLogSummand, logSummand);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   100
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   101
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   102
      public double retrieveLogSum() {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   103
        if (maxLogSummand == Double.NEGATIVE_INFINITY) return Double.NEGATIVE_INFINITY;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   104
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   105
        assert currSize <= logSummandArray.length;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   106
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   107
        double factorSum = 0;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   108
        for (int i = 0; i < currSize; i++) {
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   109
           factorSum += Math.exp(logSummandArray[i] - maxLogSummand);
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   110
        }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   111
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   112
        return Math.log(factorSum) + maxLogSummand;
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   113
      }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   114
   }
9bd47e3d7c69 8046516: Segmentation fault in JVM (easily reproducible)
kvn
parents:
diff changeset
   115
}