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

Commit

Permalink
[NF] Performance improvements.
Browse files Browse the repository at this point in the history
- Guard some Error.assertions that otherwise would do lots of
  unnecessary work.
- Save evaluated bindings.
- Improve evaluation of subscripted ranges.

Belonging to [master]:
  - #2436
  - OpenModelica/OpenModelica-testsuite#945
  • Loading branch information
perost authored and OpenModelica-Hudson committed May 16, 2018
1 parent ed95fad commit 17adb9a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
2 changes: 1 addition & 1 deletion 3rdParty
Submodule 3rdParty updated 1 files
+0 −1 README.md
10 changes: 7 additions & 3 deletions Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -1649,7 +1649,11 @@ algorithm
end for;
for i in 1:(dim-1) loop
j := min(listHead(d) for d in dimsLst);
Error.assertion(j == max(listHead(d) for d in dimsLst), getInstanceName() + ": cat got uneven dimensions for dim=" + String(i) + " " + stringDelimitList(list(toString(e) for e in exps), ", "), sourceInfo());

if j <> max(listHead(d) for d in dimsLst) then
Error.assertion(false, getInstanceName() + ": cat got uneven dimensions for dim=" + String(i) + " " + stringDelimitList(list(toString(e) for e in exps), ", "), sourceInfo());
end if;

firstDims := j :: firstDims;
dimsLst := list(listRest(d) for d in dimsLst);
end for;
Expand Down Expand Up @@ -1707,8 +1711,8 @@ algorithm
(arr, dims) := evalCatGetFlatArray(exp, dim-1, getArrayContents=getArrayContents, toString=toString);
if listEmpty(outDims) then
outDims := dims;
else
Error.assertion(valueEq(dims, outDims), getInstanceName() + ": Got unbalanced array from " + toString(e), sourceInfo());
elseif not valueEq(dims, outDims) then
Error.assertion(false, getInstanceName() + ": Got unbalanced array from " + toString(e), sourceInfo());
end if;
outExps := listAppend(arr, outExps);
i := i+1;
Expand Down
68 changes: 50 additions & 18 deletions Compiler/NFFrontEnd/NFCeval.mo
Expand Up @@ -139,11 +139,7 @@ algorithm
case Expression.CREF(cref = cref as ComponentRef.CREF(node = c as InstNode.COMPONENT_NODE(),
origin = NFComponentRef.Origin.CREF))
algorithm
exp_origin := if Class.isFunction(InstNode.getClass(InstNode.parent(c)))
then ExpOrigin.FUNCTION else ExpOrigin.CLASS;
Typing.typeComponentBinding(c, exp_origin);
binding := Component.getBinding(InstNode.component(c));
exp1 := evalBinding(binding, exp, target);
exp1 := evalComponentBinding(c, exp, target);
then
Expression.applySubscripts(cref.subscripts, exp1);

Expand Down Expand Up @@ -236,15 +232,7 @@ algorithm
exp1;

case Expression.SUBSCRIPTED_EXP()
algorithm
exp1 := evalExp(exp.exp, target);

for s in exp.subscripts loop
exp2 := evalExp(s, target);
exp1 := Expression.applyIndexSubscript(exp2, exp1);
end for;
then
exp1;
then evalSubscriptedExp(exp.exp, exp.subscripts, target);

else exp;
end match;
Expand All @@ -263,26 +251,49 @@ algorithm
end match;
end evalExpOpt;

function evalBinding
input Binding binding;
function evalComponentBinding
input InstNode node;
input Expression originExp "The expression the binding came from, e.g. a cref.";
input EvalTarget target;
output Expression exp;
protected
ExpOrigin.Type exp_origin;
Component comp;
Binding binding;
algorithm
exp_origin := if Class.isFunction(InstNode.getClass(InstNode.parent(node)))
then ExpOrigin.FUNCTION else ExpOrigin.CLASS;

Typing.typeComponentBinding(node, exp_origin);
comp := InstNode.component(node);
binding := Component.getBinding(comp);

exp := match binding
case Binding.TYPED_BINDING() then evalExp(binding.bindingExp, target);
case Binding.TYPED_BINDING()
algorithm
exp := evalExp(binding.bindingExp, target);

if not referenceEq(exp, binding.bindingExp) then
binding.bindingExp := exp;
comp := Component.setBinding(binding, comp);
InstNode.updateComponent(comp, node);
end if;
then
exp;

case Binding.UNBOUND()
algorithm
printUnboundError(target, originExp);
then
originExp;

else
algorithm
Error.addInternalError(getInstanceName() + " failed on untyped binding", sourceInfo());
then
fail();
end match;
end evalBinding;
end evalComponentBinding;

function evalTypename
input Type ty;
Expand Down Expand Up @@ -2255,5 +2266,26 @@ algorithm
end if;
end evalReduction2;

function evalSubscriptedExp
input Expression exp;
input list<Expression> subs;
input EvalTarget target;
output Expression result;
algorithm
result := match exp
case Expression.RANGE()
then Expression.RANGE(exp.ty,
evalExp(exp.start, target),
evalExpOpt(exp.step, target),
evalExp(exp.stop, target));

else evalExp(exp, target);
end match;

for s in subs loop
result := Expression.applyIndexSubscript(evalExp(s, target), result);
end for;
end evalSubscriptedExp;

annotation(__OpenModelica_Interface="frontend");
end NFCeval;
11 changes: 9 additions & 2 deletions Compiler/NFFrontEnd/NFClassTree.mo
Expand Up @@ -605,8 +605,15 @@ public
end for;

// Sanity check.
Error.assertion(comp_idx == compCount + 1, getInstanceName() + " miscounted components in " + InstNode.name(clsNode), sourceInfo());
Error.assertion(cls_idx == classCount + 1, getInstanceName() + " miscounted classes in " + InstNode.name(clsNode), sourceInfo());
if comp_idx <> compCount + 1 then
Error.assertion(false, getInstanceName() + " miscounted components in " +
InstNode.name(clsNode), sourceInfo());
end if;

if cls_idx <> classCount + 1 then
Error.assertion(false, getInstanceName() + " miscounted classes in " +
InstNode.name(clsNode), sourceInfo());
end if;

// Create a new class tree and update the class in the node.
cls.elements := INSTANTIATED_TREE(ltree, clss, comps, local_comps, exts, imps, dups);
Expand Down

0 comments on commit 17adb9a

Please sign in to comment.