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

Commit

Permalink
[NF] Preserve for loops with -d=-nfScalarize, ticket:5110, ticket:5144
Browse files Browse the repository at this point in the history
See NFFlatten.flattenEquation to enable this feature.

Belonging to [master]:
  - #2685
  - OpenModelica/OpenModelica-testsuite#1042
  • Loading branch information
rfranke authored and OpenModelica-Hudson committed Oct 1, 2018
1 parent 84d4820 commit b102b48
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 11 deletions.
10 changes: 10 additions & 0 deletions Compiler/FrontEnd/DAE.mo
Expand Up @@ -272,6 +272,16 @@ public uniontype Element
ElementSource source "the origin of the component/equation/algorithm" ;
end WHEN_EQUATION;

record FOR_EQUATION " a for-equation"
Type type_ "this is the type of the iterator";
Boolean iterIsArray "True if the iterator has an array type, otherwise false.";
Ident iter "the iterator variable";
Integer index "the index of the iterator variable, to make it unique; used by the new inst";
Exp range "range for the loop";
list<Element> equations "Equations" ;
ElementSource source "the origin of the component/equation/algorithm" ;
end FOR_EQUATION;

record IF_EQUATION " an if-equation"
list<Exp> condition1 "Condition" ;
list<list<Element>> equations2 "Equations of true branch" ;
Expand Down
11 changes: 11 additions & 0 deletions Compiler/FrontEnd/DAEDump.mo
Expand Up @@ -2996,6 +2996,17 @@ algorithm
then
str;

case (DAE.FOR_EQUATION(iter = s, range = e1, equations = xs1, source = src) :: xs, str)
equation
sourceStr = getSourceInformationStr(src);
s1 = ExpressionDump.printExpStr(e1);
str = IOStream.appendList(str, {" for ", s, " in ", s1, " loop\n"});
str = dumpEquationsStream(xs1, str);
str = IOStream.appendList(str, {" end for;\n"});
str = dumpEquationsStream(xs, str);
then
str;

case ((DAE.IF_EQUATION(condition1 = {},equations2 = {},equations3 = {}) :: _), str)
then
str;
Expand Down
16 changes: 16 additions & 0 deletions Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -424,6 +424,11 @@ algorithm
DoubleEndedList.push_back(eqs, elt);
then ();

case DAE.FOR_EQUATION()
algorithm
DoubleEndedList.push_back(eqs, elt);
then ();

case DAE.IF_EQUATION()
algorithm
DoubleEndedList.push_back(eqs, elt);
Expand Down Expand Up @@ -4160,6 +4165,15 @@ algorithm
then
();

case DAE.FOR_EQUATION(range = e1, equations = el)
algorithm
(new_e1, arg) := func(e1, arg);
if not referenceEq(e1, new_e1) then element.range := new_e1; end if;
(new_el, arg) := traverseDAEElementList(el, func, arg);
if not referenceEq(el, new_el) then element.equations := new_el; end if;
then
();

case DAE.COMP(dAElist = el)
algorithm
(new_el, arg) := traverseDAEElementList(el, func, arg);
Expand Down Expand Up @@ -5152,6 +5166,8 @@ algorithm
algorithm equations := e :: equations; then ();
case DAE.IF_EQUATION()
algorithm equations := e :: equations; then ();
case DAE.FOR_EQUATION()
algorithm equations := e :: equations; then ();
case DAE.WHEN_EQUATION()
algorithm equations := e :: equations; then ();
case DAE.REINIT()
Expand Down
31 changes: 21 additions & 10 deletions Compiler/NFFrontEnd/NFConvertDAE.mo
Expand Up @@ -491,12 +491,8 @@ algorithm
then
DAE.Element.ARRAY_EQUATION(dims, e1, e2, eq.source) :: elements;

// For equations should have been unrolled here.
case Equation.FOR()
algorithm
Error.assertion(false, getInstanceName() + " got a for equation", sourceInfo());
then
fail();
then convertForEquation(eq) :: elements;

case Equation.IF()
then convertIfEquation(eq.branches, eq.source, isInitial = false) :: elements;
Expand Down Expand Up @@ -529,6 +525,25 @@ algorithm
end match;
end convertEquation;

function convertForEquation
input Equation forEquation;
output DAE.Element forDAE;
protected
InstNode iterator;
Type ty;
Expression range;
list<Equation> body;
list<DAE.Element> dbody;
DAE.ElementSource source;
algorithm
Equation.FOR(iterator = iterator, range = SOME(range), body = body, source = source) := forEquation;
dbody := convertEquations(body);
Component.ITERATOR(ty = ty) := InstNode.component(iterator);

forDAE := DAE.Element.FOR_EQUATION(Type.toDAE(ty), Type.isArray(ty),
InstNode.name(iterator), 0, Expression.toDAE(range), dbody, source);
end convertForEquation;

function convertIfEquation
input list<Equation.Branch> ifBranches;
input DAE.ElementSource source;
Expand Down Expand Up @@ -626,12 +641,8 @@ algorithm
then
DAE.Element.INITIAL_ARRAY_EQUATION(dims, e1, e2, eq.source) :: elements;

// For equations should have been unrolled here.
case Equation.FOR()
algorithm
Error.assertion(false, getInstanceName() + " got a for equation", sourceInfo());
then
fail();
then convertForEquation(eq) :: elements;

case Equation.IF()
then convertIfEquation(eq.branches, eq.source, isInitial = true) :: elements;
Expand Down
8 changes: 7 additions & 1 deletion Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -724,7 +724,13 @@ algorithm
Equation.EQUALITY(e1, e2, eq.ty, eq.source) :: equations;

case Equation.FOR()
then unrollForLoop(eq, prefix, equations);
algorithm
if Flags.isSet(Flags.NF_SCALARIZE) then
eql := unrollForLoop(eq, prefix, equations);
else
eql := eq :: equations;
end if;
then eql;

case Equation.CONNECT()
algorithm
Expand Down
10 changes: 10 additions & 0 deletions Compiler/Template/DAEDumpTV.mo
Expand Up @@ -354,6 +354,16 @@ package DAE
ElementSource source "the origin of the component/equation/algorithm";
end WHEN_EQUATION;

record FOR_EQUATION " a for-equation"
Type type_ "this is the type of the iterator";
Boolean iterIsArray "True if the iterator has an array type, otherwise false.";
Ident iter "the iterator variable";
Integer index "the index of the iterator variable, to make it unique; used by the new inst";
Exp range "range for the loop";
list<Element> equations "Equations" ;
ElementSource source "the origin of the component/equation/algorithm" ;
end FOR_EQUATION;

record IF_EQUATION " an if-equation"
list<Exp> condition1 "Condition" ;
list<list<Element>> equations2 "Equations of true branch" ;
Expand Down
15 changes: 15 additions & 0 deletions Compiler/Template/DAEDumpTpl.tpl
Expand Up @@ -619,6 +619,7 @@ match lst
case COMPLEX_EQUATION(__) then dumpEquation(lhs, rhs, source)
case DEFINE(__) then dumpDefine(componentRef, exp, source)
case WHEN_EQUATION(__) then dumpWhenEquation(lst)
case FOR_EQUATION(__) then dumpForEquation(lst)
case IF_EQUATION(__) then dumpIfEquation(condition1, equations2, equations3, source)
case ASSERT(__) then dumpAssert(condition, message, level, source)
case INITIAL_ASSERT(__) then dumpAssert(condition, message, level, source)
Expand Down Expand Up @@ -729,6 +730,20 @@ match lst
>>
end dumpWhenEquation;

template dumpForEquation(DAE.Element lst)
::=
match lst
case FOR_EQUATION(__) then
let range_str = dumpExp(range)
let body_str = (equations |> e => dumpEquationElement(e) ;separator="\n")
let src_str = dumpSource(source)
<<
for <%iter%> in <%range_str%> loop
<%body_str%>
end for<%src_str%>;
>>
end dumpForEquation;

template dumpIfEquation(list<DAE.Exp> conds, list<list<DAE.Element>> branches,
list<DAE.Element> else_branch, DAE.ElementSource src)
::=
Expand Down

0 comments on commit b102b48

Please sign in to comment.