From 5ddd573df834fc7caed94d35c87699b9bf68dd0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Fri, 30 Jun 2023 12:58:11 +0200 Subject: [PATCH] Fix non-scalarized handling of stream operators (#10908) - Unroll equations that contain stream operators. --- OMCompiler/Compiler/NFFrontEnd/NFCall.mo | 18 ++++++++++ OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo | 17 ++++++++- .../flattening/modelica/scodeinst/Makefile | 1 + .../modelica/scodeinst/NoScalarizeConnect2.mo | 36 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 testsuite/flattening/modelica/scodeinst/NoScalarizeConnect2.mo diff --git a/OMCompiler/Compiler/NFFrontEnd/NFCall.mo b/OMCompiler/Compiler/NFFrontEnd/NFCall.mo index e9326b9cbe2..fe87d76f82e 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFCall.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFCall.mo @@ -2076,6 +2076,24 @@ public end match; end isConnectionsOperator; + function isStreamOperator + input Call call; + output Boolean isOp; + protected + String name; + algorithm + isOp := match call + case TYPED_CALL() + guard Function.isBuiltin(call.fn) + algorithm + name := functionNameFirst(call); + then + name == "actualStream" or name == "inStream"; + + else false; + end match; + end isStreamOperator; + protected function instNormalCall input Absyn.ComponentRef functionName; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo b/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo index f8931a8f5f2..66cd95dd4e4 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo @@ -1817,6 +1817,17 @@ function splitForLoop2 input list forBody; output list connects = {}; output list nonConnects = {}; +protected + function is_conn_operator + input Expression exp; + output Boolean res; + algorithm + res := match exp + case Expression.CALL() + then Call.isConnectionsOperator(exp.call) or Call.isStreamOperator(exp.call); + else false; + end match; + end is_conn_operator; protected list conns, nconns; algorithm @@ -1844,7 +1855,11 @@ algorithm else algorithm - nonConnects := eq :: nonConnects; + if Equation.containsExp(eq, function Expression.contains(func = is_conn_operator)) then + connects := eq :: connects; + else + nonConnects := eq :: nonConnects; + end if; then (); diff --git a/testsuite/flattening/modelica/scodeinst/Makefile b/testsuite/flattening/modelica/scodeinst/Makefile index c3bb8c6284f..2db5db354dc 100644 --- a/testsuite/flattening/modelica/scodeinst/Makefile +++ b/testsuite/flattening/modelica/scodeinst/Makefile @@ -859,6 +859,7 @@ NonexistentRedeclareModifier2.mo \ NoScalarize1.mo \ NoScalarize2.mo \ NoScalarizeConnect1.mo \ +NoScalarizeConnect2.mo \ OperationAdd1.mo \ OperationAddEW1.mo \ OperationDiv1.mo \ diff --git a/testsuite/flattening/modelica/scodeinst/NoScalarizeConnect2.mo b/testsuite/flattening/modelica/scodeinst/NoScalarizeConnect2.mo new file mode 100644 index 00000000000..7cbbbeaed06 --- /dev/null +++ b/testsuite/flattening/modelica/scodeinst/NoScalarizeConnect2.mo @@ -0,0 +1,36 @@ +// name: NoScalarizeConnect2 +// keywords: +// status: correct +// cflags: -d=newInst --newBackend +// + +connector C + Real e; + flow Real f; + stream Real s; +end C; + +model NoScalarizeConnect2 + C c[3]; + Real x; +equation + for i in 1:3 loop + x = actualStream(c[i].s); + end for; +end NoScalarizeConnect2; + +// Result: +// class NoScalarizeConnect2 +// Real[3] c.s; +// Real[3] c.f; +// Real[3] c.e; +// Real x; +// equation +// for $i1 in 1:3 loop +// c[$i1].f = 0.0; +// end for; +// x = c[1].s; +// x = c[2].s; +// x = c[3].s; +// end NoScalarizeConnect2; +// endResult