Repository: systemml
Updated Branches:
  refs/heads/master bafed4986 -> 0930fc0f2


[SYSTEMML-2040] Fix IPA function inlining (replicated inlined ops)

This patch fixes issues of replicated operations after repeated IPA
function inlining. Due to multiple rounds of IPA with reused function
call graph, amenable functions got inlined multiple times. For most
operations, common subexpression elimination removes this redundancy but
for print and rand this is not the case. We now properly update the
function call graph after inlining.


Project: http://git-wip-us.apache.org/repos/asf/systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/0930fc0f
Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/0930fc0f
Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/0930fc0f

Branch: refs/heads/master
Commit: 0930fc0f27d5db46a882f6e461e32c7e837cda45
Parents: bafed49
Author: Matthias Boehm <mboe...@gmail.com>
Authored: Thu Dec 7 19:20:46 2017 -0800
Committer: Matthias Boehm <mboe...@gmail.com>
Committed: Thu Dec 7 19:50:38 2017 -0800

----------------------------------------------------------------------
 .../sysml/hops/ipa/FunctionCallGraph.java       | 16 ++++++++++
 .../sysml/hops/ipa/IPAPassInlineFunctions.java  |  4 +++
 .../functions/misc/IPAFunctionInliningTest.java | 32 +++++++++++++++----
 .../scripts/functions/misc/IPAFunInline6.dml    | 33 ++++++++++++++++++++
 4 files changed, 79 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/0930fc0f/src/main/java/org/apache/sysml/hops/ipa/FunctionCallGraph.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/ipa/FunctionCallGraph.java 
b/src/main/java/org/apache/sysml/hops/ipa/FunctionCallGraph.java
index 6ea1338..d1ece9b 100644
--- a/src/main/java/org/apache/sysml/hops/ipa/FunctionCallGraph.java
+++ b/src/main/java/org/apache/sysml/hops/ipa/FunctionCallGraph.java
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map.Entry;
 import java.util.Set;
 import java.util.Stack;
 import java.util.stream.Collectors;
@@ -143,6 +144,21 @@ public class FunctionCallGraph
        }
        
        /**
+        * Removes all calls of the given function.
+        * 
+        * @param fkey function key of called function,
+        *      null indicates the main program, which has no affect
+        */
+       public void removeFunctionCalls(String fkey) {
+               _fCalls.remove(fkey);
+               _fCallsSB.remove(fkey);
+               _fRecursive.remove(fkey);
+               _fGraph.remove(fkey);
+               for( Entry<String, HashSet<String>> e : _fGraph.entrySet() )
+                       e.getValue().removeIf(s -> s.equals(fkey));
+       }
+       
+       /**
         * Indicates if the given function is either directly or indirectly 
recursive.
         * An example of an indirect recursive function is foo2 in the 
following call
         * chain: foo1 -&gt; foo2 -&gt; foo1.

http://git-wip-us.apache.org/repos/asf/systemml/blob/0930fc0f/src/main/java/org/apache/sysml/hops/ipa/IPAPassInlineFunctions.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/hops/ipa/IPAPassInlineFunctions.java 
b/src/main/java/org/apache/sysml/hops/ipa/IPAPassInlineFunctions.java
index 106eb2b..db19e26 100644
--- a/src/main/java/org/apache/sysml/hops/ipa/IPAPassInlineFunctions.java
+++ b/src/main/java/org/apache/sysml/hops/ipa/IPAPassInlineFunctions.java
@@ -104,6 +104,10 @@ public class IPAPassInlineFunctions extends IPAPass
                                        fcallsSB.get(i).getHops().remove(op);
                                        fcallsSB.get(i).getHops().addAll(hops2);
                                }
+                               
+                               //update the function call graph to avoid 
repeated inlining
+                               //(and thus op replication) on repeated IPA 
calls
+                               fgraph.removeFunctionCalls(fkey);
                        }
                }
        }

http://git-wip-us.apache.org/repos/asf/systemml/blob/0930fc0f/src/test/java/org/apache/sysml/test/integration/functions/misc/IPAFunctionInliningTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/sysml/test/integration/functions/misc/IPAFunctionInliningTest.java
 
b/src/test/java/org/apache/sysml/test/integration/functions/misc/IPAFunctionInliningTest.java
index 39f79c9..8e22c5d 100644
--- 
a/src/test/java/org/apache/sysml/test/integration/functions/misc/IPAFunctionInliningTest.java
+++ 
b/src/test/java/org/apache/sysml/test/integration/functions/misc/IPAFunctionInliningTest.java
@@ -22,12 +22,13 @@ package org.apache.sysml.test.integration.functions.misc;
 
 import org.junit.Assert;
 import org.junit.Test;
-
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.sysml.hops.OptimizerUtils;
 import org.apache.sysml.runtime.matrix.data.MatrixValue.CellIndex;
 import org.apache.sysml.test.integration.AutomatedTestBase;
 import org.apache.sysml.test.integration.TestConfiguration;
 import org.apache.sysml.test.utils.TestUtils;
+import org.apache.sysml.utils.Statistics;
 
 public class IPAFunctionInliningTest extends AutomatedTestBase 
 {
@@ -36,7 +37,7 @@ public class IPAFunctionInliningTest extends AutomatedTestBase
        private final static String TEST_NAME3 = "IPAFunInline3"; //pos 3 
(large but called once)
        private final static String TEST_NAME4 = "IPAFunInline4"; //neg 1 
(control flow)
        private final static String TEST_NAME5 = "IPAFunInline5"; //neg 2 
(large and called twice)
-       
+       private final static String TEST_NAME6 = "IPAFunInline6"; //pos 4 
(regression op replication)
        
        private final static String TEST_DIR = "functions/misc/";
        private final static String TEST_CLASS_DIR = TEST_DIR + 
IPAFunctionInliningTest.class.getSimpleName() + "/";
@@ -49,6 +50,7 @@ public class IPAFunctionInliningTest extends AutomatedTestBase
                addTestConfiguration( TEST_NAME3, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME3, new String[] { "R" }) );
                addTestConfiguration( TEST_NAME4, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME4, new String[] { "R" }) );
                addTestConfiguration( TEST_NAME5, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME5, new String[] { "R" }) );
+               addTestConfiguration( TEST_NAME6, new 
TestConfiguration(TEST_CLASS_DIR, TEST_NAME6, new String[] { "R" }) );
        }
 
        @Test
@@ -77,6 +79,11 @@ public class IPAFunctionInliningTest extends 
AutomatedTestBase
        }
        
        @Test
+       public void testFunInline6NoIPA() {
+               runIPAFunInlineTest( TEST_NAME6, false );
+       }
+       
+       @Test
        public void testFunInline1IPA() {
                runIPAFunInlineTest( TEST_NAME1, true );
        }
@@ -101,6 +108,11 @@ public class IPAFunctionInliningTest extends 
AutomatedTestBase
                runIPAFunInlineTest( TEST_NAME5, true );
        }
        
+       @Test
+       public void testFunInline6IPA() {
+               runIPAFunInlineTest( TEST_NAME6, true );
+       }
+       
        private void runIPAFunInlineTest( String testName, boolean IPA )
        {
                boolean oldFlagIPA = 
OptimizerUtils.ALLOW_INTER_PROCEDURAL_ANALYSIS;
@@ -121,13 +133,21 @@ public class IPAFunctionInliningTest extends 
AutomatedTestBase
 
                        //run script and compare output
                        runTest(true, false, null, -1); 
-                       double val = readDMLMatrixFromHDFS("R").get(new 
CellIndex(1,1));
-                       Assert.assertTrue("Wrong result: 7 vs "+val, 
Math.abs(val-7)<Math.pow(10, -14));
+                       
+                       //compare results
+                       if( !testName.equals(TEST_NAME6) ) {
+                               double val = readDMLMatrixFromHDFS("R").get(new 
CellIndex(1,1));
+                               Assert.assertTrue("Wrong result: 7 vs "+val, 
Math.abs(val-7)<Math.pow(10, -14));
+                       }
                        
                        //compare inlined functions
-                       boolean inlined = ( IPA && (testName.equals(TEST_NAME1) 
-                               || testName.equals(TEST_NAME2) || 
testName.equals(TEST_NAME3)) );
+                       boolean inlined = ( IPA && ArrayUtils.contains(
+                               new String[]{TEST_NAME1, TEST_NAME2, 
TEST_NAME3, TEST_NAME6}, testName));
                        Assert.assertTrue("Unexpected function call: "+inlined, 
!heavyHittersContainsSubString("foo")==inlined);
+               
+                       //check for incorrect operation replication
+                       if( testName.equals(TEST_NAME6) )
+                               
Assert.assertTrue(Statistics.getCPHeavyHitterCount("print")==1);
                }
                finally {
                        OptimizerUtils.ALLOW_INTER_PROCEDURAL_ANALYSIS = 
oldFlagIPA;

http://git-wip-us.apache.org/repos/asf/systemml/blob/0930fc0f/src/test/scripts/functions/misc/IPAFunInline6.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/misc/IPAFunInline6.dml 
b/src/test/scripts/functions/misc/IPAFunInline6.dml
new file mode 100644
index 0000000..8e16ea2
--- /dev/null
+++ b/src/test/scripts/functions/misc/IPAFunInline6.dml
@@ -0,0 +1,33 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+s = "Debug info: " + sum(matrix(1,10,10))
+z = foo(s);
+
+foo = function(String msg) return (boolean flag) {
+  is_debug = TRUE;
+  if( is_debug ) {
+    print(msg); flag = TRUE;
+  } 
+  else {
+    flag = FALSE;
+  }   
+}

Reply via email to