Skip to content

Commit

Permalink
- change Inline.mo: - tailrecursive, generate less new objects
Browse files Browse the repository at this point in the history
- call lateInline only once


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12747 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Aug 31, 2012
1 parent e4804f6 commit 95c0350
Show file tree
Hide file tree
Showing 4 changed files with 667 additions and 355 deletions.
64 changes: 32 additions & 32 deletions Compiler/BackEnd/BackendDAECreate.mo
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ algorithm
true = isStateOrAlgvar(inElement);
(backendVar1,states) = lowerDynamicVar(inElement, inStates);
backendVar2 = Inline.inlineVar(backendVar1,(SOME(functionTree),{DAE.NORM_INLINE()}));
(e2,source) = Inline.inlineExp(e2,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
(e2,source,_) = Inline.inlineExp(e2,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
vars = BackendVariable.addVar(backendVar2, inVars);
e1 = Expression.crefExp(cr);
then
Expand Down Expand Up @@ -937,15 +937,15 @@ algorithm

case (DAE.EQUATION(exp = e1,scalar = e2,source = source),_,_)
equation
(e1,source) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
BackendDAE.EQUATION(e1,e2,source)::inEqns;

case (DAE.INITIALEQUATION(exp1 = e1,exp2 = e2,source = source),_,_)
equation
(e1,source) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
BackendDAE.EQUATION(e1,e2,source)::inEqns;

Expand All @@ -958,54 +958,54 @@ algorithm

case (DAE.DEFINE(componentRef = cr1, exp = e2, source = source),_,_)
equation
(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
e1 = Expression.crefExp(cr1);
then
BackendDAE.EQUATION(e1,e2,source)::inEqns;

case (DAE.INITIALDEFINE(componentRef = cr1, exp = e2, source = source),_,_)
equation
e1 = Expression.crefExp(cr1);
(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
BackendDAE.EQUATION(e1,e2,source)::inEqns;

case (DAE.COMPLEX_EQUATION(lhs = e1,rhs = e2,source = source),_,_)
equation
//(e1,source) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
//(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
//(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
//(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e1,source,_) = Inline.forceInlineExp(e1,(SOME(functionTree),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),source);
(e2,source,_) = Inline.forceInlineExp(e2,(SOME(functionTree),{DAE.NORM_INLINE(),DAE.NO_INLINE()}),source);
then
lowerextendedRecordEqn(e1,e2,source,functionTree,inEqns);

case (DAE.INITIAL_COMPLEX_EQUATION(lhs = e1,rhs = e2,source = source),_,_)
equation
(e1,source) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
size = Expression.sizeOf(Expression.typeof(e1));
then
BackendDAE.COMPLEX_EQUATION(size,e1,e2,source)::inEqns;

case (DAE.ARRAY_EQUATION(dimension=dims,exp = e1,array = e2,source = source),_,_)
equation
(e1,source) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
lowerArrayEqn(dims,e1,e2,source,inEqns);

case (DAE.INITIAL_ARRAY_EQUATION(dimension=dims,exp = e1,array = e2,source = source),_,_)
equation
(e1,source) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e1,source,_) = Inline.inlineExp(e1, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e2,source,_) = Inline.inlineExp(e2, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
then
lowerArrayEqn(dims,e1,e2,source,inEqns);

case (DAE.IF_EQUATION(condition1=explst,equations2=eqnslstlst,equations3=eqnslst,source = source),_,_)
equation
b1 = not Flags.getConfigBool(Flags.CHECK_MODEL);
s = Debug.bcallret1(b1, DAEDump.dumpElementsStr, {inElement}, "");
(explst,source) = Inline.inlineExps(explst, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(explst,source,_) = Inline.inlineExps(explst, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(explst1,blst) = ExpressionSimplify.simplifyList1(explst,{},{});
source = DAEUtil.addSymbolicTransformationSimplifyLst(blst,source,explst,explst1);
then
Expand All @@ -1015,15 +1015,15 @@ algorithm
equation
b1 = not Flags.getConfigBool(Flags.CHECK_MODEL);
s = Debug.bcallret1(b1, DAEDump.dumpElementsStr, {inElement}, "");
(explst,source) = Inline.inlineExps(explst, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(explst,source,_) = Inline.inlineExps(explst, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(explst1,blst) = ExpressionSimplify.simplifyList1(explst,{},{});
source = DAEUtil.addSymbolicTransformationSimplifyLst(blst,source,explst,explst1);
then
lowerIfEquation(explst1,eqnslstlst,eqnslst,{},{},source,functionTree,inEqns);

case (DAE.ASSERT(condition=cond,message=msg,source=source),_,_)
equation
(cond,source) = Inline.inlineExp(cond,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
(cond,source,_) = Inline.inlineExp(cond,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
BackendDAEUtil.checkAssertCondition(cond,msg);
alg = DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond,msg,source)});
then
Expand Down Expand Up @@ -1302,7 +1302,7 @@ algorithm

case (DAE.WHEN_EQUATION(condition = cond,equations = eqnl,elsewhen_ = NONE(),source=source),_,_,_)
equation
(cond,source) = Inline.inlineExp(cond, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(cond,source,_) = Inline.inlineExp(cond, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(res,reinit) = lowerWhenEqn2(listReverse(eqnl), cond, functionTree, inEquationLst, {});
whenClauseList = makeWhenClauses(listLength(reinit) > 0, cond, reinit,inWhenClauseLst);
then
Expand All @@ -1311,7 +1311,7 @@ algorithm
case (DAE.WHEN_EQUATION(condition = cond,equations = eqnl,elsewhen_ = SOME(elsePart),source=source),_,_,_)
equation
(elseEqnLst,whenClauseList) = lowerWhenEqn(elsePart,functionTree,{},inWhenClauseLst);
(cond,source) = Inline.inlineExp(cond, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(cond,source,_) = Inline.inlineExp(cond, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(trueEqnLst,reinit) = lowerWhenEqn2(listReverse(eqnl), cond, functionTree, {}, {});
whenClauseList = makeWhenClauses(listLength(reinit) > 0, cond, reinit,whenClauseList);
res = mergeClauses(trueEqnLst,elseEqnLst,inEquationLst);
Expand Down Expand Up @@ -1359,22 +1359,22 @@ algorithm
case ({},_,_,_,_) then (iEquationLst,iReinitStatementLst);
case (DAE.EQUATION(exp = (cre as DAE.CREF(componentRef = cr)),scalar = e, source = source) :: xs,_,_,_,_)
equation
(e,source) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e,source,_) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(eqnl,reinit) = lowerWhenEqn2(xs,inCond, functionTree, BackendDAE.WHEN_EQUATION(1,BackendDAE.WHEN_EQ(inCond,cr,e,NONE()),source) :: iEquationLst, iReinitStatementLst);
then
(eqnl,reinit);

case (DAE.COMPLEX_EQUATION(lhs = (cre as DAE.CREF(componentRef = cr)),rhs = e,source = source) :: xs,_,_,_,_)
equation
size = Expression.sizeOf(Expression.typeof(cre));
(e,source) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e,source,_) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(eqnl,reinit) = lowerWhenEqn2(xs,inCond, functionTree, BackendDAE.WHEN_EQUATION(size,BackendDAE.WHEN_EQ(inCond,cr,e,NONE()),source) :: iEquationLst, iReinitStatementLst);
then
(eqnl,reinit);

case ((el as DAE.IF_EQUATION(condition1=expl,equations2=eqnslst,equations3=eqns, source = source)) :: xs,_,_,_,_)
equation
(expl,source) = Inline.inlineExps(expl, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(expl,source,_) = Inline.inlineExps(expl, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
b = not Flags.getConfigBool(Flags.CHECK_MODEL);
s = Debug.bcallret1(b, DAEDump.dumpElementsStr, {el}, "");
Debug.bcall3(b,Error.addSourceMessage,Error.IF_EQUATION_WARNING, {s}, DAEUtil.getElementSourceFileInfo(source));
Expand All @@ -1387,29 +1387,29 @@ algorithm
case (DAE.ARRAY_EQUATION(dimension=ds,exp = (cre as DAE.CREF(componentRef = cr)),array = e,source = source) :: xs,_,_,_,_)
equation
size = List.fold(Expression.dimensionsSizes(ds),intMul,1);
(e,source) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e,source,_) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(eqnl,reinit) = lowerWhenEqn2(xs,inCond, functionTree, BackendDAE.WHEN_EQUATION(size,BackendDAE.WHEN_EQ(inCond,cr,e,NONE()),source) :: iEquationLst, iReinitStatementLst);
then
(eqnl,reinit);

case (DAE.ASSERT(condition=cond,message = e,source = source) :: xs,_,_,_,_)
equation
(cond,source) = Inline.inlineExp(cond, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e,source) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(cond,source,_) = Inline.inlineExp(cond, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e,source,_) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(eqnl,reinit) = lowerWhenEqn2(xs,inCond, functionTree, iEquationLst, BackendDAE.ASSERT(cond,e,source) :: iReinitStatementLst);
then
(eqnl,reinit);

case (DAE.REINIT(componentRef = cr,exp = e,source = source) :: xs,_,_,_,_)
equation
(e,source) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e,source,_) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(eqnl,reinit) = lowerWhenEqn2(xs,inCond, functionTree, iEquationLst, BackendDAE.REINIT(cr,e,source) :: iReinitStatementLst);
then
(eqnl,reinit);

case (DAE.TERMINATE(message = e,source = source) :: xs,_,_,_,_)
equation
(e,source) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(e,source,_) = Inline.inlineExp(e, (SOME(functionTree),{DAE.NORM_INLINE()}), source);
(eqnl,reinit) = lowerWhenEqn2(xs,inCond, functionTree, iEquationLst, BackendDAE.TERMINATE(e,source) :: iReinitStatementLst);
then
(eqnl,reinit);
Expand Down Expand Up @@ -1516,7 +1516,7 @@ algorithm
case (DAE.ALGORITHM(algorithm_=alg, source=source),_,_,_,_)
equation
// calculate the size of the algorithm by collecting the left hand sites of the statemens
alg = Inline.inlineAlgorithm(alg,(SOME(functionTree),{DAE.NORM_INLINE()}));
(alg,_) = Inline.inlineAlgorithm(alg,(SOME(functionTree),{DAE.NORM_INLINE()}));
crefLst = CheckModel.algorithmOutputs(alg);
size = listLength(crefLst);
(eqns,reqns) = List.consOnBool(intGt(size,0), BackendDAE.ALGORITHM(size, alg, source),inEquations,inREquations);
Expand All @@ -1526,15 +1526,15 @@ algorithm
case (DAE.INITIALALGORITHM(algorithm_=alg, source=source),_,_,_,_)
equation
// calculate the size of the algorithm by collecting the left hand sites of the statemens
alg = Inline.inlineAlgorithm(alg,(SOME(functionTree),{DAE.NORM_INLINE()}));
(alg,_) = Inline.inlineAlgorithm(alg,(SOME(functionTree),{DAE.NORM_INLINE()}));
crefLst = CheckModel.algorithmOutputs(alg);
size = listLength(crefLst);
then
(inEquations,inREquations,BackendDAE.ALGORITHM(size, alg, source)::inIEquations);

case (DAE.ASSERT(condition=cond,message=msg,source=source),_,_,_,_)
equation
(cond,source) = Inline.inlineExp(cond,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
(cond,source,_) = Inline.inlineExp(cond,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
BackendDAEUtil.checkAssertCondition(cond,msg);
alg = DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond,msg,source)});
then
Expand All @@ -1551,7 +1551,7 @@ algorithm
// constrain is fine when we do check model!
b2 = Flags.getConfigBool(Flags.CHECK_MODEL);
true = boolOr(b1, b2);
(functionArgs,source) = Inline.inlineExps(functionArgs,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
(functionArgs,source,_) = Inline.inlineExps(functionArgs,(SOME(functionTree),{DAE.NORM_INLINE()}),source);
alg = DAE.ALGORITHM_STMTS({DAE.STMT_NORETCALL(DAE.CALL(functionName,functionArgs,DAE.CALL_ATTR(DAE.T_NORETCALL_DEFAULT, false, false, DAE.NORM_INLINE(), DAE.NO_TAIL())),source)});
then
(inEquations,BackendDAE.ALGORITHM(0,alg,source)::inREquations,inIEquations);
Expand Down
11 changes: 6 additions & 5 deletions Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -8167,7 +8167,7 @@ algorithm
(optdae,Util.SUCCESS()) := preoptimiseDAE(inDAE,preOptModules);

// transformation phase (matching and sorting using a index reduction method
sode := reduceIndexDAE(optdae,NONE(),matchingAlgorithm,daeHandler);
sode := reduceIndexDAE(optdae,NONE(),matchingAlgorithm,daeHandler,true);
Debug.fcall(Flags.BLT_DUMP, BackendDump.bltdump, ("bltdump",sode));

// past optimisation phase
Expand Down Expand Up @@ -8252,7 +8252,7 @@ protected
algorithm
matchingAlgorithm := getMatchingAlgorithm(strmatchingAlgorithm);
indexReductionMethod := getIndexReductionMethod(strindexReductionMethod);
outDAE := reduceIndexDAE(inDAE,inMatchingOptions,matchingAlgorithm,indexReductionMethod);
outDAE := reduceIndexDAE(inDAE,inMatchingOptions,matchingAlgorithm,indexReductionMethod,true);
end transformBackendDAE;

protected function reduceIndexDAE
Expand All @@ -8264,6 +8264,7 @@ protected function reduceIndexDAE
input Option<BackendDAE.MatchingOptions> inMatchingOptions;
input tuple<matchingAlgorithmFunc,String> matchingAlgorithm;
input tuple<StructurallySingularSystemHandlerFunc,String,stateDeselectionFunc,String> stateDeselection;
input Boolean dolateinline;
output BackendDAE.BackendDAE outDAE;
protected
list<BackendDAE.EqSystem> systs;
Expand All @@ -8274,7 +8275,7 @@ algorithm
// reduce index
(systs,shared,args) := mapReduceIndexDAE(systs,shared,inMatchingOptions,matchingAlgorithm,stateDeselection,{},{});
// do late inline
BackendDAE.DAE(systs,shared) := BackendDAEOptimize.lateInlineFunction(BackendDAE.DAE(systs,shared));
BackendDAE.DAE(systs,shared) := Debug.bcallret1(dolateinline,BackendDAEOptimize.lateInlineFunction,BackendDAE.DAE(systs,shared),BackendDAE.DAE(systs,shared));
// do state selection
(systs,shared) := mapStateSelectionDAE(systs,shared,args,stateDeselection,{});
outDAE := BackendDAE.DAE(systs,shared);
Expand Down Expand Up @@ -8453,10 +8454,10 @@ algorithm
case (_,(optModule,moduleStr,_)::rest,_,_)
equation
dae = optModule(inDAE);
dae1 = reduceIndexDAE(dae,NONE(),matchingAlgorithm,daeHandler);
Debug.execStat("pastOpt " +& moduleStr,BackendDAE.RT_CLOCK_EXECSTAT_BACKEND_MODULES);
Debug.fcall(Flags.OPT_DAE_DUMP, print, stringAppendList({"\nOptimisation Module ",moduleStr,":\n\n"}));
Debug.fcall(Flags.OPT_DAE_DUMP, BackendDump.dump, dae);
dae1 = reduceIndexDAE(dae,NONE(),matchingAlgorithm,daeHandler,false);
(dae2,status) = pastoptimiseDAE(dae1,rest,matchingAlgorithm,daeHandler);
then
(dae2,status);
Expand Down Expand Up @@ -8559,7 +8560,7 @@ algorithm
(optdae,Util.SUCCESS()) := preoptimiseDAE(inDAE,preOptModules);

// transformation phase (matching and sorting using a index reduction method
sode := reduceIndexDAE(optdae,NONE(),matchingAlgorithm,daeHandler);
sode := reduceIndexDAE(optdae,NONE(),matchingAlgorithm,daeHandler,true);
Debug.fcall(Flags.DUMP_DAE_LOW, BackendDump.bltdump, ("bltdump",sode));

// past optimisation phase
Expand Down
27 changes: 26 additions & 1 deletion Compiler/BackEnd/IndexReduction.mo
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ algorithm
case (syst as BackendDAE.EQSYSTEM(orderedVars=v,matching=BackendDAE.MATCHING(ass1=ass1,ass2=ass2)),BackendDAE.SHARED(functionTree=funcs),(so,orgEqnsLst,_,_))
equation
// do late Inline also in orgeqnslst
orgEqnsLst = traverseOrgEqns(orgEqnsLst,(SOME(funcs),{DAE.NORM_INLINE(),DAE.AFTER_INDEX_RED_INLINE()}),Inline.inlineEq,{});
orgEqnsLst = inlineOrgEqns(orgEqnsLst,(SOME(funcs),{DAE.NORM_INLINE(),DAE.AFTER_INDEX_RED_INLINE()}),{});
// replace all der(x) with dx
(orgEqnsLst,_) = traverseOrgEqnsExp(orgEqnsLst,so,replaceDerStatesStates,{});
Debug.fcall(Flags.BLT_DUMP, print, "Dynamic State Selection\n");
Expand Down Expand Up @@ -882,6 +882,31 @@ algorithm
end match;
end countOrgEqns;

protected function inlineOrgEqns
"function: inlineOrgEqns
author: Frenkel TUD 2012-08
add an equation to the ConstrainEquations."
input BackendDAE.ConstraintEquations inOrgEqns;
input Inline.Functiontuple inA;
input BackendDAE.ConstraintEquations inAcc;
output BackendDAE.ConstraintEquations outOrgEqns;
replaceable type Type_a subtypeof Any;
algorithm
outOrgEqns :=
match (inOrgEqns,inA,inAcc)
local
Integer e;
list<BackendDAE.Equation> orgeqns;
BackendDAE.ConstraintEquations rest;
case ({},_,_) then listReverse(inAcc);
case ((e,orgeqns)::rest,_,_)
equation
(orgeqns,_) = Inline.inlineEqs(orgeqns, inA,{},false);
then
inlineOrgEqns(rest,inA,(e,orgeqns)::inAcc);
end match;
end inlineOrgEqns;

protected function traverseOrgEqns
"function: traverseOrgEqns
author: Frenkel TUD 2012-06
Expand Down

0 comments on commit 95c0350

Please sign in to comment.