You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by mb...@apache.org on 2017/10/30 00:18:58 UTC

systemml git commit: [SYSTEMML-1693] More aggressive function inlining after rewrites

Repository: systemml
Updated Branches:
  refs/heads/master d75a669a4 -> 2896f3316


[SYSTEMML-1693] More aggressive function inlining after rewrites

This patch extends the new IPA pass for function inlining after
rewrites. In addition to inlining small functions, we now also inline
functions that are called once, independent of their size. This is safe
wrt the code size, as inlining can only decrease the code size here.


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

Branch: refs/heads/master
Commit: 2896f3316099241b8074615929424e51cf877d4a
Parents: d75a669
Author: Matthias Boehm <mb...@gmail.com>
Authored: Sun Oct 29 17:19:49 2017 -0700
Committer: Matthias Boehm <mb...@gmail.com>
Committed: Sun Oct 29 17:19:49 2017 -0700

----------------------------------------------------------------------
 .../sysml/hops/ipa/IPAPassInlineFunctions.java  |  8 +++-
 .../functions/misc/IPAFunctionInliningTest.java | 20 ++++++++--
 .../scripts/functions/misc/IPAFunInline5.dml    | 40 ++++++++++++++++++++
 3 files changed, 63 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/2896f331/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 0527a10..c7ee3e4 100644
--- a/src/main/java/org/apache/sysml/hops/ipa/IPAPassInlineFunctions.java
+++ b/src/main/java/org/apache/sysml/hops/ipa/IPAPassInlineFunctions.java
@@ -54,14 +54,18 @@ public class IPAPassInlineFunctions extends IPAPass
 	public void rewriteProgram( DMLProgram prog, FunctionCallGraph fgraph, FunctionCallSizeInfo fcallSizes ) 
 		throws HopsException
 	{
+		//NOTE: we inline single-statement-block (i.e., last-level block) functions
+		//that do not contain other functions, and either are small or called once
+		
 		for( String fkey : fgraph.getReachableFunctions() ) {
 			FunctionStatementBlock fsb = prog.getFunctionStatementBlock(fkey);
 			FunctionStatement fstmt = (FunctionStatement)fsb.getStatement(0);
 			if( fstmt.getBody().size() == 1 
 				&& HopRewriteUtils.isLastLevelStatementBlock(fstmt.getBody().get(0)) 
 				&& !containsFunctionOp(fstmt.getBody().get(0).get_hops())
-				&& countOperators(fstmt.getBody().get(0).get_hops()) 
-					<= InterProceduralAnalysis.INLINING_MAX_NUM_OPS )
+				&& (fgraph.getFunctionCalls(fkey).size() == 1
+					|| countOperators(fstmt.getBody().get(0).get_hops()) 
+						<= InterProceduralAnalysis.INLINING_MAX_NUM_OPS) )
 			{
 				if( LOG.isDebugEnabled() )
 					LOG.debug("IPA: Inline function '"+fkey+"'");

http://git-wip-us.apache.org/repos/asf/systemml/blob/2896f331/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 f58d400..39f79c9 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
@@ -33,8 +33,10 @@ public class IPAFunctionInliningTest extends AutomatedTestBase
 {
 	private final static String TEST_NAME1 = "IPAFunInline1"; //pos 1
 	private final static String TEST_NAME2 = "IPAFunInline2"; //pos 2
-	private final static String TEST_NAME3 = "IPAFunInline3"; //neg 1 (too large)
-	private final static String TEST_NAME4 = "IPAFunInline4"; //neg 2 (control flow)
+	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_DIR = "functions/misc/";
 	private final static String TEST_CLASS_DIR = TEST_DIR + IPAFunctionInliningTest.class.getSimpleName() + "/";
@@ -46,6 +48,7 @@ public class IPAFunctionInliningTest extends AutomatedTestBase
 		addTestConfiguration( TEST_NAME2, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME2, new String[] { "R" }) );
 		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" }) );
 	}
 
 	@Test
@@ -69,6 +72,11 @@ public class IPAFunctionInliningTest extends AutomatedTestBase
 	}
 	
 	@Test
+	public void testFunInline5NoIPA() {
+		runIPAFunInlineTest( TEST_NAME5, false );
+	}
+	
+	@Test
 	public void testFunInline1IPA() {
 		runIPAFunInlineTest( TEST_NAME1, true );
 	}
@@ -88,6 +96,11 @@ public class IPAFunctionInliningTest extends AutomatedTestBase
 		runIPAFunInlineTest( TEST_NAME4, true );
 	}
 	
+	@Test
+	public void testFunInline5IPA() {
+		runIPAFunInlineTest( TEST_NAME5, true );
+	}
+	
 	private void runIPAFunInlineTest( String testName, boolean IPA )
 	{
 		boolean oldFlagIPA = OptimizerUtils.ALLOW_INTER_PROCEDURAL_ANALYSIS;
@@ -112,7 +125,8 @@ public class IPAFunctionInliningTest extends AutomatedTestBase
 			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)) );
+			boolean inlined = ( IPA && (testName.equals(TEST_NAME1) 
+				|| testName.equals(TEST_NAME2) || testName.equals(TEST_NAME3)) );
 			Assert.assertTrue("Unexpected function call: "+inlined, !heavyHittersContainsSubString("foo")==inlined);
 		}
 		finally {

http://git-wip-us.apache.org/repos/asf/systemml/blob/2896f331/src/test/scripts/functions/misc/IPAFunInline5.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/misc/IPAFunInline5.dml b/src/test/scripts/functions/misc/IPAFunInline5.dml
new file mode 100644
index 0000000..885468c
--- /dev/null
+++ b/src/test/scripts/functions/misc/IPAFunInline5.dml
@@ -0,0 +1,40 @@
+#-------------------------------------------------------------
+#
+# 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.
+#
+#-------------------------------------------------------------
+
+
+foo = function(Matrix[Double] A, Integer type) return (Matrix[Double] B) {
+  if( type==1 ) {
+    C = (A * A * A) / 3 + 2;
+    D = (A^2 + A^2 + 7) * A;
+    E = min(C, D)
+    B = ((E != 0) * A) * A * A;
+  }
+  else {
+    B = A - 0.1;
+  } 
+}
+
+X = matrix(0.1, rows=100, cols=10);
+Y = foo(X, 1);
+Z = foo(X, 1);
+z = as.matrix((sum(Y)+sum(Z))/2*7);
+
+write(z, $1);