Skip to content

Commit

Permalink
Try to fix #3482/#3498
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Oct 8, 2015
1 parent 10f5341 commit 1363820
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 27 deletions.
13 changes: 13 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2659,6 +2659,19 @@ algorithm
(outVarLst, outIntegerLst) := getVar(inComponentRef, inShared.knownVars);
end getVarShared;

public function containsCref
input DAE.ComponentRef cr;
input BackendDAE.Variables inVariables;
output Boolean outB;
algorithm
try
getVar(cr, inVariables);
outB := true;
else
outB := false;
end try;
end containsCref;

public function getVar
"author: PA
Return a variable(s) and its index(es) in the vector.
Expand Down
93 changes: 66 additions & 27 deletions Compiler/BackEnd/SynchronousFeatures.mo
Expand Up @@ -76,7 +76,7 @@ algorithm
BackendDAE.Shared shared;

case (BackendDAE.DAE({syst}, shared)) guard(not Flags.isSet(Flags.NO_PARTITIONING))
then clockPartitioning1(syst, shared);
then clockPartitioning1(syst, shared);
// TODO: Improve support for partitioned systems of equations
case _ guard(not Flags.isSet(Flags.NO_PARTITIONING)) equation
BackendDAE.DAE({syst}, shared) = BackendDAEOptimize.collapseIndependentBlocks(inDAE);
Expand Down Expand Up @@ -126,7 +126,7 @@ protected
array<Integer> varsPartition;
list<BackendDAE.Equation> unpartRemEqs;
algorithm
syst := substituteParitionOpExps(inSyst);
syst := substituteParitionOpExps(inSyst, inShared);

(contSysts, clockedSysts, unpartRemEqs) := baseClockPartitioning(syst, shared);

Expand Down Expand Up @@ -1277,6 +1277,7 @@ protected function substituteParitionOpExps
and the equation $var_i = expr_i is added to the equation set.
Also when clauses are created for boolean clocks."
input BackendDAE.EqSystem inSyst;
input BackendDAE.Shared inShared;
output BackendDAE.EqSystem outSyst;
algorithm
outSyst := match inSyst
Expand All @@ -1292,8 +1293,8 @@ algorithm
algorithm
for i in 1:BackendDAEUtil.equationArraySize(eqs) loop
eq := BackendEquation.equationNth1(eqs, i);
(eq, (newEqs, newVars, cnt)) :=
BackendEquation.traverseExpsOfEquation(eq, substituteParitionOpExp, (newEqs, newVars, cnt));
(eq, (newEqs, newVars, cnt, _)) :=
BackendEquation.traverseExpsOfEquation(eq, substituteParitionOpExp, (newEqs, newVars, cnt, inShared));
newEqs := eq::newEqs;
end for;
syst.orderedEqs := BackendEquation.listEquation(listReverse(newEqs));
Expand All @@ -1304,24 +1305,25 @@ end substituteParitionOpExps;

protected function substituteParitionOpExp
input DAE.Exp inExp;
input tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer> inTpl;
input tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer, BackendDAE.Shared> inTpl;
output DAE.Exp outExp;
output tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer> outTpl;
output tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer, BackendDAE.Shared> outTpl;
algorithm
(outExp, outTpl) := Expression.traverseExpBottomUp(inExp, substituteParitionOpExp1, inTpl);
end substituteParitionOpExp;

protected function substituteParitionOpExp1
input DAE.Exp inExp;
input tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer> inTpl;
input tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer, BackendDAE.Shared> inTpl;
output DAE.Exp outExp;
output tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer> outTpl;
output tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer, BackendDAE.Shared> outTpl;
protected
list<BackendDAE.Equation> newEqs;
list<BackendDAE.Var> newVars;
Integer cnt;
BackendDAE.Shared shared;
algorithm
(newEqs, newVars, cnt) := inTpl;
(newEqs, newVars, cnt, shared) := inTpl;
(outExp, outTpl) := match inExp
local
Absyn.Path path;
Expand All @@ -1330,12 +1332,12 @@ algorithm
DAE.ClockKind clk;
case DAE.CLKCONST(clk)
equation
(clk, newEqs, newVars, cnt) = substClock(clk, newEqs, newVars, cnt);
(clk, newEqs, newVars, cnt) = substClock(clk, newEqs, newVars, cnt, shared);
then
(DAE.CLKCONST(clk), (newEqs, newVars, cnt));
(DAE.CLKCONST(clk), (newEqs, newVars, cnt, shared));
case DAE.CALL(path = path, expLst = exps, attr = attr)
then
substituteExpsCall(path, exps, attr, newEqs, newVars, cnt);
substituteExpsCall(path, exps, attr, newEqs, newVars, cnt, shared);
else
(inExp, inTpl);
end match;
Expand All @@ -1346,6 +1348,7 @@ protected function substClock
input list<BackendDAE.Equation> inNewEqs;
input list<BackendDAE.Var> inNewVars;
input Integer inCnt;
input BackendDAE.Shared inShared;
output DAE.ClockKind outClk;
output list<BackendDAE.Equation> outNewEqs;
output list<BackendDAE.Var> outNewVars;
Expand All @@ -1366,41 +1369,76 @@ algorithm
(DAE.BOOLEAN_CLOCK(e, f), eqs, vars, cnt);
case DAE.REAL_CLOCK(e)
equation
(e, eqs, vars, cnt) = substClockExp(e, inNewEqs, inNewVars, inCnt);
(e, eqs, vars, cnt) = substClockExp(e, inNewEqs, inNewVars, inCnt, inShared);
then
(DAE.REAL_CLOCK(e), eqs, vars, cnt);
case DAE.INTEGER_CLOCK(e, i)
equation
(e, eqs, vars, cnt) = substClockExp(e, inNewEqs, inNewVars, inCnt);
(e, eqs, vars, cnt) = substClockExp(e, inNewEqs, inNewVars, inCnt, inShared);
then
(DAE.INTEGER_CLOCK(e, i), eqs, vars, cnt);
else
(inClk, inNewEqs, inNewVars, inCnt);
end match;
end substClock;

protected function isKnownOrConstantExp "author: lochel
Returns true if the given expression is constant or at least known (parameter dependent)."
input DAE.Exp inExp;
input BackendDAE.Variables inKnownVars;
output Boolean outKnown;
algorithm
(_, (outKnown, _)) := Expression.traverseExpTopDown(inExp, isKnownOrConstantExp_traverser, (true, inKnownVars));
end isKnownOrConstantExp;

protected function isKnownOrConstantExp_traverser
input DAE.Exp inExp;
input tuple<Boolean, BackendDAE.Variables> inTpl;
output DAE.Exp outExp = inExp;
output Boolean outContinue;
output tuple<Boolean, BackendDAE.Variables> outTpl;
protected
BackendDAE.Variables knownVars;
Boolean isKnown;
algorithm
(isKnown, knownVars) := inTpl;
isKnown := match inExp
local
DAE.ComponentRef componentRef;
case DAE.CALL() then false;
case DAE.CREF(componentRef=componentRef) then BackendVariable.containsCref(componentRef, knownVars);
else isKnown;
end match;

outTpl := (isKnown, knownVars);
outContinue := isKnown;
end isKnownOrConstantExp_traverser;

protected function substClockExp
input DAE.Exp inExp;
input list<BackendDAE.Equation> inNewEqs;
input list<BackendDAE.Var> inNewVars;
input Integer inCnt;
input BackendDAE.Shared inShared;
output DAE.Exp outExp;
output list<BackendDAE.Equation> outNewEqs;
output list<BackendDAE.Var> outNewVars;
output Integer outCnt;
protected
DAE.Exp e;
list<BackendDAE.Equation> eqs;
list<BackendDAE.Var> vars;
Integer cnt;
DAE.Type ty;
algorithm
({outExp}, outNewEqs, outNewVars, outCnt) := substExp({inExp}, inNewEqs, inNewVars, inCnt);
outExp := match outExp
local DAE.Type ty;
case DAE.CREF(_, ty)
then Expression.makePureBuiltinCall("previous", {outExp}, ty);
else outExp;
end match;
if isKnownOrConstantExp(inExp, inShared.knownVars) then
outExp := inExp;
outNewEqs := inNewEqs;
outNewVars := inNewVars;
outCnt := inCnt;
else
({outExp}, outNewEqs, outNewVars, outCnt) := substExp({inExp}, inNewEqs, inNewVars, inCnt);
outExp := match outExp
case DAE.CREF(_, ty) then Expression.makePureBuiltinCall("previous", {outExp}, ty);
else outExp;
end match;
end if;
end substClockExp;

protected function substituteExpsCall
Expand All @@ -1410,8 +1448,9 @@ protected function substituteExpsCall
input list<BackendDAE.Equation> inEqs;
input list<BackendDAE.Var> inVars;
input Integer inCnt;
input BackendDAE.Shared inShared;
output DAE.Exp outExp;
output tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer> outTpl;
output tuple<list<BackendDAE.Equation>,list<BackendDAE.Var>, Integer, BackendDAE.Shared> outTpl;
protected
Boolean replace;
list<DAE.Exp> exps;
Expand All @@ -1433,7 +1472,7 @@ algorithm
if replace then substExp(inExps, inEqs, inVars, inCnt)
else (inExps, inEqs, inVars, inCnt);
outExp := DAE.CALL(inPath, exps, inAttr);
outTpl := (eqs, vars, cnt);
outTpl := (eqs, vars, cnt, inShared);
end substituteExpsCall;

protected function createVar
Expand Down

0 comments on commit 1363820

Please sign in to comment.