Skip to content

Commit

Permalink
- update partial function evaluation
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20265 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Volker Waurich committed Apr 25, 2014
1 parent e5e79f2 commit 61dfb88
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
53 changes: 53 additions & 0 deletions Compiler/BackEnd/BackendVarTransform.mo
Expand Up @@ -828,6 +828,54 @@ algorithm
end matchcontinue;
end hasReplacement;

public function hasReplacementCrefFirst "
Outputs true if the replacements contain a rule for the cref
"
input DAE.ComponentRef inComponentRef;
input VariableReplacements inVariableReplacements;
output Boolean bOut;
algorithm
bOut:=
matchcontinue (inComponentRef,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (src,REPLACEMENTS(hashTable=ht))
equation
_ = BaseHashTable.get(src,ht);
then
true;
else
equation
then false;
end matchcontinue;
end hasReplacementCrefFirst;

public function hasNoReplacementCrefFirst "
Outputs true if the replacements contain a rule for the cref
"
input DAE.ComponentRef inComponentRef;
input VariableReplacements inVariableReplacements;
output Boolean bOut;
algorithm
bOut:=
matchcontinue (inComponentRef,inVariableReplacements)
local
DAE.ComponentRef src;
DAE.Exp dst;
HashTable2.HashTable ht;
case (src,REPLACEMENTS(hashTable=ht))
equation
_ = BaseHashTable.get(src,ht);
then
false;
else
equation
then true;
end matchcontinue;
end hasNoReplacementCrefFirst;

public function getReplacementVarArraySize
input VariableReplacements inVariableReplacements;
output Integer size;
Expand Down Expand Up @@ -1990,6 +2038,7 @@ algorithm
(outStatementLst,replacementPerformed) :=
matchcontinue (inStatementLst,inVariableReplacements,inFuncTypeExpExpToBooleanOption,inAcc,inBAcc)
local
Boolean isCon;
VariableReplacements repl;
list<DAE.Statement> es,es_1,statementLst,statementLst_1;
DAE.Statement statement,statement_1;
Expand All @@ -2012,6 +2061,10 @@ algorithm
case ((DAE.STMT_ASSIGN(type_=type_,exp1=e1,exp=e2,source=source)::es),repl,_,_,_)
equation
(e1_1,b1) = replaceExp(e1, repl,inFuncTypeExpExpToBooleanOption);
//isCon = Expression.isConst(e1_1);
//e1_1 = Util.if_(isCon,e1,e1_1);
//cr = Expression.expCref(e1);
//repl = Debug.bcallret3(isCon,removeReplacement,repl,cr,NONE(),repl);
(e2_1,b2) = replaceExp(e2, repl,inFuncTypeExpExpToBooleanOption);
true = b1 or b2;
(e1_2,_) = ExpressionSimplify.simplify(e1_1);
Expand Down
17 changes: 10 additions & 7 deletions Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -815,7 +815,6 @@ algorithm
end match;
end updateFunctionBody;


protected function buildPartialFunction "build a partial function for the variable outputs of a complex function and generate some simple equations for the constant outputs.
author:Waurich TUD 2014-03"
input tuple<list<DAE.ComponentRef>,list<DAE.Element>> varPart;
Expand Down Expand Up @@ -843,7 +842,8 @@ algorithm

// build the partial function algorithm, replace the qualified crefs
stmts1 := List.mapFlat(funcAlgs, DAEUtil.getStatement);
stmts1 := List.filterOnTrue(stmts1,statementRHSIsNotConst);
//stmts1 := listReverse(stmts1);
//stmts1 := List.filterOnTrue(stmts1,statementRHSIsNotConst);
// remove the constant values
stmts1 := traverseStmtsAndUpdate(stmts1,stmtCanBeRemoved,replIn,{});
// build new crefs for the scalars
Expand Down Expand Up @@ -1187,16 +1187,17 @@ algorithm
BackendVarTransform.VariableReplacements repl, replIn;
DAE.ComponentRef cref;
DAE.ElementSource source;
DAE.Exp exp0,exp1, exp2;
DAE.Exp exp0, exp1, exp2;
DAE.Else else_;
DAE.FunctionTree funcTree,funcTree2;
DAE.Statement alg, alg2;
DAE.Type typ;
list<BackendDAE.Equation> addEqs;
list<DAE.ComponentRef> scalars, varScalars,constScalars, outputs;
list<DAE.Statement> stmts1, stmts2, rest, addStmts,stmtsNew, allStmts;
list<DAE.ComponentRef> scalars, varScalars,constScalars, outputs, initOutputs;
list<list<DAE.Exp>> lhsExpLst;
list<DAE.Statement> stmts1, stmts2, rest, addStmts,stmtsNew, allStmts, initStmts;
list<list<DAE.Statement>> stmtsLst;
list<DAE.Exp> expLst,tplExpsLHS,tplExpsRHS,lhsExps;
list<DAE.Exp> expLst,tplExpsLHS,tplExpsRHS,lhsExps,lhsExpsInit,rhsExps;
case({},(_,_,_),_)
equation
stmts1 = listReverse(lstIn);
Expand All @@ -1215,6 +1216,8 @@ algorithm
(exp2,_) = ExpressionSimplify.simplify(exp2);
expLst = Expression.getComplexContents(exp2);
//print("SIMPLIFIED\n"+&stringDelimitList(List.map({exp2},ExpressionDump.printExpStr),"\n")+&"\n");

// add the replacements for the addStmts

// check if its constant, a record or a tuple
isCon = Expression.isConst(exp2);
Expand Down Expand Up @@ -1296,7 +1299,7 @@ algorithm
stmts1 = Util.if_(simplified and isCon, listAppend(stmts1,addStmts), {alg});
Debug.bcall1(Flags.isSet(Flags.EVAL_FUNC_DUMP) and not isIf,print,"-->is it an other case? "+&boolString(simplified)+&"\n");

// if its not definite which case, try to predict a constant output
// if its not definite which case, try to predict a constant output, maybe its partially constant, then remove function outputs replacements
Debug.bcall1(Flags.isSet(Flags.EVAL_FUNC_DUMP) and not simplified,print,"-->try to predict the outputs \n");
((stmtsNew,addStmts),(funcTree,repl,idx)) = Debug.bcallret2_2(not isCon and not simplified,predictIfOutput,alg,(funcTree,replIn,idx),(stmts1,{}),(funcTree,repl,idx));
predicted = List.isNotEmpty(addStmts) or List.isEmpty(stmtsNew) and not isCon;
Expand Down

0 comments on commit 61dfb88

Please sign in to comment.