[SYSTEMML-652] Fix function inlining in unsupported expressions, tests Function calls in expressions (commonly left indexing expressions) are not supported and should result in an language exception. However, with function inlining, the target was always assumed to be a data identifier, which silently changed indexing expressions to assignments leading to potentially incorrect results. This patch prevents function inlining for unsupported target expressions for consistent error handling.
Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/5883fb54 Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/5883fb54 Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/5883fb54 Branch: refs/heads/master Commit: 5883fb54b07faadd77b08c25f93ebec5aef094d4 Parents: 6ee2f23 Author: Matthias Boehm <[email protected]> Authored: Wed Apr 27 02:50:53 2016 -0700 Committer: Matthias Boehm <[email protected]> Committed: Wed Apr 27 10:12:09 2016 -0700 ---------------------------------------------------------------------- .../org/apache/sysml/parser/StatementBlock.java | 12 ++- .../misc/InvalidFunctionAssignmentTest.java | 84 ++++++++++++++++++++ .../misc/InvalidFunctionAssignmentTest1.dml | 31 ++++++++ .../misc/InvalidFunctionAssignmentTest2.dml | 30 +++++++ .../misc/InvalidFunctionAssignmentTest3.dml | 32 ++++++++ .../misc/InvalidFunctionAssignmentTest4.dml | 31 ++++++++ .../functions/misc/ZPackageSuite.java | 1 + 7 files changed, 219 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/5883fb54/src/main/java/org/apache/sysml/parser/StatementBlock.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/parser/StatementBlock.java b/src/main/java/org/apache/sysml/parser/StatementBlock.java index b3f0cec..c67fbc3 100644 --- a/src/main/java/org/apache/sysml/parser/StatementBlock.java +++ b/src/main/java/org/apache/sysml/parser/StatementBlock.java @@ -222,12 +222,20 @@ public class StatementBlock extends LiveVariableAnalysis if (sourceExpr instanceof FunctionCallIdentifier){ FunctionCallIdentifier fcall = (FunctionCallIdentifier) sourceExpr; FunctionStatementBlock fblock = dmlProg.getFunctionStatementBlock(fcall.getNamespace(),fcall.getName()); - if (fblock == null){ + if (fblock == null) { throw new LanguageException(sourceExpr.printErrorLocation() + "function " + fcall.getName() + " is undefined in namespace " + fcall.getNamespace()); } - if( rIsInlineableFunction(fblock, dmlProg) ) + //check for unsupported target indexed identifiers (for consistent error handling) + if( stmt instanceof AssignmentStatement + && ((AssignmentStatement)stmt).getTarget() instanceof IndexedIdentifier ) { + return false; + } + + //check if function can be inlined + if( rIsInlineableFunction(fblock, dmlProg) ) { return true; + } } } http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/5883fb54/src/test/java/org/apache/sysml/test/integration/functions/misc/InvalidFunctionAssignmentTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/sysml/test/integration/functions/misc/InvalidFunctionAssignmentTest.java b/src/test/java/org/apache/sysml/test/integration/functions/misc/InvalidFunctionAssignmentTest.java new file mode 100644 index 0000000..351e26d --- /dev/null +++ b/src/test/java/org/apache/sysml/test/integration/functions/misc/InvalidFunctionAssignmentTest.java @@ -0,0 +1,84 @@ +/* + * 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. + */ + +package org.apache.sysml.test.integration.functions.misc; + +import org.junit.Test; + +import org.apache.sysml.api.DMLException; +import org.apache.sysml.test.integration.AutomatedTestBase; +import org.apache.sysml.test.integration.TestConfiguration; + +/** + * + */ +public class InvalidFunctionAssignmentTest extends AutomatedTestBase +{ + private final static String TEST_DIR = "functions/misc/"; + private final static String TEST_NAME1 = "InvalidFunctionAssignmentTest1"; + private final static String TEST_NAME2 = "InvalidFunctionAssignmentTest2"; + private final static String TEST_NAME3 = "InvalidFunctionAssignmentTest3"; + private final static String TEST_NAME4 = "InvalidFunctionAssignmentTest4"; + private final static String TEST_CLASS_DIR = TEST_DIR + InvalidFunctionAssignmentTest.class.getSimpleName() + "/"; + + @Override + public void setUp() { + addTestConfiguration(TEST_NAME1, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME1, new String[] {})); + addTestConfiguration(TEST_NAME2, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME2, new String[] {})); + addTestConfiguration(TEST_NAME3, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME3, new String[] {})); + addTestConfiguration(TEST_NAME4, new TestConfiguration(TEST_CLASS_DIR, TEST_NAME4, new String[] {})); + } + + @Test + public void testValidFunctionAssignmentInlined() { + runTest( TEST_NAME1, false ); + } + + @Test + public void testInvalidFunctionAssignmentInlined() { + runTest( TEST_NAME2, true ); + } + + @Test + public void testValidFunctionAssignment() { + runTest( TEST_NAME3, false ); + } + + @Test + public void testInvalidFunctionAssignment() { + runTest( TEST_NAME4, true ); + } + + /** + * + * @param testName + */ + private void runTest( String testName, boolean exceptionExpected ) + { + TestConfiguration config = getTestConfiguration(testName); + loadTestConfiguration(config); + + String HOME = SCRIPT_DIR + TEST_DIR; + fullDMLScriptName = HOME + testName + ".dml"; + programArgs = new String[]{}; + + //run tests + runTest(true, exceptionExpected, DMLException.class, -1); + } +} http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/5883fb54/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest1.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest1.dml b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest1.dml new file mode 100644 index 0000000..8dcbbda --- /dev/null +++ b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest1.dml @@ -0,0 +1,31 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +forward = function(matrix[double] X) return (matrix[double] out) { + out = 1 / (1 + exp(-X)) +} + +N = 3; +M = 5; +X = rand(rows=N, cols=M); +tmp = forward(X[,1:2]); +X[,1:2] = tmp; +print("X1: " + nrow(X) + "x" + ncol(X)); http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/5883fb54/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest2.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest2.dml b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest2.dml new file mode 100644 index 0000000..65b44a1 --- /dev/null +++ b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest2.dml @@ -0,0 +1,30 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +forward = function(matrix[double] X) return (matrix[double] out) { + out = 1 / (1 + exp(-X)) +} + +N = 3; +M = 5; +X = rand(rows=N, cols=M); +X[,1:2] = forward(X[,1:2]); +print("X1: " + nrow(X) + "x" + ncol(X)); http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/5883fb54/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest3.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest3.dml b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest3.dml new file mode 100644 index 0000000..a480b77 --- /dev/null +++ b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest3.dml @@ -0,0 +1,32 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +forward = function(matrix[double] X) return (matrix[double] out) { + for( i in 1:2 ) + out = 1 / (1 + exp(-X)) +} + +N = 3; +M = 5; +X = rand(rows=N, cols=M); +tmp = forward(X[,1:2]); +X[,1:2] = tmp; +print("X1: " + nrow(X) + "x" + ncol(X)); http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/5883fb54/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest4.dml ---------------------------------------------------------------------- diff --git a/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest4.dml b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest4.dml new file mode 100644 index 0000000..45d0a00 --- /dev/null +++ b/src/test/scripts/functions/misc/InvalidFunctionAssignmentTest4.dml @@ -0,0 +1,31 @@ +#------------------------------------------------------------- +# +# 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. +# +#------------------------------------------------------------- + +forward = function(matrix[double] X) return (matrix[double] out) { + for( i in 1:2 ) + out = 1 / (1 + exp(-X)) +} + +N = 3; +M = 5; +X = rand(rows=N, cols=M); +X[,1:2] = forward(X[,1:2]); +print("X1: " + nrow(X) + "x" + ncol(X)); http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/5883fb54/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java ---------------------------------------------------------------------- diff --git a/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java b/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java index cff3c67..5fa4d3a 100644 --- a/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java +++ b/src/test_suites/java/org/apache/sysml/test/integration/functions/misc/ZPackageSuite.java @@ -31,6 +31,7 @@ import org.junit.runners.Suite; DataTypeChangeTest.class, FunctionInliningTest.class, FunctionNamespaceTest.class, + InvalidFunctionAssignmentTest.class, InvalidFunctionSignatureTest.class, IPALiteralReplacementTest.class, IPAScalarRecursionTest.class,
