From dbf9e8d690d4dfab304f5776717a95858610953c Mon Sep 17 00:00:00 2001 From: Lukas Stadler <lukas.stadler@oracle.com> Date: Thu, 23 Nov 2017 12:10:17 +0100 Subject: [PATCH] small cleanups --- .../r/nodes/builtin/base/Expression.java | 1 + .../truffle/r/nodes/builtin/base/Round.java | 43 +++------- .../oracle/truffle/r/nodes/RASTBuilder.java | 13 ++- .../builtin/casts/PipelineToCastNode.java | 85 ++++++++++--------- .../r/nodes/function/PromiseHelperNode.java | 6 +- .../r/runtime/conn/NativeConnections.java | 2 +- .../truffle/r/runtime/env/REnvironment.java | 1 + 7 files changed, 69 insertions(+), 82 deletions(-) diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Expression.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Expression.java index aa20ab830c..c58f302013 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Expression.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Expression.java @@ -70,6 +70,7 @@ public abstract class Expression extends RBuiltinNode.Arg1 { } @Specialization + @TruffleBoundary protected Object doExpression(RPromise language) { return RDataFactory.createExpression(new Object[]{convert(language)}); } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Round.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Round.java index 407ce9faac..a3450d32ad 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Round.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Round.java @@ -31,6 +31,7 @@ import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; import com.oracle.truffle.api.CompilerDirectives; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.nodes.Node; +import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RRuntime; @@ -52,6 +53,8 @@ public abstract class Round extends RBuiltinNode.Arg2 { @Child private RoundArithmetic roundOp = new RoundArithmetic(); + private final ConditionProfile zeroDigitProfile = ConditionProfile.createBinaryProfile(); + private final NACheck check = NACheck.create(); @Override @@ -83,6 +86,13 @@ public abstract class Round extends RBuiltinNode.Arg2 { return check.check(x) ? RRuntime.DOUBLE_NA : x; } + @Specialization + protected double roundDigits(double x, double digits) { + check.enable(x); + int digitsInt = (int) Math.round(digits); + return check.check(x) ? RRuntime.DOUBLE_NA : zeroDigitProfile.profile(digitsInt == 0) ? roundOp.op(x) : roundOp.opd(x, digitsInt); + } + @Specialization protected RDoubleVector round(RAbstractLogicalVector x, @SuppressWarnings("unused") double digits) { double[] data = new double[x.getLength()]; @@ -105,43 +115,18 @@ public abstract class Round extends RBuiltinNode.Arg2 { return RDataFactory.createDoubleVector(data, check.neverSeenNA()); } - @Specialization(guards = "isZero(digits)") - protected double round(double x, @SuppressWarnings("unused") double digits) { - check.enable(x); - return check.check(x) ? RRuntime.DOUBLE_NA : roundOp.op(x); - } - protected double roundDigits(double x, int digits) { - check.enable(x); - return check.check(x) ? RRuntime.DOUBLE_NA : roundOp.opd(x, digits); - } - - @Specialization(guards = "!isZero(digits)") - protected double roundDigits(double x, double digits) { - return roundDigits(x, (int) Math.round(digits)); + return check.check(x) ? RRuntime.DOUBLE_NA : zeroDigitProfile.profile(digits == 0) ? roundOp.op(x) : roundOp.opd(x, digits); } - @Specialization(guards = "isZero(digits)") + @Specialization protected RDoubleVector round(RAbstractDoubleVector x, double digits) { double[] result = new double[x.getLength()]; check.enable(x); + int digitsInt = (int) Math.round(digits); for (int i = 0; i < x.getLength(); i++) { double value = x.getDataAt(i); - result[i] = check.check(value) ? RRuntime.DOUBLE_NA : round(value, digits); - } - RDoubleVector ret = RDataFactory.createDoubleVector(result, check.neverSeenNA()); - ret.copyAttributesFrom(x); - return ret; - } - - @Specialization(guards = "!isZero(dDigits)") - protected RDoubleVector roundDigits(RAbstractDoubleVector x, double dDigits) { - double[] result = new double[x.getLength()]; - int digits = (int) Math.round(dDigits); - check.enable(x); - for (int i = 0; i < x.getLength(); i++) { - double value = x.getDataAt(i); - result[i] = check.check(value) ? RRuntime.DOUBLE_NA : roundDigits(value, digits); + result[i] = check.check(value) ? RRuntime.DOUBLE_NA : zeroDigitProfile.profile(digitsInt == 0) ? roundOp.op(value) : roundOp.opd(value, digitsInt); } RDoubleVector ret = RDataFactory.createDoubleVector(result, check.neverSeenNA()); ret.copyAttributesFrom(x); diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java index 1b211974b3..9f6f6652e1 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/RASTBuilder.java @@ -258,15 +258,14 @@ public final class RASTBuilder implements RCodeBuilder<RSyntaxNode> { public RSyntaxNode constant(SourceSection source, Object value) { if (value instanceof String && !RRuntime.isNA((String) value)) { return ConstantNode.create(source, Utils.intern((String) value)); - } else { - if (value instanceof RShareable) { - RShareable shareable = (RShareable) value; - if (!shareable.isSharedPermanent()) { - return ConstantNode.create(source, shareable.makeSharedPermanent()); - } + } + if (value instanceof RShareable) { + RShareable shareable = (RShareable) value; + if (!shareable.isSharedPermanent()) { + return ConstantNode.create(source, shareable.makeSharedPermanent()); } - return ConstantNode.create(source, value); } + return ConstantNode.create(source, value); } @Override diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/casts/PipelineToCastNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/casts/PipelineToCastNode.java index e0c252dfd5..037b7b409d 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/casts/PipelineToCastNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/builtin/casts/PipelineToCastNode.java @@ -299,48 +299,49 @@ public final class PipelineToCastNode { @Override public ArgumentFilter<?, ?> visit(RTypeFilter<?> filter, ArgumentFilter<?, ?> previous) { - if (filter.getType() == RType.Integer) { - return new ArgumentFilter<Object, Object>() { - private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); - - @Override - public boolean test(Object x) { - return profile.profile(x instanceof Integer) || x instanceof RAbstractIntVector; - } - }; - } else if (filter.getType() == RType.Double) { - return new ArgumentFilter<Object, Object>() { - private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); - - @Override - public boolean test(Object x) { - return profile.profile(x instanceof Double) || x instanceof RAbstractDoubleVector; - } - }; - } else if (filter.getType() == RType.Logical) { - return new ArgumentFilter<Object, Object>() { - private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); - - @Override - public boolean test(Object x) { - return profile.profile(x instanceof Byte) || x instanceof RAbstractLogicalVector; - } - }; - } else if (filter.getType() == RType.Complex) { - return x -> x instanceof RAbstractComplexVector; - } else if (filter.getType() == RType.Character) { - return new ArgumentFilter<Object, Object>() { - private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); - - @Override - public boolean test(Object x) { - return profile.profile(x instanceof String) || x instanceof RAbstractStringVector; - } - }; - } else if (filter.getType() == RType.Raw) { - return x -> x instanceof RAbstractRawVector; - } else { - throw RInternalError.unimplemented("TODO: more types here"); + switch (filter.getType()) { + case Integer: + return new ArgumentFilter<Object, Object>() { + private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); + + @Override + public boolean test(Object x) { + return profile.profile(x instanceof Integer) || x instanceof RAbstractIntVector; + } + }; + case Double: + return new ArgumentFilter<Object, Object>() { + private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); + + @Override + public boolean test(Object x) { + return profile.profile(x instanceof Double) || x instanceof RAbstractDoubleVector; + } + }; + case Logical: + return new ArgumentFilter<Object, Object>() { + private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); + + @Override + public boolean test(Object x) { + return profile.profile(x instanceof Byte) || x instanceof RAbstractLogicalVector; + } + }; + case Complex: + return x -> x instanceof RAbstractComplexVector; + case Character: + return new ArgumentFilter<Object, Object>() { + private final ConditionProfile profile = ConditionProfile.createBinaryProfile(); + + @Override + public boolean test(Object x) { + return profile.profile(x instanceof String) || x instanceof RAbstractStringVector; + } + }; + case Raw: + return x -> x instanceof RAbstractRawVector; + default: + throw RInternalError.unimplemented("type " + filter.getType()); } } diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/function/PromiseHelperNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/function/PromiseHelperNode.java index 74d41d8ec6..2438587909 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/function/PromiseHelperNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/function/PromiseHelperNode.java @@ -58,9 +58,9 @@ import com.oracle.truffle.r.runtime.nodes.RBaseNode; * Holds {@link RPromise}-related functionality that cannot be implemented in * "com.oracle.truffle.r.runtime.data" due to package import restrictions. */ -public class PromiseHelperNode extends RBaseNode { +public final class PromiseHelperNode extends RBaseNode { - public static class PromiseCheckHelperNode extends RBaseNode { + public static final class PromiseCheckHelperNode extends RBaseNode { @Child private PromiseHelperNode promiseHelper; @@ -81,7 +81,7 @@ public class PromiseHelperNode extends RBaseNode { } } - public static class PromiseDeoptimizeFrameNode extends RBaseNode { + public static final class PromiseDeoptimizeFrameNode extends RBaseNode { private final BranchProfile deoptimizeProfile = BranchProfile.create(); /** diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/NativeConnections.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/NativeConnections.java index 4f3a36f520..7d18153e4a 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/NativeConnections.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/conn/NativeConnections.java @@ -302,7 +302,7 @@ public class NativeConnections { // converted to a pointer using RAW macro. This turns the raw vector into a native memory // backed vector and any consecutive (write) operations in the native code are actually not // done on the original vector that backs the byte buffer, so we need to copy back the date - // to the byte buffer. It would be more efficitent to use direct byte buffer, but then we'd + // to the byte buffer. It would be more efficient to use direct byte buffer, but then we'd // need to make the native call interface (CallRFFI.InvokeCallRootNode) more flexible so // that it can accept other argument types than SEXPs. diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/env/REnvironment.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/env/REnvironment.java index 014af717bc..ecfce5c159 100644 --- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/env/REnvironment.java +++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/env/REnvironment.java @@ -1147,6 +1147,7 @@ public abstract class REnvironment extends RAttributeStorage { } @Override + @TruffleBoundary public void put(String key, Object value) throws PutException { throw new PutException(RError.Message.ENV_ASSIGN_EMPTY); } -- GitLab