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

Commit 88c0b81

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Do more branch selection during flattening.
- Do more branch selection during the flattening phase, waiting until model simplification might lead to trying to e.g. evaluate invalid constants in a branch that won't be selected anyway. Belonging to [master]: - #2729 - OpenModelica/OpenModelica-testsuite#1054
1 parent 3753d6d commit 88c0b81

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ uniontype EvalTarget
108108
case ATTRIBUTE() then true;
109109
case RANGE() then true;
110110
case CONDITION() then true;
111+
case GENERIC() then true;
111112
else false;
112113
end match;
113114
end hasInfo;
@@ -121,6 +122,7 @@ uniontype EvalTarget
121122
case ATTRIBUTE() then Binding.getInfo(target.binding);
122123
case RANGE() then target.info;
123124
case CONDITION() then target.info;
125+
case GENERIC() then target.info;
124126
else Absyn.dummyInfo;
125127
end match;
126128
end getInfo;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -940,41 +940,60 @@ function flattenIfEquation
940940
input ComponentRef prefix;
941941
input output list<Equation> equations;
942942
protected
943+
Equation.Branch branch;
943944
list<Equation.Branch> branches, bl = {};
944945
Expression cond;
945946
list<Equation> eql;
946947
Variability var;
947948
Boolean has_connect;
948949
DAE.ElementSource src;
950+
SourceInfo info;
951+
Ceval.EvalTarget target;
949952
algorithm
950953
Equation.IF(branches = branches, source = src) := eq;
951954
has_connect := Equation.contains(eq, isConnectEq);
952955

953-
for b in branches loop
954-
bl := match b
956+
// Print errors for unbound constants/parameters if the if-equation contains
957+
// connects, since we must select a branch in that case.
958+
target := if has_connect then
959+
Ceval.EvalTarget.GENERIC(Equation.info(eq)) else
960+
Ceval.EvalTarget.IGNORE_ERRORS();
961+
962+
while not listEmpty(branches) loop
963+
branch :: branches := branches;
964+
965+
bl := match branch
955966
case Equation.Branch.BRANCH(cond, var, eql)
956967
algorithm
957968
// Flatten the condition and body of the branch.
958969
cond := flattenExp(cond, prefix);
959970
eql := flattenEquations(eql, prefix);
960971

961-
// Force branch selection if the if-equation contains connects.
962-
if has_connect then
963-
cond := Ceval.evalExp(cond);
972+
// Evaluate structural conditions.
973+
if var <= Variability.STRUCTURAL_PARAMETER then
974+
cond := Ceval.evalExp(cond, target);
964975

965-
if not Expression.isBoolean(cond) then
976+
// Conditions in an if-equation that contains connects must be possible to evaluate.
977+
if not Expression.isBoolean(cond) and has_connect then
966978
Error.addInternalError(
967979
"Failed to evaluate branch condition in if equation containing connect equations: `" +
968980
Expression.toString(cond) + "`", Equation.info(eq));
969981
fail();
970982
end if;
971983
end if;
972984

973-
if Expression.isTrue(cond) and listEmpty(bl) then
974-
// If the condition is literal true and we haven't collected any other
975-
// branches yet, replace the if equation with this branch.
976-
equations := listAppend(eql, equations);
977-
return;
985+
if Expression.isTrue(cond) then
986+
// The condition is true and the branch will thus always be selected
987+
// if reached, so we can discard the remaining branches.
988+
branches := {};
989+
990+
if listEmpty(bl) then
991+
// If we haven't collected any other branches yet, replace the if-equation with this branch.
992+
equations := listAppend(eql, equations);
993+
else
994+
// Otherwise, append this branch.
995+
bl := Equation.makeBranch(cond, eql, var) :: bl;
996+
end if;
978997
elseif not Expression.isFalse(cond) then
979998
// Only add the branch to the list of branches if the condition is not
980999
// literal false, otherwise just drop it since it will never trigger.
@@ -983,25 +1002,27 @@ algorithm
9831002
then
9841003
bl;
9851004

986-
// An invalid branch must have a false condition if the if-equation
987-
// contains connects, anything else is an error.
988-
case Equation.Branch.INVALID_BRANCH(branch = Equation.Branch.BRANCH(condition = cond))
1005+
// An invalid branch must have a false condition, anything else is an error.
1006+
case Equation.Branch.INVALID_BRANCH(branch =
1007+
Equation.Branch.BRANCH(condition = cond, conditionVar = var))
9891008
guard has_connect
9901009
algorithm
991-
cond := Ceval.evalExp(cond);
1010+
if var <= Variability.STRUCTURAL_PARAMETER then
1011+
cond := Ceval.evalExp(cond, target);
1012+
end if;
9921013

9931014
if not Expression.isFalse(cond) then
994-
Equation.Branch.triggerErrors(b);
1015+
Equation.Branch.triggerErrors(branch);
9951016
end if;
9961017
then
9971018
bl;
9981019

999-
else b :: bl;
1020+
else branch :: bl;
10001021
end match;
1001-
end for;
1022+
end while;
10021023

1003-
// Add the flattened if equation to the list of equations if we got this far,
1004-
// and there are any branches still remaining.
1024+
// Add the flattened if-equation to the list of equations if there are any
1025+
// branches still remaining.
10051026
if not listEmpty(bl) then
10061027
equations := Equation.IF(listReverseInPlace(bl), src) :: equations;
10071028
end if;

Compiler/NFFrontEnd/NFSimplifyModel.mo

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,7 @@ algorithm
356356
accum := match branch
357357
case Equation.Branch.BRANCH(cond, var, body)
358358
algorithm
359-
if var <= Variability.STRUCTURAL_PARAMETER then
360-
cond := Ceval.evalExp(cond);
361-
else
362-
cond := SimplifyExp.simplify(cond);
363-
end if;
359+
cond := SimplifyExp.simplify(cond);
364360

365361
// A branch with condition true will always be selected when encountered.
366362
if Expression.isTrue(cond) then

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2729,8 +2729,7 @@ algorithm
27292729
Equation.Branch.BRANCH(cond, _, eql) := b;
27302730
(cond, var) := typeCondition(cond, origin, source, Error.IF_CONDITION_TYPE_ERROR);
27312731

2732-
if ExpOrigin.flagNotSet(next_origin, ExpOrigin.NONEXPANDABLE) and
2733-
(var > Variability.PARAMETER or isNonExpandableExp(cond)) then
2732+
if var > Variability.PARAMETER or isNonExpandableExp(cond) then
27342733
// If the condition doesn't fulfill the requirements for allowing
27352734
// connections in the branch, mark the origin so we can check that when
27362735
// typing the body of the branch.

0 commit comments

Comments
 (0)