Skip to content

Commit 4e8f796

Browse files
authored
[NB] update cat() call handling (#15075)
- Inline: fix inlining of arrays of literals as input to cat() calls - Slice/Adjacency: Allow for skips that skip an array only partially (not all dimensions) - Replacements: use full subscript list (whith wholedim) when replacing a cref by an expression
1 parent 3fbf1cc commit 4e8f796

File tree

3 files changed

+63
-29
lines changed

3 files changed

+63
-29
lines changed

OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBInline.mo

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ protected
672672
subscript_exp := Expression.fromCref(iterator_name);
673673

674674
for arg in rest loop
675-
_ := match arg
675+
failed := match arg
676676
case Expression.CREF(cref = rhs) guard(not failed) algorithm
677677
ty := Expression.typeOf(arg);
678678
sz := Type.sizeOf(ty);
@@ -728,34 +728,14 @@ protected
728728
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
729729
print("-- Result: " + Equation.pointerToString(new_eqn) + "\n");
730730
end if;
731-
then ();
731+
then false;
732732

733+
// inline for literals down to element, nested arrays possible
733734
case Expression.ARRAY() guard(not failed and Expression.isLiteral(arg)) algorithm
734-
// a literal expression, does not need subscripting
735-
for elem in arg.elements loop
736-
ty := Expression.typeOf(elem);
737-
sz := Type.sizeOf(ty);
738-
// properly subscript LHS with shift
739-
lhs_sub := Expression.INTEGER(shift+1);
740-
lhs := ComponentRef.mergeSubscripts(Subscript.fillWithWholeLeft({Subscript.INDEX(lhs_sub)}, n), cref);
741-
lhs_exp := Expression.fromCref(lhs);
735+
(eqns, shift) := inlineCatCallLiterals(arg, cref, iter, attr, n, index, failed, eqns, shift);
736+
then false;
742737

743-
// create the new equation
744-
new_eqn := Equation.makeAssignment(lhs_exp, elem, index, NBEquation.SIMULATION_STR, iter, attr);
745-
746-
// bump the shift adding the size of this last equation
747-
shift := shift + sz;
748-
749-
eqns := new_eqn :: eqns;
750-
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
751-
print("-- Result: " + Equation.pointerToString(new_eqn) + "\n");
752-
end if;
753-
end for;
754-
then ();
755-
756-
else algorithm
757-
failed := true;
758-
then ();
738+
else true;
759739
end match;
760740
end for;
761741

@@ -765,6 +745,53 @@ protected
765745
end if;
766746
end inlineCatCall;
767747

748+
function inlineCatCallLiterals
749+
"recursively inlines arrays of literal expressions that were an argument to a cat() call"
750+
input Expression exp;
751+
input ComponentRef cref;
752+
input Iterator iter;
753+
input EquationAttributes attr;
754+
input Integer n;
755+
input Pointer<Integer> index;
756+
input Boolean failed;
757+
input output list<Pointer<Equation>> eqns;
758+
input output Integer shift;
759+
algorithm
760+
_ := match exp
761+
local
762+
Integer sz;
763+
ComponentRef lhs;
764+
Expression lhs_sub, lhs_exp;
765+
Pointer<Equation> new_eqn;
766+
767+
case Expression.ARRAY() guard(not failed and Expression.isLiteral(exp)) algorithm
768+
// a literal expression, does not need subscripting
769+
for elem in exp.elements loop
770+
(eqns, shift) := inlineCatCallLiterals(elem, cref, iter, attr, n, index, failed, eqns, shift);
771+
end for;
772+
then ();
773+
774+
else algorithm
775+
sz := Type.sizeOf(Expression.typeOf(exp));
776+
// properly subscript LHS with shift
777+
lhs_sub := Expression.INTEGER(shift+1);
778+
lhs := ComponentRef.mergeSubscripts(Subscript.fillWithWholeLeft({Subscript.INDEX(lhs_sub)}, n), cref);
779+
lhs_exp := Expression.fromCref(lhs);
780+
781+
// create the new equation
782+
new_eqn := Equation.makeAssignment(lhs_exp, exp, index, NBEquation.SIMULATION_STR, iter, attr);
783+
784+
// bump the shift adding the size of this last equation
785+
shift := shift + sz;
786+
787+
eqns := new_eqn :: eqns;
788+
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
789+
print("-- Result: " + Equation.pointerToString(new_eqn) + "\n");
790+
end if;
791+
then ();
792+
end match;
793+
end inlineCatCallLiterals;
794+
768795
function createInlinedEquation
769796
"used for inlining record, tuple and array equations.
770797
tries to create new equation from lhs and rhs and applying

OMCompiler/Compiler/NBackEnd/Util/NBReplacements.mo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ public
203203
// try to strip the subscripts and see if that cref occurs
204204
stripped := ComponentRef.stripSubscriptsAll(exp.cref);
205205
if UnorderedMap.contains(stripped, replacements) then
206-
subs := ComponentRef.subscriptsAllFlat(exp.cref);
207-
subs := list(s for s guard(not Subscript.isWhole(s)) in subs);
206+
subs := ComponentRef.subscriptsAllWithWholeFlat(exp.cref);
208207
res := UnorderedMap.getOrFail(stripped, replacements);
209208
res := Expression.applySubscripts(subs, res, true);
210209
else

OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,7 @@ protected
12081208
local
12091209
Integer skip;
12101210
list<Integer> rest, tail;
1211+
list<Dimension> rest_dim, tail_dim;
12111212
Type sub_ty;
12121213
list<Type> rest_ty;
12131214
Pointer<Variable> parent;
@@ -1263,12 +1264,19 @@ protected
12631264
case (Type.ARRAY(), rest) guard(Dimension.sizesProduct(ty.dimensions, true) == 1) algorithm
12641265
then resolveSkips(index, ty.elementType, rest, cref, fullmap);
12651266

1266-
// skip to an array element
1267+
// skip to an array element with more or equal skips to dimensions
12671268
case (Type.ARRAY(), rest) guard List.compareLength(rest, ty.dimensions) >= 0 algorithm
12681269
(rest, tail) := List.split(rest, listLength(ty.dimensions));
12691270
index := locationToIndex(list(Dimension.size(dim, true) for dim in ty.dimensions), rest, index);
12701271
then resolveSkips(index, ty.elementType, tail, cref, fullmap);
12711272

1273+
// skip to an array with less skips then dimensions
1274+
case (Type.ARRAY(), rest) algorithm
1275+
(rest_dim, tail_dim) := List.split(ty.dimensions, listLength(rest));
1276+
index := locationToIndex(list(Dimension.size(dim, true) for dim in rest_dim), rest, index);
1277+
ty.dimensions := tail_dim;
1278+
then (index, ty);
1279+
12721280
// skip for tuple or array, but the skip is too large
12731281
case (_, skip::_) guard(Type.isTuple(ty) or Type.isArray(ty)) algorithm
12741282
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed because skip of " + intString(skip)

0 commit comments

Comments
 (0)