Skip to content
Snippets Groups Projects
Commit 620d8955 authored by Mick Jordan's avatar Mick Jordan
Browse files

Merge pull request #54 in G/fastr from...

Merge pull request #54 in G/fastr from ~MICK.JORDAN_ORACLE.COM/fastr:feature/fastr-builtin to master

* commit 'a00233f8':
  Remove type assertion in RASTBuilder.constant
  Add missing file from previous commit
  Add tests for trace/.fastr.trace
  Fix crash when boolean FastROption set as String
  addendum to .fastr.compile removal
  Remove .fastr.compile
parents 64d00ce1 a00233f8
No related branches found
No related tags found
No related merge requests found
......@@ -45,8 +45,6 @@ import com.oracle.truffle.r.nodes.builtin.base.foreign.ForeignFunctions;
import com.oracle.truffle.r.nodes.builtin.base.foreign.ForeignFunctionsFactory;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRCallCounting;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRCallCountingFactory;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRCompile;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRCompileNodeGen;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRContext;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRContextFactory;
import com.oracle.truffle.r.nodes.builtin.fastr.FastRDebug;
......@@ -277,7 +275,6 @@ public class BasePackage extends RBuiltinPackage {
add(Expression.class, ExpressionNodeGen::create);
add(FastRCallCounting.CreateCallCounter.class, FastRCallCountingFactory.CreateCallCounterNodeGen::create);
add(FastRCallCounting.GetCallCounter.class, FastRCallCountingFactory.GetCallCounterNodeGen::create);
add(FastRCompile.class, FastRCompileNodeGen::create);
add(FastRContext.CloseChannel.class, FastRContextFactory.CloseChannelNodeGen::create);
add(FastRContext.Create.class, FastRContextFactory.CreateNodeGen::create);
add(FastRContext.CreateChannel.class, FastRContextFactory.CreateChannelNodeGen::create);
......
/*
* Copyright (c) 2013, 2016, 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.truffle.r.nodes.builtin.fastr;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.Future;
import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.r.nodes.builtin.RBuiltinNode;
import com.oracle.truffle.r.runtime.RBuiltin;
import com.oracle.truffle.r.runtime.RBuiltinKind;
import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.RRuntime;
import com.oracle.truffle.r.runtime.Utils;
import com.oracle.truffle.r.runtime.data.RFunction;
import com.oracle.truffle.r.runtime.data.RMissing;
@RBuiltin(name = ".fastr.compile", kind = RBuiltinKind.PRIMITIVE, parameterNames = {"func", "background"})
public abstract class FastRCompile extends RBuiltinNode {
private static final class Compiler {
private final Class<?> optimizedCallTarget;
private final Class<?> graalTruffleRuntime;
private final Method submitForCompilationMethod;
private final Method finishCompilationMethod;
private Compiler() {
try {
optimizedCallTarget = Class.forName("com.oracle.graal.truffle.OptimizedCallTarget", false, Truffle.getRuntime().getClass().getClassLoader());
graalTruffleRuntime = Class.forName("com.oracle.graal.truffle.GraalTruffleRuntime", false, Truffle.getRuntime().getClass().getClassLoader());
submitForCompilationMethod = graalTruffleRuntime.getDeclaredMethod("submitForCompilation", optimizedCallTarget);
finishCompilationMethod = graalTruffleRuntime.getDeclaredMethod("finishCompilation", optimizedCallTarget, Future.class, boolean.class);
} catch (ClassNotFoundException | IllegalArgumentException | NoSuchMethodException | SecurityException e) {
throw Utils.fail("fastr.compile: failed to find 'compile' method");
}
}
static Compiler getCompiler() {
if (System.getProperty("fastr.truffle.compile", "true").equals("true") && Truffle.getRuntime().getName().contains("Graal")) {
return new Compiler();
} else {
return null;
}
}
boolean compile(CallTarget callTarget, boolean background) throws InvocationTargetException, IllegalAccessException {
if (optimizedCallTarget.isInstance(callTarget)) {
Future<?> submitted = (Future<?>) submitForCompilationMethod.invoke(Truffle.getRuntime(), callTarget);
finishCompilationMethod.invoke(Truffle.getRuntime(), callTarget, submitted, background);
return true;
} else {
return false;
}
}
}
private static final Compiler compiler = Compiler.getCompiler();
@Override
public Object[] getDefaultParameterValues() {
return new Object[]{RMissing.instance, RRuntime.LOGICAL_FALSE};
}
@Specialization
protected byte compileFunction(RFunction function, byte background) {
controlVisibility();
if (compiler != null) {
try {
if (compiler.compile(function.getTarget(), background == RRuntime.LOGICAL_TRUE)) {
return RRuntime.LOGICAL_TRUE;
}
} catch (InvocationTargetException | IllegalAccessException e) {
throw RError.error(this, RError.Message.GENERIC, e.toString());
}
} else {
throw RError.error(this, RError.Message.GENERIC, "fastr.compile not supported in this environment");
}
return RRuntime.LOGICAL_FALSE;
}
@SuppressWarnings("unused")
@Fallback
protected Object fallback(Object a1, Object a2) {
throw RError.error(this, RError.Message.INVALID_OR_UNIMPLEMENTED_ARGUMENTS);
}
}
......@@ -170,7 +170,7 @@ public class TraceHandling {
if (!disabled()) {
int depth = RArguments.getDepth(frame);
try {
for (int i = 0; i < depth; i++) {
for (int i = 0; i < depth - 1; i++) {
outputHandler.writeString(" ", false);
}
String callString = getCallSource(frame);
......
......@@ -437,8 +437,6 @@ public final class RASTBuilder implements RCodeBuilder<RSyntaxNode> {
@Override
public RSyntaxNode constant(SourceSection source, Object value) {
assert value instanceof Byte || value instanceof Integer || value instanceof Double || value instanceof RComplex || value instanceof String || value instanceof RNull ||
value instanceof REmpty || value instanceof RSymbol || value instanceof RAbstractVector || value instanceof RFunction : value.getClass();
if (value instanceof String && !RRuntime.isNA((String) value)) {
return ConstantNode.create(source, ((String) value).intern());
} else {
......
......@@ -80,12 +80,25 @@ public enum FastROptions {
public boolean getBooleanValue() {
assert isBoolean;
return (Boolean) value;
if (value instanceof Boolean) {
return (Boolean) value;
} else {
System.out.println("boolean option value expected with " + name() + " - forgot +/- ?");
System.exit(2);
return false;
}
}
public String getStringValue() {
assert !isBoolean;
return (String) value;
if (value == null || value instanceof String) {
return (String) value;
} else {
System.out.println("string option value expected with " + name());
System.exit(2);
return "";
}
}
private static FastROptions[] VALUES = values();
......
......@@ -106354,6 +106354,110 @@ debug at #4: t
exiting from: f(5)
[1] 6
 
##com.oracle.truffle.r.test.library.utils.TestTrace.testCondTrace
#f <- function(x) {}; (if (exists('.fastr.trace')) .fastr.trace else trace)(f, tracer=quote(if (x == 3 || x == 7) print(x))); g <- function() for (i in 1:10) f(i); g()
[1] "f"
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
[1] 3
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
[1] 7
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
##com.oracle.truffle.r.test.library.utils.TestTrace.testCondTrace
#f <- function(x) {}; trace(f, tracer=quote(if (x == 3 || x == 7) print(x))); g <- function() for (i in 1:10) f(i); g()
[1] "f"
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
[1] 3
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
[1] 7
Tracing f(i) on entry
Tracing f(i) on entry
Tracing f(i) on entry
##com.oracle.truffle.r.test.library.utils.TestTrace.testMultiTrace
#f <- function(x) {}; (if (exists('.fastr.trace')) .fastr.trace else trace)(f, tracer=quote(print(x))); g <- function() for (i in 1:10) f(i); g()
[1] "f"
Tracing f(i) on entry
[1] 1
Tracing f(i) on entry
[1] 2
Tracing f(i) on entry
[1] 3
Tracing f(i) on entry
[1] 4
Tracing f(i) on entry
[1] 5
Tracing f(i) on entry
[1] 6
Tracing f(i) on entry
[1] 7
Tracing f(i) on entry
[1] 8
Tracing f(i) on entry
[1] 9
Tracing f(i) on entry
[1] 10
##com.oracle.truffle.r.test.library.utils.TestTrace.testMultiTrace
#f <- function(x) {}; trace(f, tracer=quote(print(x))); g <- function() for (i in 1:10) f(i); g()
[1] "f"
Tracing f(i) on entry
[1] 1
Tracing f(i) on entry
[1] 2
Tracing f(i) on entry
[1] 3
Tracing f(i) on entry
[1] 4
Tracing f(i) on entry
[1] 5
Tracing f(i) on entry
[1] 6
Tracing f(i) on entry
[1] 7
Tracing f(i) on entry
[1] 8
Tracing f(i) on entry
[1] 9
Tracing f(i) on entry
[1] 10
##com.oracle.truffle.r.test.library.utils.TestTrace.testSimple
#f <- function(x) {}; (if (exists('.fastr.trace')) .fastr.trace else trace)(f); f()
trace: f()
NULL
##com.oracle.truffle.r.test.library.utils.TestTrace.testSimple
#f <- function(x) {}; trace(f); f()
trace: f()
NULL
##com.oracle.truffle.r.test.library.utils.TestTrace.testSimpleTrace
#f <- function(x) {}; (if (exists('.fastr.trace')) .fastr.trace else trace)(f, tracer=quote(print(x))); f(100)
[1] "f"
Tracing f(100) on entry
[1] 100
NULL
##com.oracle.truffle.r.test.library.utils.TestTrace.testSimpleTrace
#f <- function(x) {}; trace(f, tracer=quote(print(x))); f(100)
[1] "f"
Tracing f(100) on entry
[1] 100
NULL
##com.oracle.truffle.r.test.library.utils.TestUtils.testHeadNTail
#{head(letters)}
[1] "a" "b" "c" "d" "e" "f"
/*
* Copyright (c) 2016, 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.truffle.r.test.library.utils;
import org.junit.Test;
import com.oracle.truffle.r.test.TestBase;
public class TestTrace extends TestBase {
private static final String FASTR_TRACE = "(if (exists('.fastr.trace')) .fastr.trace else trace)";
private static final String PLAIN_TRACE = "trace";
private static final String[] TRACE_VARIANTS = new String[]{PLAIN_TRACE, FASTR_TRACE};
@Test
public void testSimple() {
assertEval(template("f <- function(x) {}; %0(f); f()", TRACE_VARIANTS));
}
@Test
public void testSimpleTrace() {
assertEval(template("f <- function(x) {}; %0(f, tracer=quote(print(x))); f(100)", TRACE_VARIANTS));
}
@Test
public void testMultiTrace() {
assertEval(template("f <- function(x) {}; %0(f, tracer=quote(print(x))); g <- function() for (i in 1:10) f(i); g()", TRACE_VARIANTS));
}
@Test
public void testCondTrace() {
assertEval(template("f <- function(x) {}; %0(f, tracer=quote(if (x == 3 || x == 7) print(x))); g <- function() for (i in 1:10) f(i); g()", TRACE_VARIANTS));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment