Skip to content

Commit

Permalink
Improve flattening performance (#8573)
Browse files Browse the repository at this point in the history
- Change the flattening of subscripted expressions with split indices so
  that the subscripting is done before flattening the subscripted
  expression, to avoid flattening large expressions over and over.
  • Loading branch information
perost committed Feb 17, 2022
1 parent ca7559a commit caa8835
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
9 changes: 6 additions & 3 deletions OMCompiler/Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -1608,20 +1608,23 @@ public
case UNTYPED_ARRAY_CONSTRUCTOR()
algorithm
e := func(call.exp);
iters := mapIteratorsExpShallow(call.iters, func);
then
UNTYPED_ARRAY_CONSTRUCTOR(e, call.iters);
UNTYPED_ARRAY_CONSTRUCTOR(e, iters);

case TYPED_ARRAY_CONSTRUCTOR()
algorithm
e := func(call.exp);
iters := mapIteratorsExpShallow(call.iters, func);
then
TYPED_ARRAY_CONSTRUCTOR(call.ty, call.var, call.purity, e, call.iters);
TYPED_ARRAY_CONSTRUCTOR(call.ty, call.var, call.purity, e, iters);

case UNTYPED_REDUCTION()
algorithm
e := func(call.exp);
iters := mapIteratorsExpShallow(call.iters, func);
then
UNTYPED_REDUCTION(call.ref, e, call.iters);
UNTYPED_REDUCTION(call.ref, e, iters);

case TYPED_REDUCTION()
algorithm
Expand Down
25 changes: 13 additions & 12 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -1034,31 +1034,31 @@ end flattenBinding;
public function flattenExp
input output Expression exp;
input ComponentRef prefix;
algorithm
exp := Expression.map(exp, function flattenExp_traverse(prefix = prefix));
end flattenExp;

function flattenExp_traverse
input output Expression exp;
input ComponentRef prefix;
algorithm
exp := match exp
case Expression.CREF(cref = ComponentRef.CREF())
algorithm
exp.cref := ComponentRef.mapExpShallow(exp.cref, function flattenExp(prefix = prefix));
exp.cref := flattenCref(exp.cref, prefix);
exp.ty := flattenType(exp.ty, prefix);
then
exp;

case Expression.SUBSCRIPTED_EXP()
then replaceSplitIndices(exp.exp, exp.subscripts, prefix);
case Expression.SUBSCRIPTED_EXP(split = true)
then Expression.mapShallow(
replaceSplitIndices(exp.exp, exp.subscripts, prefix),
function flattenExp(prefix = prefix));

case Expression.IF(ty = Type.CONDITIONAL_ARRAY()) then flattenConditionalArrayIfExp(exp);
else exp;
case Expression.IF(ty = Type.CONDITIONAL_ARRAY())
then Expression.mapShallow(
flattenConditionalArrayIfExp(exp),
function flattenExp(prefix = prefix));

else Expression.mapShallow(exp, function flattenExp(prefix = prefix));
end match;

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

function replaceSplitIndices
input output Expression exp;
Expand Down Expand Up @@ -1086,6 +1086,7 @@ algorithm

subs := Subscript.expandSplitIndices(subs);
exp := Expression.applySubscripts(subs, exp);
exp := flattenExp(exp, prefix);
end replaceSplitIndices;

function replaceSplitIndices2
Expand Down

0 comments on commit caa8835

Please sign in to comment.