Skip to content

Commit 10321c2

Browse files
committed
Fix flattening of crefs with no scalarization
- Keep destination subscripts in ComponentRef.transferSubscripts when there are no source ones, to avoid loosing subscripts when reflattening a cref with a prefix without subscripts. Fixes OpenModelica#13602
1 parent 918a7a3 commit 10321c2

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ public
907907
input ComponentRef srcCref;
908908
input ComponentRef dstCref;
909909
output ComponentRef cref;
910+
protected
911+
list<Subscript> subs;
910912
algorithm
911913
cref := match (srcCref, dstCref)
912914
case (EMPTY(), _) then dstCref;
@@ -923,8 +925,12 @@ public
923925
case (CREF(), CREF()) guard InstNode.refEqual(srcCref.node, dstCref.node)
924926
algorithm
925927
cref := transferSubscripts(srcCref.restCref, dstCref.restCref);
928+
// Don't remove subscripts unless there's something to replace them with.
929+
// This avoids loosing subscripts when flattening an already flattened cref with
930+
// a prefix without subscripts, which can happen in the non-scalarized path.
931+
subs := if listEmpty(srcCref.subscripts) then dstCref.subscripts else srcCref.subscripts;
926932
then
927-
CREF(dstCref.node, srcCref.subscripts, dstCref.ty, dstCref.origin, cref);
933+
CREF(dstCref.node, subs, dstCref.ty, dstCref.origin, cref);
928934

929935
case (CREF(), CREF())
930936
then transferSubscripts(srcCref.restCref, dstCref);

testsuite/flattening/modelica/scodeinst/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,7 @@ VectorizeBindings1.mo \
11901190
VectorizeBindings2.mo \
11911191
VectorizeBindings3.mo \
11921192
VectorizeBindings4.mo \
1193+
VectorizeBindings5.mo \
11931194
VectorTest.mo \
11941195
Visibility1.mo \
11951196
Visibility2.mo \
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// name: VectorizeBindings5
2+
// keywords:
3+
// status: correct
4+
//
5+
6+
model Failing
7+
parameter Boolean initialEquation = true;
8+
Real pOut(start=0);
9+
initial equation
10+
if initialEquation then
11+
pOut = 1;
12+
end if;
13+
equation
14+
der(pOut) = sin(time);
15+
end Failing;
16+
17+
model Module
18+
parameter Boolean initialEquation = true;
19+
Failing f(initialEquation = initialEquation);
20+
end Module;
21+
22+
model VectorizeBindings5
23+
parameter Integer N = 2;
24+
parameter Boolean initialEquation[N] = fill(true,N);
25+
Module module[N](initialEquation = initialEquation);
26+
annotation(__OpenModelica_commandLineOptions="--newBackend");
27+
end VectorizeBindings5;
28+
29+
// Result:
30+
// class VectorizeBindings5
31+
// final parameter Integer N = 2;
32+
// parameter Boolean[2] initialEquation = array(true for $f1 in 1:2);
33+
// parameter Boolean[2] module.initialEquation = array(initialEquation[$module1] for $module1 in 1:2);
34+
// final parameter Boolean[2] module.f.initialEquation = array(module[$module1].initialEquation for $module1 in 1:2);
35+
// Real[2] module.f.pOut(start = array(0.0 for $f1 in 1:2));
36+
// initial equation
37+
// for $i1 in 1:2 loop
38+
// module[$i1].f.pOut = 1.0;
39+
// end for;
40+
// equation
41+
// for $i1 in 1:2 loop
42+
// der(module[$i1].f.pOut) = sin(time);
43+
// end for;
44+
// end VectorizeBindings5;
45+
// endResult

0 commit comments

Comments
 (0)