Skip to content

Commit 9fe6151

Browse files
committed
[NF] Simplification improvements.
- Expand sum/product reductions with constant iteration ranges. - Simplify vector calls with arrays as argument. - Fix some cases where arrays were incorrectly marked as literal.
1 parent bf1c298 commit 9fe6151

File tree

5 files changed

+216
-112
lines changed

5 files changed

+216
-112
lines changed

OMCompiler/Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ function evalBinaryArrayScalar
11631163
algorithm
11641164
exp := match arrayExp
11651165
case Expression.ARRAY()
1166-
then Expression.ARRAY(arrayExp.ty,
1166+
then Expression.makeArray(arrayExp.ty,
11671167
list(evalBinaryArrayScalar(e, scalarExp, opFunc) for e in arrayExp.elements),
11681168
literal = true);
11691169

@@ -2898,22 +2898,10 @@ protected
28982898
list<Expression> expl;
28992899
Type ty;
29002900
algorithm
2901-
expl := Expression.fold(arg, evalBuiltinVector2, {});
2902-
ty := Type.liftArrayLeft(Type.arrayElementType(Expression.typeOf(arg)),
2903-
Dimension.fromInteger(listLength(expl)));
2904-
result := Expression.makeArray(ty, listReverse(expl), literal = true);
2901+
expl := Expression.arrayScalarElements(arg);
2902+
result := Expression.makeExpArray(expl, isLiteral = true);
29052903
end evalBuiltinVector;
29062904

2907-
function evalBuiltinVector2
2908-
input Expression exp;
2909-
input output list<Expression> expl;
2910-
algorithm
2911-
expl := match exp
2912-
case Expression.ARRAY() then expl;
2913-
else exp :: expl;
2914-
end match;
2915-
end evalBuiltinVector2;
2916-
29172905
function evalBuiltinZeros
29182906
input list<Expression> args;
29192907
output Expression result;
@@ -3196,13 +3184,18 @@ protected
31963184
Function fn;
31973185
Expression exp, default_exp;
31983186
list<tuple<InstNode, Expression>> iters;
3199-
list<Mutable<Expression>> iter_exps;
3200-
list<Expression> ranges;
32013187
Type ty;
32023188
ReductionFn red_fn;
3189+
3190+
function reductionFn
3191+
input Expression exp1;
3192+
input Expression exp2;
3193+
input EvalTarget target;
3194+
input ReductionFn fn;
3195+
output Expression result = fn(exp1, evalExp_impl(exp2, target));
3196+
end reductionFn;
32033197
algorithm
32043198
Expression.CALL(call = Call.TYPED_REDUCTION(fn = fn, exp = exp, iters = iters)) := callExp;
3205-
(exp, ranges, iter_exps) := createIterationRanges(exp, iters);
32063199
ty := Expression.typeOf(exp);
32073200

32083201
(red_fn, default_exp) := match AbsynUtil.pathString(Function.name(fn))
@@ -3218,41 +3211,10 @@ algorithm
32183211
fail();
32193212
end match;
32203213

3221-
result := evalReduction2(exp, ranges, iter_exps, default_exp, red_fn);
3214+
result := Expression.foldReduction(exp, iters, default_exp,
3215+
function evalExp_impl(target = EvalTarget.IGNORE_ERRORS()), red_fn);
32223216
end evalReduction;
32233217

3224-
function evalReduction2
3225-
input Expression exp;
3226-
input list<Expression> ranges;
3227-
input list<Mutable<Expression>> iterators;
3228-
input Expression foldExp;
3229-
input ReductionFn fn;
3230-
output Expression result;
3231-
protected
3232-
Expression range;
3233-
list<Expression> ranges_rest, expl = {};
3234-
Mutable<Expression> iter;
3235-
list<Mutable<Expression>> iters_rest;
3236-
ExpressionIterator range_iter;
3237-
Expression value;
3238-
Type el_ty;
3239-
algorithm
3240-
if listEmpty(ranges) then
3241-
result := fn(foldExp, evalExp_impl(exp, EvalTarget.IGNORE_ERRORS()));
3242-
else
3243-
range :: ranges_rest := ranges;
3244-
iter :: iters_rest := iterators;
3245-
range_iter := ExpressionIterator.fromExp(range);
3246-
result := foldExp;
3247-
3248-
while ExpressionIterator.hasNext(range_iter) loop
3249-
(range_iter, value) := ExpressionIterator.next(range_iter);
3250-
Mutable.update(iter, value);
3251-
result := evalReduction2(exp, ranges_rest, iters_rest, result, fn);
3252-
end while;
3253-
end if;
3254-
end evalReduction2;
3255-
32563218
function evalSize
32573219
input Expression exp;
32583220
input Option<Expression> optIndex;

OMCompiler/Compiler/NFFrontEnd/NFEvalFunction.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ algorithm
11011101
algorithm
11021102
dims := ModelicaExternalC.ModelicaIO_readMatrixSizes(s1, s2);
11031103
then
1104-
Expression.ARRAY(Type.ARRAY(Type.INTEGER(), {Dimension.fromInteger(2)}),
1104+
Expression.makeArray(Type.ARRAY(Type.INTEGER(), {Dimension.fromInteger(2)}),
11051105
{Expression.INTEGER(dims[1]), Expression.INTEGER(dims[2])}, true);
11061106

11071107
case ("ModelicaIO_readRealMatrix",
@@ -1169,11 +1169,11 @@ algorithm
11691169
for c in 1:ncol loop
11701170
row := Expression.REAL(matrix[r, c]) :: row;
11711171
end for;
1172-
rows := Expression.ARRAY(ty, row, literal = true) :: rows;
1172+
rows := Expression.makeArray(ty, row, literal = true) :: rows;
11731173
end for;
11741174

11751175
ty := Type.liftArrayLeft(ty, Dimension.fromInteger(nrow));
1176-
result := Expression.ARRAY(ty, rows, literal = true);
1176+
result := Expression.makeArray(ty, rows, literal = true);
11771177
end evaluateModelicaIO_readRealMatrix;
11781178

11791179
function evaluateExternal2

0 commit comments

Comments
 (0)