Skip to content

Commit

Permalink
[NF] Ragged dimension improvements.
Browse files Browse the repository at this point in the history
- Flatten and evaluate dimensions in types.
- Various fixes for binding expressions.
  • Loading branch information
perost committed Jun 11, 2020
1 parent bdcc647 commit 89bb7c5
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 14 deletions.
9 changes: 8 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFBuiltinCall.mo
Expand Up @@ -1000,7 +1000,8 @@ protected
if arg_var <= Variability.STRUCTURAL_PARAMETER and
not ExpOrigin.flagSet(origin, ExpOrigin.FUNCTION) and
not Expression.containsIterator(arg, origin) then
arg := Ceval.evalExp(arg);
arg := Ceval.evalExpBinding(arg);
arg := Expression.getScalarBindingExp(arg);
arg_ty := Expression.typeOf(arg);
else
evaluated := false;
Expand All @@ -1013,6 +1014,12 @@ protected
Expression.toString(arg), Type.toString(arg_ty), "Integer"}, info);
end if;

if not Expression.isInteger(arg) then
// Argument might be a binding expression that needs to be flattened
// before we can evaluate the fill call.
evaluated := false;
end if;

variability := Prefixes.variabilityMax(variability, arg_var);
ty_args := arg :: ty_args;
dims := Dimension.fromExp(arg, arg_var) :: dims;
Expand Down
8 changes: 6 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -757,9 +757,13 @@ protected
Integer max_prop_count;
algorithm
Expression.RANGE(ty = ty, start = start_exp, step = step_exp, stop = stop_exp) := rangeExp;
start_exp := evalExp(start_exp, target);
start_exp := evalExp_impl(start_exp, target);
step_exp := evalExpOpt(step_exp, target);
stop_exp := evalExp(stop_exp, target);
stop_exp := evalExp_impl(stop_exp, target);

start_exp := Expression.getScalarBindingExp(start_exp);
step_exp := Util.applyOption(step_exp, Expression.getScalarBindingExp);
stop_exp := Expression.getScalarBindingExp(stop_exp);

if EvalTarget.isRange(target) then
ty := TypeCheck.getRangeType(start_exp, step_exp, stop_exp,
Expand Down
15 changes: 15 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -195,6 +195,21 @@ public
CREF(ty = ty) := cref;
end nodeType;

function setNodeType
input Type ty;
input output ComponentRef cref;
algorithm
() := match cref
case CREF()
algorithm
cref.ty := ty;
then
();

else ();
end match;
end setNodeType;

function updateNodeType
input output ComponentRef cref;
algorithm
Expand Down
15 changes: 15 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFDimension.mo
Expand Up @@ -35,6 +35,7 @@ protected
import Operator = NFOperator;
import Prefixes = NFPrefixes;
import List;
import SimplifyExp = NFSimplifyExp;

public
import Absyn.{Exp, Path, Subscript};
Expand Down Expand Up @@ -445,5 +446,19 @@ public
end for;
end foldExpList;

function simplify
input output Dimension dim;
algorithm
() := match dim
case EXP()
algorithm
dim.exp := SimplifyExp.simplify(dim.exp);
then
();

else ();
end match;
end simplify;

annotation(__OpenModelica_Interface="frontend");
end NFDimension;
42 changes: 36 additions & 6 deletions OMCompiler/Compiler/NFFrontEnd/NFEvalConstants.mo
Expand Up @@ -47,6 +47,7 @@ import Variable = NFVariable;
import Algorithm = NFAlgorithm;
import NFEquation.Branch;
import Dimension = NFDimension;
import NFTyping.ExpOrigin;

protected
import MetaModelica.Dangerous.*;
Expand Down Expand Up @@ -140,7 +141,7 @@ function evaluateExpTraverser
protected
Expression e;
ComponentRef cref;
Type ty;
Type ty, ty2;
Variability var;
algorithm
outExp := match exp
Expand All @@ -150,15 +151,18 @@ algorithm
Expression.mapFoldShallow(exp,
function evaluateExpTraverser(info = info), false);

// Evaluate constants and structural parameters.
if ComponentRef.nodeVariability(cref) <= Variability.STRUCTURAL_PARAMETER then
// Evaluate all constants and structural parameters.
outExp := Ceval.evalCref(cref, outExp, Ceval.EvalTarget.IGNORE_ERRORS(), evalSubscripts = false);
outExp := Expression.stripBindingInfo(outExp);
outChanged := true;
elseif outChanged then
// If the cref's subscripts changed, recalculate its type.
outExp := Expression.CREF(ComponentRef.getSubscriptedType(cref), cref);
ty := ComponentRef.getSubscriptedType(cref);
end if;

ty2 := evaluateType(ty, info);
if not referenceEq(ty, ty2) then
outExp := Expression.setType(ty2, outExp);
end if;
then
outExp;
Expand All @@ -173,13 +177,34 @@ algorithm
algorithm
(outExp, outChanged) := Expression.mapFoldShallow(exp,
function evaluateExpTraverser(info = info), false);

ty := Expression.typeOf(outExp);
ty2 := evaluateType(ty, info);
then
if outChanged then Expression.retype(outExp) else outExp;
if referenceEq(ty, ty2) then outExp else Expression.setType(ty2, outExp);
end match;

outChanged := changed or outChanged;
end evaluateExpTraverser;

function evaluateType
input output Type ty;
input SourceInfo info;
algorithm
ty := match ty
case Type.ARRAY()
algorithm
ty.dimensions := list(evaluateDimension(d, info) for d in ty.dimensions);
then
ty;

case Type.CONDITIONAL_ARRAY()
then Type.simplifyConditionalArray(ty);

else ty;
end match;
end evaluateType;

function evaluateDimension
input Dimension dim;
input SourceInfo info;
Expand All @@ -191,7 +216,12 @@ algorithm

case Dimension.EXP()
algorithm
e := evaluateExp(dim.exp, info);
if dim.var <= Variability.STRUCTURAL_PARAMETER and not
Expression.containsIterator(dim.exp, ExpOrigin.FOR) then
e := Ceval.evalExp(dim.exp, Ceval.EvalTarget.GENERIC(info));
else
e := evaluateExp(dim.exp, info);
end if;
then
if referenceEq(e, dim.exp) then dim else Dimension.fromExp(e, dim.var);

Expand Down
6 changes: 6 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFExpandExp.mo
Expand Up @@ -59,6 +59,12 @@ public
local
list<Expression> expl;

case Expression.INTEGER() then (exp, true);
case Expression.REAL() then (exp, true);
case Expression.STRING() then (exp, true);
case Expression.BOOLEAN() then (exp, true);
case Expression.ENUM_LITERAL() then (exp, true);

case Expression.CREF(ty = Type.ARRAY()) then expandCref(exp);

// One-dimensional arrays are already expanded.
Expand Down
13 changes: 13 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFExpression.mo
Expand Up @@ -4407,6 +4407,19 @@ public
end match;
end getBindingExp;

function getScalarBindingExp
"Returns the expression contained in a binding expression if it's a scalar,
otherwise returns the given binding expression."
input Expression bindingExp;
output Expression outExp;
algorithm
outExp := getBindingExp(bindingExp);

if Type.isArray(typeOf(outExp)) then
outExp := bindingExp;
end if;
end getScalarBindingExp;

function setBindingExp
"Replaces the expression contained in a binding expression with the given
expression. The given expression is assumed to have the same type as the
Expand Down
38 changes: 35 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -807,6 +807,7 @@ algorithm
end if;

binding.bindingExp := flattenBindingExp(binding.bindingExp, prefix, isTypeAttribute);
binding.bindingType := flattenType(binding.bindingType, prefix);
binding.isFlattened := true;
then
binding;
Expand Down Expand Up @@ -917,19 +918,35 @@ function flattenExp_traverse
input ComponentRef prefix;
algorithm
exp := match exp
case Expression.CREF()
case Expression.CREF(cref = ComponentRef.CREF())
algorithm
exp.cref := ComponentRef.transferSubscripts(prefix, exp.cref);
exp.cref := flattenCref(exp.cref, prefix);
then
exp;

case Expression.BINDING_EXP() then flattenBindingExp(exp, prefix);
case Expression.IF(ty = Type.CONDITIONAL_ARRAY()) then flattenConditionalArrayIfExp(exp);

else exp;
end match;

exp := flattenExpType(exp, prefix);
end flattenExp_traverse;

function flattenCref
input output ComponentRef cref;
input ComponentRef prefix;
protected
Type ty, ty2;
algorithm
cref := ComponentRef.transferSubscripts(prefix, cref);
ty := ComponentRef.nodeType(cref);
ty2 := flattenType(ty, prefix);

if not referenceEq(ty, ty2) then
cref := ComponentRef.setNodeType(ty2, cref);
end if;
end flattenCref;

function flattenConditionalArrayIfExp
input output Expression exp;
protected
Expand All @@ -944,6 +961,20 @@ algorithm
end if;
end flattenConditionalArrayIfExp;

function flattenExpType
input output Expression exp;
input ComponentRef prefix;
protected
Type ty;
algorithm
ty := Expression.typeOf(exp);

if Type.isArray(ty) then
ty := flattenType(ty, prefix);
exp := Expression.setType(ty, exp);
end if;
end flattenExpType;

function flattenType
input output Type ty;
input ComponentRef prefix;
Expand Down Expand Up @@ -1213,6 +1244,7 @@ algorithm
// Unroll the loop by replacing the iterator with each of its values in the for loop body.
range := flattenExp(range, prefix);
range := Ceval.evalExp(range, Ceval.EvalTarget.RANGE(Equation.info(forLoop)));
range := Expression.stripBindingInfo(range);
range_iter := RangeIterator.fromExp(range);

while RangeIterator.hasNext(range_iter) loop
Expand Down
7 changes: 5 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFSimplifyExp.mo
Expand Up @@ -118,17 +118,19 @@ function simplifyRange
protected
Expression start_exp1, stop_exp1, start_exp2, stop_exp2;
Option<Expression> step_exp1, step_exp2;
Type ty;
Type ty, ty2;
algorithm
Expression.RANGE(ty = ty, start = start_exp1, step = step_exp1, stop = stop_exp1) := range;

start_exp2 := simplify(start_exp1);
step_exp2 := simplifyOpt(step_exp1);
stop_exp2 := simplify(stop_exp1);
ty2 := Type.simplify(ty);

if referenceEq(start_exp1, start_exp2) and
referenceEq(step_exp1, step_exp2) and
referenceEq(stop_exp1, stop_exp2) then
referenceEq(stop_exp1, stop_exp2) and
referenceEq(ty, ty2) then
exp := range;
else
ty := TypeCheck.getRangeType(start_exp2, step_exp2, stop_exp2,
Expand Down Expand Up @@ -337,6 +339,7 @@ algorithm
else
algorithm
exp := simplify(exp);
ty := Type.simplify(ty);
then
Expression.CALL(Call.TYPED_ARRAY_CONSTRUCTOR(ty, var, exp, iters));
end matchcontinue;
Expand Down
14 changes: 14 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFType.mo
Expand Up @@ -1212,5 +1212,19 @@ public
str := stringAppendList(strl);
end subscriptedTypeName;

function simplify
input output Type ty;
algorithm
() := match ty
case ARRAY()
algorithm
ty.dimensions := list(Dimension.simplify(d) for d in ty.dimensions);
then
();

else ();
end match;
end simplify;

annotation(__OpenModelica_Interface="frontend");
end NFType;

0 comments on commit 89bb7c5

Please sign in to comment.