Skip to content

Commit 599f6c7

Browse files
author
Volker Waurich
committed
- some features for partial function evaluation: cast support, expand tuple equation
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20229 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent b4466e2 commit 599f6c7

File tree

2 files changed

+198
-19
lines changed

2 files changed

+198
-19
lines changed

Compiler/BackEnd/EvaluateFunctions.mo

Lines changed: 183 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ algorithm
176176
shared = BackendDAEUtil.addFunctionTree(funcs,shared);
177177
size = DAEUtil.getTupleSize(lhsExp);
178178
eq = Util.if_(intEq(size,0),BackendDAE.EQUATION(lhsExp,rhsExp,source,diff),BackendDAE.COMPLEX_EQUATION(size,lhsExp,rhsExp,source,diff));
179-
179+
(eq,addEqs) = convertTupleEquations(eq,addEqs);
180180
//print("the lhs:\n");
181181
//ExpressionDump.dumpExp(lhsExp);
182182
//print("the rhs \n");
@@ -192,6 +192,62 @@ algorithm
192192
end matchcontinue;
193193
end evalFunctions_findFuncs;
194194

195+
protected function convertTupleEquations"converts an equation tupleExp=tupleExp to several simple equations of exp=const.
196+
author:Waurich TUD 2014-04"
197+
input BackendDAE.Equation eqIn;
198+
input list<BackendDAE.Equation> addEqsIn;
199+
output BackendDAE.Equation eqOut;
200+
output list<BackendDAE.Equation> addEqsOut;
201+
algorithm
202+
(eqOut,addEqsOut) := matchcontinue(eqIn,addEqsIn)
203+
local
204+
BackendDAE.Equation eq;
205+
DAE.Exp lhsExp,rhsExp;
206+
list<DAE.Exp> lhs,rhs;
207+
list<BackendDAE.Equation> eqs;
208+
case(BackendDAE.COMPLEX_EQUATION(size=_,left=lhsExp,right=rhsExp,source=_,differentiated=_),_)
209+
equation
210+
DAE.TUPLE(lhs) = lhsExp;
211+
DAE.TUPLE(rhs) = rhsExp;
212+
eqs = makeBackendEquation(lhs,rhs,{});
213+
eq::eqs = eqs;
214+
eqs = listAppend(eqs,addEqsIn);
215+
then
216+
(eq,eqs);
217+
else
218+
then
219+
(eqIn,addEqsIn);
220+
end matchcontinue;
221+
end convertTupleEquations;
222+
223+
protected function makeBackendEquation"builds backendEquations for the list of lhs-exps and rhs-exps.
224+
author:Waurich TUD 2014-04"
225+
input list<DAE.Exp> ls;
226+
input list<DAE.Exp> rs;
227+
input list<BackendDAE.Equation> eqLstIn;
228+
output list<BackendDAE.Equation> eqLstOut;
229+
algorithm
230+
eqLstOut := matchcontinue(ls,rs,eqLstIn)
231+
local
232+
list<DAE.Exp> lrest;
233+
list<DAE.Exp> rrest;
234+
BackendDAE.Equation eq;
235+
list<BackendDAE.Equation> eqLst;
236+
DAE.Exp l;
237+
DAE.Exp r;
238+
case({},{},_)
239+
then
240+
eqLstIn;
241+
case(l::lrest,r::rrest,_)
242+
equation
243+
eq = BackendDAE.EQUATION(r,l,DAE.emptyElementSource,false);
244+
eqLst = eq::eqLstIn;
245+
eqLst = makeBackendEquation(lrest,rrest,eqLst);
246+
then
247+
eqLst;
248+
end matchcontinue;
249+
end makeBackendEquation;
250+
195251
protected function evaluateConstantFunction
196252
input DAE.Exp rhsExpIn;
197253
input DAE.Exp lhsExpIn;
@@ -213,10 +269,11 @@ algorithm
213269
DAE.Exp exp, exp2, constExp, outputExp;
214270
DAE.Function func;
215271
DAE.FunctionTree funcs;
272+
DAE.Type ty;
216273
list<BackendDAE.Equation> constEqs;
217274
list<DAE.ComponentRef> inputCrefs, outputCrefs, allInputCrefs, allOutputCrefs, constInputCrefs, constCrefs, varScalarCrefsInFunc,constComplexCrefs,varComplexCrefs,constScalarCrefs, constScalarCrefsOut,varScalarCrefs;
218275
list<DAE.Element> elements, algs, allInputs, protectVars, inputs1d, allOutputs, varOutputs;
219-
list<DAE.Exp> exps, inputExps, complexExp, allInputExps, constInputExps, constExps, constComplexExps, constScalarExps;
276+
list<DAE.Exp> exps, inputExps, complexExp, allInputExps, constInputExps, constExps, constComplexExps, constScalarExps, lhsExps;
220277
list<list<DAE.Exp>> scalarExp;
221278
list<DAE.Statement> stmts;
222279
list<list<DAE.ComponentRef>> scalarInputs, scalarOutputs;
@@ -323,6 +380,9 @@ algorithm
323380

324381
//decide which lhs or which rhs to take
325382
outputExp = Util.if_(funcIsPartConst,outputExp,lhsExpIn);
383+
lhsExps = getCrefsForRecord(lhsExpIn);
384+
outputExp = Util.if_(isConstRec,DAE.TUPLE(lhsExps),outputExp);
385+
326386
exp2 = Debug.bcallret1(List.hasOneElement(constComplexExps) and funcIsConst,List.first,constComplexExps,DAE.TUPLE(constComplexExps)); // either a single equation or a tuple equation
327387
exp = Util.if_(funcIsConst,exp2,rhsExpIn);
328388
exp = Util.if_(funcIsPartConst,DAE.CALL(path, exps, attr2),exp);
@@ -341,6 +401,30 @@ algorithm
341401
((rhsExpIn,lhsExpIn,{},funcsIn,eqIdx));
342402
end matchcontinue;
343403
end evaluateConstantFunction;
404+
405+
public function getCrefsForRecord "
406+
author:Waurich TUD 2014-04"
407+
input DAE.Exp e;
408+
output list<DAE.Exp> es;
409+
algorithm
410+
es := match(e)
411+
local
412+
DAE.ComponentRef cref;
413+
DAE.Exp exp;
414+
list<DAE.Exp> expLst;
415+
list<DAE.ComponentRef> crefs;
416+
case(DAE.CREF(componentRef = cref,ty=_))
417+
equation
418+
crefs = ComponentReference.expandCref(cref,true);
419+
expLst = List.map(crefs,Expression.crefExp);
420+
then
421+
expLst;
422+
else
423+
equation
424+
then
425+
{};
426+
end match;
427+
end getCrefsForRecord;
344428

345429
protected function replaceExpsInRecord
346430
input DAE.ComponentRef crefIn;
@@ -450,7 +534,6 @@ algorithm
450534
//print("\n constScalarCrefs \n"+&stringDelimitList(List.map(constScalarCrefs,ComponentReference.printComponentRefStr),"\n")+&"\n");
451535
//print("\n allOutputs "+&"\n"+&DAEDump.dumpElementsStr(allOutputs)+&"\n");
452536
//print("\n lhsExpIn "+&"\n"+&ExpressionDump.dumpExpStr(lhsExpIn,0)+&"\n");
453-
print("this case\n");
454537
lhsCref = Expression.expCref(lhsExpIn);
455538
outputCrefs = List.map(constScalarCrefs,ComponentReference.crefStripFirstIdent);
456539
outputCrefs = List.map1(outputCrefs,ComponentReference.joinCrefsR,lhsCref);
@@ -1071,7 +1154,7 @@ author:Waurich TUD 2014-03"
10711154
algorithm
10721155
(algsOut,tplOut) := matchcontinue(algsIn,tplIn,lstIn)
10731156
local
1074-
Boolean changed, isCon, simplified, isIf, isRec, predicted, debugB;
1157+
Boolean changed, isCon, simplified, isIf, isRec, isTpl, predicted, debugB;
10751158
Integer idx, size,s;
10761159
BackendVarTransform.VariableReplacements repl, replIn;
10771160
DAE.ComponentRef cref;
@@ -1084,7 +1167,7 @@ algorithm
10841167
list<BackendDAE.Equation> addEqs;
10851168
list<DAE.ComponentRef> scalars;
10861169
list<DAE.Statement> stmts1, stmts2, rest, addStmts,stmtsNew;
1087-
list<DAE.Exp> expLst;
1170+
list<DAE.Exp> expLst,tplExpsLHS,tplExpsRHS;
10881171
case({},(_,_,_),_)
10891172
equation
10901173
then
@@ -1101,24 +1184,28 @@ algorithm
11011184
expLst = Expression.getComplexContents(exp2);
11021185
isCon = Expression.isConst(exp2);
11031186
isRec = ComponentReference.isRecord(cref);
1104-
1105-
//print("is it const? "+&boolString(isCon)+&" ,is it rec: "+&boolString(isRec)+&"\n");
1187+
isTpl = Expression.isTuple(exp1) and Expression.isTuple(exp2);
1188+
//print("is it const? "+&boolString(isCon)+&" ,is it rec: "+&boolString(isRec)+&" ,is it tpl: "+&boolString(isTpl)+&"\n");
11061189
//Debug.bcall(isCon and not isRec,print,"add the replacement: "+&ComponentReference.crefStr(cref)+&" --> "+&ExpressionDump.printExpStr(exp2)+&"\n");
1107-
//Debug.bcall(not isCon,print,"remove the replacement for: "+&ComponentReference.crefStr(cref)+&"\n");
1190+
//Debug.bcall(not isCon,print,"remove the replacement for: "+&ComponentReference.crefStr(cref)+&"\n");
1191+
11081192
repl = Debug.bcallret4(isCon and not isRec, BackendVarTransform.addReplacement, replIn,cref,exp2,NONE(),replIn);
11091193
repl = Debug.bcallret4(isCon and isRec, BackendVarTransform.addReplacements, repl,scalars,expLst,NONE(),repl);
11101194
repl = Debug.bcallret3(not isCon, BackendVarTransform.removeReplacement, repl,cref,NONE(),repl);
11111195
//BackendVarTransform.dumpReplacements(repl);
1112-
1196+
11131197
//alg = Util.if_(isCon and not isRec,DAE.STMT_ASSIGN(typ,exp1,exp2,source),List.first(algsIn));
11141198
//alg = Util.if_(not isRec,DAE.STMT_ASSIGN(typ,exp1,exp2,source),List.first(algsIn));
11151199
alg = Util.if_(isCon,DAE.STMT_ASSIGN(typ,exp1,exp2,source),List.first(algsIn));
1200+
tplExpsLHS = Debug.bcallret1(isTpl,Expression.getComplexContents,exp1,{});
1201+
alg = Util.if_(isTpl,DAE.STMT_TUPLE_ASSIGN(typ,tplExpsLHS,exp2,source),List.first(algsIn));
11161202

11171203
//print("the STMT_ASSIGN after : "+&DAEDump.ppStatementStr(alg)+&"\n");
11181204
stmts1 = listAppend(lstIn,{alg});
11191205
(rest,(funcTree,repl,idx)) = evaluateFunctions_updateStatement(rest,(funcTree,repl,idx),stmts1);
11201206
then
11211207
(rest,(funcTree,repl,idx));
1208+
11221209
case (DAE.STMT_ASSIGN_ARR(type_=_, componentRef=_, exp=_)::rest,(funcTree,_,idx),_)
11231210
equation
11241211
//print("STMT_ASSIGN_ARR");
@@ -1156,7 +1243,7 @@ algorithm
11561243
stmts1 = Util.if_(predicted, stmtsNew,stmts1);
11571244
rest = listAppend(addStmts,rest);
11581245

1159-
//print("the STMT_IF after: \n"+&stringDelimitList(List.map(stmts1,DAEDump.ppStatementStr),"\n")+&"\n");
1246+
//print("the STMT_IF after: \n"+&stringDelimitList(List.map(listAppend(stmts1,addStmts),DAEDump.ppStatementStr),"\n")+&"\n");
11601247
//print("\nthe REST if after :"+&stringDelimitList(List.map(rest,DAEDump.ppStatementStr),"\n")+&"\n");
11611248

11621249
stmts1 = listAppend(stmts1,lstIn);
@@ -1186,6 +1273,10 @@ algorithm
11861273
//BackendDump.dumpEquationList(addEqs,"the additional equations after");
11871274
((_,isCon)) = Expression.traverseExp(exp1,expIsConstTraverser,true);
11881275
exp1 = Util.if_(isCon,exp1,exp0);
1276+
//print("is the tupl const? "+&boolString(isCon)+&"\n");
1277+
1278+
// add the replacements
1279+
repl = Debug.bcallret3(isCon,addTplReplacements,replIn,exp1,exp2,replIn);
11891280

11901281
// build the new statements
11911282
size = DAEUtil.getTupleSize(exp2);
@@ -1199,7 +1290,7 @@ algorithm
11991290
//print("idx: "+&intString(idx)+&"\n");
12001291
//print("\nthe traverse LIST tpl after :"+&stringDelimitList(List.map(stmts2,DAEDump.ppStatementStr),"\n")+&"\n");
12011292
//print("\nthe REST tpl after :"+&stringDelimitList(List.map(rest,DAEDump.ppStatementStr),"\n")+&"\n");
1202-
(rest,(funcTree,repl,idx)) = evaluateFunctions_updateStatement(rest,(funcTree2,replIn,idx),stmts2);
1293+
(rest,(funcTree,repl,idx)) = evaluateFunctions_updateStatement(rest,(funcTree2,repl,idx),stmts2);
12031294
then
12041295
(rest,(funcTree,repl,idx));
12051296
else
@@ -1210,6 +1301,32 @@ algorithm
12101301
end matchcontinue;
12111302
end evaluateFunctions_updateStatement;
12121303

1304+
protected function addTplReplacements
1305+
input BackendVarTransform.VariableReplacements replIn;
1306+
input DAE.Exp e1;
1307+
input DAE.Exp e2;
1308+
output BackendVarTransform.VariableReplacements replOut;
1309+
algorithm
1310+
replOut := matchcontinue(replIn,e1,e2)
1311+
local
1312+
list<DAE.Exp> tplLHS, tplRHS;
1313+
list<DAE.ComponentRef> crefs;
1314+
BackendVarTransform.VariableReplacements repl;
1315+
case(_,_,_)
1316+
equation
1317+
tplRHS = DAEUtil.getTupleExps(e1);
1318+
tplLHS = DAEUtil.getTupleExps(e2);
1319+
crefs = List.map(tplLHS,Expression.expCref);
1320+
repl = BackendVarTransform.addReplacements(replIn,crefs,tplRHS,NONE());
1321+
//print("add the tpl replacements: "+&stringDelimitList(List.map(crefs,ComponentReference.printComponentRefStr),",")+&stringDelimitList(List.map(tplRHS,ExpressionDump.printExpStr),",")+&"\n");
1322+
then
1323+
repl;
1324+
else
1325+
then
1326+
replIn;
1327+
end matchcontinue;
1328+
end addTplReplacements;
1329+
12131330
protected function expIsConstTraverser "traverser function to check if there are any variable expressions"
12141331
input tuple<DAE.Exp,Boolean> tplIn;
12151332
output tuple<DAE.Exp,Boolean> tplOut;
@@ -1261,8 +1378,11 @@ author:Waurich TUD 2014-04"
12611378
algorithm
12621379
lhs := match(stmt,expsIn)
12631380
local
1381+
DAE.Else else_;
12641382
DAE.Exp exp;
12651383
list<DAE.Exp> expLst;
1384+
list<DAE.Statement> stmtLst1,stmtLst2;
1385+
list<list<DAE.Statement>> stmtLstLst;
12661386
case(DAE.STMT_ASSIGN(type_=_,exp1=exp,exp=_),_)
12671387
equation
12681388
then
@@ -1277,11 +1397,14 @@ algorithm
12771397
print("IMPLEMENT STMT_ASSIGN_ARR in getStatementLHS\n");
12781398
then
12791399
fail();
1280-
case(DAE.STMT_IF(exp=_,statementLst=_,else_=_,source=_),_)
1400+
case(DAE.STMT_IF(exp=_,statementLst=stmtLst1,else_=else_,source=_),_)
12811401
equation
1282-
print("IMPLEMENT STMT_IF in getStatementLHS\n");
1402+
stmtLstLst = getDAEelseStatemntLsts(else_,{});
1403+
stmtLst2 = List.flatten(stmtLstLst);
1404+
stmtLst1 = listAppend(stmtLst1,stmtLst2);
1405+
expLst = List.fold(stmtLst1,getStatementLHS,expsIn);
12831406
then
1284-
fail();
1407+
expLst;
12851408
else
12861409
equation
12871410
print("getStatementLHS update!\n");
@@ -1350,7 +1473,7 @@ algorithm
13501473
((rhs,lhs,addEqs,funcs,idx)) = evaluateConstantFunction(rhs,lhs,funcs,idx);
13511474

13521475

1353-
Debug.bcall1(List.isNotEmpty(addEqs),print,"THERE ARE ADD EQS IN SUBFUNC\n");
1476+
//Debug.bcall1(List.isNotEmpty(addEqs),print,"THERE ARE ADD EQS IN SUBFUNC\n");
13541477
stmts = List.map(addEqs,equationToStmt);
13551478
stmts = listAppend(stmts,stmtsIn);
13561479
then
@@ -1506,6 +1629,8 @@ algorithm
15061629
end matchcontinue;
15071630
end getScalarsForComplexVar;
15081631

1632+
1633+
15091634
protected function isNotComplexVar"returns true if the given var is one dimensional (no array,record...).
15101635
author: Waurich TUD 2014-03"
15111636
input DAE.Element inElem;
@@ -1668,6 +1793,7 @@ algorithm
16681793
replLst = List.map(tplLst,Util.tuple22);
16691794

16701795
//print("all evaled stmts2: \n"+&stringDelimitList(List.map(List.flatten(stmtsLst),DAEDump.ppStatementStr),"---------\n")+&"\n");
1796+
replLst = List.map(replLst,getOnlyConstantReplacements);
16711797
//List.map_0(replLst,BackendVarTransform.dumpReplacements);
16721798

16731799
// get the outputs of every case
@@ -1676,10 +1802,10 @@ algorithm
16761802
allLHS = listReverse(expLst);
16771803
//print("the outputs: "+&stringDelimitList(List.map(allLHS,ExpressionDump.printExpStr),"\n")+&"\n");
16781804
expLstLst = List.map1(replLst,replaceExps,allLHS);
1679-
//print("the outputs replaced: "+&stringDelimitList(List.map(expLstLst,ExpressionDump.printExpListStr),"\n")+&"\n");
1805+
//print("the outputs replaced: \n"+&stringDelimitList(List.map(expLstLst,ExpressionDump.printExpListStr),"\n")+&"\n\n");
16801806

16811807
// compare the constant outputs
1682-
constantOutputs = compareConstantExps(expLstLst,List.intRange(listLength(allLHS)));
1808+
constantOutputs = compareConstantExps(expLstLst);
16831809
outExps = List.map1(constantOutputs,List.getIndexFirst,allLHS);
16841810
_ = List.map(outExps,Expression.expCref);
16851811
//print("constantOutputs: "+&stringDelimitList(List.map(constantOutputs,intString),",")+&"\n");
@@ -1698,7 +1824,7 @@ algorithm
16981824
addStmts = List.map2(List.intRange(listLength(outExps)),makeAssignmentMap,outExps,expLst);
16991825
stmtNew = updateStatementsInIfStmt(stmtsLst,stmtIn);
17001826

1701-
//print("the new predicted stmts: \n"+&stringDelimitList(List.map({stmtNew},DAEDump.ppStatementStr),"\n")+&"\nand the additional "+&stringDelimitList(List.map(addStmts,DAEDump.ppStatementStr),"\n")+&"\n");
1827+
//print("the new predicted stmts: \n"+&stringDelimitList(List.map({stmtNew},DAEDump.ppStatementStr),"\n")+&"\nAnd the additional "+&stringDelimitList(List.map(addStmts,DAEDump.ppStatementStr),"\n")+&"\n");
17021828

17031829
//repl = BackendVarTransform.addReplacements(replIn,crefs,expLst,NONE());
17041830
then
@@ -1781,6 +1907,43 @@ algorithm
17811907
end match;
17821908
end updateStatementsInElse;
17831909

1910+
1911+
protected function compareConstantExps "compares the lists of expressions if there are the same constants at the same position
1912+
author:Waurich TUD 2014-04"
1913+
input list<list<DAE.Exp>> expLstLstIn;
1914+
output list<Integer> posLstOut;
1915+
protected
1916+
Integer num;
1917+
list<Integer> idcs;
1918+
algorithm
1919+
num := listLength(List.first(expLstLstIn));
1920+
idcs := List.intRange(num);
1921+
posLstOut := List.fold1(idcs,compareConstantExps2,expLstLstIn,{});
1922+
end compareConstantExps;
1923+
1924+
protected function compareConstantExps2
1925+
input Integer idx;
1926+
input list<list<DAE.Exp>> expLstLst;
1927+
input list<Integer> posIn;
1928+
output list<Integer> posOut;
1929+
protected
1930+
Boolean b1,b2;
1931+
list<Boolean> bLst;
1932+
list<Integer> posLst;
1933+
DAE.Exp firstExp;
1934+
list<DAE.Exp> expLst,rest;
1935+
algorithm
1936+
expLst := List.map1(expLstLst,listGet,idx);
1937+
firstExp::rest := expLst;
1938+
bLst := List.map(expLst,Expression.isConst);
1939+
b1 := List.fold(bLst,boolAnd,true);
1940+
bLst := List.map1(rest,Expression.expEqual,firstExp);
1941+
b2 := List.fold(bLst,boolAnd,true);
1942+
posLst := idx::posIn;
1943+
posOut := Util.if_(b1 and b2,posLst,posIn);
1944+
end compareConstantExps2;
1945+
1946+
/*
17841947
protected function compareConstantExps "compares the lists of expressions if there are the same constants at the same position
17851948
author:Waurich TUD 2014-04"
17861949
input list<list<DAE.Exp>> expLstLstIn;
@@ -1841,6 +2004,7 @@ algorithm
18412004
posLstIn;
18422005
end match;
18432006
end compareConstantExps2;
2007+
*/
18442008

18452009
protected function makeAssignmentMap"mapping functino fo build the statements for a list of lhs and rhs exps.
18462010
author:Waurich TUD 2014-04"
@@ -1867,4 +2031,4 @@ algorithm
18672031
stmtOut := DAE.STMT_ASSIGN(ty,lhs,rhs,DAE.emptyElementSource);
18682032
end makeAssignment;
18692033

1870-
end EvaluateFunctions;
2034+
end EvaluateFunctions;

0 commit comments

Comments
 (0)