-
Notifications
You must be signed in to change notification settings - Fork 297
/
NBInline.mo
416 lines (378 loc) · 16 KB
/
NBInline.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2020, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated uniontype NBInline<T>
" file: NBInline.mo
package: NBInline
description: This file contains functions for inlining operations.
"
protected
import Inline = NBInline;
// OF imports
import Absyn;
import AbsynUtil;
import DAE;
import DAEDump;
import DAEUtil;
// NF imports
import Call = NFCall;
import ComponentRef = NFComponentRef;
import Expression = NFExpression;
import NFFunction.Function;
import NFFlatten.FunctionTree;
import Statement = NFStatement;
import Type = NFType;
// NB imports
import Module = NBModule;
import BackendDAE = NBackendDAE;
import BEquation = NBEquation;
import NBEquation.{Equation, EquationPointers, EqData, EquationAttributes, Iterator};
import Replacements = NBReplacements;
import NBVariable.{VariablePointers, VarData};
// Util
import Slice = NBSlice;
import StringUtil;
// =========================================================================
// MAIN ROUTINE, PLEASE DO NOT CHANGE
// =========================================================================
public
function main
"Wrapper function for any inlining function. This will be
called during simulation and gets the corresponding subfunction from
the given input."
extends Module.wrapper;
input list<DAE.InlineType> inline_types;
algorithm
bdae := match bdae
case BackendDAE.MAIN()
algorithm
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
print(StringUtil.headline_4("[dumpBackendInline] Inlining operatations for: "
+ List.toString(inline_types, DAEDump.dumpInlineTypeBackendStr)));
end if;
bdae.eqData := inline(bdae.eqData, bdae.varData, bdae.funcTree, inline_types);
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
print("\n");
end if;
then bdae;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed."});
then fail();
end match;
end main;
// =========================================================================
// TYPES, UNIONTYPES AND MEMBER FUNCTIONS
// =========================================================================
function inlineForEquation
"inlines for-equations of size 1 to its body equation by replacing
the iterators by the only values they are ever going to be."
input output Equation eqn;
algorithm
eqn := match eqn
local
Equation new_eqn;
UnorderedMap<ComponentRef, Expression> replacements "replacement map for iterator crefs";
list<ComponentRef> names;
list<Expression> ranges;
ComponentRef name;
Expression range;
Integer start;
case Equation.FOR_EQUATION(body = {new_eqn}) guard(Equation.size(Pointer.create(eqn)) == 1) algorithm
replacements := UnorderedMap.new<Expression>(ComponentRef.hash, ComponentRef.isEqual);
(names, ranges) := Iterator.getFrames(eqn.iter);
for tpl in List.zip(names, ranges) loop
(name, range) := tpl;
(start, _, _) := Expression.getIntegerRange(range);
UnorderedMap.add(name, Expression.INTEGER(start), replacements);
end for;
new_eqn := Equation.map(new_eqn, function Replacements.applySimpleExp(replacements = replacements));
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
print("[" + getInstanceName() + "] Inlining: " + Equation.toString(eqn) + "\n");
print("-- Result: " + Equation.toString(new_eqn) + "\n");
end if;
then new_eqn;
else eqn;
end match;
end inlineForEquation;
function functionInlineable
"returns true if the function can be inlined"
input Function fn;
output Boolean b = false;
algorithm
// currently we only inline single assignments
// also check for single output?
if Function.hasSingleOrEmptyBody(fn) then
b := match Function.getBody(fn)
case {Statement.ASSIGNMENT()} then true;
else false;
end match;
end if;
end functionInlineable;
function inlineRecords
"also inlines simple record equalities"
input output EqData eqData;
input VariablePointers variables;
protected
Pointer<Integer> index = EqData.getUniqueIndex(eqData);
Pointer<list<Pointer<Equation>>> new_eqns = Pointer.create({});
algorithm
eqData := EqData.map(eqData, function inlineRecordEquation(variables = variables, record_eqns = new_eqns, index = index, inlineSimple = true));
eqData := EqData.addUntypedList(eqData, Pointer.access(new_eqns), false);
eqData := EqData.compress(eqData);
end inlineRecords;
function inlineRecordSliceEquation
input Slice<Pointer<Equation>> slice;
input VariablePointers variables;
input Pointer<Integer> index;
input Boolean inlineSimple;
output list<Slice<Pointer<Equation>>> slices;
protected
Pointer<list<Pointer<Equation>>> record_eqns = Pointer.create({});
algorithm
inlineRecordEquation(Pointer.access(Slice.getT(slice)), variables, record_eqns, index, inlineSimple);
// somehow split slice.indices
slices := list(Slice.SLICE(eqn, {}) for eqn in Pointer.access(record_eqns));
end inlineRecordSliceEquation;
protected
function inline extends Module.inlineInterface;
protected
UnorderedMap<Absyn.Path, Function> replacements "rules for replacements are stored inside here";
algorithm
// collect functions
replacements := UnorderedMap.new<Function>(AbsynUtil.pathHash, AbsynUtil.pathEqual);
replacements := FunctionTree.fold(funcTree, function collectInlineFunctions(inline_types = inline_types), replacements);
// apply replacements
eqData := Replacements.replaceFunctions(eqData, replacements);
// replace record constucters after functions because record operator
// functions will produce record constructors once inlined
eqData := inlineRecordsTuples(eqData, VarData.getVariables(varData));
end inline;
function collectInlineFunctions
"collects all functions that have one of the inline types,
use with FunctionTree.fold()"
input Absyn.Path key;
input Function value;
input output UnorderedMap<Absyn.Path, Function> replacements;
input list<DAE.InlineType> inline_types;
algorithm
// only add to the map if the function has one of the inline types and is inlineable
if List.contains(inline_types, Function.inlineBuiltin(value), DAEUtil.inlineTypeEqual) and functionInlineable(value) then
UnorderedMap.add(key, value, replacements);
end if;
end collectInlineFunctions;
function inlineRecordsTuples
"does not inline simple record equalities"
input output EqData eqData;
input VariablePointers variables;
protected
Pointer<Integer> index = EqData.getUniqueIndex(eqData);
Pointer<list<Pointer<Equation>>> new_eqns = Pointer.create({});
algorithm
eqData := EqData.map(eqData, function inlineRecordEquation(variables = variables, record_eqns = new_eqns, index = index, inlineSimple = false));
eqData := EqData.map(eqData, function inlineTupleEquation(tuple_eqns = new_eqns, index = index));
eqData := EqData.addUntypedList(eqData, Pointer.access(new_eqns), false);
eqData := EqData.compress(eqData);
end inlineRecordsTuples;
function inlineRecordEquation
"tries to inline a record equation. Removes the old equation by making it a dummy
and appends new equations to the mutable list.
EquationPointers.compress() should be used afterwards to remove the dummy equations."
input output Equation eqn;
input VariablePointers variables;
input Pointer<list<Pointer<Equation>>> record_eqns;
input Pointer<Integer> index;
input Boolean inlineSimple;
algorithm
eqn := match eqn
local
Equation new_eqn;
Integer size;
// don't inline simple cref equalities
case Equation.RECORD_EQUATION(lhs = Expression.CREF(), rhs = Expression.CREF()) guard(not inlineSimple) then eqn;
case Equation.ARRAY_EQUATION(lhs = Expression.CREF(), rhs = Expression.CREF()) guard(not inlineSimple) then eqn;
// try to inline other record equations. try catch to be sure to not discard
case Equation.RECORD_EQUATION(ty = Type.COMPLEX()) algorithm
try
if Flags.isSet(Flags.DUMPBACKENDINLINE) then print("[" + getInstanceName() + "] Inlining: " + Equation.toString(eqn) + "\n"); end if;
new_eqn := inlineRecordEquationWork(eqn.lhs, eqn.rhs, eqn.attr, eqn.source, eqn.recordSize, variables, record_eqns, index, inlineSimple);
if Flags.isSet(Flags.DUMPBACKENDINLINE) then print("\n"); end if;
else
// inlining failed, keep old equation
new_eqn := eqn;
end try;
then new_eqn;
// only if record size is not NONE()
case Equation.ARRAY_EQUATION(recordSize = SOME(size)) algorithm
try
if Flags.isSet(Flags.DUMPBACKENDINLINE) then print("[" + getInstanceName() + "] Inlining: " + Equation.toString(eqn) + "\n"); end if;
new_eqn := inlineRecordEquationWork(eqn.lhs, eqn.rhs, eqn.attr, eqn.source, size, variables, record_eqns, index, inlineSimple);
else
// inlining failed, keep old equation
new_eqn := eqn;
end try;
then new_eqn;
// iterate over body equations of for-loop
case Equation.FOR_EQUATION() algorithm
eqn.body := list(inlineRecordEquation(body_eqn, variables, record_eqns, index, inlineSimple) for body_eqn in eqn.body);
then eqn;
else eqn;
end match;
end inlineRecordEquation;
function inlineRecordEquationWork
input Expression lhs;
input Expression rhs;
input EquationAttributes attr;
input DAE.ElementSource src;
input Integer recordSize;
input VariablePointers variables;
input Pointer<list<Pointer<Equation>>> record_eqns;
input Pointer<Integer> index;
input Boolean inlineSimple;
output Equation new_eqn;
protected
list<Pointer<Equation>> tmp_eqns;
Pointer<Equation> tmp_eqn;
Pointer<list<Pointer<Equation>>> tmp_eqns_ptr;
Expression new_lhs, new_rhs;
algorithm
tmp_eqns := Pointer.access(record_eqns);
for i in 1:recordSize loop
new_lhs := Expression.nthRecordElement(i, lhs);
new_rhs := Expression.nthRecordElement(i, rhs);
// lower indexed record constructor elements
new_lhs := Expression.map(new_lhs, inlineRecordConstructorElements);
new_rhs := Expression.map(new_rhs, inlineRecordConstructorElements);
// lower the new component references of record attributes
new_lhs := Expression.map(new_lhs, function BackendDAE.lowerComponentReferenceExp(variables = variables));
new_rhs := Expression.map(new_rhs, function BackendDAE.lowerComponentReferenceExp(variables = variables));
// create new equation
tmp_eqn := Equation.fromLHSandRHS(new_lhs, new_rhs, index, NBEquation.SIMULATION_STR, attr, src);
// if the equation still has a record type, inline it further
if Equation.isRecordEquation(tmp_eqn) then
tmp_eqns_ptr := Pointer.create(tmp_eqns);
_ := inlineRecordEquation(Pointer.access(tmp_eqn), variables, tmp_eqns_ptr, index, inlineSimple);
tmp_eqns := Pointer.access(tmp_eqns_ptr);
else
tmp_eqns := tmp_eqn :: tmp_eqns;
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
print("-- Result: " + Equation.toString(Pointer.access(tmp_eqn)) + "\n");
end if;
end if;
end for;
Pointer.update(record_eqns, tmp_eqns);
new_eqn := Equation.DUMMY_EQUATION();
end inlineRecordEquationWork;
function inlineRecordConstructorElements
"removes indexed constructor element calls
Constructor(a,b,c)[2] --> b"
input output Expression exp;
algorithm
exp := match exp
local
Expression new_exp;
Call call;
Function fn;
case Expression.RECORD_ELEMENT(recordExp = Expression.CALL(call = call as Call.TYPED_CALL(fn = fn))) algorithm
if Function.isDefaultRecordConstructor(fn) then
new_exp := listGet(call.arguments, exp.index);
elseif Function.isNonDefaultRecordConstructor(fn) then
// ToDo: this has to be mapped correctly with the body.
// for non default record constructors its not always the
// case that inputs map 1:1 to attributes
new_exp := listGet(call.arguments, exp.index);
else
new_exp := exp;
end if;
then new_exp;
else exp;
end match;
end inlineRecordConstructorElements;
function inlineTupleEquation
"inlines equations of the form TPL1 = TPL2 which is not modelica standard but can be created
by the function alias module and need to be removed afterwards"
input output Equation eqn;
input Pointer<Integer> index;
input Pointer<list<Pointer<Equation>>> tuple_eqns;
algorithm
eqn := match eqn
local
list<Pointer<Equation>> eqns;
list<Expression> lhs_elems, rhs_elems;
Expression lhs, rhs;
Pointer<Equation> tmp_eqn;
Equation new_eqn;
case Equation.RECORD_EQUATION() algorithm
lhs_elems := getElementList(eqn.lhs);
rhs_elems := getElementList(eqn.rhs);
if not listEmpty(lhs_elems) and listLength(lhs_elems) == listLength(rhs_elems) then
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
print("[" + getInstanceName() + "] Inlining: " + Equation.toString(eqn) + "\n");
end if;
eqns := Pointer.access(tuple_eqns);
for tpl in List.zip(lhs_elems, rhs_elems) loop
(lhs, rhs) := tpl;
if not (Expression.isWildCref(lhs) or Expression.isWildCref(rhs)) then
tmp_eqn := Equation.makeAssignment(lhs, rhs, index, NBVariable.AUXILIARY_STR, Iterator.EMPTY(), eqn.attr);
if Flags.isSet(Flags.DUMPBACKENDINLINE) then
print("-- Result: " + Equation.toString(Pointer.access(tmp_eqn)) + "\n");
end if;
eqns := tmp_eqn :: eqns;
end if;
end for;
Pointer.update(tuple_eqns, eqns);
new_eqn := Equation.DUMMY_EQUATION();
else
new_eqn := eqn;
end if;
then new_eqn;
else eqn;
end match;
end inlineTupleEquation;
function getElementList
input Expression exp;
output list<Expression> elements;
algorithm
elements := match exp
local
Expression sub_exp, elem;
case Expression.TUPLE() then exp.elements;
case Expression.TUPLE_ELEMENT(tupleExp = sub_exp as Expression.TUPLE()) algorithm
if exp.index > listLength(sub_exp.elements) then
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed to get subscripted tuple element: " + Expression.toString(exp)});
fail();
else
elem := listGet(sub_exp.elements, exp.index);
end if;
then {elem};
else {};
end match;
end getElementList;
annotation(__OpenModelica_Interface="backend");
end NBInline;