[SYSTEMML-1990] New cleanup rewrite for removal of empty basic blocks

This new rewrite removes empty basic blocks, i.e., last-level blocks
without hops, which can originate from the original program or a
sequence of rewrites.


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

Branch: refs/heads/master
Commit: d90073d804322f6b601b0f06d03979dae6322c25
Parents: 78586a1
Author: Matthias Boehm <mboe...@gmail.com>
Authored: Wed Nov 29 22:30:30 2017 -0800
Committer: Matthias Boehm <mboe...@gmail.com>
Committed: Wed Nov 29 22:30:54 2017 -0800

----------------------------------------------------------------------
 .../sysml/hops/rewrite/ProgramRewriter.java     |  3 +-
 .../rewrite/RewriteRemoveEmptyBasicBlocks.java  | 58 ++++++++++++++++++++
 2 files changed, 60 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/d90073d8/src/main/java/org/apache/sysml/hops/rewrite/ProgramRewriter.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/rewrite/ProgramRewriter.java 
b/src/main/java/org/apache/sysml/hops/rewrite/ProgramRewriter.java
index ee8d27c..a73ea6d 100644
--- a/src/main/java/org/apache/sysml/hops/rewrite/ProgramRewriter.java
+++ b/src/main/java/org/apache/sysml/hops/rewrite/ProgramRewriter.java
@@ -105,7 +105,7 @@ public class ProgramRewriter
                        if( OptimizerUtils.ALLOW_BRANCH_REMOVAL ) {
                                _sbRuleSet.add(  new 
RewriteRemoveUnnecessaryBranches()          ); //dependency: constant folding
                                _sbRuleSet.add(  new 
RewriteMergeBlockSequence()                 ); //dependency: remove branches
-                       }
+                       }
                        _sbRuleSet.add(      new RewriteCompressedReblock()     
             );
                        if( OptimizerUtils.ALLOW_SPLIT_HOP_DAGS )
                                _sbRuleSet.add(  new 
RewriteSplitDagUnknownCSVRead()             ); //dependency: reblock, merge 
blocks
@@ -137,6 +137,7 @@ public class ProgramRewriter
                        _dagRuleSet.add( new RewriteRemoveUnnecessaryCasts()    
         );
                if( OptimizerUtils.ALLOW_COMMON_SUBEXPRESSION_ELIMINATION )
                        _dagRuleSet.add( new 
RewriteCommonSubexpressionElimination(true) );
+               _sbRuleSet.add(  new RewriteRemoveEmptyBasicBlocks()            
     );
        }
        
        /**

http://git-wip-us.apache.org/repos/asf/systemml/blob/d90073d8/src/main/java/org/apache/sysml/hops/rewrite/RewriteRemoveEmptyBasicBlocks.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/hops/rewrite/RewriteRemoveEmptyBasicBlocks.java
 
b/src/main/java/org/apache/sysml/hops/rewrite/RewriteRemoveEmptyBasicBlocks.java
new file mode 100644
index 0000000..5010618
--- /dev/null
+++ 
b/src/main/java/org/apache/sysml/hops/rewrite/RewriteRemoveEmptyBasicBlocks.java
@@ -0,0 +1,58 @@
+/*
+ * 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.hops.rewrite;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sysml.hops.HopsException;
+import org.apache.sysml.parser.StatementBlock;
+
+/**
+ * Rule: Simplify program structure by removing empty last-level blocks,
+ * which may originate from the original program or due to a sequence of
+ * rewrites (e.g., checkpoint injection and subsequent IPA).
+ */
+public class RewriteRemoveEmptyBasicBlocks extends StatementBlockRewriteRule
+{
+       @Override
+       public List<StatementBlock> rewriteStatementBlock(StatementBlock sb, 
ProgramRewriteStatus state)
+               throws HopsException 
+       {
+               ArrayList<StatementBlock> ret = new ArrayList<>();
+               
+               //prune last level blocks with empty hops
+               if( HopRewriteUtils.isLastLevelStatementBlock(sb)
+                       && (sb.getHops() == null || sb.getHops().isEmpty()) ) {
+                       if( LOG.isDebugEnabled() )
+                               LOG.debug("Applied removeEmptyBasicBlocks 
(lines "+sb.getBeginLine()+"-"+sb.getEndLine()+").");
+               }
+               else //keep original sb
+                       ret.add( sb );
+               
+               return ret;
+       }
+       
+       @Override
+       public List<StatementBlock> rewriteStatementBlocks(List<StatementBlock> 
sbs, ProgramRewriteStatus sate) 
+               throws HopsException {
+               return sbs;
+       }
+}

Reply via email to