Skip to content

Commit

Permalink
Add new debug flag combineSubscripts
Browse files Browse the repository at this point in the history
- Add new debug flag combineSubscripts that when used together with
  newInst moves all subscripts to the end of component references.
  • Loading branch information
perost committed Nov 3, 2020
1 parent 2ecb8dc commit dac3cfb
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 1 deletion.
22 changes: 22 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFComponentRef.mo
Expand Up @@ -635,6 +635,28 @@ public
end match;
end replaceWholeSubscripts;

function combineSubscripts
"Moves all subscripts to the end of the cref.
Ex: a[1, 2].b[4] => a.b[1, 2, 4]"
input output ComponentRef cref;
protected
list<Subscript> subs;
algorithm
// Fill in : subscripts where needed so the cref is fully subscripted.
cref := fillSubscripts(cref);
// Fetch all the subscripts.
subs := List.flatten(subscriptsAll(cref));

if listEmpty(subs) then
return;
end if;

// Remove unnecessary : subscripts from the head of the list.
subs := List.trim(subs, Subscript.isWhole);
// Replace the cref's subscripts.
cref := setSubscripts(subs, stripSubscriptsAll(cref));
end combineSubscripts;

function compare
input ComponentRef cref1;
input ComponentRef cref2;
Expand Down
15 changes: 15 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -94,6 +94,21 @@ public
ElementSource source;
end FLAT_MODEL;

function mapExp
input output FlatModel flatModel;
input MapFn fn;

partial function MapFn
input output Expression exp;
end MapFn;
algorithm
flatModel.variables := list(Variable.mapExp(v, fn) for v in flatModel.variables);
flatModel.equations := Equation.mapExpList(flatModel.equations, fn);
flatModel.initialEquations := Equation.mapExpList(flatModel.initialEquations, fn);
flatModel.algorithms := Algorithm.mapExpList(flatModel.algorithms, fn);
flatModel.initialAlgorithms := Algorithm.mapExpList(flatModel.initialAlgorithms, fn);
end mapExp;

function toString
input FlatModel flatModel;
input Boolean printBindingTypes = false;
Expand Down
18 changes: 18 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -193,6 +193,10 @@ algorithm

VerifyModel.verify(flatModel);

if Flags.isSet(Flags.COMBINE_SUBSCRIPTS) then
flatModel := FlatModel.mapExp(flatModel, combineSubscripts);
end if;

if Flags.isSet(Flags.NF_DUMP_FLAT) then
print("FlatModel:\n" + FlatModel.toString(flatModel) + "\n");
end if;
Expand Down Expand Up @@ -3586,5 +3590,19 @@ algorithm
end if;
end checkPartialClass;

function combineSubscripts
input output Expression exp;
algorithm
() := match exp
case Expression.CREF()
algorithm
exp.cref := ComponentRef.combineSubscripts(exp.cref);
then
();

else ();
end match;
end combineSubscripts;

annotation(__OpenModelica_Interface="frontend");
end NFInst;
13 changes: 13 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
Expand Up @@ -142,6 +142,19 @@ public
binding := NFBinding.EMPTY_BINDING;
end lookupTypeAttribute;

function mapExp
input output Variable var;
input MapFn fn;

partial function MapFn
input output Expression exp;
end MapFn;
algorithm
var.binding := Binding.mapExp(var.binding, fn);
var.typeAttributes := list(
(Util.tuple21(a), Binding.mapExp(Util.tuple22(a), fn)) for a in var.typeAttributes);
end mapExp;

function toString
input Variable var;
input String indent = "";
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -562,6 +562,8 @@ constant DebugFlag DUMP_DATARECONCILIATION = DEBUG_FLAG(193, "dataReconciliation
Gettext.gettext("Dumps all the dataReconciliation extraction algorithm procedure"));
constant DebugFlag ARRAY_CONNECT = DEBUG_FLAG(194, "arrayConnect", false,
Gettext.gettext("Use experimental array connection handler."));
constant DebugFlag COMBINE_SUBSCRIPTS = DEBUG_FLAG(195, "combineSubscripts", false,
Gettext.gettext("Move all subscripts to the end of component references."));

public
// CONFIGURATION FLAGS
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/Util/FlagsUtil.mo
Expand Up @@ -250,7 +250,8 @@ constant list<Flags.DebugFlag> allDebugFlags = {
Flags.NF_DUMP_FLAT,
Flags.DUMP_FORCE_FMI_ATTRIBUTES,
Flags.DUMP_DATARECONCILIATION,
Flags.ARRAY_CONNECT
Flags.ARRAY_CONNECT,
Flags.COMBINE_SUBSCRIPTS
};

protected
Expand Down
15 changes: 15 additions & 0 deletions OMCompiler/Compiler/Util/List.mo
Expand Up @@ -7690,6 +7690,21 @@ algorithm
end for;
end maxElement;

function trim<T>
"Removes elements from the head of the list while the given function returns
true for the first element, or until the list is empty."
input output list<T> l;
input PredFn fn;

partial function PredFn
input T e;
output Boolean res;
end PredFn;
algorithm
while not listEmpty(l) and fn(listHead(l)) loop
l := listRest(l);
end while;
end trim;

annotation(__OpenModelica_Interface="util");
end List;
27 changes: 27 additions & 0 deletions testsuite/flattening/modelica/scodeinst/CombineSubscripts1.mo
@@ -0,0 +1,27 @@
// name: CombineSubscripts1
// keywords:
// status: correct
// cflags: -d=newInst,-nfScalarize,combineSubscripts
//

model B
Real[10] c;
end B;

model CombineSubscripts1
B[100] b;
equation
for i in 1:100 loop
b[i].c[10] = 2;
end for;
end CombineSubscripts1;

// Result:
// class CombineSubscripts1
// Real[100, 10] b.c;
// equation
// for i in 1:100 loop
// b.c[i,10] = 2.0;
// end for;
// end CombineSubscripts1;
// endResult
41 changes: 41 additions & 0 deletions testsuite/flattening/modelica/scodeinst/CombineSubscripts2.mo
@@ -0,0 +1,41 @@
// name: CombineSubscripts2
// keywords:
// status: correct
// cflags: -d=newInst,-nfScalarize,combineSubscripts
//

record A
Real[4] a;
end A;

model CombineSubscripts2
A[3] b;
equation
for i in 1:4 loop
b[3].a[i] = 1;
end for;

for i in 1:4 loop
b.a[i] = {1, 2, 3};
end for;

for i in 1:3 loop
b[i].a = {1, 2, 3, 4};
end for;
end CombineSubscripts2;

// Result:
// class CombineSubscripts2
// Real[3, 4] b.a;
// equation
// for i in 1:4 loop
// b.a[3,i] = 1.0;
// end for;
// for i in 1:4 loop
// b.a[i] = {1.0, 2.0, 3.0};
// end for;
// for i in 1:3 loop
// b.a[i,:] = {1.0, 2.0, 3.0, 4.0};
// end for;
// end CombineSubscripts2;
// endResult
2 changes: 2 additions & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -203,6 +203,8 @@ ClassMod4.mo \
ClassMod5.mo \
ClassMod6.mo \
ClockConstructor1.mo \
CombineSubscripts1.mo \
CombineSubscripts2.mo \
Comment1.mo \
CompAsFunc.mo \
ComponentAsTypeError.mo \
Expand Down

0 comments on commit dac3cfb

Please sign in to comment.