diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsCharacter.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsCharacter.java index 1ed97d305414adfdabe561c1bbc23af56ab21266..7c29e7beb057ffa40802f9bf1c38f497b40fc028 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsCharacter.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsCharacter.java @@ -28,6 +28,7 @@ import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE; import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.CastBuilder; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; import com.oracle.truffle.r.runtime.RDeparse; @@ -42,6 +43,8 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractStringVector; @RBuiltin(name = "as.character", kind = PRIMITIVE, parameterNames = {"x", "..."}, dispatch = INTERNAL_GENERIC, behavior = PURE) public abstract class AsCharacter extends RBuiltinNode { + private final ConditionProfile noAttributes = ConditionProfile.createBinaryProfile(); + @Override protected void createCasts(CastBuilder casts) { casts.arg("x").mapIf(instanceOf(RAbstractListVector.class).not(), asStringVector()); @@ -54,7 +57,11 @@ public abstract class AsCharacter extends RBuiltinNode { @Specialization protected RAbstractStringVector asCharacter(RAbstractStringVector v) { - return v; + if (noAttributes.profile(v.getAttributes() == null)) { + return v; + } else { + return (RAbstractStringVector) v.copyDropAttributes(); + } } @Specialization diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsComplex.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsComplex.java index 337f93436cf36ec1d7281bb6400722e79adbabaa..964c8bceec1b1198a9d4b2d4a4cc554e722f92a3 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsComplex.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsComplex.java @@ -26,6 +26,7 @@ import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE; import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.CastBuilder; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; import com.oracle.truffle.r.runtime.builtins.RBuiltin; @@ -36,6 +37,8 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractComplexVector; @RBuiltin(name = "as.complex", kind = PRIMITIVE, parameterNames = {"x", "..."}, behavior = PURE) public abstract class AsComplex extends RBuiltinNode { + private final ConditionProfile noAttributes = ConditionProfile.createBinaryProfile(); + @Override protected void createCasts(CastBuilder casts) { casts.arg("x").asComplexVector(); @@ -48,7 +51,11 @@ public abstract class AsComplex extends RBuiltinNode { @Specialization protected RAbstractComplexVector asComplex(RAbstractComplexVector v) { - return v; + if (noAttributes.profile(v.getAttributes() == null)) { + return v; + } else { + return (RAbstractComplexVector) v.copyDropAttributes(); + } } } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsDouble.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsDouble.java index 721b3befc7a6c6c29860a9417132226d921eea82..39780bb2a6ad5b9880a96d7d39422154be6673fe 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsDouble.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsDouble.java @@ -26,6 +26,7 @@ import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE; import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.CastBuilder; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; import com.oracle.truffle.r.runtime.builtins.RBuiltin; @@ -36,6 +37,8 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractDoubleVector; @RBuiltin(name = "as.double", aliases = {"as.numeric"}, kind = PRIMITIVE, parameterNames = {"x", "..."}, behavior = PURE) public abstract class AsDouble extends RBuiltinNode { + private final ConditionProfile noAttributes = ConditionProfile.createBinaryProfile(); + @Override protected void createCasts(CastBuilder casts) { casts.arg("x").asDoubleVector(); @@ -48,6 +51,10 @@ public abstract class AsDouble extends RBuiltinNode { @Specialization protected RAbstractDoubleVector asDouble(RAbstractDoubleVector v) { - return v; + if (noAttributes.profile(v.getAttributes() == null)) { + return v; + } else { + return (RAbstractDoubleVector) v.copyDropAttributes(); + } } } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsInteger.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsInteger.java index 68b39d3ab763c16711b97a9da3f3fab6c7457a68..c6a78959fa4142db09ac12146080ef1d05d2f176 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsInteger.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsInteger.java @@ -26,6 +26,7 @@ import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE; import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.CastBuilder; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; import com.oracle.truffle.r.runtime.builtins.RBuiltin; @@ -37,6 +38,8 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractIntVector; @RBuiltin(name = "as.integer", kind = PRIMITIVE, parameterNames = {"x", "..."}, behavior = PURE) public abstract class AsInteger extends RBuiltinNode { + private final ConditionProfile noAttributes = ConditionProfile.createBinaryProfile(); + @Override protected void createCasts(CastBuilder casts) { casts.arg("x").asIntegerVector(); @@ -49,7 +52,11 @@ public abstract class AsInteger extends RBuiltinNode { @Specialization protected RAbstractIntVector asInteger(RAbstractIntVector v) { - return v; + if (noAttributes.profile(v.getAttributes() == null)) { + return v; + } else { + return (RAbstractIntVector) v.copyDropAttributes(); + } } @Specialization diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsLogical.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsLogical.java index c3300b39f40d2675ea184924628f42ce68134e27..d92ed1e1a988451b5bbc6501fb6d855bcb10d436 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsLogical.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsLogical.java @@ -26,6 +26,7 @@ import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE; import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.CastBuilder; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; import com.oracle.truffle.r.runtime.builtins.RBuiltin; @@ -36,6 +37,8 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractLogicalVector; @RBuiltin(name = "as.logical", kind = PRIMITIVE, parameterNames = {"x", "..."}, behavior = PURE) public abstract class AsLogical extends RBuiltinNode { + private final ConditionProfile noAttributes = ConditionProfile.createBinaryProfile(); + @Override protected void createCasts(CastBuilder casts) { casts.arg("x").asLogicalVector(); @@ -48,6 +51,10 @@ public abstract class AsLogical extends RBuiltinNode { @Specialization protected RAbstractLogicalVector asLogicaleger(RAbstractLogicalVector v) { - return v; + if (noAttributes.profile(v.getAttributes() == null)) { + return v; + } else { + return (RAbstractLogicalVector) v.copyDropAttributes(); + } } } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsRaw.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsRaw.java index a51b1cee4184798ecd850fc6fd960fc21f382f24..1300bfa38b2addff31d700096f857731d464eda8 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsRaw.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsRaw.java @@ -26,6 +26,7 @@ import static com.oracle.truffle.r.runtime.builtins.RBehavior.PURE; import static com.oracle.truffle.r.runtime.builtins.RBuiltinKind.PRIMITIVE; import com.oracle.truffle.api.dsl.Specialization; +import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.CastBuilder; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; import com.oracle.truffle.r.runtime.builtins.RBuiltin; @@ -36,6 +37,8 @@ import com.oracle.truffle.r.runtime.data.model.RAbstractRawVector; @RBuiltin(name = "as.raw", kind = PRIMITIVE, parameterNames = {"x"}, behavior = PURE) public abstract class AsRaw extends RBuiltinNode { + private final ConditionProfile noAttributes = ConditionProfile.createBinaryProfile(); + @Override protected void createCasts(CastBuilder casts) { casts.arg("x").asRawVector(); @@ -48,6 +51,10 @@ public abstract class AsRaw extends RBuiltinNode { @Specialization protected RAbstractRawVector asRaw(RAbstractRawVector v) { - return v; + if (noAttributes.profile(v.getAttributes() == null)) { + return v; + } else { + return (RAbstractRawVector) v.copyDropAttributes(); + } } } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsVector.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsVector.java index 77e3ff3f897bc2a370e4ddbfe8e3e31757d91254..0b2ccbc14eccadf9815d60c7143b773489712d2e 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsVector.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/AsVector.java @@ -34,6 +34,7 @@ import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.api.dsl.TypeSystemReference; import com.oracle.truffle.api.frame.VirtualFrame; import com.oracle.truffle.api.nodes.Node; +import com.oracle.truffle.api.profiles.BranchProfile; import com.oracle.truffle.api.profiles.ConditionProfile; import com.oracle.truffle.r.nodes.builtin.CastBuilder; import com.oracle.truffle.r.nodes.builtin.RBuiltinNode; @@ -57,6 +58,7 @@ import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RRuntime; import com.oracle.truffle.r.runtime.RType; import com.oracle.truffle.r.runtime.builtins.RBuiltin; +import com.oracle.truffle.r.runtime.data.RAttributable; import com.oracle.truffle.r.runtime.data.RAttributeProfiles; import com.oracle.truffle.r.runtime.data.RComplex; import com.oracle.truffle.r.runtime.data.RDataFactory; @@ -118,41 +120,54 @@ public abstract class AsVector extends RBuiltinNode { public abstract Object execute(Object x, String mode); private final RAttributeProfiles attrProfiles = RAttributeProfiles.create(); + private final BranchProfile hasAttributes = BranchProfile.create(); + + private Object dropAttributesIfNeeded(Object o) { + Object res = o; + if (res instanceof RAttributable && ((RAttributable) res).getAttributes() != null) { + // the assertion should hold because of how cast works and it's only used for + // vectors (as per as.vector docs) + assert res instanceof RAbstractVector; + hasAttributes.enter(); + res = ((RAbstractVector) res).copyDropAttributes(); + } + return res; + } @Specialization(guards = "castToString(mode)") protected Object asVectorString(Object x, @SuppressWarnings("unused") String mode, // @Cached("createNonPreserving()") CastStringNode cast) { - return cast.execute(x); + return dropAttributesIfNeeded(cast.execute(x)); } @Specialization(guards = "castToInt(x, mode)") protected Object asVectorInt(RAbstractContainer x, @SuppressWarnings("unused") String mode, // @Cached("createNonPreserving()") CastIntegerNode cast) { - return cast.execute(x); + return dropAttributesIfNeeded(cast.execute(x)); } @Specialization(guards = "castToDouble(x, mode)") protected Object asVectorDouble(RAbstractContainer x, @SuppressWarnings("unused") String mode, // @Cached("createNonPreserving()") CastDoubleNode cast) { - return cast.execute(x); + return dropAttributesIfNeeded(cast.execute(x)); } @Specialization(guards = "castToComplex(x, mode)") protected Object asVectorComplex(RAbstractContainer x, @SuppressWarnings("unused") String mode, // @Cached("createNonPreserving()") CastComplexNode cast) { - return cast.execute(x); + return dropAttributesIfNeeded(cast.execute(x)); } @Specialization(guards = "castToLogical(x, mode)") protected Object asVectorLogical(RAbstractContainer x, @SuppressWarnings("unused") String mode, // @Cached("createNonPreserving()") CastLogicalNode cast) { - return cast.execute(x); + return dropAttributesIfNeeded(cast.execute(x)); } @Specialization(guards = "castToRaw(x, mode)") protected Object asVectorRaw(RAbstractContainer x, @SuppressWarnings("unused") String mode, // @Cached("createNonPreserving()") CastRawNode cast) { - return cast.execute(x); + return dropAttributesIfNeeded(cast.execute(x)); } protected static CastListNode createListCast() { diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java index 699b9fe4219a49ab2dd8e3b8977e5dacf4125e7f..fef9305da7c55355d10060f099bf09024f8e2a32 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Paste.java @@ -77,7 +77,7 @@ public abstract class Paste extends RBuiltinNode { } else if (ret == RNull.instance) { return RDataFactory.createEmptyStringVector(); } else { - return (RStringVector) ret; + return (RStringVector) ((RStringVector) ret).copyDropAttributes(); } } diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/UpdateNames.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/UpdateNames.java index fd9b979a7964a6a0b0532c753bfff0a6e8d5c3f4..93587752d7411f1f797a66f85692de3cc0bf4dbf 100644 --- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/UpdateNames.java +++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/UpdateNames.java @@ -69,7 +69,7 @@ public abstract class UpdateNames extends RBuiltinNode { if (newNames instanceof String) { stringVector = RDataFactory.createStringVector((String) newNames); } else { - stringVector = (RStringVector) ((RAbstractVector) newNames).materialize(); + stringVector = (RStringVector) ((RAbstractVector) newNames).materialize().copyDropAttributes(); } RAbstractContainer result = (RAbstractContainer) container.getNonShared(); if (stringVector.getLength() < result.getLength()) { diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastDoubleNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastDoubleNode.java index ae0bcc6da8233149959c90103de6290c912100dd..9e5e541a9d2839cd2ac67f88b782a58a864ec385 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastDoubleNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastDoubleNode.java @@ -60,15 +60,6 @@ public abstract class CastDoubleNode extends CastDoubleBaseNode { return recursiveCastDouble.executeDouble(o); } - private RDoubleVector createResultVector(RAbstractVector operand, double[] ddata) { - RDoubleVector ret = RDataFactory.createDoubleVector(ddata, naCheck.neverSeenNA(), getPreservedDimensions(operand), getPreservedNames(operand)); - preserveDimensionNames(operand, ret); - if (preserveAttributes()) { - ret.copyRegAttributesFrom(operand); - } - return ret; - } - private RDoubleVector vectorCopy(RAbstractContainer operand, double[] data, boolean isComplete) { RDoubleVector ret = RDataFactory.createDoubleVector(data, isComplete, getPreservedDimensions(operand), getPreservedNames(operand)); preserveDimensionNames(operand, ret); @@ -152,7 +143,7 @@ public abstract class CastDoubleNode extends CastDoubleBaseNode { warningBranch.enter(); RError.warning(this, RError.Message.IMAGINARY_PARTS_DISCARDED_IN_COERCION); } - return createResultVector(operand, ddata); + return vectorCopy(operand, ddata, naCheck.neverSeenNA()); } @Specialization @@ -162,11 +153,7 @@ public abstract class CastDoubleNode extends CastDoubleBaseNode { @Specialization protected RDoubleVector doDoubleVector(RDoubleVector operand) { - if (preserveAttributes() && preserveDimensions() && preserveNames()) { - return operand; - } else { - return vectorCopy(operand, operand.getDataCopy(), operand.isComplete()); - } + return operand; } @Specialization diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastIntegerNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastIntegerNode.java index 188070eb1f63af468597bf64ce12b8fdd6b42d93..59245636fa3c80316e0febe0fa94e27645bd9ebe 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastIntegerNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastIntegerNode.java @@ -62,11 +62,7 @@ public abstract class CastIntegerNode extends CastIntegerBaseNode { @Specialization protected RIntVector doIntVector(RIntVector operand) { - if (preserveAttributes() && preserveDimensions() && preserveNames()) { - return operand; - } else { - return vectorCopy(operand, operand.getDataCopy(), operand.isComplete()); - } + return operand; } @Specialization diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastLogicalNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastLogicalNode.java index 94b1eba4677ee0eafe86cfef256312128569ad71..bb333f788f9fe878b320be956239f66f7cf84399 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastLogicalNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastLogicalNode.java @@ -103,11 +103,7 @@ public abstract class CastLogicalNode extends CastLogicalBaseNode { @Specialization protected RLogicalVector doLogicalVector(RLogicalVector operand) { - if (preserveAttributes() && preserveDimensions() && preserveNames()) { - return operand; - } else { - return vectorCopy(operand, operand.getDataCopy(), operand.isComplete()); - } + return operand; } @Specialization(guards = "!isFactor(operand)") diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastRawNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastRawNode.java index 420735f555e979100eba33ee9cab3a99ccf733b4..69d13990142a7dcd8d52515c28c579f65576df7a 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastRawNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastRawNode.java @@ -288,11 +288,7 @@ public abstract class CastRawNode extends CastBaseNode { @Specialization protected RRawVector doRawVector(RRawVector operand) { - if (preserveAttributes() && preserveDimensions() && preserveNames()) { - return operand; - } else { - return vectorCopy(operand, operand.getDataCopy()); - } + return operand; } @Specialization diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastStringNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastStringNode.java index d416956e4a2689e6f5f1098a9b7a4de604697d56..53e23e6e8be1b17af91843b85c673e5d2b31b082 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastStringNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastStringNode.java @@ -61,11 +61,7 @@ public abstract class CastStringNode extends CastStringBaseNode { @Specialization protected RStringVector doStringVector(RStringVector vector) { - if (preserveAttributes() && preserveDimensions() && preserveNames()) { - return vector; - } else { - return vectorCopy(vector, vector.getDataCopy()); - } + return vector; } @Specialization