Mercurial > hg > truffle
changeset 17444:2834b4432586
Truffle: introduce CompilerDirectives.isCompilationConstant.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 14 Oct 2014 20:02:44 +0200 |
parents | 90c6a996f9cd |
children | 8cc283ce4bd1 |
files | graal/com.oracle.graal.truffle.test/sl/TestIsCompilationConstant1.sl graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/builtins/SLIsCompilationConstantBuiltin.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/IsCompilationConstantNode.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/CompilerDirectivesSubstitutions.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java |
diffstat | 5 files changed, 174 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle.test/sl/TestIsCompilationConstant1.sl Tue Oct 14 20:02:44 2014 +0200 @@ -0,0 +1,40 @@ + + +function testConstantValue1() { + return isCompilationConstant(42); +} + +function testConstantValue2() { + return isCompilationConstant(21 + 21); +} + +function testConstantSequence() { + 40; + return isCompilationConstant(42); +} + +function testConstantLocalVariable() { + x = 42; + return isCompilationConstant(x); +} + +function testNonConstantAdd() { + return isCompilationConstant(42 + "42"); +} + + +function main() { + callFunctionsWith("testConstant", harnessTrue); + callFunctionsWith("testNonConstant", harnessFalse); +} + +function harnessTrue(testFunction) { + callUntilOptimized(testFunction); + assertTrue(testFunction(), "test function " + testFunction + " is not constant"); +} + + +function harnessFalse(testFunction) { + callUntilOptimized(testFunction); + assertFalse(testFunction(), "test function " + testFunction + " is constant"); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle.test/src/com/oracle/graal/truffle/test/builtins/SLIsCompilationConstantBuiltin.java Tue Oct 14 20:02:44 2014 +0200 @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2014, 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 com.oracle.graal.truffle.test.builtins; + +import com.oracle.truffle.api.*; +import com.oracle.truffle.api.dsl.*; +import com.oracle.truffle.api.nodes.*; + +/** + * Checks whether or not a function is optimized by the Graal runtime. + */ +@NodeInfo(shortName = "isCompilationConstant") +public abstract class SLIsCompilationConstantBuiltin extends SLGraalRuntimeBuiltin { + + @Specialization + public boolean isCompilationConstant(Object value) { + if (CompilerDirectives.inCompiledCode()) { + return CompilerDirectives.isCompilationConstant(value); + } else { + return true; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/IsCompilationConstantNode.java Tue Oct 14 20:02:44 2014 +0200 @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2014, 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 com.oracle.graal.truffle.nodes; + +import com.oracle.graal.graph.*; +import com.oracle.graal.graph.spi.*; +import com.oracle.graal.nodeinfo.*; +import com.oracle.graal.nodes.*; +import com.oracle.graal.nodes.extended.*; +import com.oracle.graal.nodes.spi.*; +import com.oracle.graal.replacements.nodes.*; + +@NodeInfo +public class IsCompilationConstantNode extends MacroStateSplitNode implements Canonicalizable { + + public static IsCompilationConstantNode create(Invoke invoke) { + return USE_GENERATED_NODES ? new IsCompilationConstantNodeGen(invoke) : new IsCompilationConstantNode(invoke); + } + + protected IsCompilationConstantNode(Invoke invoke) { + super(invoke); + assert arguments.size() == 1; + } + + @Override + public void lower(LoweringTool tool) { + /* Invoke will return false. */ + replaceWithInvoke().lower(tool); + } + + @Override + public Node canonical(CanonicalizerTool tool) { + ValueNode arg0 = arguments.get(0); + if (arg0 instanceof BoxNode) { + arg0 = ((BoxNode) arg0).getValue(); + } + if (arg0.isConstant()) { + return ConstantNode.forBoolean(true); + } + return this; + } +}
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/CompilerDirectivesSubstitutions.java Tue Oct 14 20:02:44 2014 +0200 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/CompilerDirectivesSubstitutions.java Tue Oct 14 20:02:44 2014 +0200 @@ -84,6 +84,9 @@ return FrameWithoutBoxing.class; } + @MacroSubstitution(macro = IsCompilationConstantNode.class, isStatic = true) + public static native boolean isCompilationConstant(Object value); + @MacroSubstitution(macro = CustomizedUnsafeLoadMacroNode.class, isStatic = true) public static native boolean unsafeGetBoolean(Object receiver, long offset, boolean condition, Object locationIdentity);
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java Tue Oct 14 20:02:44 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java Tue Oct 14 20:02:44 2014 +0200 @@ -99,6 +99,32 @@ } /** + * Returns a boolean indicating whether or not a given value is seen as constant in optimized + * code. If this method is called in the interpreter this method will always return + * <code>false</code>. This API may be used in combination with {@link #inCompiledCode()} to + * implement compilation constant assertions in the following way: + * + * <pre> + * <code> + * void assertCompilationConstant(Object value) { + * if (inCompiledCode()) { + * if (!isCompilationConstant(value)) { + * throw new AssertionError("Given value is not constant"); + * } + * } + * } + * </code> + * </pre> + * + * @param value + * @return {@code true} when given value is seen as compilation constant, {@code false} if not + * compilation constant. + */ + public static boolean isCompilationConstant(Object value) { + return false; + } + + /** * Directive for the compiler that the given runnable should only be executed in the interpreter * and ignored in the compiled code. *