Skip to content

Commit

Permalink
Making sure node using CyclicAssumption can compile down to constant
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaroslav Tulach committed Apr 28, 2017
1 parent 4bdb2dd commit ea21347
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.compiler.truffle.test;

import org.junit.Assert;
import org.junit.Test;

import org.graalvm.compiler.truffle.OptimizedCallTarget;
import org.graalvm.compiler.truffle.test.nodes.RootTestNode;
import com.oracle.truffle.api.frame.FrameDescriptor;
import com.oracle.truffle.api.utilities.CyclicAssumption;
import org.graalvm.compiler.truffle.test.nodes.ConstantWithCyclicAssumptionTestNode;

public class CyclicAssumptionPartialEvaluationTest extends PartialEvaluationTest {
public static Object constant42() {
return 42;
}

@Test
public void constantValue() {
CyclicAssumption assumption = new CyclicAssumption("cyclic");
ConstantWithCyclicAssumptionTestNode assumingNode = new ConstantWithCyclicAssumptionTestNode(assumption, 42);
RootTestNode rootNode = new RootTestNode(new FrameDescriptor(), "constantValue", assumingNode);
OptimizedCallTarget callTarget = assertPartialEvalEquals("constant42", rootNode);
Assert.assertTrue(callTarget.isValid());
assertDeepEquals(42, callTarget.call());
Assert.assertTrue(callTarget.isValid());
assumingNode.invalidate();
Assert.assertTrue("cyclicly valid again", assumption.getAssumption().isValid());
Assert.assertFalse(callTarget.isValid());
assertDeepEquals(43, callTarget.call());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.graalvm.compiler.truffle.test.nodes;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.utilities.CyclicAssumption;

@NodeInfo
public class ConstantWithCyclicAssumptionTestNode extends AbstractTestNode {
@CompilerDirectives.CompilationFinal private int value;
private final CyclicAssumption assumption;

public ConstantWithCyclicAssumptionTestNode(CyclicAssumption assumption, int value) {
this.value = value;
this.assumption = assumption;
}

@Override
public int execute(VirtualFrame frame) {
try {
assumption.getAssumption().check();
return value;
} catch (InvalidAssumptionException e) {
return invalidate();
}
}

public int invalidate() {
int v = ++value;
assumption.invalidate();
return v;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

import com.oracle.truffle.api.Assumption;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;

Expand All @@ -42,7 +43,7 @@
public class CyclicAssumption {

private final String name;
private volatile Assumption assumption;
@CompilerDirectives.CompilationFinal private volatile Assumption assumption;

private static final AtomicReferenceFieldUpdater<CyclicAssumption, Assumption> ASSUMPTION_UPDATER = AtomicReferenceFieldUpdater.newUpdater(CyclicAssumption.class, Assumption.class, "assumption");

Expand Down

0 comments on commit ea21347

Please sign in to comment.