From c89b608cfa1af5f2d0abf235d9680118750ee3ff Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Tue, 7 Jun 2016 11:12:35 +0200
Subject: [PATCH] CachedReplaceVector: handle cases where lsh == rhs

For example pattern "vector[some.permutation.of.indices] <- vector" which is used in lm.fit
---
 .../vector/CachedReplaceVectorNode.java       |  9 +++--
 .../truffle/r/test/ExpectedTestOutput.test    |  4 ++
 .../builtins/TestBuiltin_extract_replace.java | 40 +++++++++++++++++++
 3 files changed, 49 insertions(+), 4 deletions(-)
 create mode 100644 com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_extract_replace.java

diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/access/vector/CachedReplaceVectorNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/access/vector/CachedReplaceVectorNode.java
index 0d528893f4..4429a74571 100644
--- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/access/vector/CachedReplaceVectorNode.java
+++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/access/vector/CachedReplaceVectorNode.java
@@ -230,7 +230,7 @@ final class CachedReplaceVectorNode extends CachedVectorNode {
             value = ((RAbstractVector) value).castSafe(castType, valueIsNA);
         }
 
-        vector = share(vector);
+        vector = share(vector, value);
 
         int maxOutOfBounds = positionsCheckNode.getMaxOutOfBounds(positionProfiles);
         if (maxOutOfBounds > vectorLength) {
@@ -239,7 +239,6 @@ final class CachedReplaceVectorNode extends CachedVectorNode {
                 return wrapResult(vector, repType);
             }
             vector = resizeVector(vector, maxOutOfBounds);
-            vectorLength = maxOutOfBounds;
         }
         vector = vector.materialize();
 
@@ -467,16 +466,18 @@ final class CachedReplaceVectorNode extends CachedVectorNode {
 
     private final ValueProfile sharedClassProfile = ValueProfile.createClassProfile();
 
+    private final ConditionProfile valueEqualsVectorProfile = ConditionProfile.createBinaryProfile();
+
     /*
      * TODO (chumer) share code between {@link #share(RAbstractVector)} and {@link
      * #copyValueOnAssignment(RAbstractContainer)}
      */
-    private RAbstractVector share(RAbstractVector vector) {
+    private RAbstractVector share(RAbstractVector vector, Object value) {
         RAbstractVector returnVector = vector;
         if (returnVector instanceof RShareable) {
             RShareable shareable = (RShareable) returnVector;
             // TODO find out if we need to copy always in the recursive case
-            if (sharedConditionProfile.profile(shareable.isShared()) || recursive) {
+            if (sharedConditionProfile.profile(shareable.isShared()) || recursive || valueEqualsVectorProfile.profile(vector == value)) {
                 sharedProfile.enter();
                 shareable = (RShareable) returnVector.copy();
                 returnVector = (RAbstractVector) shareable;
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
index e56dcc464d..658617f4ef 100644
--- a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/ExpectedTestOutput.test
@@ -19116,6 +19116,10 @@ Levels: A B C D E
 [21] 1456434_x_at 1415993_at   1416481_s_at 1452003_at   1439373_x_at
 147 Levels: 1415787_at 1415904_at 1415993_at 1416164_at ... 1460359_at
 
+##com.oracle.truffle.r.test.builtins.TestBuiltin_extract_replace.extractAndReplaceByItself
+#tmp <- c(1,8,NA,3); pivot <- c(1,2,4,3); tmp[pivot] <- tmp; tmp
+[1]  1  8  3 NA
+
 ##com.oracle.truffle.r.test.builtins.TestBuiltin_factor.testFactor
 #{ as.logical(factor(c("a", "b", "a"))) }
 [1] NA NA NA
diff --git a/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_extract_replace.java b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_extract_replace.java
new file mode 100644
index 0000000000..42d60834f2
--- /dev/null
+++ b/com.oracle.truffle.r.test/src/com/oracle/truffle/r/test/builtins/TestBuiltin_extract_replace.java
@@ -0,0 +1,40 @@
+/*
+ * 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.builtins;
+
+import org.junit.Test;
+
+import com.oracle.truffle.r.test.TestBase;
+
+// Checkstyle: stop line length check
+
+/**
+ * Tests for assignments that go through {@code CachedReplaceVectorNode}.
+ */
+public class TestBuiltin_extract_replace extends TestBase {
+
+    @Test
+    public void extractAndReplaceByItself() {
+        assertEval("tmp <- c(1,8,NA,3); pivot <- c(1,2,4,3); tmp[pivot] <- tmp; tmp");
+    }
+}
-- 
GitLab