< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/UnsafeVirtualizationTest.java

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.core.test;
  24 
  25 import java.lang.reflect.Field;
  26 
  27 import org.graalvm.compiler.nodes.StructuredGraph;
  28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  29 import org.graalvm.compiler.options.OptionValues;
  30 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  31 import org.graalvm.compiler.phases.tiers.PhaseContext;
  32 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  33 import org.junit.Test;
  34 



  35 public class UnsafeVirtualizationTest extends GraalCompilerTest {
  36 
  37     public static class A {








  38         int f1;
  39         int f2;
  40     }
  41 
  42     private static final long AF1Offset;
  43     private static final long AF2Offset;
  44     static {
  45         long o1 = -1;
  46         long o2 = -1;
  47         try {
  48             Field f1 = A.class.getDeclaredField("f1");
  49             Field f2 = A.class.getDeclaredField("f2");
  50             o1 = UNSAFE.objectFieldOffset(f1);
  51             o2 = UNSAFE.objectFieldOffset(f2);
  52         } catch (NoSuchFieldException | SecurityException e) {
  53             throw new AssertionError(e);
  54         }
  55         AF1Offset = o1;
  56         AF2Offset = o2;
  57     }
  58 
  59     public static int unsafeSnippet0(int i1, int i2) {
  60         A a = new A();
  61         UNSAFE.putDouble(a, AF1Offset, i1 + i2);
  62         return UNSAFE.getInt(a, AF1Offset) + UNSAFE.getInt(a, AF2Offset);
  63     }
  64 
  65     public static int unsafeSnippet1(int i1, int i2) {
  66         A a = new A();
  67         UNSAFE.putDouble(a, AF1Offset, i1 + i2);







  68         a.f2 = i1;
  69         return (int) UNSAFE.getDouble(a, AF1Offset);





















  70     }
  71 
  72     @Test
  73     public void testUnsafePEA01() {
  74         testPartialEscapeReadElimination(parseEager("unsafeSnippet0", AllowAssumptions.NO), false);
  75         testPartialEscapeReadElimination(parseEager("unsafeSnippet0", AllowAssumptions.NO), true);
  76     }
  77 
  78     @Test
  79     public void testUnsafePEA02() {
  80         testPartialEscapeReadElimination(parseEager("unsafeSnippet1", AllowAssumptions.NO), false);
  81         testPartialEscapeReadElimination(parseEager("unsafeSnippet1", AllowAssumptions.NO), true);












  82     }
  83 
  84     public void testPartialEscapeReadElimination(StructuredGraph graph, boolean canonicalizeBefore) {










  85         OptionValues options = graph.getOptions();
  86         PhaseContext context = getDefaultHighTierContext();
  87         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  88         if (canonicalizeBefore) {
  89             canonicalizer.apply(graph, context);
  90         }

  91         new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context);







  92     }
  93 
  94 }


  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.core.test;
  24 
  25 import java.lang.reflect.Field;
  26 
  27 import org.graalvm.compiler.nodes.StructuredGraph;
  28 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  29 import org.graalvm.compiler.options.OptionValues;
  30 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  31 import org.graalvm.compiler.phases.tiers.PhaseContext;
  32 import org.graalvm.compiler.virtual.phases.ea.PartialEscapePhase;
  33 import org.junit.Test;
  34 
  35 import jdk.vm.ci.code.InstalledCode;
  36 import jdk.vm.ci.meta.ResolvedJavaMethod;
  37 
  38 public class UnsafeVirtualizationTest extends GraalCompilerTest {
  39 
  40     public static class Base {
  41         /*
  42          * This padding ensure that the size of the Base class ends up as a multiple of 8, which
  43          * makes the first field of the subclass 8-byte aligned.
  44          */
  45         double padding;
  46     }
  47 
  48     public static class A extends Base {
  49         int f1;
  50         int f2;
  51     }
  52 
  53     private static final long AF1Offset;
  54     private static final long AF2Offset;
  55     static {
  56         long o1 = -1;
  57         long o2 = -1;
  58         try {
  59             Field f1 = A.class.getDeclaredField("f1");
  60             Field f2 = A.class.getDeclaredField("f2");
  61             o1 = UNSAFE.objectFieldOffset(f1);
  62             o2 = UNSAFE.objectFieldOffset(f2);
  63         } catch (NoSuchFieldException | SecurityException e) {
  64             throw new AssertionError(e);
  65         }
  66         AF1Offset = o1;
  67         AF2Offset = o2;
  68     }
  69 
  70     public static int unsafeSnippet1(double i1) {
  71         A a = new A();
  72         UNSAFE.putDouble(a, AF1Offset, i1);
  73         return UNSAFE.getInt(a, AF1Offset) + UNSAFE.getInt(a, AF2Offset);
  74     }
  75 
  76     public static long unsafeSnippet2a(int i1) {
  77         A a = new A();
  78         UNSAFE.putDouble(a, AF1Offset, i1);
  79         a.f1 = i1;
  80         return UNSAFE.getLong(a, AF1Offset);
  81     }
  82 
  83     public static long unsafeSnippet2b(int i1) {
  84         A a = new A();
  85         UNSAFE.putDouble(a, AF1Offset, i1);
  86         a.f2 = i1;
  87         return UNSAFE.getLong(a, AF1Offset);
  88     }
  89 
  90     public static long unsafeSnippet3a(int i1) {
  91         A a = new A();
  92         UNSAFE.putDouble(a, AF1Offset, i1);
  93         UNSAFE.putInt(a, AF1Offset, i1);
  94         return UNSAFE.getLong(a, AF1Offset);
  95     }
  96 
  97     public static long unsafeSnippet3b(int i1) {
  98         A a = new A();
  99         UNSAFE.putDouble(a, AF1Offset, i1);
 100         UNSAFE.putInt(a, AF2Offset, i1);
 101         return UNSAFE.getLong(a, AF1Offset);
 102     }
 103 
 104     public static int unsafeSnippet4(double i1) {
 105         A a = new A();
 106         UNSAFE.putDouble(a, AF1Offset, i1);
 107         UNSAFE.putDouble(a, AF1Offset, i1);
 108         return UNSAFE.getInt(a, AF1Offset) + UNSAFE.getInt(a, AF2Offset);
 109     }
 110 
 111     @Test
 112     public void testUnsafePEA01() {
 113         testPartialEscapeReadElimination("unsafeSnippet1", false, 1.0);
 114         testPartialEscapeReadElimination("unsafeSnippet1", true, 1.0);
 115     }
 116 
 117     @Test
 118     public void testUnsafePEA02() {
 119         testPartialEscapeReadElimination("unsafeSnippet2a", false, 1);
 120         testPartialEscapeReadElimination("unsafeSnippet2a", true, 1);
 121 
 122         testPartialEscapeReadElimination("unsafeSnippet2b", false, 1);
 123         testPartialEscapeReadElimination("unsafeSnippet2b", true, 1);
 124     }
 125 
 126     @Test
 127     public void testUnsafePEA03() {
 128         testPartialEscapeReadElimination("unsafeSnippet3a", false, 1);
 129         testPartialEscapeReadElimination("unsafeSnippet3a", true, 1);
 130 
 131         testPartialEscapeReadElimination("unsafeSnippet3b", false, 1);
 132         testPartialEscapeReadElimination("unsafeSnippet3b", true, 1);
 133     }
 134 
 135     @Test
 136     public void testUnsafePEA04() {
 137         testPartialEscapeReadElimination("unsafeSnippet4", false, 1.0);
 138         testPartialEscapeReadElimination("unsafeSnippet4", true, 1.0);
 139     }
 140 
 141     public void testPartialEscapeReadElimination(String snippet, boolean canonicalizeBefore, Object... args) {
 142         assert AF1Offset % 8 == 0 : "First of the two int-fields must be 8-byte aligned";
 143 
 144         ResolvedJavaMethod method = getResolvedJavaMethod(snippet);
 145         StructuredGraph graph = parseEager(snippet, AllowAssumptions.NO);
 146         OptionValues options = graph.getOptions();
 147         PhaseContext context = getDefaultHighTierContext();
 148         CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
 149         if (canonicalizeBefore) {
 150             canonicalizer.apply(graph, context);
 151         }
 152         Result r = executeExpected(method, null, args);
 153         new PartialEscapePhase(true, true, canonicalizer, null, options).apply(graph, context);
 154         try {
 155             InstalledCode code = getCode(method, graph);
 156             Object result = code.executeVarargs(args);
 157             assertEquals(r, new Result(result, null));
 158         } catch (Throwable e) {
 159             assertFalse(true, e.toString());
 160         }
 161     }

 162 }
< prev index next >