Skip to content

Commit

Permalink
- remove unused functions Bug [# 1708]
Browse files Browse the repository at this point in the history
- move Modelica.Media.Examples.TestOnly.IdealGasN2.mos 
Modelica.Media.Examples.Tests.MediaTestModels.LinearFluid.LinearColdWater.mos 
Modelica.Media.Examples.TwoPhaseWater.TestTwoPhaseStates.mos 
Modelica.Media.Incompressible.Examples.TestGlycol.mos 
 to working test
- update CMakeLists.txt

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11318 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Mar 6, 2012
1 parent 810dea9 commit 13085f1
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 26 deletions.
1 change: 0 additions & 1 deletion Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -44,7 +44,6 @@ public import DAE;

protected import Algorithm;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import BaseHashTable;
Expand Down
174 changes: 174 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -2973,6 +2973,180 @@ algorithm
end matchcontinue;
end checkUnusedVariablesExp;

/*
* remove unused functions
*/

public function removeUnusedFunctionsPast
"function removeUnusedFunctionsPast"
input BackendDAE.BackendDAE inDAE;
input DAE.FunctionTree inFunctionTree;
output BackendDAE.BackendDAE outDAE;
output DAE.FunctionTree outFunctionTree;
output Boolean outRunMatching;
protected
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
algorithm
(outDAE,outFunctionTree) := removeUnusedFunctions(inDAE,inFunctionTree);
outRunMatching := false;
end removeUnusedFunctionsPast;

public function removeUnusedFunctions
"function: removeUnusedFunctions
autor: Frenkel TUD 2012-03
This function remove unused functions
from DAE.FunctionTree to get speed up for compilation of
target code"
input BackendDAE.BackendDAE inDlow;
input DAE.FunctionTree inFunctionTree;
output BackendDAE.BackendDAE outDlow;
output DAE.FunctionTree outFunctionTree;
algorithm
(outDlow,outFunctionTree) := match (inDlow,inFunctionTree)
local
BackendDAE.BackendDAE dae,dae1;
DAE.FunctionTree funcs,usedfuncs;
BackendDAE.Variables vars,knvars,exobj,avars,knvars1;
BackendDAE.AliasVariables aliasVars;
BackendDAE.EquationArray eqns,remeqns,inieqns;
array<BackendDAE.MultiDimEquation> arreqns;
array<DAE.Algorithm> algorithms;
array<BackendDAE.ComplexEquation> complEqs;
BackendDAE.EventInfo einfo;
list<BackendDAE.WhenClause> whenClauseLst;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.EqSystems eqs;
BackendDAE.BackendDAEType btp;

case (dae as BackendDAE.DAE(eqs,BackendDAE.SHARED(knvars,exobj,aliasVars as BackendDAE.ALIASVARS(aliasVars=avars),inieqns,remeqns,arreqns,algorithms,complEqs,einfo as BackendDAE.EVENT_INFO(whenClauseLst=whenClauseLst),eoc,btp)),funcs)
equation
usedfuncs = copyRecordConstructor(funcs);
((_,usedfuncs)) = List.fold1(eqs,BackendDAEUtil.traverseBackendDAEExpsEqSystem,checkUnusedFunctions,(funcs,usedfuncs));
((_,usedfuncs)) = BackendDAEUtil.traverseBackendDAEExpsVars(knvars,checkUnusedFunctions,(funcs,usedfuncs));
((_,usedfuncs)) = BackendDAEUtil.traverseBackendDAEExpsVars(avars,checkUnusedFunctions,(funcs,usedfuncs));
((_,usedfuncs)) = BackendDAEUtil.traverseBackendDAEExpsEqns(remeqns,checkUnusedFunctions,(funcs,usedfuncs));
((_,usedfuncs)) = BackendDAEUtil.traverseBackendDAEExpsEqns(inieqns,checkUnusedFunctions,(funcs,usedfuncs));
((_,usedfuncs)) = BackendDAEUtil.traverseBackendDAEArrayNoCopy(arreqns,checkUnusedFunctions,BackendDAEUtil.traverseBackendDAEExpsArrayEqn,1,arrayLength(arreqns),(funcs,usedfuncs));
((_,usedfuncs)) = BackendDAEUtil.traverseBackendDAEArrayNoCopy(algorithms,checkUnusedFunctions,BackendDAEUtil.traverseAlgorithmExps,1,arrayLength(algorithms),(funcs,usedfuncs));
((_,usedfuncs)) = BackendDAEUtil.traverseBackendDAEArrayNoCopy(complEqs,checkUnusedFunctions,BackendDAEUtil.traverseBackendDAEExpsComplexEqn,1,arrayLength(complEqs),(funcs,usedfuncs));
(_,(_,usedfuncs)) = BackendDAETransform.traverseBackendDAEExpsWhenClauseLst(whenClauseLst,checkUnusedFunctions,(funcs,usedfuncs));
then (dae,usedfuncs);
end match;
end removeUnusedFunctions;

protected function copyRecordConstructor
input DAE.FunctionTree inFunctions;
output DAE.FunctionTree outFunctions;
protected
list<DAE.Function> funcelems;
algorithm
funcelems := DAEUtil.getFunctionList(inFunctions);
outFunctions := List.fold(funcelems,copyRecordConstructorFold,DAEUtil.emptyFuncTree);
end copyRecordConstructor;

protected function copyRecordConstructorFold
input DAE.Function inFunction;
input DAE.FunctionTree inFunctions;
output DAE.FunctionTree outFunctions;
algorithm
outFunctions :=
matchcontinue (inFunction,inFunctions)
local
DAE.Function f;
DAE.FunctionTree funcs,funcs1;
Absyn.Path path;
case (f as DAE.RECORD_CONSTRUCTOR(path=path),funcs)
equation
funcs1 = DAEUtil.avlTreeAdd(funcs, path, SOME(f));
then
funcs1;
case (f,funcs) then funcs;
end matchcontinue;
end copyRecordConstructorFold;


protected function checkUnusedFunctions
input tuple<DAE.Exp, tuple<DAE.FunctionTree,DAE.FunctionTree>> inTpl;
output tuple<DAE.Exp, tuple<DAE.FunctionTree,DAE.FunctionTree>> outTpl;
algorithm
outTpl :=
matchcontinue inTpl
local
DAE.Exp exp;
DAE.FunctionTree func,usefuncs,usefuncs1;
case ((exp,(func,usefuncs)))
equation
((_,(_,usefuncs1))) = Expression.traverseExp(exp,checkUnusedFunctionsExp,(func,usefuncs));
then
((exp,(func,usefuncs1)));
case inTpl then inTpl;
end matchcontinue;
end checkUnusedFunctions;

protected function checkUnusedFunctionsExp
input tuple<DAE.Exp, tuple<DAE.FunctionTree,DAE.FunctionTree>> inTuple;
output tuple<DAE.Exp, tuple<DAE.FunctionTree,DAE.FunctionTree>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
DAE.Exp e;
DAE.FunctionTree func,usefuncs,usefuncs1,usefuncs2;
Absyn.Path path;
Option<DAE.Function> f;
list<DAE.Element> body;

// already there
case ((e as DAE.CALL(path = path),(func,usefuncs)))
equation
_ = DAEUtil.avlTreeGet(usefuncs, path);
then
((e, (func,usefuncs)));

// add it
case ((e as DAE.CALL(path = path),(func,usefuncs)))
equation
(f,body) = getFunctionAndBody(path,func);
usefuncs1 = DAEUtil.avlTreeAdd(usefuncs, path, f);
// print("add function " +& Absyn.pathStringNoQual(path) +& "\n");
(_,(_,usefuncs2)) = DAEUtil.traverseDAE2(body,checkUnusedFunctions,(func,usefuncs1));
then
((e, (func,usefuncs2)));

case inTuple then inTuple;
end matchcontinue;
end checkUnusedFunctionsExp;

protected function getFunctionAndBody
"function: getFunctionBody
returns the body of a function"
input Absyn.Path inPath;
input DAE.FunctionTree fns;
output Option<DAE.Function> outFn;
output list<DAE.Element> outFnBody;
algorithm
(outFn,outFnBody) := matchcontinue(inPath,fns)
local
Absyn.Path p;
Option<DAE.Function> fn;
list<DAE.Element> body;
DAE.FunctionTree ftree;
String msg;
case(p,ftree)
equation
(fn as SOME(DAE.FUNCTION( functions = DAE.FUNCTION_DEF(body = body)::_))) = DAEUtil.avlTreeGet(ftree,p);
then (fn,body);
case(p,_)
equation
msg = "BackendDAEOptimize.getFunctionBody failed for function " +& Absyn.pathStringNoQual(p);
// print(msg +& "\n");
Debug.fprintln(Flags.FAILTRACE, msg);
// Error.addMessage(Error.INTERNAL_ERROR, {msg});
then
fail();
end matchcontinue;
end getFunctionAndBody;

/*
* constant jacobians. Linear system of equations (A x = b) where
* A and b are constants.
Expand Down
5 changes: 4 additions & 1 deletion Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -6046,6 +6046,7 @@ public function getSolvedSystem
input Option<String> strdaeHandler;
input Option<list<String>> strPastOptModules;
output BackendDAE.BackendDAE outSODE;
output DAE.FunctionTree outFunctionTree;
protected
BackendDAE.BackendDAE dae,optdae,sode,sode1,sode2,optsode,indexed_dlow;
Option<BackendDAE.IncidenceMatrix> om,omT;
Expand Down Expand Up @@ -6074,6 +6075,8 @@ algorithm

// past optimisation phase
(optsode,Util.SUCCESS()) := pastoptimiseDAE(sode,functionTree,pastOptModules,daeHandler);
(_,outFunctionTree) := BackendDAEOptimize.removeUnusedFunctions(optsode, functionTree);
Debug.execStat("pastOpt removeUnusedFunctions",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
sode1 := BackendDAECreate.findZeroCrossings(optsode);
Debug.execStat("findZeroCrossings",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
indexed_dlow := translateDae(sode1,NONE());
Expand All @@ -6084,7 +6087,7 @@ algorithm
Debug.execStat("expandAlgorithmsbyInitStmts",BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
Debug.fcall(Flags.DUMP_INDX_DAE, print, "dumpindxdae:\n");
Debug.fcall(Flags.DUMP_INDX_DAE, BackendDump.dump, outSODE);
Debug.fcall(Flags.DUMP_BACKENDDAE_INFO, BackendDump.dumpCompShort, outSODE);
Debug.fcall(Flags.DUMP_BACKENDDAE_INFO, BackendDump.dumpCompShort, outSODE);
end getSolvedSystem;

public function preOptimiseBackendDAE
Expand Down
19 changes: 9 additions & 10 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -1009,7 +1009,7 @@ algorithm
Absyn.Program p,ptot;
//DAE.Exp fileprefix;
Env.Cache cache;
DAE.FunctionTree funcs;
DAE.FunctionTree funcs,funcs1;
Real timeSimCode, timeTemplates, timeBackend, timeFrontend;
case (cache,env,className,(st as Interactive.SYMBOLTABLE(ast = p)),filenameprefix,addDummy, inSimSettingsOpt)
equation
Expand All @@ -1025,11 +1025,11 @@ algorithm
dae = DAEUtil.transformationsBeforeBackend(cache,dae);
funcs = Env.getFunctionTree(cache);
dlow = BackendDAECreate.lower(dae,funcs,true);
dlow_1 = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,
(dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,
NONE(), NONE(), NONE());
Debug.fprintln(Flags.DYN_LOAD, "translateModel: Generating simulation code and functions.");
(indexed_dlow_1,libs,file_dir,timeBackend,timeSimCode,timeTemplates) =
generateModelCodeFMU(dlow_1,funcs,p, dae, className, filenameprefix, inSimSettingsOpt);
generateModelCodeFMU(dlow_1,funcs1,p, dae, className, filenameprefix, inSimSettingsOpt);
resultValues =
{("timeTemplates",Values.REAL(timeTemplates)),
("timeSimCode", Values.REAL(timeSimCode)),
Expand Down Expand Up @@ -1227,7 +1227,7 @@ algorithm
Absyn.Program p,ptot;
//DAE.Exp fileprefix;
Env.Cache cache;
DAE.FunctionTree funcs;
DAE.FunctionTree funcs,funcs1;
Real timeSimCode, timeTemplates, timeBackend, timeFrontend, timeJacobians;

case (cache,env,className,(st as Interactive.SYMBOLTABLE(ast = p)),filenameprefix,addDummy, inSimSettingsOpt,args)
Expand All @@ -1243,10 +1243,10 @@ algorithm
dae = DAEUtil.transformationsBeforeBackend(cache,dae);
funcs = Env.getFunctionTree(cache);
dlow = BackendDAECreate.lower(dae,funcs,true);
dlow_1 = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,
(dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,
NONE(), NONE(), NONE());
(indexed_dlow_1,libs,file_dir,timeBackend,timeSimCode,timeTemplates, timeJacobians) =
generateModelCode(dlow_1,funcs,p, dae, className, filenameprefix, inSimSettingsOpt, args);
generateModelCode(dlow_1,funcs1,p, dae, className, filenameprefix, inSimSettingsOpt, args);
resultValues =
{("timeTemplates",Values.REAL(timeTemplates)),
("timeSimCode", Values.REAL(timeSimCode)),
Expand Down Expand Up @@ -1672,16 +1672,14 @@ algorithm
equation
// get all the used functions from the function tree
funcelems = DAEUtil.getFunctionList(functionTree);

part_func_elems = PartFn.createPartEvalFunctions(funcelems);
(dae, part_func_elems) = PartFn.partEvalDAE(dae, part_func_elems);
(dlow, part_func_elems) = BackendDAEUtil.mapEqSystemAndFold1(dlow, PartFn.partEvalBackendDAE, true /*dummy*/, part_func_elems);
funcelems = List.union(part_func_elems, part_func_elems);
funcelems = part_func_elems; //List.union(part_func_elems, part_func_elems);
//funcelems = List.union(funcelems, part_func_elems);
funcelems = Inline.inlineCallsInFunctions(funcelems,(NONE(),{DAE.NORM_INLINE(), DAE.AFTER_INDEX_RED_INLINE()}));
//Debug.fprintln(Flags.INFO, "Generating functions, call Codegen.\n") "debug" ;
(funcelems,literals as (_,_,lits)) = simulationFindLiterals(dlow,funcelems);

(fns, recordDecls, includes2, includeDirs2, libs2) = elaborateFunctions(funcelems, {}, lits, {}); // Do we need metarecords here as well?
then
(libs2, includes2, includeDirs2, recordDecls, fns, dlow, dae, literals);
Expand Down Expand Up @@ -1762,10 +1760,11 @@ algorithm
list<RecordDeclaration> decls;
String name;
list<String> includeDirs;
Absyn.Path path;

case ({}, accfns, rt, decls, includes, includeDirs, libs)
then (listReverse(accfns), rt, decls, includes, includeDirs, libs);
case ((DAE.FUNCTION(type_ = DAE.T_FUNCTION(functionAttributes=DAE.FUNCTION_ATTRIBUTES(isBuiltin=DAE.FUNCTION_BUILTIN_PTR()))) :: rest), accfns, rt, decls, includes, includeDirs, libs)
case ((DAE.FUNCTION(path=path,type_ = DAE.T_FUNCTION(functionAttributes=DAE.FUNCTION_ATTRIBUTES(isBuiltin=DAE.FUNCTION_BUILTIN_PTR()))) :: rest), accfns, rt, decls, includes, includeDirs, libs)
equation
// skip over builtin functions
(fns, rt_2, decls, includes, includeDirs, libs) = elaborateFunctions2(rest, accfns, rt, decls, includes, includeDirs, libs);
Expand Down
4 changes: 2 additions & 2 deletions Compiler/BackEnd/Uncertainties.mo
Expand Up @@ -107,7 +107,7 @@ algorithm
Absyn.Program p,ptot;
//DAE.Exp fileprefix;
Env.Cache cache;
DAE.FunctionTree funcs;
DAE.FunctionTree funcs,funcs1;
Real timeSimCode, timeTemplates, timeBackend, timeFrontend;
BackendDAE.IncidenceMatrix m,mt;
array<Integer> ass1,ass2;
Expand Down Expand Up @@ -143,7 +143,7 @@ algorithm
print("* Lower ok\n");


dlow_1 = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,SOME({"removeFinalParameters", "removeEqualFunctionCalls","partitionIndependentBlocks", "expandDerOperator"}), NONE(), NONE());
(dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs,SOME({"removeFinalParameters", "removeEqualFunctionCalls","partitionIndependentBlocks", "expandDerOperator"}), NONE(), NONE());
print("*** Lowered: \n");
BackendDump.dump(dlow_1);
print("*** end Lowered: \n");
Expand Down
6 changes: 3 additions & 3 deletions Compiler/Main/Main.mo
Expand Up @@ -766,18 +766,18 @@ algorithm
Absyn.Path classname;
Env.Cache cache;
Env.Env env;
DAE.FunctionTree funcs;
DAE.FunctionTree funcs,funcs_1;
String str;

case (cache,env,p,ap,dae,daeimpl,classname)
equation
true = runBackendQ();
funcs = Env.getFunctionTree(cache);
dlow = BackendDAECreate.lower(dae,funcs,true);
dlow_1 = BackendDAEUtil.getSolvedSystem(cache,env,dlow,funcs,NONE(),NONE(),NONE());
(dlow_1,funcs_1) = BackendDAEUtil.getSolvedSystem(cache,env,dlow,funcs,NONE(),NONE(),NONE());
modpar(dlow_1);
Debug.execStat("Lowering Done",CevalScript.RT_CLOCK_EXECSTAT_MAIN);
simcodegen(dlow_1,funcs,classname,ap,daeimpl);
simcodegen(dlow_1,funcs_1,classname,ap,daeimpl);
then
();
else
Expand Down
6 changes: 3 additions & 3 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -3810,7 +3810,7 @@ algorithm
Option<BackendDAE.IncidenceMatrix> om,omT;
list<SCode.Element> p_1,sp;
list<list<Integer>> comps;
DAE.FunctionTree funcs;
DAE.FunctionTree funcs,funcs1;

case (cache,env,{Values.CODE(Absyn.C_TYPENAME(classname)),Values.STRING(string="flat"),Values.BOOL(addOriginalIncidenceMatrix),Values.BOOL(addSolvingInfo),Values.BOOL(addMathMLCode),Values.BOOL(dumpResiduals),Values.STRING(filenameprefix),Values.BOOL(cdToTemp)},(st as Interactive.SYMBOLTABLE(ast = p)),msg)
equation
Expand Down Expand Up @@ -3878,9 +3878,9 @@ algorithm
dae = DAEUtil.transformationsBeforeBackend(cache,dae_1);
funcs = Env.getFunctionTree(cache);
dlow = BackendDAECreate.lower(dae, funcs, true);
indexed_dlow = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs, NONE(), NONE(), NONE());
(indexed_dlow,funcs1) = BackendDAEUtil.getSolvedSystem(cache, env, dlow, funcs, NONE(), NONE(), NONE());
xml_filename = stringAppendList({filenameprefix,".xml"});
funcelems = DAEUtil.getFunctionList(funcs);
funcelems = DAEUtil.getFunctionList(funcs1);
Print.clearBuf();
XMLDump.dumpBackendDAE(indexed_dlow,funcelems,addOriginalIncidenceMatrix,addSolvingInfo,addMathMLCode,dumpResiduals);
xml_contents = Print.getString();
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/c/CMakeLists.txt
Expand Up @@ -245,7 +245,7 @@ MACRO(BUILDMODELFMU model dir Flags CSRC)
${OMCTRUNCHOME}/c_runtime/fmiModelTypes.h)

ADD_LIBRARY(${model} SHARED ${OMC_OUTPUT} ${CSRC} ${OMC_FMU_CODE})
TARGET_LINK_LIBRARIES(${model} sim c_runtime f2c meta ModelicaExternalC libexpat)
TARGET_LINK_LIBRARIES(${model} simulation util math-support results solver f2c meta ModelicaExternalC libexpat)

# Dependencies
ADD_DEPENDENCIES(${model} ${model}codegen)
Expand Down Expand Up @@ -308,7 +308,7 @@ MACRO(BUILDMODELFMUMOS model mos Flags CSRC)

ADD_DEFINITIONS(/TP ${model}.c)
ADD_LIBRARY(${model} SHARED ${OMC_OUTPUT} ${CSRC} ${OMC_FMU_CODE})
TARGET_LINK_LIBRARIES(${model} sim c_runtime f2c meta ModelicaExternalC libexpat)
TARGET_LINK_LIBRARIES(${model} simulation util math-support results solver f2c meta ModelicaExternalC libexpat)

# Dependencies
ADD_DEPENDENCIES(${model} ${model}codegen)
Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/c/simulation/results/CMakeLists.txt
Expand Up @@ -2,10 +2,10 @@
# CMakefile for compilation of OMC

# Quellen und Header
SET(results_sources read_csv.cpp read_matlab4.c simulation_result_csv.cpp
SET(results_sources read_csv.cpp simulation_result_csv.cpp
simulation_result_mat.cpp simulation_result_plt.cpp )

SET(results_headers read_csv.h read_matlab4.h simulation_result_csv.h
SET(results_headers read_csv.h simulation_result_csv.h
simulation_result_mat.h simulation_result_plt.h
simulation_result.h simulation_result_empty.h)

Expand Down
4 changes: 2 additions & 2 deletions SimulationRuntime/c/util/CMakeLists.txt
Expand Up @@ -2,13 +2,13 @@
SET(util_sources base_array.c boolean_array.c division.c error.c index_spec.c
integer_array.c java_interface.c list.c memory_pool.c modelica_string.c
modelinfo.c read_write.c real_array.c ringbuffer.c rtclock.c
string_array.c utility.c varinfo.c)
string_array.c utility.c varinfo.c read_matlab4.c)

SET(util_headers base_array.h boolean_array.h division.h index_spec.h error.h
index_spec.h integer_array.h java_interface.h jni.h jni_md.h
jni_md_solaris.h jni_md_windows.h list.h memory_pool.h
modelica.h modelica_string.h modelinfo.h read_write.h real_array.h
rtclock.h string_array.h utility.h varinfo.h)
rtclock.h string_array.h utility.h varinfo.h read_matlab4.h)

# Library util
ADD_LIBRARY(util ${util_sources} ${util_headers})
Expand Down

0 comments on commit 13085f1

Please sign in to comment.