Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit e37e5cc

Browse files
rfrankeOpenModelica-Hudson
authored andcommitted
Extend treatment of for-equations in backend
Basic assumption: one for-equation defines one array variable. This makes sense along with -d=newInst,-nfScalarize. Belonging to [master]: - #2692 - OpenModelica/OpenModelica-testsuite#1044
1 parent 9220d77 commit e37e5cc

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

Compiler/BackEnd/BackendDAETransform.mo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ algorithm
565565
(outEquation, outTypeA) := matchcontinue (inEquation)
566566
local
567567
DAE.Exp e1_1, e2_1, e1, e2, cond;
568+
DAE.Exp iter, start, stop;
568569
DAE.ComponentRef cr, cr1;
569570
Integer size;
570571
list<DAE.Exp> expl;
@@ -596,6 +597,12 @@ algorithm
596597
source = List.foldr(ops, ElementSource.addSymbolicTransformation, source);
597598
then (BackendDAE.ARRAY_EQUATION(dimSize, e1_1, e2_1, source, eqAttr), ext_arg_2);
598599

600+
case BackendDAE.FOR_EQUATION(iter = iter, start = start, stop = stop, left = e1, right = e2, source = source, attr = eqAttr) equation
601+
(e1_1, (ops, ext_arg_1)) = func(e1, ({}, inTypeA));
602+
(e2_1, (ops, ext_arg_2)) = func(e2, (ops, ext_arg_1));
603+
source = List.foldr(ops, ElementSource.addSymbolicTransformation, source);
604+
then (BackendDAE.FOR_EQUATION(iter, start, stop, e1_1, e2_1, source, eqAttr), ext_arg_2);
605+
599606
case BackendDAE.SOLVED_EQUATION(componentRef = cr, exp = e2, source=source, attr=eqAttr) equation
600607
e1 = Expression.crefExp(cr);
601608
(DAE.CREF(cr1, _), (ops, ext_arg_1)) = func(e1, ({}, inTypeA));

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,18 @@ algorithm
25382538
then
25392539
(res,size);
25402540

2541+
// FOR_EQUATION
2542+
case BackendDAE.FOR_EQUATION(left = e1, right = e2, start = e, stop = cond,
2543+
iter = DAE.CREF(componentRef = DAE.CREF_IDENT(ident = str)))
2544+
equation
2545+
// assume one equation defining a whole array var (no NF_SCALARIZE)
2546+
e1 = Expression.traverseExpTopDown(e1, stripIterSub, str);
2547+
e2 = Expression.traverseExpTopDown(e2, stripIterSub, str);
2548+
lst1 = incidenceRowExp(e1, vars, iRow, functionTree, inIndexType);
2549+
res = incidenceRowExp(e2, vars, lst1, functionTree, inIndexType);
2550+
then
2551+
(res,1);
2552+
25412553
// SOLVED_EQUATION
25422554
case BackendDAE.SOLVED_EQUATION(componentRef = cr,exp = e)
25432555
equation
@@ -2585,14 +2597,35 @@ algorithm
25852597
else
25862598
equation
25872599
eqnstr = BackendDump.equationString(inEquation);
2588-
str = "- BackendDAE.incidenceRow failed for equation: " + eqnstr;
2600+
str = "- BackendDAEUtil.incidenceRow failed for equation: " + eqnstr;
25892601
Error.addMessage(Error.INTERNAL_ERROR, {str});
25902602
then
25912603
fail();
25922604
end matchcontinue;
25932605
outIntegerLst := AvlSetInt.addList(outIntegerLst, whenIntegerLst);
25942606
end incidenceRow;
25952607

2608+
protected
2609+
function stripIterSub
2610+
"Strips the last subscript if it is equal to given inIter ident.
2611+
This enables incicence analysis for array variables defined in for loops.
2612+
author: rfranke"
2613+
input DAE.Exp inExp;
2614+
input DAE.Ident inIter;
2615+
output DAE.Exp outExp;
2616+
output Boolean cont;
2617+
output DAE.Ident outIter = inIter;
2618+
algorithm
2619+
(outExp, cont) := match inExp
2620+
local
2621+
DAE.ComponentRef cr;
2622+
DAE.Type ty;
2623+
case DAE.CREF(componentRef = cr, ty = ty)
2624+
then (DAE.CREF(ComponentReference.crefStripIterSub(cr, inIter), ty), false);
2625+
else (inExp, true);
2626+
end match;
2627+
end stripIterSub;
2628+
25962629
protected function incidenceRowLst
25972630
"author: Frenkel TUD
25982631
Helper function to incidenceMatrix. Calculates the indidence row

Compiler/BackEnd/BackendEquation.mo

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,22 @@ algorithm
283283
end for;
284284
end traverseEquationArray_WithUpdate;
285285

286+
public
287+
function getForEquationIterIdent
288+
"Get the iterator of a for-equation
289+
author: rfranke"
290+
input BackendDAE.Equation inEquation;
291+
output Option<DAE.Ident> forIter;
292+
algorithm
293+
forIter := match inEquation
294+
local
295+
DAE.Ident iter;
296+
case BackendDAE.FOR_EQUATION(iter = DAE.CREF(componentRef = DAE.CREF_IDENT(ident = iter)))
297+
then SOME(iter);
298+
else NONE();
299+
end match;
300+
end getForEquationIterIdent;
301+
286302
public function getWhenEquationExpr "Get the left and right hand parts from an equation appearing in a when clause"
287303
input BackendDAE.WhenEquation inWhenEquation;
288304
output DAE.ComponentRef outComponentRef;
@@ -1614,6 +1630,7 @@ algorithm
16141630
source := match eq
16151631
case BackendDAE.EQUATION(source=source) then source;
16161632
case BackendDAE.ARRAY_EQUATION(source=source) then source;
1633+
case BackendDAE.FOR_EQUATION(source=source) then source;
16171634
case BackendDAE.SOLVED_EQUATION(source=source) then source;
16181635
case BackendDAE.RESIDUAL_EQUATION(source=source) then source;
16191636
case BackendDAE.WHEN_EQUATION(source=source) then source;
@@ -1647,6 +1664,7 @@ algorithm
16471664
osize := match eq
16481665
local
16491666
list<Integer> ds;
1667+
DAE.Exp e1, e2;
16501668
Integer size, start, stop;
16511669
list<BackendDAE.Equation> eqnsfalse;
16521670

@@ -1676,8 +1694,14 @@ algorithm
16761694
size = equationLstSize(eqnsfalse);
16771695
then size;
16781696

1679-
case BackendDAE.FOR_EQUATION(start=DAE.ICONST(start), stop=DAE.ICONST(stop)) equation
1680-
size = stop-start+1;
1697+
case BackendDAE.FOR_EQUATION(start = e1, stop = e2) equation
1698+
if Flags.isSet(Flags.NF_SCALARIZE) then
1699+
DAE.ICONST(start) = e1;
1700+
DAE.ICONST(stop) = e2;
1701+
size = stop - start + 1;
1702+
else
1703+
size = 1;
1704+
end if;
16811705
then size;
16821706

16831707
else equation
@@ -1774,6 +1798,7 @@ algorithm
17741798

17751799
case BackendDAE.EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
17761800
case BackendDAE.ARRAY_EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
1801+
case BackendDAE.FOR_EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
17771802
case BackendDAE.SOLVED_EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
17781803
case BackendDAE.RESIDUAL_EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
17791804
case BackendDAE.WHEN_EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
@@ -1952,6 +1977,7 @@ algorithm
19521977
list<Integer> dimSize;
19531978
DAE.Exp lhs;
19541979
DAE.Exp rhs;
1980+
DAE.Exp iter, start, stop;
19551981
DAE.ComponentRef componentRef;
19561982
Integer size;
19571983
DAE.Algorithm alg;
@@ -1967,6 +1993,9 @@ algorithm
19671993
case (BackendDAE.ARRAY_EQUATION(dimSize=dimSize, left=lhs, right=rhs, source=source), _)
19681994
then BackendDAE.ARRAY_EQUATION(dimSize, lhs, rhs, source, inAttr);
19691995

1996+
case (BackendDAE.FOR_EQUATION(iter=iter, start=start, stop=stop, left=lhs, right=rhs, source=source), _)
1997+
then BackendDAE.FOR_EQUATION(iter, start, stop, lhs, rhs, source, inAttr);
1998+
19701999
case (BackendDAE.SOLVED_EQUATION(componentRef=componentRef, exp=rhs, source=source), _)
19712000
then BackendDAE.SOLVED_EQUATION(componentRef, rhs, source, inAttr);
19722001

Compiler/FrontEnd/ComponentReference.mo

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,6 +2770,29 @@ algorithm
27702770
end match;
27712771
end crefStripLastSubs;
27722772

2773+
public function crefStripIterSub
2774+
"Strips the last sub if it is equal to the given iter ident.
2775+
This gives an array variable that is defined in a for loop (no NF_SCALARIZE).
2776+
author: rfranke"
2777+
input DAE.ComponentRef inComponentRef;
2778+
input DAE.Ident iter;
2779+
output DAE.ComponentRef outComponentRef;
2780+
protected
2781+
DAE.Ident index;
2782+
algorithm
2783+
outComponentRef := match crefLastSubs(inComponentRef)
2784+
case {DAE.INDEX(exp = DAE.CREF(componentRef = DAE.CREF_IDENT(ident = index)))}
2785+
algorithm
2786+
if index == iter then
2787+
outComponentRef := crefStripLastSubs(inComponentRef);
2788+
else
2789+
outComponentRef := inComponentRef;
2790+
end if;
2791+
then outComponentRef;
2792+
else inComponentRef;
2793+
end match;
2794+
end crefStripIterSub;
2795+
27732796
public function crefStripFirstIdent
27742797
"Strips the first part of a component reference,
27752798
i.e the identifier and eventual subscripts"

0 commit comments

Comments
 (0)