Search in sources :

Example 16 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class IPAPassPropagateReplaceLiterals method rReplaceLiterals.

private void rReplaceLiterals(StatementBlock sb, LocalVariableMap constants) {
    // remove updated literals
    for (String varname : sb.variablesUpdated().getVariableNames()) if (constants.keySet().contains(varname))
        constants.remove(varname);
    // propagate and replace literals
    if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement ws = (WhileStatement) sb.getStatement(0);
        replaceLiterals(wsb.getPredicateHops(), constants);
        for (StatementBlock current : ws.getBody()) rReplaceLiterals(current, constants);
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement ifs = (IfStatement) sb.getStatement(0);
        replaceLiterals(isb.getPredicateHops(), constants);
        for (StatementBlock current : ifs.getIfBody()) rReplaceLiterals(current, constants);
        for (StatementBlock current : ifs.getElseBody()) rReplaceLiterals(current, constants);
    } else if (sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fs = (ForStatement) sb.getStatement(0);
        replaceLiterals(fsb.getFromHops(), constants);
        replaceLiterals(fsb.getToHops(), constants);
        replaceLiterals(fsb.getIncrementHops(), constants);
        for (StatementBlock current : fs.getBody()) rReplaceLiterals(current, constants);
    } else {
        replaceLiterals(sb.getHops(), constants);
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) IfStatement(org.apache.sysml.parser.IfStatement) WhileStatement(org.apache.sysml.parser.WhileStatement) ForStatement(org.apache.sysml.parser.ForStatement) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 17 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class IPAPassRemoveConstantBinaryOps method rRemoveConstantBinaryOp.

private static void rRemoveConstantBinaryOp(StatementBlock sb, HashMap<String, Hop> mOnes) {
    if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        for (StatementBlock c : istmt.getIfBody()) rRemoveConstantBinaryOp(c, mOnes);
        if (istmt.getElseBody() != null)
            for (StatementBlock c : istmt.getElseBody()) rRemoveConstantBinaryOp(c, mOnes);
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        for (StatementBlock c : wstmt.getBody()) rRemoveConstantBinaryOp(c, mOnes);
    } else if (sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        for (StatementBlock c : fstmt.getBody()) rRemoveConstantBinaryOp(c, mOnes);
    } else {
        if (sb.getHops() != null) {
            Hop.resetVisitStatus(sb.getHops());
            for (Hop hop : sb.getHops()) rRemoveConstantBinaryOp(hop, mOnes);
        }
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) IfStatement(org.apache.sysml.parser.IfStatement) Hop(org.apache.sysml.hops.Hop) WhileStatement(org.apache.sysml.parser.WhileStatement) ForStatement(org.apache.sysml.parser.ForStatement) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 18 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class InterProceduralAnalysis method propagateStatisticsAcrossBlock.

// ///////////////////////////
// INTRA-PROCEDURE ANALYSIS
// ////
private void propagateStatisticsAcrossBlock(StatementBlock sb, LocalVariableMap callVars, FunctionCallSizeInfo fcallSizes, Set<String> fnStack) {
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        // old stats into predicate
        propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, wsb);
        // check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, wsb)) {
            // second pass if required
            propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
            for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        }
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        // old stats into predicate
        propagateStatisticsAcrossPredicateDAG(isb.getPredicateHops(), callVars);
        // check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        LocalVariableMap callVarsElse = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : istmt.getIfBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        for (StatementBlock sbi : istmt.getElseBody()) propagateStatisticsAcrossBlock(sbi, callVarsElse, fcallSizes, fnStack);
        callVars = Recompiler.reconcileUpdatedCallVarsIf(oldCallVars, callVars, callVarsElse, isb);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else if (// incl parfor
    sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        // old stats into predicate
        propagateStatisticsAcrossPredicateDAG(fsb.getFromHops(), callVars);
        propagateStatisticsAcrossPredicateDAG(fsb.getToHops(), callVars);
        propagateStatisticsAcrossPredicateDAG(fsb.getIncrementHops(), callVars);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, fsb);
        // check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, fsb))
            for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else // generic (last-level)
    {
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
        // old stats in, new stats out if updated
        ArrayList<Hop> roots = sb.getHops();
        DMLProgram prog = sb.getDMLProg();
        // replace scalar reads with literals
        Hop.resetVisitStatus(roots);
        propagateScalarsAcrossDAG(roots, callVars);
        // refresh stats across dag
        Hop.resetVisitStatus(roots);
        propagateStatisticsAcrossDAG(roots, callVars);
        // propagate stats into function calls
        Hop.resetVisitStatus(roots);
        propagateStatisticsIntoFunctions(prog, roots, callVars, fcallSizes, fnStack);
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ArrayList(java.util.ArrayList) DMLProgram(org.apache.sysml.parser.DMLProgram) WhileStatement(org.apache.sysml.parser.WhileStatement) ForStatement(org.apache.sysml.parser.ForStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 19 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class RewriteRemoveUnnecessaryBranches method rewriteStatementBlock.

@Override
public List<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus state) {
    ArrayList<StatementBlock> ret = new ArrayList<>();
    if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        Hop pred = isb.getPredicateHops().getInput().get(0);
        // apply rewrite if literal op (constant value)
        if (pred instanceof LiteralOp) {
            IfStatement istmt = (IfStatement) isb.getStatement(0);
            LiteralOp litpred = (LiteralOp) pred;
            boolean condition = HopRewriteUtils.getBooleanValue(litpred);
            if (condition) {
                // pull-out simple if body
                if (!istmt.getIfBody().isEmpty())
                    // pull if-branch
                    ret.addAll(istmt.getIfBody());
            // otherwise: add nothing (remove if-else)
            } else {
                // pull-out simple else body
                if (!istmt.getElseBody().isEmpty())
                    // pull else-branch
                    ret.addAll(istmt.getElseBody());
            // otherwise: add nothing (remove if-else)
            }
            state.setRemovedBranches();
            LOG.debug("Applied removeUnnecessaryBranches (lines " + sb.getBeginLine() + "-" + sb.getEndLine() + ").");
        } else
            // keep original sb (non-constant condition)
            ret.add(sb);
    } else
        // keep original sb (no if)
        ret.add(sb);
    return ret;
}
Also used : IfStatement(org.apache.sysml.parser.IfStatement) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) StatementBlock(org.apache.sysml.parser.StatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 20 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class ProgramRecompiler method isApplicableForReuseVariable.

private static boolean isApplicableForReuseVariable(StatementBlock sb, StatementBlock parforSB, String var) {
    boolean ret = false;
    if (sb instanceof IfStatementBlock) {
        IfStatement is = (IfStatement) sb.getStatement(0);
        for (StatementBlock lsb : is.getIfBody()) ret |= isApplicableForReuseVariable(lsb, parforSB, var);
        for (StatementBlock lsb : is.getElseBody()) ret |= isApplicableForReuseVariable(lsb, parforSB, var);
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatement ws = (WhileStatement) sb.getStatement(0);
        for (StatementBlock lsb : ws.getBody()) ret |= isApplicableForReuseVariable(lsb, parforSB, var);
    } else if (sb instanceof ForStatementBlock) {
        // for or parfor
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fs = (ForStatement) fsb.getStatement(0);
        if (fsb == parforSB) {
            // found parfor statement
            ret = true;
        } else {
            for (StatementBlock lsb : fs.getBody()) ret |= isApplicableForReuseVariable(lsb, parforSB, var);
        }
    }
    return ret && !sb.variablesUpdated().containsVariable(var);
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) IfStatement(org.apache.sysml.parser.IfStatement) WhileStatement(org.apache.sysml.parser.WhileStatement) ForStatement(org.apache.sysml.parser.ForStatement) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Aggregations

IfStatement (org.apache.sysml.parser.IfStatement)44 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)38 StatementBlock (org.apache.sysml.parser.StatementBlock)38 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)36 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)36 ForStatement (org.apache.sysml.parser.ForStatement)33 WhileStatement (org.apache.sysml.parser.WhileStatement)33 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)28 FunctionStatement (org.apache.sysml.parser.FunctionStatement)23 Hop (org.apache.sysml.hops.Hop)16 ArrayList (java.util.ArrayList)13 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)12 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)9 ConditionalPredicate (org.apache.sysml.parser.ConditionalPredicate)6 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)6 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)6 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)6 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)6 DMLProgram (org.apache.sysml.parser.DMLProgram)5 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)5