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

Commit 8fcf925

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Various improvements.
- Fix ordering of equations/statements during simplification, when replacing an if-equation/statement with the first branch. - Unroll for-loops in algorithms when the range has size 1. Belonging to [master]: - #2901
1 parent fcedc05 commit 8fcf925

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

3rdParty

Compiler/NFFrontEnd/NFDimension.mo

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,22 @@ public
218218
algorithm
219219
isZero := match dim
220220
case INTEGER() then dim.size == 0;
221+
case ENUM() then Type.enumSize(dim.enumType) == 0;
221222
else false;
222223
end match;
223224
end isZero;
224225

226+
function isOne
227+
input Dimension dim;
228+
output Boolean isOne;
229+
algorithm
230+
isOne := match dim
231+
case INTEGER() then dim.size == 1;
232+
case ENUM() then Type.enumSize(dim.enumType) == 1;
233+
else false;
234+
end match;
235+
end isOne;
236+
225237
function subscriptType
226238
"Returns the expected type of a subscript for the given dimension."
227239
input Dimension dim;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,8 @@ algorithm
10981098

10991099
while RangeIterator.hasNext(range_iter) loop
11001100
(range_iter, val) := RangeIterator.next(range_iter);
1101-
unrolled_body := list(Equation.mapExp(eq,
1102-
function Expression.replaceIterator(iterator = iter, iteratorValue = val)) for eq in body);
1101+
unrolled_body := Equation.mapExpList(body,
1102+
function Expression.replaceIterator(iterator = iter, iteratorValue = val));
11031103
unrolled_body := flattenEquations(unrolled_body, prefix);
11041104
equations := listAppend(unrolled_body, equations);
11051105
end while;

Compiler/NFFrontEnd/NFSimplifyModel.mo

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import NFBinding.Binding;
4646
import Variable = NFVariable;
4747
import Algorithm = NFAlgorithm;
4848
import Dimension = NFDimension;
49+
import Subscript = NFSubscript;
4950

5051
protected
5152
import MetaModelica.Dangerous.*;
@@ -250,6 +251,8 @@ algorithm
250251
local
251252
Expression e, lhs, rhs;
252253
Type ty;
254+
Dimension dim;
255+
list<Statement> body;
253256

254257
case Statement.ASSIGNMENT()
255258
algorithm
@@ -265,8 +268,17 @@ algorithm
265268

266269
case Statement.FOR(range = SOME(e))
267270
algorithm
268-
if not Type.isEmptyArray(Expression.typeOf(e)) then
269-
stmt.range := SimplifyExp.simplifyOpt(stmt.range);
271+
ty := Expression.typeOf(e);
272+
dim := Type.nthDimension(ty, 1);
273+
274+
if Dimension.isOne(dim) then
275+
e := Expression.applySubscript(Subscript.INDEX(Expression.INTEGER(1)), e);
276+
body := Statement.mapExpList(stmt.body,
277+
function Expression.replaceIterator(iterator = stmt.iterator, iteratorValue = e));
278+
body := simplifyStatements(body);
279+
statements := listAppend(body, statements);
280+
elseif not Dimension.isZero(dim) then
281+
stmt.range := SOME(SimplifyExp.simplify(e));
270282
stmt.body := simplifyStatements(stmt.body);
271283
statements := stmt :: statements;
272284
end if;
@@ -363,7 +375,7 @@ algorithm
363375
if Expression.isTrue(cond) then
364376
if listEmpty(accum) then
365377
// If it's the first branch, remove the if and keep only the branch body.
366-
elements := listAppend(simplifyEquations(body), elements);
378+
elements := listAppend(listReverse(simplifyEquations(body)), elements);
367379
return;
368380
else
369381
// Otherwise just discard the rest of the branches.
@@ -431,7 +443,7 @@ algorithm
431443
if Expression.isTrue(cond) then
432444
if listEmpty(accum) then
433445
// If it's the first branch, remove the if and keep only the branch body.
434-
elements := listAppend(simplifyFunc(body), elements);
446+
elements := listAppend(listReverse(simplifyFunc(body)), elements);
435447
return;
436448
else
437449
// Otherwise just discard the rest of the branches.

Compiler/NFFrontEnd/NFType.mo

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,16 @@ public
822822
ENUMERATION(typePath = name) := ty;
823823
end enumName;
824824

825+
function enumSize
826+
input Type ty;
827+
output Integer size;
828+
protected
829+
list<String> literals;
830+
algorithm
831+
ENUMERATION(literals = literals) := ty;
832+
size := listLength(literals);
833+
end enumSize;
834+
825835
function box
826836
input Type ty;
827837
output Type boxedType;

0 commit comments

Comments
 (0)