Skip to content

Commit a74ecac

Browse files
authored
[OB] Fix simplification of if-equation (#13935) (#13941)
Fixes #13927
1 parent 7272f16 commit a74ecac

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

OMCompiler/Compiler/BackEnd/BackendDAEOptimize.mo

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import ExpressionSimplify;
7676
import Error;
7777
import Flags;
7878
import GCExt;
79+
import HashSet;
7980
import HashTableExpToIndex;
8081
import HpcOmTaskGraph;
8182
import List;
@@ -2489,7 +2490,7 @@ algorithm
24892490
iHt;
24902491
case (c::explst,eqns::rest,_)
24912492
equation
2492-
ht = simplifySolvedIfEqns1(c,eqns,iHt);
2493+
ht = simplifySolvedIfEqns1(c,eqns,iHt, HashSet.emptyHashSet());
24932494
then
24942495
simplifySolvedIfEqns(explst,rest,ht);
24952496
end match;
@@ -2501,35 +2502,47 @@ protected function simplifySolvedIfEqns1
25012502
input DAE.Exp condition;
25022503
input list<BackendDAE.Equation> brancheqns;
25032504
input HashTable2.HashTable iHt;
2505+
input HashSet.HashSet iHs;
25042506
output HashTable2.HashTable oHt;
25052507
algorithm
2508+
// ticket #13927
2509+
// Only proceed if `cr` was not already assigned within current branch.
2510+
// This is checked by collecting all `cr` in `hs`.
2511+
// Prevents errors like:
2512+
// if b then a = r1; a = r2; else a = r3; c = r4; end if;
2513+
// to
2514+
// a = if b then r2 else if b then r1 else r3; c = r4;
2515+
25062516
oHt := match(condition,brancheqns,iHt)
25072517
local
25082518
DAE.ComponentRef cr;
25092519
DAE.Exp e,exp;
25102520
DAE.ElementSource source;
25112521
HashTable2.HashTable ht;
2522+
HashSet.HashSet hs;
25122523
list<BackendDAE.Equation> rest;
25132524
case (_,{},_)
25142525
then
25152526
iHt;
25162527
case (_,BackendDAE.EQUATION(exp=DAE.CREF(componentRef=cr), scalar=e)::rest,_)
25172528
equation
25182529
false = Expression.expHasCref(e, cr);
2530+
hs = BaseHashSet.addUnique(cr, iHs);
25192531
exp = BaseHashTable.get(cr, iHt);
25202532
exp = DAE.IFEXP(condition, e, exp);
25212533
ht = BaseHashTable.add((cr,exp), iHt);
25222534
then
2523-
simplifySolvedIfEqns1(condition,rest,ht);
2535+
simplifySolvedIfEqns1(condition,rest,ht, hs);
25242536
case (_,BackendDAE.EQUATION(exp=DAE.UNARY(operator=DAE.UMINUS(), exp=DAE.CREF(componentRef=cr)), scalar=e)::rest,_)
25252537
equation
25262538
false = Expression.expHasCref(e, cr);
2539+
hs = BaseHashSet.addUnique(cr, iHs);
25272540
exp = BaseHashTable.get(cr, iHt);
25282541
e = Expression.negate(e);
25292542
exp = DAE.IFEXP(condition, e, exp);
25302543
ht = BaseHashTable.add((cr,exp), iHt);
25312544
then
2532-
simplifySolvedIfEqns1(condition,rest,ht);
2545+
simplifySolvedIfEqns1(condition,rest,ht, hs);
25332546
end match;
25342547
end simplifySolvedIfEqns1;
25352548

0 commit comments

Comments
 (0)