Skip to content

Commit

Permalink
- treat entire arrays as output if they are used as lhs of algorithm …
Browse files Browse the repository at this point in the history
…sections [#2271]

- do not replace lhs of algorithms (due to array/scalar differences) [#2271]
- prettify RemoveSimpleEquation dumps


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16626 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Jul 18, 2013
1 parent 63b7ba2 commit d7242d2
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 141 deletions.
15 changes: 14 additions & 1 deletion Compiler/BackEnd/BackendDump.mo
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ encapsulated package BackendDump

public import BackendDAE;
public import DAE;
public import HashSet;

protected import Absyn;
protected import Algorithm;
protected import BackendDAETransform;
protected import BackendDAEUtil;
protected import BackendVariable;
protected import BackendEquation;
protected import BackendVariable;
protected import BaseHashSet;
protected import ClassInf;
protected import ComponentReference;
protected import DAEDump;
Expand Down Expand Up @@ -398,6 +400,7 @@ end printSparsityPattern;
// - dumpEquation
// - dumpEquationArray
// - dumpEquationList
// - dumpHashSet
// - dumpSparsityPattern
// - dumpTearing
// - dumpVariables
Expand Down Expand Up @@ -508,6 +511,16 @@ algorithm
print("\n");
end dumpConstraintArray;

public function dumpHashSet "function dumpHashSet
author lochel"
input HashSet.HashSet hashSet;
input String heading;
algorithm
print("\n" +& heading +& "\n" +& UNDERLINE +& "\n");
BaseHashSet.printHashSet(hashSet);
print("\n");
end dumpHashSet;

public function dumpSparsityPattern "function dumpSparsityPattern
author lochel"
input BackendDAE.SparsePattern inPattern;
Expand Down
39 changes: 18 additions & 21 deletions Compiler/BackEnd/BackendVarTransform.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2298,32 +2298,29 @@ end replaceSTMT_IF;
/* dump replacements */
/*********************************************************/

public function dumpReplacements
"function: dumpReplacements
public function dumpReplacements "function dumpReplacements
Prints the variable replacements on form var1 -> var2"
input VariableReplacements inVariableReplacements;
algorithm
_:=
match (inVariableReplacements)
_ := match (inVariableReplacements)
local
String str,len_str;
String str, len_str;
Integer len;
HashTable2.HashTable ht;
list<tuple<DAE.ComponentRef,DAE.Exp>> tplLst;
case (REPLACEMENTS(hashTable= ht))
equation
(tplLst) = BaseHashTable.hashTableList(ht);
str = stringDelimitList(List.map(tplLst,printReplacementTupleStr),"\n");
print("Replacements: (");
len = listLength(tplLst);
len_str = intString(len);
print(len_str);
print(")\n");
print("=============\n");
print(str);
print("\n");
then
();

case (REPLACEMENTS(hashTable= ht)) equation
(tplLst) = BaseHashTable.hashTableList(ht);
str = stringDelimitList(List.map(tplLst,printReplacementTupleStr), "\n");
print("Replacements: (");
len = listLength(tplLst);
len_str = intString(len);
print(len_str);
print(")\n");
print("========================================\n");
print(str);
print("\n");
then ();
end match;
end dumpReplacements;

Expand All @@ -2348,7 +2345,7 @@ algorithm
len_str = intString(len);
print(len_str);
print(")\n");
print("=============\n");
print("========================================\n");
print(str);
print("\n");
then
Expand Down Expand Up @@ -2377,7 +2374,7 @@ algorithm
len_str = intString(len);
print(len_str);
print(")\n");
print("=============\n");
print("========================================\n");
print(str);
print("\n");
then
Expand Down
119 changes: 62 additions & 57 deletions Compiler/BackEnd/RemoveSimpleEquations.mo
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public import BackendDAE;
public import DAE;
public import Env;

protected import BackendDAEUtil;
protected import BackendDAETransform;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVarTransform;
protected import BackendVariable;
protected import BaseHashTable;
protected import BackendVarTransform;
protected import BaseHashSet;
protected import BaseHashTable;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
Expand All @@ -72,8 +72,8 @@ protected import Flags;
protected import HashSet;
protected import Inline;
protected import List;
protected import Util;
protected import Types;
protected import Util;

protected type EquationAttributes = tuple<DAE.ElementSource,Boolean> "eqnAttributes";

Expand Down Expand Up @@ -148,8 +148,7 @@ algorithm
unreplacable := HashSet.emptyHashSet();
unreplacable := BackendDAEUtil.traverseBackendDAEExps(dae,traverserUnreplacable,unreplacable);
unreplacable := addUnreplacableFromWhens(dae,unreplacable);
Debug.fcall(Flags.DUMP_REPL, print, "Unreplacable Crefs:\n");
Debug.fcall(Flags.DUMP_REPL, BaseHashSet.dumpHashSet, unreplacable);
Debug.fcall2(Flags.DUMP_REPL, BackendDump.dumpHashSet, unreplacable, "Unreplacable Crefs:");
// traverse all systems and remove simple equations
(odae,(repl,b,_,_)) := BackendDAEUtil.mapEqSystemAndFold(dae,fastAcausal1,(repl,false,unreplacable,Flags.getConfigInt(Flags.MAXTRAVERSALS)));
// traverse the shared parts
Expand Down Expand Up @@ -292,8 +291,7 @@ algorithm
unreplacable := HashSet.emptyHashSet();
unreplacable := BackendDAEUtil.traverseBackendDAEExps(inDAE,traverserUnreplacable,unreplacable);
unreplacable := addUnreplacableFromWhens(inDAE,unreplacable);
Debug.fcall(Flags.DUMP_REPL, print, "Unreplacable Crefs:\n");
Debug.fcall(Flags.DUMP_REPL, BaseHashSet.dumpHashSet, unreplacable);
Debug.fcall2(Flags.DUMP_REPL, BackendDump.dumpHashSet, unreplacable, "Unreplacable Crefs:");
(outDAE,(repl,_,b)) := BackendDAEUtil.mapEqSystemAndFold(inDAE,allAcausal1,(repl,unreplacable,false));
outDAE := removeSimpleEquationsShared(b,outDAE,repl);
// until remove simple equations does not update assignments and comps remove them
Expand Down Expand Up @@ -360,8 +358,7 @@ algorithm
unreplacable := addUnreplacableFromWhens(inDAE,unreplacable);
// do not replace state sets
unreplacable := addUnreplacableFromStateSets(inDAE,unreplacable);
Debug.fcall(Flags.DUMP_REPL, print, "Unreplacable Crefs:\n");
Debug.fcall(Flags.DUMP_REPL, BaseHashSet.dumpHashSet, unreplacable);
Debug.fcall2(Flags.DUMP_REPL, BackendDump.dumpHashSet, unreplacable, "Unreplacable Crefs:");
(outDAE,(repl,_,b)) := BackendDAEUtil.mapEqSystemAndFold(inDAE,causal1,(repl,unreplacable,false));
outDAE := removeSimpleEquationsShared(b,outDAE,repl);
// until remove simple equations does not update assignments and comps remove them
Expand Down Expand Up @@ -3696,70 +3693,78 @@ end addUnreplacableFromWhenEqn;

protected function addUnreplacableFromWhenStmt "function addUnreplacableFromWhenStmt
author: Frenkel TUD 2012-12"
input DAE.Statement inWhen;
input HashSet.HashSet iHs;
output HashSet.HashSet oHs;
input DAE.Statement inStmt;
input HashSet.HashSet inHS;
output HashSet.HashSet outHS;
algorithm
oHs := match(inWhen,iHs)
outHS := matchcontinue(inStmt, inHS)
local
DAE.Statement stmt;
list< DAE.Statement> stmts;
list<DAE.Statement> stmts;
HashSet.HashSet hs;
DAE.ComponentRef cr;
case (DAE.STMT_WHEN(statementLst=stmts, elseWhen=NONE()),_)
equation
hs = List.fold(stmts,addUnreplacableFromStmt,iHs);
then
hs;
case (DAE.STMT_WHEN(statementLst=stmts, elseWhen=SOME(stmt)),_)
equation
hs = List.fold(stmts,addUnreplacableFromStmt,iHs);
hs = addUnreplacableFromWhenStmt(stmt,hs);
then
hs;

case (DAE.STMT_WHEN(statementLst=stmts, elseWhen=NONE()), _) equation
hs = List.fold(stmts, addUnreplacableFromStmt, inHS);
then hs;

case (DAE.STMT_WHEN(statementLst=stmts, elseWhen=SOME(stmt)), _) equation
hs = List.fold(stmts, addUnreplacableFromStmt, inHS);
hs = addUnreplacableFromWhenStmt(stmt, hs);
then hs;

// add also lhs of array assign stmts because these are not replacable with array(...)
case (DAE.STMT_ASSIGN_ARR(componentRef=cr),_)
equation
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr, iHs);
then
hs;
else then iHs;
end match;
case (DAE.STMT_ASSIGN_ARR(componentRef=cr), _) equation
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr, inHS);
then hs;

// lochel: do not replace arrays that appear on lhs [#2271]
// TODO: improve this case, it blocks too much simplifications
case (DAE.STMT_ASSIGN(exp1=DAE.CREF(componentRef=cr)), _) equation
// DAE.T_ARRAY(ty=_) = ComponentReference.crefLastType(cr);
// failure({} = ComponentReference.crefLastSubs(cr));
// true = ComponentReference.isArrayElement(cr);
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr, inHS);
then hs;

else
then inHS;
end matchcontinue;
end addUnreplacableFromWhenStmt;

protected function addUnreplacableFromStmt "function addUnreplacableFromStmt
author: Frenkel TUD 2012-12"
input DAE.Statement inStmt;
input HashSet.HashSet iHs;
output HashSet.HashSet oHs;
input HashSet.HashSet inHS;
output HashSet.HashSet outHS;
algorithm
oHs := match(inStmt,iHs)
outHS := match(inStmt, inHS)
local
DAE.ComponentRef cr;
HashSet.HashSet hs;
list<DAE.Exp> expExpLst;
list<DAE.ComponentRef> crlst;
case (DAE.STMT_ASSIGN(exp1=DAE.CREF(componentRef=cr)),_)
equation
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr,iHs);
then
hs;
case (DAE.STMT_TUPLE_ASSIGN(expExpLst=expExpLst),_)
equation
crlst = List.flatten(List.map(expExpLst,Expression.extractCrefsFromExp));
crlst = List.map(crlst,ComponentReference.crefStripLastSubs);
hs = List.fold(crlst,BaseHashSet.add,iHs);
then
hs;
case (DAE.STMT_ASSIGN_ARR(componentRef=cr),_)
equation
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr,iHs);
then
hs;
else then iHs;

case (DAE.STMT_ASSIGN(exp1=DAE.CREF(componentRef=cr)), _) equation
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr, inHS);
then hs;

case (DAE.STMT_TUPLE_ASSIGN(expExpLst=expExpLst), _) equation
crlst = List.flatten(List.map(expExpLst, Expression.extractCrefsFromExp));
crlst = List.map(crlst, ComponentReference.crefStripLastSubs);
hs = List.fold(crlst, BaseHashSet.add, inHS);
then hs;

case (DAE.STMT_ASSIGN_ARR(componentRef=cr), _) equation
cr = ComponentReference.crefStripLastSubs(cr);
hs = BaseHashSet.add(cr, inHS);
then hs;

else
then inHS;
end match;
end addUnreplacableFromStmt;

Expand Down
77 changes: 46 additions & 31 deletions Compiler/FrontEnd/CheckModel.mo
Original file line number Diff line number Diff line change
Expand Up @@ -413,25 +413,33 @@ algorithm
DAE.Type tp;
DAE.Ident iteratorName;
String str;

// a := expr;
case (DAE.STMT_ASSIGN(exp1 = exp1), _)
equation
((_, ht)) = Expression.traverseExpTopDown(exp1, statementOutputsCrefFinder, iht);
then
ht;
// (a, b, c) := foo(...)
case (DAE.STMT_TUPLE_ASSIGN(expExpLst = expl), _)
equation
((_, ht)) = Expression.traverseExpListTopDown(expl, statementOutputsCrefFinder, iht);
then
ht;
// v := expr where v is array.
case (DAE.STMT_ASSIGN_ARR(componentRef = cr), _)
equation
crlst = ComponentReference.expandCref(cr, true);
ht = List.fold(crlst, BaseHashSet.add, iht);
then ht;
DAE.Ident ident;

// a := expr;
case (DAE.STMT_ASSIGN(exp1 = exp1), _) equation
((_, ht)) = Expression.traverseExpTopDown(exp1, statementOutputsCrefFinder, iht);
then ht;

// (a, b, ...) := expr;
case (DAE.STMT_TUPLE_ASSIGN(expExpLst = expl), _) equation
((_, ht)) = Expression.traverseExpListTopDown(expl, statementOutputsCrefFinder, iht);
then ht;

// a := expr; // where a is array with an empty list as subscript
case (DAE.STMT_ASSIGN_ARR(componentRef=cr), _) equation
{} = ComponentReference.crefLastSubs(cr);
crlst = ComponentReference.expandCref(cr, true);
ht = List.fold(crlst, BaseHashSet.add, iht);
then ht;

// a := expr; // where a is array
case (DAE.STMT_ASSIGN_ARR(componentRef=cr), _) equation
failure({} = ComponentReference.crefLastSubs(cr));
cr = ComponentReference.crefSetLastSubs(cr, {DAE.WHOLEDIM()});
crlst = ComponentReference.expandCref(cr, true);
ht = List.fold(crlst, BaseHashSet.add, iht);
then ht;

case(DAE.STMT_IF(statementLst = stmts, else_ = elsebranch), _)
equation
ht = List.fold(stmts, statementOutputs, iht);
Expand Down Expand Up @@ -491,12 +499,11 @@ algorithm
ht = List.fold(stmts, statementOutputs, iht);
then ht;
case(DAE.STMT_THROW(source=_), _) then iht;
else
equation
str = DAEDump.ppStatementStr(inStatement);
Debug.fprintln(Flags.FAILTRACE, "- CheckModel.statementOutputs failed for " +& str +& "\n");
then
fail();

else equation
str = DAEDump.ppStatementStr(inStatement);
Debug.fprintln(Flags.FAILTRACE, "- CheckModel.statementOutputs failed for " +& str +& "\n");
then fail();
end matchcontinue;
end statementOutputs;

Expand Down Expand Up @@ -550,12 +557,20 @@ algorithm
case((e as DAE.CREF(ty=DAE.T_COMPLEX(complexClassType = ClassInf.EXTERNAL_OBJ(path=_))), ht))
then
((e, false, ht));
case((e as DAE.CREF(componentRef=cr), ht))
equation
crlst = ComponentReference.expandCref(cr, true);
ht = List.fold(crlst, BaseHashSet.add, ht);
then
((e, false, ht));

case((e as DAE.CREF(componentRef=cr), ht)) equation
{} = ComponentReference.crefLastSubs(cr);
crlst = ComponentReference.expandCref(cr, true);
ht = List.fold(crlst, BaseHashSet.add, ht);
then ((e, false, ht));

case((e as DAE.CREF(componentRef=cr), ht)) equation
failure({} = ComponentReference.crefLastSubs(cr));
cr = ComponentReference.crefSetLastSubs(cr, {DAE.WHOLEDIM()});
crlst = ComponentReference.expandCref(cr, true);
ht = List.fold(crlst, BaseHashSet.add, ht);
then ((e, false, ht));

case((e as DAE.ASUB(exp=exp), ht))
equation
((_, ht)) = Expression.traverseExpTopDown(exp, statementOutputsCrefFinder, ht);
Expand Down

0 comments on commit d7242d2

Please sign in to comment.