Skip to content

Commit 203c8f2

Browse files
authored
Traverse exaustively when inlining functions. (#534)
- Use traverseExp* functions for trversal. - This way things like subscripts and function function arguments get traversed as well
1 parent 9e8c062 commit 203c8f2

File tree

1 file changed

+49
-225
lines changed

1 file changed

+49
-225
lines changed

OMCompiler/Compiler/Util/VarTransform.mo

Lines changed: 49 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,189 +1221,65 @@ algorithm
12211221
end matchcontinue;
12221222
end replaceExpRepeated2;
12231223

1224-
public function replaceExp "
1225-
Takes a set of replacement rules and an expression and a function
1226-
giving a boolean value for an expression.
1227-
The function replaces all variables in the expression using
1228-
the replacement rules, if the boolean value is true children of the
1229-
expression is visited (including the expression itself). If it is false,
1230-
no replacemet is performed."
1224+
public function replaceExp
12311225
input DAE.Exp inExp;
1232-
input VariableReplacements inVariableReplacements;
1233-
input Option<FuncTypeExp_ExpToBoolean> inFuncTypeExpExpToBooleanOption;
1226+
input VariableReplacements inVarReplacements;
1227+
input Option<FuncTypeExp_ExpToBoolean> inCondition;
12341228
output DAE.Exp outExp;
12351229
output Boolean replacementPerformed;
12361230
partial function FuncTypeExp_ExpToBoolean
12371231
input DAE.Exp inExp;
12381232
output Boolean outBoolean;
12391233
end FuncTypeExp_ExpToBoolean;
12401234
algorithm
1241-
(outExp,replacementPerformed) :=
1242-
matchcontinue (inExp,inVariableReplacements,inFuncTypeExpExpToBooleanOption)
1235+
1236+
outExp := inExp;
1237+
1238+
if replaceExpCond(inCondition, inExp) then
1239+
(outExp, _) := Expression.traverseExpBottomUp(inExp, function replaceExpCref(inVarReplacements = inVarReplacements, inCondition=inCondition), true);
1240+
end if;
1241+
1242+
replacementPerformed := not referenceEq(outExp, inExp);
1243+
end replaceExp;
1244+
1245+
1246+
protected function replaceExpCref
1247+
input DAE.Exp inExp;
1248+
input VariableReplacements inVarReplacements;
1249+
input Option<FuncTypeExp_ExpToBoolean> inCondition;
1250+
input Boolean inReplacementPerformed;
1251+
output DAE.Exp outExp;
1252+
output Boolean replacementPerformed;
1253+
partial function FuncTypeExp_ExpToBoolean
1254+
input DAE.Exp inExp;
1255+
output Boolean outBoolean;
1256+
end FuncTypeExp_ExpToBoolean;
1257+
algorithm
1258+
1259+
if not replaceExpCond(inCondition, inExp) then
1260+
Error.addInternalError("Got exp to replace when condition is not allowing replacements. Check traversal.", sourceInfo());
1261+
end if;
1262+
1263+
replacementPerformed := false;
1264+
outExp := inExp;
1265+
1266+
_ := match inExp
12431267
local
12441268
DAE.ComponentRef cr;
1245-
DAE.Exp e,e_1,e1_1,e2_1,e1,e2,e3_1,e3;
1246-
DAE.Type t,tp,ety;
1247-
VariableReplacements repl;
1248-
Option<FuncTypeExp_ExpToBoolean> cond;
1249-
DAE.Operator op;
1250-
list<DAE.Exp> expl_1,expl;
1251-
Absyn.Path path;
1252-
Boolean c,c1,c2,c3;
1253-
Integer b;
1254-
Absyn.CodeNode a;
1255-
list<list<DAE.Exp>> bexpl_1,bexpl;
1256-
Integer index_;
1257-
Option<tuple<DAE.Exp,Integer,Integer>> isExpisASUB;
1258-
DAE.ReductionInfo reductionInfo;
1259-
DAE.ReductionIterators iters;
1260-
DAE.CallAttributes attr;
1261-
1262-
// Note: Most of these functions check if a subexpression did a replacement.
1263-
// If it did not, we do not create a new copy of the expression (to save some memory).
1264-
1265-
case ((DAE.CREF(componentRef = cr,ty = t)),repl,cond)
1266-
equation
1267-
true = replaceExpCond(cond, inExp);
1268-
e1 = getReplacement(repl, cr);
1269-
e2 = avoidDoubleHashLookup(e1,t);
1270-
then
1271-
(e2,true);
1272-
case ((DAE.BINARY(exp1 = e1,operator = op,exp2 = e2)),repl,cond)
1273-
equation
1274-
true = replaceExpCond(cond, inExp);
1275-
(e1_1,c1) = replaceExp(e1, repl, cond);
1276-
(e2_1,c2) = replaceExp(e2, repl, cond);
1277-
true = c1 or c2;
1278-
then
1279-
(DAE.BINARY(e1_1,op,e2_1),true);
1280-
case ((DAE.LBINARY(exp1 = e1,operator = op,exp2 = e2)),repl,cond)
1281-
equation
1282-
true = replaceExpCond(cond, inExp);
1283-
(e1_1,c1) = replaceExp(e1, repl, cond);
1284-
(e2_1,c2) = replaceExp(e2, repl, cond);
1285-
true = c1 or c2;
1286-
then
1287-
(DAE.LBINARY(e1_1,op,e2_1),true);
1288-
case ((DAE.UNARY(operator = op,exp = e1)),repl,cond)
1289-
equation
1290-
true = replaceExpCond(cond, inExp);
1291-
(e1_1,true) = replaceExp(e1, repl, cond);
1292-
then
1293-
(DAE.UNARY(op,e1_1),true);
1294-
case ((DAE.LUNARY(operator = op,exp = e1)),repl,cond)
1295-
equation
1296-
true = replaceExpCond(cond, inExp);
1297-
(e1_1,true) = replaceExp(e1, repl, cond);
1298-
then
1299-
(DAE.LUNARY(op,e1_1),true);
1300-
case (DAE.RELATION(exp1 = e1,operator = op,exp2 = e2, index=index_, optionExpisASUB= isExpisASUB),repl,cond)
1301-
equation
1302-
(e1_1,c1) = replaceExp(e1, repl, cond);
1303-
(e2_1,c2) = replaceExp(e2, repl, cond);
1304-
true = c1 or c2;
1305-
then
1306-
(DAE.RELATION(e1_1,op,e2_1,index_,isExpisASUB),true);
1307-
case ((DAE.IFEXP(expCond = e1,expThen = e2,expElse = e3)),repl,cond)
1308-
equation
1309-
true = replaceExpCond(cond, inExp);
1310-
(e1_1,c1) = replaceExp(e1, repl, cond);
1311-
(e2_1,c2) = replaceExp(e2, repl, cond);
1312-
(e3_1,c3) = replaceExp(e3, repl, cond);
1313-
true = c1 or c2 or c3;
1314-
then
1315-
(DAE.IFEXP(e1_1,e2_1,e3_1),true);
1316-
/* Special case when a variable in pre() is an alias for unary minus of another */
1317-
case (DAE.CALL(path = path as Absyn.IDENT("pre"),expLst = {e as DAE.CREF()},attr=attr),repl,cond)
1318-
equation
1319-
true = replaceExpCond(cond, e);
1320-
(DAE.UNARY(DAE.UMINUS(ety),e_1),true) = replaceExp(e, repl, cond);
1321-
then
1322-
(DAE.UNARY(DAE.UMINUS(ety),DAE.CALL(path,{e_1},attr)),true);
1323-
case ((e as DAE.CALL(path = path,expLst = expl,attr=attr)),repl,cond)
1324-
equation
1325-
true = replaceExpCond(cond, e);
1326-
(expl_1,true) = replaceExpList(expl, repl, cond);
1327-
then
1328-
(DAE.CALL(path,expl_1,attr),true);
1329-
case ((DAE.ARRAY(ty = tp,scalar = c,array = expl)),repl,cond)
1330-
equation
1331-
true = replaceExpCond(cond, inExp);
1332-
(expl_1,true) = replaceExpList(expl, repl, cond);
1333-
then
1334-
(DAE.ARRAY(tp,c,expl_1),true);
1335-
case ((DAE.MATRIX(ty = t,integer = b,matrix = bexpl)),repl,cond)
1336-
equation
1337-
true = replaceExpCond(cond, inExp);
1338-
(bexpl_1,true) = replaceExpMatrix(bexpl, repl, cond);
1339-
then
1340-
(DAE.MATRIX(t,b,bexpl_1),true);
1341-
case ((DAE.RANGE(ty = tp,start = e1,step = NONE(),stop = e2)),repl,cond)
1342-
equation
1343-
true = replaceExpCond(cond, inExp);
1344-
(e1_1,c1) = replaceExp(e1, repl, cond);
1345-
(e2_1,c2) = replaceExp(e2, repl, cond);
1346-
true = c1 or c2;
1347-
then
1348-
(DAE.RANGE(tp,e1_1,NONE(),e2_1),true);
1349-
case ((DAE.RANGE(ty = tp,start = e1,step = SOME(e3),stop = e2)),repl,cond)
1350-
equation
1351-
true = replaceExpCond(cond, inExp);
1352-
(e1_1,c1) = replaceExp(e1, repl, cond);
1353-
(e2_1,c2) = replaceExp(e2, repl, cond);
1354-
(e3_1,c3) = replaceExp(e3, repl, cond);
1355-
true = c1 or c2 or c3;
1356-
then
1357-
(DAE.RANGE(tp,e1_1,SOME(e3_1),e2_1),true);
1358-
case ((DAE.TUPLE(PR = expl)),repl,cond)
1359-
equation
1360-
true = replaceExpCond(cond, inExp);
1361-
(expl_1,true) = replaceExpList(expl, repl, cond);
1362-
then
1363-
(DAE.TUPLE(expl_1),true);
1364-
case ((DAE.CAST(ty = tp,exp = e1)),repl,cond)
1365-
equation
1366-
true = replaceExpCond(cond, inExp);
1367-
(e1_1,true) = replaceExp(e1, repl, cond);
1368-
then
1369-
(DAE.CAST(tp,e1_1),true);
1370-
case ((DAE.ASUB(exp = e1,sub = expl)),repl,cond)
1371-
equation
1372-
true = replaceExpCond(cond, inExp);
1373-
(e1_1,c1) = replaceExp(e1, repl, cond);
1374-
(expl_1,c2) = replaceExpList(expl, repl, cond);
1375-
true = c1 or c2;
1376-
then
1377-
(Expression.makeASUB(e1_1,expl_1),true);
1378-
case ((DAE.SIZE(exp = e1,sz = NONE())),repl,cond)
1379-
equation
1380-
true = replaceExpCond(cond, inExp);
1381-
(e1_1,true) = replaceExp(e1, repl, cond);
1382-
then
1383-
(DAE.SIZE(e1_1,NONE()),true);
1384-
case ((DAE.SIZE(exp = e1,sz = SOME(e2))),repl,cond)
1385-
equation
1386-
true = replaceExpCond(cond, inExp);
1387-
(e1_1,c1) = replaceExp(e1, repl, cond);
1388-
(e2_1,c2) = replaceExp(e2, repl, cond);
1389-
true = c1 or c2;
1390-
then
1391-
(DAE.SIZE(e1_1,SOME(e2_1)),true);
1392-
case (DAE.CODE(code = a,ty = tp),_,_)
1393-
equation
1394-
print("replace_exp on CODE not impl.\n");
1395-
then
1396-
(DAE.CODE(a,tp),false);
1397-
case ((DAE.REDUCTION(reductionInfo = reductionInfo,expr = e1,iterators = iters)),repl,cond)
1398-
equation
1399-
true = replaceExpCond(cond, inExp);
1400-
(e1_1,_) = replaceExp(e1, repl, cond);
1401-
(iters,true) = replaceExpIters(iters, repl, cond);
1402-
then (DAE.REDUCTION(reductionInfo,e1_1,iters),true);
1403-
case (e,_,_)
1404-
then (e,false);
1405-
end matchcontinue;
1406-
end replaceExp;
1269+
1270+
case DAE.CREF(componentRef = cr) algorithm
1271+
try
1272+
outExp := getReplacement(inVarReplacements, cr);
1273+
outExp := avoidDoubleHashLookup(outExp, inExp.ty);
1274+
replacementPerformed := true;
1275+
else
1276+
end try;
1277+
then ();
1278+
1279+
else ();
1280+
1281+
end match;
1282+
end replaceExpCref;
14071283

14081284
public function replaceExpList
14091285
input list<DAE.Exp> iexpl;
@@ -1429,58 +1305,6 @@ algorithm
14291305
replacementPerformed := acc2;
14301306
end replaceExpList;
14311307

1432-
protected function replaceExpIters
1433-
input list<DAE.ReductionIterator> inIters;
1434-
input VariableReplacements repl;
1435-
input Option<FuncTypeExp_ExpToBoolean> cond;
1436-
output list<DAE.ReductionIterator> outIter;
1437-
output Boolean replacementPerformed;
1438-
partial function FuncTypeExp_ExpToBoolean
1439-
input DAE.Exp inExp;
1440-
output Boolean outBoolean;
1441-
end FuncTypeExp_ExpToBoolean;
1442-
protected
1443-
list<DAE.ReductionIterator> acc1 = {};
1444-
Boolean acc2 = false;
1445-
algorithm
1446-
for iter in inIters loop
1447-
_ := match (iter)
1448-
local
1449-
String id;
1450-
DAE.Exp exp,gexp;
1451-
DAE.Type ty;
1452-
Boolean b1,b2;
1453-
case (DAE.REDUCTIONITER(id,exp,NONE(),ty))
1454-
equation
1455-
(exp,b1) = replaceExp(exp, repl, cond);
1456-
if b1 then
1457-
acc1 = DAE.REDUCTIONITER(id,exp,NONE(),ty)::acc1;
1458-
acc2 = true;
1459-
else
1460-
acc1 = iter::acc1;
1461-
end if;
1462-
then ();
1463-
case (DAE.REDUCTIONITER(id,exp,SOME(gexp),ty))
1464-
equation
1465-
(exp,b1) = replaceExp(exp, repl, cond);
1466-
(gexp,b2) = replaceExp(gexp, repl, cond);
1467-
if b1 or b2 then
1468-
acc1 = DAE.REDUCTIONITER(id,exp,SOME(gexp),ty)::acc1;
1469-
acc2 = true;
1470-
else
1471-
acc1 = iter::acc1;
1472-
end if;
1473-
then ();
1474-
else
1475-
equation
1476-
acc1 = iter::acc1;
1477-
then ();
1478-
end match;
1479-
end for;
1480-
outIter := listReverseInPlace(acc1);
1481-
replacementPerformed := acc2;
1482-
end replaceExpIters;
1483-
14841308
protected function replaceExpCond "function replaceExpCond(cond,e) => true &
14851309

14861310
Helper function to replace_Expression. Evaluates a condition function if

0 commit comments

Comments
 (0)