Skip to content

Commit

Permalink
remove unused functions as a last step of backend
Browse files Browse the repository at this point in the history
  • Loading branch information
wibraun authored and OpenModelica-Hudson committed May 23, 2017
1 parent f821aa3 commit ddfec0e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 25 deletions.
49 changes: 29 additions & 20 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -1358,8 +1358,12 @@ end checkUnusedVariablesExp;
public function removeUnusedFunctions "author: Frenkel TUD 2012-03
This function remove unused functions from DAE.FunctionTree to get speed up
for compilation of target code."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
input BackendDAE.EqSystems inEqs;
input BackendDAE.Shared inShared;
input list<BackendDAE.Equation> inEquationLst;
input DAE.FunctionTree inFunctionTree;
input DAE.FunctionTree inusedFunctions;
output DAE.FunctionTree outFunctionTree;
protected
partial function FuncType
input DAE.Exp inExp;
Expand All @@ -1369,27 +1373,31 @@ protected
end FuncType;

FuncType func;
BackendDAE.EqSystems eqs;
BackendDAE.Shared shared;
DAE.FunctionTree funcs, usedfuncs;
algorithm
BackendDAE.DAE(eqs, shared) := inDAE;
funcs := shared.functionTree;
usedfuncs := copyRecordConstructorAndExternalObjConstructorDestructor(funcs);
funcs := inFunctionTree;
usedfuncs := inusedFunctions;
func := function checkUnusedFunctions(inFunctions = funcs);
usedfuncs := List.fold1(eqs, BackendDAEUtil.traverseBackendDAEExpsEqSystem, func, usedfuncs);
usedfuncs := List.fold1(eqs, BackendDAEUtil.traverseBackendDAEExpsEqSystemJacobians, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsVars(shared.globalKnownVars, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsVars(shared.externalObjects, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsVars(shared.aliasVars, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsEqns(shared.removedEqs, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsEqns(shared.initialEqs, func, usedfuncs);
usedfuncs := removeUnusedFunctionsSymJacs(shared.symjacs, funcs, usedfuncs);
shared.functionTree := usedfuncs;
outDAE := BackendDAE.DAE(eqs, shared);

// equation system
usedfuncs := List.fold1(inEqs, BackendDAEUtil.traverseBackendDAEExpsEqSystem, func, usedfuncs);
usedfuncs := List.fold1(inEqs, BackendDAEUtil.traverseBackendDAEExpsEqSystemJacobians, func, usedfuncs);

// equation list
usedfuncs := List.fold1(inEquationLst, BackendEquation.traverseExpsOfEquationList_WithoutChange, func, usedfuncs);

// shared object
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsVars(inShared.globalKnownVars, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsVars(inShared.externalObjects, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsVars(inShared.aliasVars, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsEqns(inShared.removedEqs, func, usedfuncs);
usedfuncs := BackendDAEUtil.traverseBackendDAEExpsEqns(inShared.initialEqs, func, usedfuncs);
usedfuncs := removeUnusedFunctionsSymJacs(inShared.symjacs, funcs, usedfuncs);

outFunctionTree := usedfuncs;
end removeUnusedFunctions;

protected function copyRecordConstructorAndExternalObjConstructorDestructor
public function copyRecordConstructorAndExternalObjConstructorDestructor
input DAE.FunctionTree inFunctions;
output DAE.FunctionTree outFunctions;
protected
Expand Down Expand Up @@ -1440,12 +1448,13 @@ algorithm
local
BackendDAE.BackendDAE bdae;
DAE.FunctionTree usedfuncs;
BackendDAE.Shared shared;

case (SOME((bdae, _, _, _, _)), _, _)
equation
bdae = BackendDAEUtil.setFunctionTree(bdae, inFunctions);
BackendDAE.DAE(shared = BackendDAE.SHARED(functionTree = usedfuncs)) =
removeUnusedFunctions(bdae);
shared = bdae.shared;
usedfuncs = removeUnusedFunctions(bdae.eqs, shared, {}, shared.functionTree, inUsedFunctions);
outUsedFunctions = DAE.AvlTreePathFunction.join(outUsedFunctions, usedfuncs);
then
();
Expand Down
16 changes: 11 additions & 5 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -6831,6 +6831,7 @@ protected
BackendDAE.InlineData inlineData;
BackendDAE.Variables globalKnownVars;
Integer numCheckpoints;
DAE.FunctionTree funcTree;
algorithm
numCheckpoints:=ErrorExt.getNumCheckpoints();
try
Expand Down Expand Up @@ -6861,9 +6862,6 @@ algorithm
BackendDump.dumpBackendDAE(dae, "synchronousFeatures");
end if;

dae := BackendDAEOptimize.removeUnusedFunctions(dae);
execStat("remove unused functions");

if Flags.isSet(Flags.GRAPHML) then
BackendDump.dumpBipartiteGraphDAE(dae, fileNamePrefix);
end if;
Expand Down Expand Up @@ -6893,10 +6891,18 @@ algorithm
outInlineData := SymbolicImplicitSolver.symSolver(simDAE);

// post-optimization phase
outSimDAE := postOptimizeDAE(simDAE, postOptModules, matchingAlgorithm, daeHandler);
simDAE := postOptimizeDAE(simDAE, postOptModules, matchingAlgorithm, daeHandler);

// sort the globalKnownVars
outSimDAE := sortGlobalKnownVarsInDAE(outSimDAE);
simDAE := sortGlobalKnownVarsInDAE(simDAE);
execStat("sort global known variables");

// remove unused functions
funcTree := BackendDAEOptimize.copyRecordConstructorAndExternalObjConstructorDestructor(BackendDAEUtil.getFunctions(simDAE.shared));
funcTree := BackendDAEOptimize.removeUnusedFunctions(outInitDAE.eqs, outInitDAE.shared, outRemovedInitialEquationLst, BackendDAEUtil.getFunctions(simDAE.shared), funcTree);
funcTree := BackendDAEOptimize.removeUnusedFunctions(simDAE.eqs, simDAE.shared, {}, BackendDAEUtil.getFunctions(simDAE.shared), funcTree);
outSimDAE := BackendDAEUtil.setFunctionTree(simDAE, funcTree);
execStat("remove unused functions");

if Flags.isSet(Flags.DUMP_INDX_DAE) then
BackendDump.dumpBackendDAE(outSimDAE, "dumpindxdae");
Expand Down
16 changes: 16 additions & 0 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -713,6 +713,22 @@ algorithm
end for;
end traverseExpsOfEquationList_WithStop;

public function traverseExpsOfEquationList_WithoutChange<ArgT> "
Traverse all expressions of a list of Equations"
input BackendDAE.Equation inEquation;
input FuncExpType func;
input ArgT inArg;
output ArgT outArg = inArg;

partial function FuncExpType
input output DAE.Exp inoutExp;
input output ArgT inoutArg;
end FuncExpType;
algorithm
(_, outArg) := traverseExpsOfEquation(inEquation, func, outArg);
end traverseExpsOfEquationList_WithoutChange;


protected function traverseExpsOfEquationListList_WithStop<T> "author: Frenkel TUD 2012-09
Traverses all expressions of a list of equations.
It is possible to change the equations."
Expand Down

0 comments on commit ddfec0e

Please sign in to comment.