package org.graalvm.compiler.hotspot.test;
import static org.graalvm.compiler.core.common.GraalOptions.FullUnroll;
import static org.graalvm.compiler.core.common.GraalOptions.LoopPeeling;
import static org.graalvm.compiler.core.common.GraalOptions.PartialEscapeAnalysis;
import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.referentOffset;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.EnumSet;
import java.util.ListIterator;
import java.util.Objects;
import org.graalvm.compiler.api.test.Graal;
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
import org.graalvm.compiler.hotspot.HotSpotBackend;
import org.graalvm.compiler.hotspot.HotSpotGraalRuntime.HotSpotGC;
import org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil;
import org.graalvm.compiler.nodeinfo.NodeSize;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.gc.G1PostWriteBarrier;
import org.graalvm.compiler.nodes.gc.G1PreWriteBarrier;
import org.graalvm.compiler.nodes.gc.G1ReferentFieldReadBarrier;
import org.graalvm.compiler.nodes.gc.SerialWriteBarrier;
import org.graalvm.compiler.nodes.memory.OnHeapMemoryAccess.BarrierType;
import org.graalvm.compiler.nodes.memory.ReadNode;
import org.graalvm.compiler.nodes.memory.WriteNode;
import org.graalvm.compiler.nodes.memory.address.OffsetAddressNode;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.phases.BasePhase;
import org.graalvm.compiler.phases.Phase;
import org.graalvm.compiler.phases.common.WriteBarrierAdditionPhase;
import org.graalvm.compiler.phases.tiers.MidTierContext;
import org.graalvm.compiler.phases.tiers.Suites;
import org.graalvm.compiler.runtime.RuntimeProvider;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.MetaAccessProvider;
public class WriteBarrierAdditionTest extends HotSpotGraalCompilerTest {
private static EnumSet<HotSpotGC> knownSupport = EnumSet.of(HotSpotGC.G1, HotSpotGC.CMS, HotSpotGC.Parallel, HotSpotGC.Serial);
private final GraalHotSpotVMConfig config = runtime().getVMConfig();
public static class Container {
public Container a;
public Container b;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Container container = (Container) o;
return Objects.equals(a, container.a) && Objects.equals(b, container.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
}
private int expectedBarriers;
@Test
public void testAllocation() throws Exception {
this.expectedBarriers = (config.useG1GC) ? 4 : 2;
testWithoutPEA("testAllocationSnippet");
}
public static Container testAllocationSnippet() {
Container main = new Container();
Container temp1 = new Container();
Container temp2 = new Container();
main.a = temp1;
main.b = temp2;
return main;
}
@Test
public void testLoopAllocation1() throws Exception {
this.expectedBarriers = config.useG1GC ? 8 : 4;
testWithoutPEA("test2Snippet", false);
testWithoutPEA("test2Snippet", true);
}
public static void test2Snippet(boolean test) {
Container main = new Container();
Container temp1 = new Container();
Container temp2 = new Container();
for (int i = 0; i < 10; i++) {
if (test) {
main.a = temp1;
main.b = temp2;
} else {
main.a = temp2;
main.b = temp1;
}
}
}
@Test
public void testLoopAllocation2() throws Exception {
this.expectedBarriers = config.useG1GC ? 8 : 4;
testWithoutPEA("test3Snippet");
}
public static void test3Snippet() {
Container[] main = new Container[10];
Container temp1 = new Container();
Container temp2 = new Container();
for (int i = 0; i < 10; i++) {
main[i].a = main[i].b = temp1;
}
for (int i = 0; i < 10; i++) {
main[i].a = main[i].b = temp2;
}
}
@Test
public void testReferenceGet() throws Exception {
this.expectedBarriers = config.useG1GC ? 1 : 0;
test("testReferenceGetSnippet");
}
public static Object testReferenceGetSnippet() {
return weakReference.get();
}
static class DummyReference {
Object referent;
}
private static MetaAccessProvider getStaticMetaAccess() {
return ((HotSpotBackend) Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend()).getRuntime().getHostProviders().getMetaAccess();
}
private static final WeakReference<?> weakReference = new WeakReference<>(new Object());
private static final Object weakReferenceAsObject = new WeakReference<>(new Object());
private static final long referenceReferentFieldOffset = HotSpotReplacementsUtil.getFieldOffset(getStaticMetaAccess().lookupJavaType(Reference.class), "referent");
private static final long referenceQueueFieldOffset = HotSpotReplacementsUtil.getFieldOffset(getStaticMetaAccess().lookupJavaType(Reference.class), "queue");
private static final DummyReference dummyReference = new DummyReference();
private static final long dummyReferenceReferentFieldOffset = HotSpotReplacementsUtil.getFieldOffset(getStaticMetaAccess().lookupJavaType(DummyReference.class), "referent");
@Test
public void testReferenceReferent1() throws Exception {
this.expectedBarriers = config.useG1GC ? 1 : 0;
test("testReferenceReferentSnippet");
}
public Object testReferenceReferentSnippet() {
return UNSAFE.getObject(weakReference, referenceReferentFieldOffset);
}
@Test
public void testReferenceReferent2() throws Exception {
this.expectedBarriers = config.useG1GC ? 1 : 0;
test("testReferenceReferent2Snippet", referenceReferentFieldOffset);
}
public Object testReferenceReferent2Snippet(long offset) {
return UNSAFE.getObject(weakReference, offset);
}
@Test
public void testReferenceReferent3() throws Exception {
this.expectedBarriers = 0;
test("testReferenceReferent3Snippet");
}
public Object testReferenceReferent3Snippet() {
return UNSAFE.getObject(weakReference, referenceQueueFieldOffset);
}
@Test
public void testReferenceReferent4() throws Exception {
this.expectedBarriers = config.useG1GC ? 1 : 0;
test("testReferenceReferent4Snippet");
}
public Object testReferenceReferent4Snippet() {
return UNSAFE.getObject(weakReferenceAsObject, referenceReferentFieldOffset);
}
@Test
public void testReferenceReferent5() throws Exception {
this.expectedBarriers = 0;
Assert.assertEquals("expected fields to have the same offset", referenceReferentFieldOffset, dummyReferenceReferentFieldOffset);
test("testReferenceReferent5Snippet");
}
public Object testReferenceReferent5Snippet() {
return UNSAFE.getObject(dummyReference, referenceReferentFieldOffset);
}
static Object[] src = new Object[1];
static Object[] dst = new Object[1];
static {
for (int i = 0; i < src.length; i++) {
src[i] = new Object();
}
for (int i = 0; i < dst.length; i++) {
dst[i] = new Object();
}
}
public static void testArrayCopySnippet(Object a, Object b, Object c) throws Exception {
System.arraycopy(a, 0, b, 0, (int) c);
}
@Test
public void testArrayCopy() throws Exception {
this.expectedBarriers = 0;
test("testArrayCopySnippet", src, dst, dst.length);
}
private void verifyBarriers(StructuredGraph graph) {
Assert.assertTrue("Unknown collector selected", knownSupport.contains(runtime().getGarbageCollector()));
Assert.assertNotEquals("test must set expected barrier count", expectedBarriers, -1);
int barriers = 0;
if (config.useG1GC) {
barriers = graph.getNodes().filter(G1ReferentFieldReadBarrier.class).count() + graph.getNodes().filter(G1PreWriteBarrier.class).count() +
graph.getNodes().filter(G1PostWriteBarrier.class).count();
} else {
barriers = graph.getNodes().filter(SerialWriteBarrier.class).count();
}
if (expectedBarriers != barriers) {
Assert.assertEquals(expectedBarriers, barriers);
}
for (WriteNode write : graph.getNodes().filter(WriteNode.class)) {
if (config.useG1GC) {
if (write.getBarrierType() != BarrierType.NONE) {
Assert.assertEquals(1, write.successors().count());
Assert.assertTrue(write.next() instanceof G1PostWriteBarrier);
Assert.assertTrue(write.predecessor() instanceof G1PreWriteBarrier || write.getLocationIdentity().isImmutable());
}
} else {
if (write.getBarrierType() != BarrierType.NONE) {
Assert.assertEquals(1, write.successors().count());
Assert.assertTrue(write.next() instanceof SerialWriteBarrier);
}
}
}
for (ReadNode read : graph.getNodes().filter(ReadNode.class)) {
if (read.getBarrierType() != BarrierType.NONE) {
if (read.getAddress() instanceof OffsetAddressNode) {
JavaConstant constDisp = ((OffsetAddressNode) read.getAddress()).getOffset().asJavaConstant();
if (constDisp != null) {
Assert.assertEquals(referentOffset(getMetaAccess()), constDisp.asLong());
}
}
Assert.assertTrue(BarrierType.WEAK_FIELD == read.getBarrierType() || BarrierType.MAYBE_WEAK_FIELD == read.getBarrierType());
if (config.useG1GC) {
Assert.assertTrue(read.next() instanceof G1ReferentFieldReadBarrier);
}
}
}
}
protected Result testWithoutPEA(String name, Object... args) {
return test(new OptionValues(getInitialOptions(), PartialEscapeAnalysis, false, FullUnroll, false, LoopPeeling, false), name, args);
}
@Before
public void before() {
expectedBarriers = -1;
}
@Override
protected Suites createSuites(OptionValues opts) {
Suites ret = getBackend().getSuites().getDefaultSuites(opts).copy();
ListIterator<BasePhase<? super MidTierContext>> iter = ret.getMidTier().findPhase(WriteBarrierAdditionPhase.class, true);
iter.add(new Phase() {
@Override
protected void run(StructuredGraph graph) {
verifyBarriers(graph);
}
@Override
public float codeSizeIncrease() {
return NodeSize.IGNORE_SIZE_CONTRACT_FACTOR;
}
@Override
protected CharSequence getName() {
return "VerifyBarriersPhase";
}
});
return ret;
}
}